From f050203aa518631b2d25bfcfc62436be7b865b78 Mon Sep 17 00:00:00 2001 From: Ningyi Xie Date: Sat, 28 Feb 2026 09:32:24 -0800 Subject: [PATCH] Gate ChatGPT user ID metrics to app surfaces --- codex-rs/core/src/auth.rs | 6 +++ codex-rs/core/src/codex.rs | 6 +++ codex-rs/core/src/default_client.rs | 35 ++++++++++++++++ codex-rs/otel/src/lib.rs | 18 ++++++++- codex-rs/otel/src/traces/otel_manager.rs | 1 + codex-rs/otel/tests/suite/manager_metrics.rs | 42 ++++++++++++++++++++ codex-rs/tui/src/app.rs | 13 +++++- 7 files changed, 118 insertions(+), 3 deletions(-) diff --git a/codex-rs/core/src/auth.rs b/codex-rs/core/src/auth.rs index cca57d5c0f..ea60b6636d 100644 --- a/codex-rs/core/src/auth.rs +++ b/codex-rs/core/src/auth.rs @@ -262,6 +262,12 @@ impl CodexAuth { self.get_current_token_data().and_then(|t| t.id_token.email) } + /// Returns `None` if `is_chatgpt_auth()` is false. + pub fn get_chatgpt_user_id(&self) -> Option { + self.get_current_token_data() + .and_then(|t| t.id_token.chatgpt_user_id) + } + /// Account-facing plan classification derived from the current token. /// Returns a high-level `AccountPlanType` (e.g., Free/Plus/Pro/Team/…) /// mapped from the ID token's internal plan value. Prefer this when you diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index deb8b36039..a93be665ec 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -1282,6 +1282,9 @@ impl Session { let account_id = auth.and_then(CodexAuth::get_account_id); let account_email = auth.and_then(CodexAuth::get_account_email); let originator = crate::default_client::originator().value; + let chatgpt_user_id = auth.and_then(CodexAuth::get_chatgpt_user_id).filter(|_| { + crate::default_client::should_emit_chatgpt_user_id_metrics(originator.as_str()) + }); let terminal_type = terminal::user_agent(); let session_model = session_configuration.collaboration_mode.model().to_string(); let mut otel_manager = OtelManager::new( @@ -1296,6 +1299,9 @@ impl Session { terminal_type.clone(), session_configuration.session_source.clone(), ); + if let Some(chatgpt_user_id) = chatgpt_user_id.as_deref() { + otel_manager = otel_manager.with_chatgpt_user_id(chatgpt_user_id); + } if let Some(service_name) = session_configuration.metrics_service_name.as_deref() { otel_manager = otel_manager.with_metrics_service_name(service_name); } diff --git a/codex-rs/core/src/default_client.rs b/codex-rs/core/src/default_client.rs index aa490e8264..6c152f2252 100644 --- a/codex-rs/core/src/default_client.rs +++ b/codex-rs/core/src/default_client.rs @@ -26,6 +26,8 @@ use std::sync::RwLock; pub static USER_AGENT_SUFFIX: LazyLock>> = LazyLock::new(|| Mutex::new(None)); pub const DEFAULT_ORIGINATOR: &str = "codex_cli_rs"; pub const CODEX_INTERNAL_ORIGINATOR_OVERRIDE_ENV_VAR: &str = "CODEX_INTERNAL_ORIGINATOR_OVERRIDE"; +pub const CODEX_INTERNAL_EMIT_CHATGPT_USER_ID_METRICS_ENV_VAR: &str = + "CODEX_INTERNAL_EMIT_CHATGPT_USER_ID_METRICS"; pub const RESIDENCY_HEADER_NAME: &str = "x-openai-internal-codex-residency"; #[derive(Debug, Clone)] @@ -118,6 +120,15 @@ pub fn is_first_party_chat_originator(originator_value: &str) -> bool { originator_value == "codex_atlas" || originator_value == "codex_chatgpt_desktop" } +pub fn is_app_originator_allowed_for_chatgpt_user_id_metrics(originator_value: &str) -> bool { + originator_value == "codex_vscode" || originator_value == "Codex Desktop" +} + +pub fn should_emit_chatgpt_user_id_metrics(originator_value: &str) -> bool { + std::env::var_os(CODEX_INTERNAL_EMIT_CHATGPT_USER_ID_METRICS_ENV_VAR).is_some() + && is_app_originator_allowed_for_chatgpt_user_id_metrics(originator_value) +} + pub fn get_codex_user_agent() -> String { let build_version = env!("CARGO_PKG_VERSION"); let os_info = os_info::get(); @@ -249,6 +260,30 @@ mod tests { assert_eq!(is_first_party_chat_originator("codex_vscode"), false); } + #[test] + fn app_originator_allowlist_for_chatgpt_user_id_metrics_is_narrow() { + assert_eq!( + is_app_originator_allowed_for_chatgpt_user_id_metrics("codex_vscode"), + true + ); + assert_eq!( + is_app_originator_allowed_for_chatgpt_user_id_metrics("Codex Desktop"), + true + ); + assert_eq!( + is_app_originator_allowed_for_chatgpt_user_id_metrics(DEFAULT_ORIGINATOR), + false + ); + assert_eq!( + is_app_originator_allowed_for_chatgpt_user_id_metrics("Codex Something Else"), + false + ); + assert_eq!( + is_app_originator_allowed_for_chatgpt_user_id_metrics("codex_chatgpt_desktop"), + false + ); + } + #[tokio::test] async fn test_create_client_sets_default_headers() { skip_if_no_network!(); diff --git a/codex-rs/otel/src/lib.rs b/codex-rs/otel/src/lib.rs index 5aaee5f5a9..8dfbd923f7 100644 --- a/codex-rs/otel/src/lib.rs +++ b/codex-rs/otel/src/lib.rs @@ -44,6 +44,7 @@ pub struct OtelEventMetadata { pub(crate) auth_mode: Option, pub(crate) account_id: Option, pub(crate) account_email: Option, + pub(crate) chatgpt_user_id: Option, pub(crate) originator: String, pub(crate) service_name: Option, pub(crate) session_source: String, @@ -73,6 +74,11 @@ impl OtelManager { self } + pub fn with_chatgpt_user_id(mut self, chatgpt_user_id: &str) -> Self { + self.metadata.chatgpt_user_id = Some(sanitize_metric_tag_value(chatgpt_user_id)); + self + } + pub fn with_metrics(mut self, metrics: MetricsClient) -> Self { self.metrics = Some(metrics); self.metrics_use_metadata_tags = true; @@ -203,7 +209,7 @@ impl OtelManager { if !self.metrics_use_metadata_tags { return Ok(Vec::new()); } - let mut tags = Vec::with_capacity(7); + let mut tags = Vec::with_capacity(9); Self::push_metadata_tag(&mut tags, "auth_mode", self.metadata.auth_mode.as_deref())?; Self::push_metadata_tag( &mut tags, @@ -221,6 +227,16 @@ impl OtelManager { self.metadata.service_name.as_deref(), )?; Self::push_metadata_tag(&mut tags, "model", Some(self.metadata.model.as_str()))?; + Self::push_metadata_tag( + &mut tags, + "enduser.id", + self.metadata.chatgpt_user_id.as_deref(), + )?; + Self::push_metadata_tag( + &mut tags, + "user_id", + self.metadata.chatgpt_user_id.as_deref(), + )?; Self::push_metadata_tag(&mut tags, "app.version", Some(self.metadata.app_version))?; Ok(tags) } diff --git a/codex-rs/otel/src/traces/otel_manager.rs b/codex-rs/otel/src/traces/otel_manager.rs index 32b9fba860..b4492e28e6 100644 --- a/codex-rs/otel/src/traces/otel_manager.rs +++ b/codex-rs/otel/src/traces/otel_manager.rs @@ -78,6 +78,7 @@ impl OtelManager { auth_mode: auth_mode.map(|m| m.to_string()), account_id, account_email, + chatgpt_user_id: None, originator: sanitize_metric_tag_value(originator.as_str()), service_name: None, session_source: session_source.to_string(), diff --git a/codex-rs/otel/tests/suite/manager_metrics.rs b/codex-rs/otel/tests/suite/manager_metrics.rs index 53a9cc89d6..4090a19cb2 100644 --- a/codex-rs/otel/tests/suite/manager_metrics.rs +++ b/codex-rs/otel/tests/suite/manager_metrics.rs @@ -153,3 +153,45 @@ fn manager_attaches_optional_service_name_tag() -> Result<()> { Ok(()) } + +#[test] +fn manager_attaches_chatgpt_user_id_tags_when_present() -> Result<()> { + let (metrics, exporter) = build_metrics_with_defaults(&[])?; + let manager = OtelManager::new( + ThreadId::new(), + "gpt-5.1", + "gpt-5.1", + Some("account-id".to_string()), + None, + Some(TelemetryAuthMode::Chatgpt), + "codex_vscode".to_string(), + true, + "tty".to_string(), + SessionSource::Cli, + ) + .with_chatgpt_user_id("user-123") + .with_metrics(metrics); + + manager.counter("codex.session_started", 1, &[]); + manager.shutdown_metrics()?; + + let resource_metrics = latest_metrics(&exporter); + let metric = + find_metric(&resource_metrics, "codex.session_started").expect("counter metric missing"); + let attrs = match metric.data() { + AggregatedMetrics::U64(data) => match data { + MetricData::Sum(sum) => { + let points: Vec<_> = sum.data_points().collect(); + assert_eq!(points.len(), 1); + attributes_to_map(points[0].attributes()) + } + _ => panic!("unexpected counter aggregation"), + }, + _ => panic!("unexpected counter data type"), + }; + + assert_eq!(attrs.get("enduser.id"), Some(&"user-123".to_string())); + assert_eq!(attrs.get("user_id"), Some(&"user-123".to_string())); + + Ok(()) +} diff --git a/codex-rs/tui/src/app.rs b/codex-rs/tui/src/app.rs index 52aefb4727..82f7fa8fd1 100644 --- a/codex-rs/tui/src/app.rs +++ b/codex-rs/tui/src/app.rs @@ -1560,18 +1560,27 @@ impl App { let auth_mode = auth_ref .map(CodexAuth::auth_mode) .map(TelemetryAuthMode::from); - let otel_manager = OtelManager::new( + let originator = codex_core::default_client::originator().value; + let chatgpt_user_id = auth_ref + .and_then(CodexAuth::get_chatgpt_user_id) + .filter(|_| { + codex_core::default_client::should_emit_chatgpt_user_id_metrics(originator.as_str()) + }); + let mut otel_manager = OtelManager::new( ThreadId::new(), model.as_str(), model.as_str(), auth_ref.and_then(CodexAuth::get_account_id), auth_ref.and_then(CodexAuth::get_account_email), auth_mode, - codex_core::default_client::originator().value, + originator, config.otel.log_user_prompt, codex_core::terminal::user_agent(), SessionSource::Cli, ); + if let Some(chatgpt_user_id) = chatgpt_user_id.as_deref() { + otel_manager = otel_manager.with_chatgpt_user_id(chatgpt_user_id); + } if config .tui_status_line .as_ref()