Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

Align installed app duration metrics with the legacy baseline (#35015)

Matthew Zengceb2ffb793b7
diff --git a/codex-rs/app-server/src/request_processors/apps_processor/installed.rs b/codex-rs/app-server/src/request_processors/apps_processor/installed.rs
index 75192c0549..e2360f4744 100644
--- a/codex-rs/app-server/src/request_processors/apps_processor/installed.rs
+++ b/codex-rs/app-server/src/request_processors/apps_processor/installed.rs
@@ -14,6 +14,10 @@ use codex_mcp::host_owned_codex_apps_enabled;
use codex_mcp::tool_is_model_visible;
use codex_protocol::models::PermissionProfile;
+#[cfg(test)]
+#[path = "installed_tests.rs"]
+mod tests;
+
const CONNECTOR_RUNTIME_REFRESH_TIMEOUT: Duration = Duration::from_secs(30);
const APPS_INSTALLED_SUBMIT_ID: &str = "app-installed";
const APPS_INSTALLED_RESPONSE_BYTES_METRIC: &str = "codex.apps.installed.response_bytes";
@@ -21,6 +25,11 @@ const APPS_INSTALLED_CONNECTOR_COUNT_METRIC: &str = "codex.apps.installed.connec
const APPS_INSTALLED_TOOL_COUNT_METRIC: &str = "codex.apps.installed.tool_count";
const APPS_SNAPSHOT_AGE_METRIC: &str = "codex.apps.snapshot.age_ms";
+struct AppsInstalledSnapshotMetrics {
+ age: Option<Duration>,
+ tool_count: usize,
+}
+
impl AppsRequestProcessor {
pub(crate) async fn apps_installed(
&self,
@@ -172,15 +181,20 @@ impl AppsRequestProcessor {
}
.await;
- record_apps_installed_metrics(
- started_at,
- force_refresh,
- retained_previous_snapshot,
- refresh_disposition,
- snapshot_age,
- snapshot_tool_count,
- result.as_ref().ok(),
- );
+ if let Some(metrics) = codex_otel::global() {
+ record_apps_installed_metrics(
+ &metrics,
+ started_at,
+ force_refresh,
+ retained_previous_snapshot,
+ refresh_disposition,
+ AppsInstalledSnapshotMetrics {
+ age: snapshot_age,
+ tool_count: snapshot_tool_count,
+ },
+ result.as_ref().ok(),
+ );
+ }
result
}
@@ -220,23 +234,18 @@ fn connector_runtime_tool(tool: &ToolInfo) -> ConnectorRuntimeTool<'_> {
}
fn record_apps_installed_metrics(
+ metrics: &codex_otel::MetricsClient,
started_at: Instant,
force_refresh: bool,
retained_previous_snapshot: bool,
refresh_disposition: &'static str,
- snapshot_age: Option<Duration>,
- snapshot_tool_count: usize,
+ snapshot_metrics: AppsInstalledSnapshotMetrics,
response: Option<&AppsInstalledResponse>,
) {
- let Some(metrics) = codex_otel::global() else {
+ let Some(response) = response else {
return;
};
let force_refresh = if force_refresh { "true" } else { "false" };
- let outcome = if response.is_some() {
- "success"
- } else {
- "error"
- };
let retained_previous_snapshot = if retained_previous_snapshot {
"true"
} else {
@@ -246,16 +255,14 @@ fn record_apps_installed_metrics(
APPS_INSTALLED_DURATION_METRIC,
started_at.elapsed(),
&[
- ("path", "new"),
+ ("path", "installed"),
+ ("reload", force_refresh),
("force_refresh", force_refresh),
("refresh", refresh_disposition),
- ("outcome", outcome),
+ ("outcome", "success"),
("retained_previous_snapshot", retained_previous_snapshot),
],
);
- let Some(response) = response else {
- return;
- };
if let Ok(bytes) = serde_json::to_vec(response) {
let _ = metrics.histogram(
APPS_INSTALLED_RESPONSE_BYTES_METRIC,
@@ -270,10 +277,10 @@ fn record_apps_installed_metrics(
);
let _ = metrics.histogram(
APPS_INSTALLED_TOOL_COUNT_METRIC,
- i64::try_from(snapshot_tool_count).unwrap_or(i64::MAX),
+ i64::try_from(snapshot_metrics.tool_count).unwrap_or(i64::MAX),
&[("path", "new")],
);
- if let Some(snapshot_age) = snapshot_age {
+ if let Some(snapshot_age) = snapshot_metrics.age {
let _ = metrics.record_duration(
APPS_SNAPSHOT_AGE_METRIC,
snapshot_age,
diff --git a/codex-rs/app-server/src/request_processors/apps_processor/installed_tests.rs b/codex-rs/app-server/src/request_processors/apps_processor/installed_tests.rs
new file mode 100644
index 0000000000..94f106fcde
--- /dev/null
+++ b/codex-rs/app-server/src/request_processors/apps_processor/installed_tests.rs
@@ -0,0 +1,149 @@
+use super::APPS_INSTALLED_DURATION_METRIC;
+use super::AppsInstalledSnapshotMetrics;
+use super::record_apps_installed_metrics;
+use anyhow::Result;
+use codex_app_server_protocol::AppsInstalledResponse;
+use codex_otel::MetricsClient;
+use codex_otel::MetricsConfig;
+use opentelemetry_sdk::metrics::InMemoryMetricExporter;
+use opentelemetry_sdk::metrics::data::AggregatedMetrics;
+use opentelemetry_sdk::metrics::data::MetricData;
+use opentelemetry_sdk::metrics::data::ScopeMetrics;
+use pretty_assertions::assert_eq;
+use std::collections::BTreeMap;
+use std::time::Instant;
+
+fn test_metrics() -> Result<MetricsClient> {
+ Ok(MetricsClient::new(
+ MetricsConfig::in_memory(
+ "test",
+ "codex-app-server",
+ env!("CARGO_PKG_VERSION"),
+ InMemoryMetricExporter::default(),
+ )
+ .with_runtime_reader(),
+ )?)
+}
+
+#[test]
+fn installed_duration_records_one_sample_per_success_with_legacy_comparison_dimensions()
+-> Result<()> {
+ let metrics = test_metrics()?;
+ let response = AppsInstalledResponse { apps: Vec::new() };
+
+ record_apps_installed_metrics(
+ &metrics,
+ Instant::now(),
+ /*force_refresh*/ false,
+ /*retained_previous_snapshot*/ false,
+ "not_requested",
+ AppsInstalledSnapshotMetrics {
+ age: None,
+ tool_count: 0,
+ },
+ Some(&response),
+ );
+ record_apps_installed_metrics(
+ &metrics,
+ Instant::now(),
+ /*force_refresh*/ true,
+ /*retained_previous_snapshot*/ false,
+ "success",
+ AppsInstalledSnapshotMetrics {
+ age: None,
+ tool_count: 0,
+ },
+ Some(&response),
+ );
+
+ let snapshot = metrics.snapshot()?;
+ let metric = snapshot
+ .scope_metrics()
+ .flat_map(ScopeMetrics::metrics)
+ .find(|metric| metric.name() == APPS_INSTALLED_DURATION_METRIC)
+ .expect("installed duration metric should be recorded");
+ let mut points = match metric.data() {
+ AggregatedMetrics::F64(MetricData::Histogram(histogram)) => histogram
+ .data_points()
+ .map(|point| {
+ let attributes = point
+ .attributes()
+ .map(|attribute| {
+ (
+ attribute.key.as_str().to_string(),
+ attribute.value.as_str().to_string(),
+ )
+ })
+ .collect::<BTreeMap<_, _>>();
+ (attributes, point.count())
+ })
+ .collect::<Vec<_>>(),
+ _ => panic!("installed duration should be a floating-point histogram"),
+ };
+ points.sort_by(|(left, _), (right, _)| left.cmp(right));
+
+ assert_eq!(
+ points,
+ vec![
+ (
+ BTreeMap::from([
+ ("force_refresh".to_string(), "false".to_string()),
+ ("outcome".to_string(), "success".to_string()),
+ ("path".to_string(), "installed".to_string()),
+ ("refresh".to_string(), "not_requested".to_string()),
+ ("reload".to_string(), "false".to_string()),
+ (
+ "retained_previous_snapshot".to_string(),
+ "false".to_string()
+ ),
+ ]),
+ 1,
+ ),
+ (
+ BTreeMap::from([
+ ("force_refresh".to_string(), "true".to_string()),
+ ("outcome".to_string(), "success".to_string()),
+ ("path".to_string(), "installed".to_string()),
+ ("refresh".to_string(), "success".to_string()),
+ ("reload".to_string(), "true".to_string()),
+ (
+ "retained_previous_snapshot".to_string(),
+ "false".to_string()
+ ),
+ ]),
+ 1,
+ ),
+ ]
+ );
+
+ Ok(())
+}
+
+#[test]
+fn installed_duration_does_not_record_failed_requests() -> Result<()> {
+ let metrics = test_metrics()?;
+
+ record_apps_installed_metrics(
+ &metrics,
+ Instant::now(),
+ /*force_refresh*/ true,
+ /*retained_previous_snapshot*/ true,
+ "error",
+ AppsInstalledSnapshotMetrics {
+ age: None,
+ tool_count: 0,
+ },
+ /*response*/ None,
+ );
+
+ let snapshot = metrics.snapshot()?;
+ assert!(
+ snapshot
+ .scope_metrics()
+ .flat_map(ScopeMetrics::metrics)
+ .all(|metric| metric.name() != APPS_INSTALLED_DURATION_METRIC),
+ "failed installed requests must not record a successful duration sample",
+ );
+
+ Ok(())
+}