Files
codex/codex-rs/tui/src/daemon_telemetry.rs
T
Ian MacLeod c22a06c180 Record bucketed terminal sizes in TUI telemetry (#53859)
## What changed

- Add `terminal_columns` and `terminal_rows` buckets to `codex.tui.start`.
- Record `codex.tui.terminal_size` for new turns in the active thread and for resize bucket changes after the terminal size settles for 15 seconds. Reuse launch context and suppress duplicate turn notifications.
- Respect analytics consent for the built-in metrics exporter while allowing explicitly configured OTLP collectors. Apply consent checks to the active exporter across provider replacements.

## Testing

Add coverage for exporter replacement and consent after config reloads. Extend the PTY integration test to verify startup size buckets, turn-start observations, and delayed resize observations with analytics disabled and an explicit OTLP collector.

GitOrigin-RevId: e01a900cd3f04a36731078f725c75374596cb01b
2026-10-14 03:52:30 +00:00

68 lines
2.4 KiB
Rust

//! Records automatic daemon startup and ensures launch observations survive setup failures.
use super::*;
use crate::terminal_size_telemetry::LaunchTags;
/// Emit once after connection, or as unconfirmed if startup returns early.
pub(super) struct Launch<F: FnOnce(&AppServerTarget, bool) -> LaunchTags>(pub(super) Option<F>);
impl<F: FnOnce(&AppServerTarget, bool) -> LaunchTags> Launch<F> {
pub(super) fn record(mut self, target: &AppServerTarget, connected: bool) -> LaunchTags {
self.0
.take()
.map(|record| record(target, connected))
.unwrap_or_default()
}
}
impl<F: FnOnce(&AppServerTarget, bool) -> LaunchTags> Drop for Launch<F> {
fn drop(&mut self) {
if let Some(record) = self.0.take() {
record(&AppServerTarget::Embedded, /*connected*/ false);
}
}
}
pub(super) async fn record_start(
config: &Config,
result: &anyhow::Result<codex_app_server_daemon::LifecycleOutput>,
) {
use codex_app_server_daemon::LifecycleStatus;
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",
};
// 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() {
let mut tags = codex_app_server_daemon::telemetry::settings_tags(&config.codex_home)
.await
.to_vec();
tags.extend([
("initiation_source", "tui_auto_start"),
("outcome", outcome),
]);
let _ = metrics.counter("codex.daemon.start", /*inc*/ 1, &tags);
}
let _ = otel
.shutdown_with_timeout(INTERACTIVE_OTEL_SHUTDOWN_TIMEOUT)
.await;
}