mirror of
https://github.com/openai/codex.git
synced 2026-11-26 23:40:43 +00:00
Attribute tool telemetry to the invoking turn's product SKU (#47741)
## Why Tool telemetry needs to distinguish product SKUs without turning arbitrary configuration values into metric labels. ## What changed - Read `apps_mcp_product_sku` from the invoking turn when dispatching tools, replacing any stale SKU in the telemetry metadata. - Add `product_sku` to tool call count and duration metrics and tool result log and trace events. - Preserve `codex`, map other nonempty values to `other`, and omit the attribute for empty or missing values. ## Testing Add coverage for SKU separation on a shared metrics client, normalization, and successful and failing dispatches with stale telemetry metadata. Extend export routing tests to check SKU attributes in logs and matching traces. GitOrigin-RevId: 3fc58c9f7245fd5b65f7cba122e08d7ea838b2d6
This commit is contained in:
@@ -527,7 +527,11 @@ impl ToolRegistry {
|
||||
) -> Result<AnyToolResult, FunctionCallError> {
|
||||
let tool_name = invocation.tool_name.clone();
|
||||
let call_id_owned = invocation.call_id.clone();
|
||||
let otel = invocation.step_context.session_telemetry.clone();
|
||||
let otel = invocation
|
||||
.step_context
|
||||
.session_telemetry
|
||||
.clone()
|
||||
.with_product_sku(invocation.turn.config.apps_mcp_product_sku.as_deref());
|
||||
// TODO(anp): Reconcile these tags with TurnEnvironment::sandbox_context
|
||||
// instead of reporting the thread-wide backend for environment-scoped tools.
|
||||
let sandbox_tags = invocation.turn.turn_metadata_state.sandbox_tags;
|
||||
|
||||
@@ -1,11 +1,20 @@
|
||||
use super::*;
|
||||
use crate::session::session::Session;
|
||||
use crate::session::step_context::StepContext;
|
||||
use codex_otel::MetricsClient;
|
||||
use codex_otel::MetricsConfig;
|
||||
use codex_otel::TOOL_CALL_COUNT_METRIC;
|
||||
use codex_otel::TOOL_CALL_DURATION_METRIC;
|
||||
use codex_protocol::DEFAULT_FUNCTION_NAMESPACE;
|
||||
use codex_protocol::models::ResponseItem;
|
||||
use codex_utils_output_truncation::TruncationPolicy;
|
||||
use futures::future::BoxFuture;
|
||||
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::sync::atomic::AtomicUsize;
|
||||
use std::sync::atomic::Ordering;
|
||||
|
||||
@@ -819,6 +828,94 @@ async fn dispatch_uses_canonical_tool_names_for_lifecycle_contributors() -> anyh
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn dispatch_uses_the_invoking_turn_product_sku() -> anyhow::Result<()> {
|
||||
let metrics = MetricsClient::new(
|
||||
MetricsConfig::in_memory(
|
||||
"test",
|
||||
"codex-core",
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
InMemoryMetricExporter::default(),
|
||||
)
|
||||
.with_runtime_reader(),
|
||||
)?;
|
||||
let registry = ToolRegistry::from_tools([
|
||||
Arc::new(LifecycleTestHandler {
|
||||
tool_name: codex_tools::ToolName::plain("ok_tool"),
|
||||
result: LifecycleTestResult::Ok { success: true },
|
||||
}) as Arc<dyn CoreToolRuntime>,
|
||||
Arc::new(LifecycleTestHandler {
|
||||
tool_name: codex_tools::ToolName::plain("failing_tool"),
|
||||
result: LifecycleTestResult::Err,
|
||||
}) as Arc<dyn CoreToolRuntime>,
|
||||
]);
|
||||
let mut expected = BTreeMap::new();
|
||||
let product_skus = [Some("codex"), None];
|
||||
for sku in product_skus {
|
||||
let (session, mut turn) = crate::session::tests::make_session_and_context().await;
|
||||
Arc::make_mut(&mut turn.config).apps_mcp_product_sku = sku.map(str::to_owned);
|
||||
turn.session_telemetry = turn
|
||||
.session_telemetry
|
||||
.clone()
|
||||
.with_product_sku(Some("stale-product"))
|
||||
.with_metrics(metrics.clone());
|
||||
let session = Arc::new(session);
|
||||
let turn = Arc::new(turn);
|
||||
for (tool, success) in [("ok_tool", true), ("failing_tool", false)] {
|
||||
let result = registry
|
||||
.dispatch_any_with_state(
|
||||
test_invocation(
|
||||
Arc::clone(&session),
|
||||
Arc::clone(&turn),
|
||||
"sku-call",
|
||||
codex_tools::ToolName::plain(tool).with_default_namespace(),
|
||||
),
|
||||
/*call_state*/ None,
|
||||
)
|
||||
.await;
|
||||
assert_eq!(result.is_ok(), success);
|
||||
expected.insert((sku.map(str::to_owned), success.to_string()), 1);
|
||||
}
|
||||
}
|
||||
let snapshot = metrics.snapshot()?;
|
||||
for name in [TOOL_CALL_COUNT_METRIC, TOOL_CALL_DURATION_METRIC] {
|
||||
let metric = snapshot
|
||||
.scope_metrics()
|
||||
.flat_map(ScopeMetrics::metrics)
|
||||
.find(|metric| metric.name() == name)
|
||||
.expect("tool metric");
|
||||
let points = match metric.data() {
|
||||
AggregatedMetrics::U64(MetricData::Sum(sum)) => sum
|
||||
.data_points()
|
||||
.map(|point| (point.attributes().collect::<Vec<_>>(), point.value()))
|
||||
.collect::<Vec<_>>(),
|
||||
AggregatedMetrics::F64(MetricData::Histogram(histogram)) => histogram
|
||||
.data_points()
|
||||
.map(|point| (point.attributes().collect::<Vec<_>>(), point.count()))
|
||||
.collect::<Vec<_>>(),
|
||||
_ => panic!("unexpected tool metric aggregation"),
|
||||
};
|
||||
let actual = points
|
||||
.into_iter()
|
||||
.map(|(attributes, count)| {
|
||||
let labels = attributes
|
||||
.into_iter()
|
||||
.map(|attribute| (attribute.key.as_str(), attribute.value.as_str().to_string()))
|
||||
.collect::<BTreeMap<_, _>>();
|
||||
(
|
||||
(
|
||||
labels.get("product_sku").cloned(),
|
||||
labels["success"].clone(),
|
||||
),
|
||||
count,
|
||||
)
|
||||
})
|
||||
.collect::<BTreeMap<_, _>>();
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn test_invocation(
|
||||
session: Arc<crate::session::session::Session>,
|
||||
turn: Arc<crate::session::turn_context::TurnContext>,
|
||||
|
||||
Reference in New Issue
Block a user