mirror of
https://github.com/openai/codex.git
synced 2026-12-03 00:41:03 +00:00
Count remote model catalog ETag hint outcomes (#65110)
## What changed Record one increment of `codex.remote_models.etag_hint` per comparison when metrics are enabled. Set `result` to `match` or `mismatch` when the current endpoint identity has a cached ETag, and to `no_baseline` when there is no comparable cached ETag. Count the outcome even if the subsequent refresh fails or authentication prevents a fetch. ## Testing Add an in-memory metrics test that checks the exact counts and verifies that a matching hint skips the fetch and an equal ETag from another endpoint identity counts as `no_baseline`. GitOrigin-RevId: 9570f415d43cf19bac8f4d82960466c86e883f73
This commit is contained in:
Generated
+1
@@ -4577,6 +4577,7 @@ dependencies = [
|
||||
"codex-protocol",
|
||||
"codex-utils-cached-str",
|
||||
"codex-utils-output-truncation",
|
||||
"opentelemetry_sdk",
|
||||
"pretty_assertions",
|
||||
"serde",
|
||||
"serde_json",
|
||||
|
||||
@@ -27,6 +27,7 @@ tokio = { workspace = true, features = ["fs", "sync", "time"] }
|
||||
tracing = { workspace = true, features = ["log"] }
|
||||
|
||||
[dev-dependencies]
|
||||
opentelemetry_sdk = { workspace = true }
|
||||
pretty_assertions = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
tempfile = { workspace = true }
|
||||
|
||||
@@ -777,13 +777,23 @@ impl OpenAiModelsManager {
|
||||
let entry = self.remote_models.read().await;
|
||||
(entry.entry.identity.clone(), entry.entry.etag.clone())
|
||||
};
|
||||
if let Some(identity) = identity
|
||||
&& Some(&identity) == self.endpoint_client.identity().as_ref()
|
||||
&& current_etag.as_deref() == Some(etag.as_str())
|
||||
{
|
||||
let identity = identity.filter(|id| Some(id) == self.endpoint_client.identity().as_ref());
|
||||
let (result, matched_identity) = match (identity.as_deref(), current_etag.as_deref()) {
|
||||
(Some(id), Some(cached)) if cached == etag => ("match", Some(id)),
|
||||
(Some(_), Some(_)) => ("mismatch", None),
|
||||
_ => ("no_baseline", None),
|
||||
};
|
||||
if let Some(metrics) = codex_otel::global() {
|
||||
let _ = metrics.counter(
|
||||
"codex.remote_models.etag_hint",
|
||||
/*inc*/ 1,
|
||||
&[("result", result)],
|
||||
);
|
||||
}
|
||||
if let Some(identity) = matched_identity {
|
||||
if let Some(cache) = self.settings.cache.as_ref()
|
||||
&& let Err(err) = cache
|
||||
.refresh_ttl(&crate::client_version_to_whole(), &identity, &etag)
|
||||
.refresh_ttl(&crate::client_version_to_whole(), identity, &etag)
|
||||
.await
|
||||
{
|
||||
error!("failed to renew cache TTL: {err}");
|
||||
|
||||
@@ -611,6 +611,98 @@ async fn initialization_retains_loaded_catalogs_including_empty_responses() {
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn etag_hint_counts_one_outcome_per_comparison() -> Result<(), Box<dyn std::error::Error>> {
|
||||
use opentelemetry_sdk::metrics::data::AggregatedMetrics;
|
||||
use opentelemetry_sdk::metrics::data::MetricData;
|
||||
use opentelemetry_sdk::metrics::data::ScopeMetrics;
|
||||
|
||||
// The metrics installation is process-wide. Isolate it from other manager tests under libtest.
|
||||
if std::env::var_os("CODEX_ETAG_METRICS_TEST_CHILD").is_none() {
|
||||
let output = std::process::Command::new(std::env::current_exe()?)
|
||||
.args([
|
||||
"--exact",
|
||||
std::thread::current().name().unwrap(),
|
||||
"--nocapture",
|
||||
])
|
||||
.env("CODEX_ETAG_METRICS_TEST_CHILD", "1")
|
||||
.output()?;
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
assert!(
|
||||
output.status.success() && stdout.contains("1 passed"),
|
||||
"{stdout}{}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
let metrics = codex_otel::install_global_metrics(codex_otel::MetricsClient::new(
|
||||
codex_otel::MetricsConfig::in_memory("test", "test", "1", Default::default())
|
||||
.with_runtime_reader(),
|
||||
)?);
|
||||
let endpoint = Arc::new(TestModelsEndpoint {
|
||||
uses_codex_backend: true,
|
||||
etag: Some("current".into()),
|
||||
responses: Mutex::new(vec![Ok(vec![]), Err(CodexErr::RequestTimeout)].into()),
|
||||
..TestModelsEndpoint::default()
|
||||
});
|
||||
let manager =
|
||||
OpenAiModelsManager::new_without_cache(endpoint.clone(), /*auth_manager*/ None);
|
||||
|
||||
// The first hint has no cached ETag. The fetch supplies the baseline for subsequent hints.
|
||||
manager
|
||||
.refresh_if_new_etag("current".into(), DEFAULT_HTTP_CLIENT_FACTORY)
|
||||
.await;
|
||||
manager
|
||||
.refresh_if_new_etag("current".into(), DEFAULT_HTTP_CLIENT_FACTORY)
|
||||
.await;
|
||||
assert_eq!(endpoint.fetch_count(), 1);
|
||||
manager
|
||||
.refresh_if_new_etag("different".into(), DEFAULT_HTTP_CLIENT_FACTORY)
|
||||
.await;
|
||||
assert_eq!(endpoint.fetch_count(), 2);
|
||||
|
||||
// The comparison counts even when auth prevents a fetch. An equal ETag from another
|
||||
// identity is not comparable.
|
||||
let disabled = TestModelsEndpoint::without_refresh(vec![]);
|
||||
let manager =
|
||||
OpenAiModelsManager::new_without_cache(disabled.clone(), /*auth_manager*/ None);
|
||||
manager.remote_models.write().await.entry.etag = Some("old".into());
|
||||
manager
|
||||
.refresh_if_new_etag("new".into(), DEFAULT_HTTP_CLIENT_FACTORY)
|
||||
.await;
|
||||
manager.remote_models.write().await.entry.identity = Some("other-provider".into());
|
||||
manager
|
||||
.refresh_if_new_etag("old".into(), DEFAULT_HTTP_CLIENT_FACTORY)
|
||||
.await;
|
||||
assert_eq!(disabled.fetch_count(), 0);
|
||||
|
||||
let snapshot = metrics.snapshot()?;
|
||||
let mut counts = Vec::new();
|
||||
for metric in snapshot.scope_metrics().flat_map(ScopeMetrics::metrics) {
|
||||
if metric.name() == "codex.remote_models.etag_hint"
|
||||
&& let AggregatedMetrics::U64(MetricData::Sum(sum)) = metric.data()
|
||||
{
|
||||
for point in sum.data_points() {
|
||||
let mut tags = point.attributes();
|
||||
let tag = tags.next().unwrap();
|
||||
assert_eq!(tag.key.as_str(), "result");
|
||||
assert!(tags.next().is_none());
|
||||
counts.push((tag.value.to_string(), point.value()));
|
||||
}
|
||||
}
|
||||
}
|
||||
counts.sort();
|
||||
assert_eq!(
|
||||
counts,
|
||||
vec![
|
||||
("match".into(), 1),
|
||||
("mismatch".into(), 2),
|
||||
("no_baseline".into(), 2),
|
||||
],
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn injected_cache_ttl_refresh_preserves_cached_payload() {
|
||||
let cached_models = vec![remote_model("cached", "Cached", /*priority*/ 0)];
|
||||
|
||||
Reference in New Issue
Block a user