Skip to content

Commit f61fb5b

Browse files
authored
Merge branch 'main' into mincore-diff-snapshots
2 parents 5199aa0 + c476050 commit f61fb5b

File tree

1 file changed

+5
-22
lines changed

1 file changed

+5
-22
lines changed

src/vmm/src/logger/metrics.rs

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -141,28 +141,11 @@ impl<T: Serialize + Debug, M: Write + Send + Debug> Metrics<T, M> {
141141
/// known deadlock potential.
142142
pub fn write(&self) -> Result<bool, MetricsError> {
143143
if let Some(lock) = self.metrics_buf.get() {
144-
match serde_json::to_string(&self.app_metrics) {
145-
Ok(msg) => {
146-
if let Ok(mut guard) = lock.lock() {
147-
// No need to explicitly call flush because the underlying LineWriter
148-
// flushes automatically whenever a newline is
149-
// detected (and we always end with a newline the
150-
// current write).
151-
guard
152-
.write_all(format!("{msg}\n",).as_bytes())
153-
.map_err(MetricsError::Write)
154-
.map(|_| true)
155-
} else {
156-
// We have not incremented `missed_metrics_count` as there is no way to push
157-
// metrics if destination lock got poisoned.
158-
panic!(
159-
"Failed to write to the provided metrics destination due to poisoned \
160-
lock"
161-
);
162-
}
163-
}
164-
Err(err) => Err(MetricsError::Serde(err.to_string())),
165-
}
144+
let mut writer = lock.lock().expect("poisoned lock");
145+
serde_json::to_writer(writer.by_ref(), &self.app_metrics)
146+
.map_err(|err| MetricsError::Serde(err.to_string()))?;
147+
writer.write_all(b"\n").map_err(MetricsError::Write)?;
148+
Ok(true)
166149
} else {
167150
// If the metrics are not initialized, no error is thrown but we do let the user know
168151
// that metrics were not written.

0 commit comments

Comments
 (0)