Skip to content

Commit d5c1a84

Browse files
authored
Merge pull request #13918 from gitbutlerapp/metrics-soft-fail
fix(but): never fail a command due to failing to emit metrics
2 parents 84095ee + 592fdf9 commit d5c1a84

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

crates/but/src/utils/metrics.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,17 @@ where
418418
return self;
419419
};
420420

421-
tokio::process::Command::new(binary_path::current_exe_for_but_exec()?)
421+
// We can fail both in resolving the path to the but binary, and in invoking it. As metrics
422+
// emissions shouldn't impact user experience, we swallow these errors.
423+
let but_path = match binary_path::current_exe_for_but_exec() {
424+
Err(err) => {
425+
tracing::warn!(?err, "Failed to resolve binary path to `but`");
426+
return self;
427+
}
428+
Ok(path) => path,
429+
};
430+
431+
let _ = tokio::process::Command::new(but_path)
422432
.arg("metrics")
423433
.arg("--command-name")
424434
.arg(v.get_name())
@@ -428,7 +438,9 @@ where
428438
.stdout(std::process::Stdio::null())
429439
.group()
430440
.kill_on_drop(false)
431-
.spawn()?;
441+
.spawn()
442+
.map_err(|err| tracing::warn!(?err, "Failed to emit metrics"));
443+
432444
self
433445
}
434446
}

crates/but/src/utils/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,13 @@ impl ResultErrorExt for anyhow::Result<()> {
4343
}
4444
}
4545

46-
/// Utilities attached to `anyhow::Result<T>`.
46+
/// Metrics utilities for results
4747
pub trait ResultMetricsExt<T, E> {
48+
/// Emit metrics for the [`Result`].
49+
///
50+
/// The result must simply be propagated through this method, regardless of if emitting metrics
51+
/// is successful or not. We do not want a failure to emit metrics to impact the user
52+
/// experience.
4853
fn emit_metrics(self, ctx: Option<OneshotMetricsContext>) -> Result<T, E>;
4954
}
5055

0 commit comments

Comments
 (0)