Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

Keep API request metrics out of Statsig exports (#36840)

pradeepg-oai5af85998c24f
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::<String>()
);
+ assert!(
+ body.contains("\"codex.api_request\""),
+ "expected API-request counter not found; body prefix: {}",
+ &body.chars().take(2000).collect::<String>()
+ );
+ assert!(
+ body.contains("\"codex.api_request.duration_ms\""),
+ "expected API-request duration not found; body prefix: {}",
+ &body.chars().take(2000).collect::<String>()
+ );
assert!(
body.contains("\"codex.tool.call\""),
"expected tool-call counter not found; body prefix: {}",