Use explicit histogram buckets for tool and skill context metrics (#48819)

## What changed

- Give tool fragment sizes and namespace counts logarithmic boundaries through 32,768.
- Give enabled and kept skill counts integer boundaries from 0 through 512, removed description characters logarithmic boundaries through 131,072, and skill truncation flags boundaries at 0 and 1.
- Record these metrics with explicit boundaries, separating zero values and retaining overflow buckets above each range.

## Testing

Extend tool and skill metric tests to assert explicit boundaries. Add an export test covering zero, one, the upper boundary, and overflow, including bucket counts, ordering, and sample totals.

GitOrigin-RevId: 9fbf3acbe352d7c74f3ad11f0b690148c083a6ef
This commit is contained in:
Matthew Zeng
2026-09-28 00:05:54 +00:00
committed by copyberry
parent 659b35f131
commit 456212ca21
6 changed files with 140 additions and 28 deletions
@@ -9,8 +9,8 @@ use super::WorldStateSection;
use crate::context::ContextualUserFragment;
use codex_extension_api::ExtensionMetrics;
use codex_extension_api::RenderedWorldStateFragment;
use codex_otel::CONTEXT_FRAGMENT_BYTES_BUCKETS;
use codex_otel::THREAD_TOOLS_FRAGMENT_BYTES_METRIC;
use codex_otel::THREAD_TOOLS_METRIC_BUCKETS;
use codex_otel::THREAD_TOOLS_NAMESPACES_TOTAL_METRIC;
use codex_protocol::models::ContentItemKind;
use codex_protocol::protocol::TOOLS_CLOSE_TAG;
@@ -231,15 +231,16 @@ fn record_fragment_metrics(
};
for (stage, size) in [("before", &rendered.before), ("after", &rendered.after)] {
let tags = [("stage", stage), ("kind", kind)];
metrics.histogram(
metrics.histogram_with_boundaries(
THREAD_TOOLS_NAMESPACES_TOTAL_METRIC,
i64::try_from(size.namespaces).unwrap_or(i64::MAX),
THREAD_TOOLS_METRIC_BUCKETS.as_slice(),
&tags,
);
metrics.histogram_with_boundaries(
THREAD_TOOLS_FRAGMENT_BYTES_METRIC,
i64::try_from(size.bytes).unwrap_or(i64::MAX),
CONTEXT_FRAGMENT_BYTES_BUCKETS,
THREAD_TOOLS_METRIC_BUCKETS.as_slice(),
&tags,
);
}
@@ -6,6 +6,7 @@ use crate::context::world_state::WorldStateSection;
use crate::context::world_state::test_support::render_section_cases;
use codex_extension_api::ExtensionMetrics;
use codex_otel::THREAD_TOOLS_FRAGMENT_BYTES_METRIC;
use codex_otel::THREAD_TOOLS_METRIC_BUCKETS;
use codex_otel::THREAD_TOOLS_NAMESPACES_TOTAL_METRIC;
use pretty_assertions::assert_eq;
use std::collections::BTreeMap;
@@ -398,7 +399,13 @@ fn reserves_the_empty_state_notice_when_all_namespaces_are_removed() {
#[derive(Default)]
struct RecordingMetrics {
samples: Mutex<BTreeMap<(String, String, String), i64>>,
samples: Mutex<BTreeMap<(String, String, String), RecordedHistogram>>,
}
#[derive(Debug, PartialEq)]
struct RecordedHistogram {
value: i64,
boundaries: Vec<f64>,
}
impl ExtensionMetrics for RecordingMetrics {
@@ -410,25 +417,28 @@ impl ExtensionMetrics for RecordingMetrics {
&self,
name: &str,
value: i64,
_boundaries: &[f64],
boundaries: &[f64],
tags: &[(&str, &str)],
) {
self.histogram(name, value, tags);
}
fn histogram(&self, name: &str, value: i64, tags: &[(&str, &str)]) {
let [("stage", stage), ("kind", kind)] = tags else {
panic!("unexpected tags: {tags:?}")
};
self.samples.lock().expect("metric samples lock").insert(
(name.to_string(), (*stage).to_string(), (*kind).to_string()),
value,
RecordedHistogram {
value,
boundaries: boundaries.to_vec(),
},
);
}
fn histogram(&self, name: &str, _value: i64, _tags: &[(&str, &str)]) {
panic!("expected explicit boundaries for {name}");
}
}
impl RecordingMetrics {
fn take(&self) -> BTreeMap<(String, String, String), i64> {
fn take(&self) -> BTreeMap<(String, String, String), RecordedHistogram> {
std::mem::take(&mut *self.samples.lock().expect("metric samples lock"))
}
}
@@ -437,7 +447,7 @@ fn expected_metrics(
kind: &str,
before: (usize, &str),
after: (usize, &str),
) -> BTreeMap<(String, String, String), i64> {
) -> BTreeMap<(String, String, String), RecordedHistogram> {
[("before", before), ("after", after)]
.into_iter()
.flat_map(|(stage, (count, text))| {
@@ -448,7 +458,10 @@ fn expected_metrics(
.map(|(name, value)| {
(
(name.to_string(), stage.to_string(), kind.to_string()),
value as i64,
RecordedHistogram {
value: value as i64,
boundaries: THREAD_TOOLS_METRIC_BUCKETS.to_vec(),
},
)
})
})