mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +00:00
## 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
116 lines
3.5 KiB
Rust
116 lines
3.5 KiB
Rust
use crate::SessionTelemetry;
|
|
use crate::events::shared::log_and_trace_event;
|
|
use crate::events::shared::log_event;
|
|
use crate::events::shared::tool_namespace;
|
|
use crate::events::shared::trace_event;
|
|
use codex_protocol::ToolName;
|
|
use codex_protocol::config_types::ToolResultLogConfig;
|
|
use codex_utils_string::take_bytes_at_char_boundary;
|
|
use std::borrow::Cow;
|
|
use std::sync::atomic::AtomicU64;
|
|
use std::sync::atomic::Ordering;
|
|
use std::time::Duration;
|
|
|
|
const TRUNCATION_NOTICE: &str = "[... telemetry preview truncated ...]";
|
|
|
|
/// Result-recording order across all sessions in this process, not tool-start order.
|
|
/// Independent of the optional rollout-trace writer and reset on process restart.
|
|
fn next_tool_result_seq() -> u64 {
|
|
static NEXT_SEQ: AtomicU64 = AtomicU64::new(1);
|
|
NEXT_SEQ.fetch_add(/*val*/ 1, Ordering::Relaxed)
|
|
}
|
|
|
|
pub(crate) struct ToolResultEvent<'a> {
|
|
pub tool_name: &'a ToolName,
|
|
pub call_id: &'a str,
|
|
pub arguments: &'a str,
|
|
pub mcp_server: &'a str,
|
|
pub mcp_server_origin: &'a str,
|
|
pub duration: Duration,
|
|
pub success: bool,
|
|
pub output: &'a str,
|
|
}
|
|
|
|
/// Emits diagnostic logs and trace-safe metadata without changing tool-call metrics.
|
|
pub(crate) fn emit_tool_result(
|
|
telemetry: &SessionTelemetry,
|
|
limits: ToolResultLogConfig,
|
|
event: ToolResultEvent<'_>,
|
|
) {
|
|
let ToolResultEvent {
|
|
tool_name,
|
|
call_id,
|
|
arguments,
|
|
mcp_server,
|
|
mcp_server_origin,
|
|
duration,
|
|
success,
|
|
output,
|
|
} = event;
|
|
let tool_namespace = tool_namespace(tool_name);
|
|
let preview = telemetry_preview(output, limits);
|
|
let tool_result_seq = next_tool_result_seq();
|
|
|
|
log_and_trace_event!(
|
|
telemetry,
|
|
common: {
|
|
event.name = "codex.tool_result",
|
|
tool_result_seq = tool_result_seq,
|
|
tool_name = %tool_name.name,
|
|
tool_namespace = %tool_namespace,
|
|
call_id = %call_id,
|
|
product_sku = telemetry.metadata.product_sku,
|
|
duration_ms = %duration.as_millis(),
|
|
success = %success,
|
|
output_truncated = preview.truncated,
|
|
},
|
|
log: {
|
|
agent_name = %telemetry.metadata.agent_name,
|
|
arguments = %arguments,
|
|
output = %preview.text,
|
|
mcp_server = %mcp_server,
|
|
mcp_server_origin = %mcp_server_origin,
|
|
},
|
|
trace: {
|
|
arguments_length = arguments.len() as i64,
|
|
output_length = output.len() as i64,
|
|
output_line_count = output.lines().count() as i64,
|
|
tool_origin = if mcp_server.is_empty() { "builtin" } else { "mcp" },
|
|
mcp_tool = !mcp_server.is_empty(),
|
|
},
|
|
);
|
|
}
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
pub(crate) struct ToolResultPreview<'a> {
|
|
pub text: Cow<'a, str>,
|
|
/// Whether this telemetry limit removed text, not whether the tool result was lossless.
|
|
pub truncated: bool,
|
|
}
|
|
|
|
pub(crate) fn telemetry_preview(
|
|
content: &str,
|
|
limits: ToolResultLogConfig,
|
|
) -> ToolResultPreview<'_> {
|
|
let prefix = take_bytes_at_char_boundary(content, limits.max_bytes);
|
|
if prefix.len() == content.len() {
|
|
return ToolResultPreview {
|
|
text: Cow::Borrowed(content),
|
|
truncated: false,
|
|
};
|
|
}
|
|
let separator = if prefix.is_empty() || prefix.ends_with('\n') {
|
|
""
|
|
} else {
|
|
"\n"
|
|
};
|
|
ToolResultPreview {
|
|
text: Cow::Owned(format!("{prefix}{separator}{TRUNCATION_NOTICE}")),
|
|
truncated: true,
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[path = "tool_result_tests.rs"]
|
|
mod tests;
|