Files
codex/codex-rs/app-server-daemon/src/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

117 lines
4.3 KiB
Rust

//! Bounded observations for foreground callers. This module never exports telemetry;
//! in particular, the detached updater must not create a telemetry provider.
use std::io::ErrorKind;
use std::path::Path;
/// Shared reason for the failed daemon attempt and a confirmed embedded launch.
pub const WINDOWS_DETACH_RESTRICTED: &str = "windows_detach_restricted";
/// Failure of a foreground auto-start, with the original diagnostic kept separate from the metric.
#[derive(Debug)]
pub struct StartError {
pub error: anyhow::Error,
stage: &'static str,
}
impl StartError {
pub fn failure_reason(&self) -> &'static str {
self.classification().0
}
pub fn failure_tags(&self) -> [(&'static str, &'static str); 3] {
let (reason, stage) = self.classification();
// Only export known categories. OS codes and diagnostic text stay local.
let kind = self.error.chain().find_map(|cause| {
cause
.downcast_ref::<std::io::Error>()
.map(std::io::Error::kind)
});
let os_error = match kind {
Some(ErrorKind::PermissionDenied) => "permission_denied",
Some(ErrorKind::NotFound) => "not_found",
Some(ErrorKind::AlreadyExists) => "already_exists",
Some(ErrorKind::WouldBlock) => "would_block",
Some(ErrorKind::TimedOut) => "timed_out",
Some(ErrorKind::ConnectionRefused) => "connection_refused",
Some(ErrorKind::InvalidInput) => "invalid_input",
Some(ErrorKind::InvalidData) => "invalid_data",
Some(ErrorKind::StorageFull) => "storage_full",
Some(ErrorKind::ReadOnlyFilesystem) => "read_only_filesystem",
Some(ErrorKind::ResourceBusy) => "resource_busy",
Some(_) => "other",
None => "none",
};
[
("failure_reason", reason),
("failure_stage", stage),
("os_error_kind", os_error),
]
}
fn classification(&self) -> (&'static str, &'static str) {
#[cfg(windows)]
if self.error.is::<crate::DetachedLaunchRestricted>() {
return (WINDOWS_DETACH_RESTRICTED, "process_spawn");
}
match self.error.downcast_ref::<StartFailure>() {
Some(StartFailure::Spawn(_)) => ("spawn", "process_spawn"),
Some(StartFailure::EarlyChildExit(stage, _)) => ("early_child_exit", stage),
Some(StartFailure::ReadinessTimeout(_)) => ("readiness_timeout", "readiness"),
Some(StartFailure::Backend(stage, _)) => ("other", stage),
None if self.stage == "install" => ("install", self.stage),
None => ("other", self.stage),
}
}
pub(crate) fn at(stage: &'static str) -> impl FnOnce(anyhow::Error) -> Self {
move |error| Self { error, stage }
}
}
impl From<anyhow::Error> for StartError {
fn from(error: anyhow::Error) -> Self {
Self {
error,
stage: "unknown",
}
}
}
/// Typed anyhow context preserves both the original diagnostic and downcasts through it.
#[derive(Debug)]
pub(crate) enum StartFailure {
Spawn(String),
EarlyChildExit(&'static str, String),
Backend(&'static str, String),
ReadinessTimeout(String),
}
impl std::fmt::Display for StartFailure {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Spawn(message)
| Self::EarlyChildExit(_, message)
| Self::Backend(_, message)
| Self::ReadinessTimeout(message) => f.write_str(message),
}
}
}
/// Suppress child reporting when the foreground TUI handoff owns the observation.
pub const HANDOFF_ENV: &str = "CODEX_DAEMON_TELEMETRY_HANDOFF";
/// Only presence and enabled state leave the settings reader, never values or contents.
/// Missing files mean defaults; unreadable or invalid settings remain unknown.
pub async fn settings_tags(codex_home: &Path) -> [(&'static str, &'static str); 4] {
let path = codex_home
.join(crate::STATE_DIR_NAME)
.join(crate::SETTINGS_FILE_NAME);
crate::settings::telemetry_tags(&path).await.unwrap_or([
("auto_update", "unknown"),
("auto_update_setting", "unknown"),
("update_interval_setting", "unknown"),
("shutdown_grace_setting", "unknown"),
])
}