Files
codex/codex-rs/otel/src/metrics/mod.rs
T
jif d9eac10406 Reload app-server telemetry after account changes (#37339)
## Why

An account change can select different OpenTelemetry collector settings. The
long-running app server needs to stop exporting telemetry with the previous
account's configuration.

## What changed

- Rebuild and install the log, trace, and metrics exporters when authentication
  changes, and shut down the previous provider without blocking the app server.
- Make cached global metrics clients and the tracing layer follow the replacement
  provider so existing callers continue exporting through the current account's
  configuration.
- Clear the cached cloud configuration loader after login and logout so telemetry
  reloads from the new account context.

## Testing

- Add an app-server integration test that switches accounts and verifies logs,
  traces, and metrics reach the new collector while trace context is preserved.
- Add a metrics test covering cached clients after provider replacement.

GitOrigin-RevId: a74359777a35edbee5bbe0b2baccc155074cb172
2026-08-07 00:28:32 +00:00

53 lines
1.6 KiB
Rust

mod client;
mod config;
mod error;
pub(crate) mod names;
mod process;
pub(crate) mod runtime_metrics;
pub(crate) mod tags;
pub(crate) mod timer;
pub(crate) mod validation;
use crate::config::StatsigMetricsSettings;
pub use crate::metrics::client::MetricsClient;
pub use crate::metrics::config::MetricsConfig;
pub use crate::metrics::config::MetricsExporter;
pub use crate::metrics::error::MetricsError;
pub use crate::metrics::error::Result;
pub use crate::metrics::process::record_process_start_once;
pub use names::*;
use std::sync::Arc;
use std::sync::OnceLock;
use std::sync::RwLock;
pub use tags::ORIGINATOR_TAG;
pub use tags::SessionMetricTagValues;
pub use tags::bounded_originator_tag_value;
static GLOBAL_METRICS: OnceLock<MetricsClient> = OnceLock::new();
static GLOBAL_STATSIG_METRICS_SETTINGS: OnceLock<StatsigMetricsSettings> = OnceLock::new();
pub(crate) fn install_global(mut metrics: MetricsClient) -> MetricsClient {
let active = GLOBAL_METRICS
.get()
.and_then(|current| current.active.clone())
.unwrap_or_else(|| Arc::new(RwLock::new(Arc::clone(&metrics.inner))));
*active
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner) = Arc::clone(&metrics.inner);
metrics.active = Some(active);
let _ = GLOBAL_METRICS.set(metrics.clone());
metrics
}
pub fn global() -> Option<MetricsClient> {
GLOBAL_METRICS.get().cloned()
}
pub(crate) fn install_global_statsig_settings(settings: StatsigMetricsSettings) {
let _ = GLOBAL_STATSIG_METRICS_SETTINGS.set(settings);
}
pub(crate) fn global_statsig_settings() -> Option<StatsigMetricsSettings> {
GLOBAL_STATSIG_METRICS_SETTINGS.get().cloned()
}