Files
codex/codex-rs/tui/src/daemon_telemetry.rs
T
Eric Traut b6a53810db Add failure stages and OS error kinds to daemon startup metrics (#65049)
## Why

The TUI's daemon auto-start metric reports a failure reason, but broad
reasons such as `other` leave it unclear which startup step failed.

## What changed

Add `failure_stage` and `os_error_kind` to failed `codex.daemon.start`
attempts. For example, a missing executable reports `spawn` at
`process_spawn` with `not_found`. The metric uses a fixed set of error
categories and keeps raw OS codes and diagnostic text local.

Report Windows detached-launch restrictions as
`windows_detach_restricted`, matching the reason recorded when the TUI
falls back to an embedded app server.

## Testing

Cover missing executables, Windows launch restrictions, and preservation
of the underlying I/O error. An in-memory metrics test checks that a failed
attempt increments the counter once and maps an unknown OS code to `other`.

GitOrigin-RevId: c5976517e1178fb8d65257362a893ad6254a2ddd
2026-12-01 18:08:05 +00:00

78 lines
2.7 KiB
Rust

//! Records automatic daemon startup and ensures launch observations survive setup failures.
use super::*;
use crate::terminal_size_telemetry::LaunchTags;
use codex_app_server_daemon::LifecycleOutput;
use codex_app_server_daemon::LifecycleStatus;
use codex_app_server_daemon::telemetry::StartError;
/// Record the connection mode or update handoff once. Other early exits are unconfirmed.
pub(super) struct Launch<F: FnOnce(&'static str) -> LaunchTags>(pub(super) Option<F>);
impl<F: FnOnce(&'static str) -> LaunchTags> Launch<F> {
pub(super) fn record(mut self, app_server_mode: &'static str) -> LaunchTags {
self.0
.take()
.map(|record| record(app_server_mode))
.unwrap_or_default()
}
}
impl<F: FnOnce(&'static str) -> LaunchTags> Drop for Launch<F> {
fn drop(&mut self) {
if let Some(record) = self.0.take() {
record("unconfirmed");
}
}
}
pub(super) async fn record_attempt(
metrics: &codex_otel::MetricsClient,
codex_home: &std::path::Path,
result: &Result<LifecycleOutput, StartError>,
) {
let outcome = match result {
Ok(output) => match output.status {
LifecycleStatus::Started => "started",
LifecycleStatus::AlreadyRunning => "already_running",
LifecycleStatus::Restarted => "restarted",
LifecycleStatus::Stopped | LifecycleStatus::NotRunning | LifecycleStatus::Running => {
"unconfirmed"
}
},
Err(_) => "failed",
};
let mut tags = codex_app_server_daemon::telemetry::settings_tags(codex_home)
.await
.to_vec();
tags.extend([
("initiation_source", "tui_auto_start"),
("outcome", outcome),
]);
if let Err(error) = result {
tags.extend(error.failure_tags());
}
let _ = metrics.counter("codex.daemon.start", /*inc*/ 1, &tags);
}
pub(super) async fn record_start(config: &Config, result: &Result<LifecycleOutput, StartError>) {
// Failure can happen before normal TUI telemetry is initialized. This short-lived
// provider uses exactly the same consent and identity construction as TUI startup.
let Ok(Ok(Some(otel))) = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
codex_app_server_client::build_otel_provider(
config,
env!("CARGO_PKG_VERSION"),
/*service_name_override*/ None,
/*default_analytics_enabled*/ true,
)
})) else {
return;
};
if let Some(metrics) = otel.metrics() {
record_attempt(metrics, &config.codex_home, result).await;
}
let _ = otel
.shutdown_with_timeout(INTERACTIVE_OTEL_SHUTDOWN_TIMEOUT)
.await;
}