From 5af85998c24fb3353ddd8164c3ed472057b03cb3 Mon Sep 17 00:00:00 2001 From: pradeepg-oai Date: Tue, 4 Aug 2026 05:19:11 +0000 Subject: [PATCH] Keep API request metrics out of Statsig exports (#36840) ## What changed - Treat `codex.api_request` and `codex.api_request.duration_ms` as runtime-only metrics for Statsig exporters, matching the existing tool-call metric behavior. - Keep both metrics available through other OTLP exporters. ## Testing - Extend the Statsig filtering test to cover both API request metrics. - Verify the OTLP HTTP exporter still sends the API request count and duration. GitOrigin-RevId: f0f800ade7979e4da72ae59641bf42aa8a3ebd5a --- codex-rs/otel/src/metrics/config.rs | 9 ++++++++- codex-rs/otel/src/provider.rs | 4 ++++ codex-rs/otel/tests/suite/otlp_http_loopback.rs | 16 ++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/codex-rs/otel/src/metrics/config.rs b/codex-rs/otel/src/metrics/config.rs index 6d9c18a87b..4228a0a71d 100644 --- a/codex-rs/otel/src/metrics/config.rs +++ b/codex-rs/otel/src/metrics/config.rs @@ -1,5 +1,7 @@ use crate::config::OtelExporter; use crate::metrics::Result; +use crate::metrics::names::API_CALL_COUNT_METRIC; +use crate::metrics::names::API_CALL_DURATION_METRIC; use crate::metrics::names::TOOL_CALL_COUNT_METRIC; use crate::metrics::names::TOOL_CALL_DURATION_METRIC; use crate::metrics::validation::validate_tag_key; @@ -8,7 +10,12 @@ use opentelemetry_sdk::metrics::InMemoryMetricExporter; use std::collections::BTreeMap; use std::time::Duration; -const RUNTIME_ONLY_METRICS: &[&str] = &[TOOL_CALL_COUNT_METRIC, TOOL_CALL_DURATION_METRIC]; +const RUNTIME_ONLY_METRICS: &[&str] = &[ + API_CALL_COUNT_METRIC, + API_CALL_DURATION_METRIC, + TOOL_CALL_COUNT_METRIC, + TOOL_CALL_DURATION_METRIC, +]; #[derive(Clone, Debug)] pub enum MetricsExporter { diff --git a/codex-rs/otel/src/provider.rs b/codex-rs/otel/src/provider.rs index 72a66d5651..448bf07ac0 100644 --- a/codex-rs/otel/src/provider.rs +++ b/codex-rs/otel/src/provider.rs @@ -462,6 +462,8 @@ mod shutdown_tests; #[cfg(test)] mod tests { use super::*; + use crate::metrics::API_CALL_COUNT_METRIC; + use crate::metrics::API_CALL_DURATION_METRIC; use crate::metrics::MetricsExporter; use crate::metrics::TOOL_CALL_COUNT_METRIC; use crate::metrics::TOOL_CALL_DURATION_METRIC; @@ -544,6 +546,8 @@ mod tests { config.exporter = MetricsExporter::InMemory(exporter.clone()); let metrics = MetricsClient::new(config)?; + metrics.counter(API_CALL_COUNT_METRIC, /*inc*/ 1, &[])?; + metrics.record_duration(API_CALL_DURATION_METRIC, Duration::from_millis(100), &[])?; metrics.counter(TOOL_CALL_COUNT_METRIC, /*inc*/ 1, &[])?; metrics.record_duration(TOOL_CALL_DURATION_METRIC, Duration::from_millis(25), &[])?; metrics.counter("codex.turns", /*inc*/ 1, &[])?; diff --git a/codex-rs/otel/tests/suite/otlp_http_loopback.rs b/codex-rs/otel/tests/suite/otlp_http_loopback.rs index b3351dabe8..01bd6c25db 100644 --- a/codex-rs/otel/tests/suite/otlp_http_loopback.rs +++ b/codex-rs/otel/tests/suite/otlp_http_loopback.rs @@ -186,6 +186,12 @@ fn otlp_http_exporter_sends_metrics_to_collector() -> Result<()> { ))?; metrics.counter("codex.turns", /*inc*/ 1, &[("source", "test")])?; + metrics.counter("codex.api_request", /*inc*/ 1, &[("status", "200")])?; + metrics.record_duration( + "codex.api_request.duration_ms", + Duration::from_millis(100), + &[("status", "200")], + )?; metrics.counter("codex.tool.call", /*inc*/ 1, &[("tool", "test")])?; metrics.record_duration( "codex.tool.call.duration_ms", @@ -227,6 +233,16 @@ fn otlp_http_exporter_sends_metrics_to_collector() -> Result<()> { "expected gauge not found; body prefix: {}", &body.chars().take(2000).collect::() ); + assert!( + body.contains("\"codex.api_request\""), + "expected API-request counter not found; body prefix: {}", + &body.chars().take(2000).collect::() + ); + assert!( + body.contains("\"codex.api_request.duration_ms\""), + "expected API-request duration not found; body prefix: {}", + &body.chars().take(2000).collect::() + ); assert!( body.contains("\"codex.tool.call\""), "expected tool-call counter not found; body prefix: {}",