mirror of
https://github.com/openai/codex.git
synced 2026-11-26 23:40:43 +00:00
## Why A Statsig gate can return `false` while bootstrap is in progress or because the cached evaluation is disabled. Update failures also share a generic debug message, which makes recurring failures hard to diagnose. ## What changed Record OpenTelemetry counts and durations for bootstrap and refresh, tagged by outcome and failure category. Record gate and dynamic config counts after successful updates. Skip update metrics while waiting for authentication. Count gate and dynamic config reads by name, kind, and reason. Use fixed reason labels to distinguish fallback reads from evaluated reads while keeping targeting values out of the metrics. ## Testing Check that a gate read during bootstrap and a read after bootstrap emit distinct reasons, and that wrapped network policy denials retain the `policy` category. GitOrigin-RevId: ca4a248ee8ec7ed1318b2b2d320b9e9df08cbe5e
197 lines
6.8 KiB
Rust
197 lines
6.8 KiB
Rust
#![cfg(test)]
|
|
|
|
use codex_login::AuthManager;
|
|
use codex_login::CodexAuth;
|
|
use codex_model_provider::ProviderAuthContext;
|
|
use opentelemetry_sdk::metrics::data::AggregatedMetrics;
|
|
use opentelemetry_sdk::metrics::data::MetricData;
|
|
use opentelemetry_sdk::metrics::data::ScopeMetrics;
|
|
use pretty_assertions::assert_eq;
|
|
use serde_json::json;
|
|
use statsig_client::StatsigOptions;
|
|
use wiremock::Mock;
|
|
use wiremock::MockServer;
|
|
use wiremock::ResponseTemplate;
|
|
use wiremock::matchers::body_partial_json;
|
|
use wiremock::matchers::method;
|
|
use wiremock::matchers::path;
|
|
|
|
use super::StatsigBootstrapContext;
|
|
use super::StatsigEvaluations;
|
|
use super::StatsigIdentity;
|
|
|
|
#[test]
|
|
fn wrapped_policy_denials_keep_their_category() {
|
|
let denied = codex_http_client::NetworkPolicyDenied::Destination;
|
|
for error in [
|
|
anyhow::Error::new(codex_api::AuthError::Policy(denied)).context("auth"),
|
|
anyhow::Error::new(codex_backend_client::RequestError::Policy(denied)).context("http"),
|
|
] {
|
|
assert_eq!(super::telemetry::failure_category(&error), "policy");
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn bootstraps_once_and_refreshes_directly_through_the_sdk() -> anyhow::Result<()> {
|
|
let server = MockServer::start().await;
|
|
let home = std::env::temp_dir().join(format!("statsig-auth-{}", server.address().port()));
|
|
let auth_manager = AuthManager::from_auth_for_testing_with_home(
|
|
CodexAuth::create_dummy_chatgpt_auth_for_testing(),
|
|
home.clone(),
|
|
);
|
|
let account = ProviderAuthContext {
|
|
credentials: auth_manager.clone(),
|
|
runtime_auth: None,
|
|
};
|
|
let context = StatsigBootstrapContext {
|
|
target_app: "test-target-app",
|
|
stable_id: "test-install".into(),
|
|
app_version: "1".into(),
|
|
locale: None,
|
|
system_name: "test-os".into(),
|
|
system_version: "1".into(),
|
|
window_type: "cli",
|
|
brand_name: "codex",
|
|
};
|
|
const PROBE: super::Gate = super::Gate::new("probe");
|
|
assert!(!PROBE.is_enabled());
|
|
assert!(super::DynamicConfig::new("probe").value().is_empty());
|
|
let user = json!({"userID": "test-user", "customIDs": {"stableID": "test-install"}});
|
|
let values = |enabled: bool, limit: u32| {
|
|
json!({
|
|
"has_updates": true,
|
|
"hash_used": "none",
|
|
"user": user,
|
|
"feature_gates": {"probe": {"value": enabled}},
|
|
"dynamic_configs": {"probe": {"value": {"limit": limit}}},
|
|
"layer_configs": {}
|
|
})
|
|
};
|
|
let bootstrap = json!({"statsigPayload": values(true, 1).to_string()});
|
|
let failed = Mock::given(method("POST"))
|
|
.and(path("/api/codex/statsig/bootstrap"))
|
|
.respond_with(ResponseTemplate::new(503))
|
|
.expect(1)
|
|
.mount_as_scoped(&server)
|
|
.await;
|
|
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 runtime = super::StatsigRuntime::start_process(
|
|
StatsigIdentity::Account(account.clone()),
|
|
server.uri(),
|
|
"client-test".into(),
|
|
context.clone(),
|
|
/*exposure_logging_enabled*/ false,
|
|
);
|
|
assert!(!PROBE.is_enabled());
|
|
failed.wait_until_satisfied().await;
|
|
drop(failed);
|
|
let failed_retry = Mock::given(method("POST"))
|
|
.and(path("/api/codex/statsig/bootstrap"))
|
|
.respond_with(ResponseTemplate::new(503))
|
|
.expect(1)
|
|
.mount_as_scoped(&server)
|
|
.await;
|
|
let token = "e30.eyJodHRwczovL2FwaS5vcGVuYWkuY29tL2F1dGgiOnsidXNlcl9pZCI6InRlc3QtdXNlciJ9fQ";
|
|
codex_login::auth::login_with_chatgpt_auth_tokens(
|
|
&home,
|
|
&format!("{token}.a"),
|
|
"account_id",
|
|
/*chatgpt_plan_type*/ None,
|
|
)?;
|
|
assert!(auth_manager.reload().await);
|
|
failed_retry.wait_until_satisfied().await;
|
|
drop(failed_retry);
|
|
Mock::given(method("POST"))
|
|
.and(path("/api/codex/statsig/bootstrap"))
|
|
.respond_with(ResponseTemplate::new(200).set_body_json(bootstrap))
|
|
.expect(2)
|
|
.mount(&server)
|
|
.await;
|
|
let owner = auth_manager.auth_owner();
|
|
codex_login::auth::login_with_chatgpt_auth_tokens(
|
|
&home,
|
|
&format!("{token}.b"),
|
|
"account_id",
|
|
/*chatgpt_plan_type*/ None,
|
|
)?;
|
|
auth_manager.reload().await;
|
|
assert!(owner.is_current());
|
|
let mut evaluations = runtime.evaluations.clone();
|
|
tokio::time::timeout(
|
|
std::time::Duration::from_secs(5),
|
|
evaluations.wait_for(Result::is_ok),
|
|
)
|
|
.await??;
|
|
assert!(PROBE.is_enabled());
|
|
let mut checks = Vec::new();
|
|
let snapshot = metrics.snapshot()?;
|
|
for metric in snapshot.scope_metrics().flat_map(ScopeMetrics::metrics) {
|
|
if metric.name() == "codex.statsig.evaluation.count"
|
|
&& let AggregatedMetrics::U64(MetricData::Sum(sum)) = metric.data()
|
|
{
|
|
for point in sum.data_points() {
|
|
let tags = ["kind", "name", "reason"].map(|key| {
|
|
let tag = point.attributes().find(|attr| attr.key.as_str() == key);
|
|
tag.expect(key).value.to_string()
|
|
});
|
|
checks.push((tags, point.value()));
|
|
}
|
|
}
|
|
}
|
|
checks.sort();
|
|
assert_eq!(
|
|
checks,
|
|
["bootstrap_in_progress", "evaluated"]
|
|
.map(|reason| (["gate", "probe", reason].map(str::to_owned), 1))
|
|
);
|
|
assert_eq!(
|
|
super::DynamicConfig::new("probe").value(),
|
|
json!({"limit": 1}).as_object().unwrap().clone()
|
|
);
|
|
drop(evaluations);
|
|
super::StatsigRuntime::finish(Some(runtime)).await;
|
|
metrics.shutdown()?;
|
|
for response in [
|
|
ResponseTemplate::new(503),
|
|
ResponseTemplate::new(200).set_body_json(values(false, 2)),
|
|
] {
|
|
Mock::given(method("POST"))
|
|
.and(path("/initialize"))
|
|
.and(body_partial_json(json!({"user": user})))
|
|
.respond_with(response)
|
|
.up_to_n_times(1)
|
|
.expect(1)
|
|
.mount(&server)
|
|
.await;
|
|
}
|
|
let options = StatsigOptions::new()
|
|
.with_initialize_url(format!("{}/initialize", server.uri()))
|
|
.with_initialization_request_attempts(/* count */ 1)
|
|
.with_disable_all_logging(/* disabled */ true);
|
|
let store = StatsigEvaluations::start(
|
|
&StatsigIdentity::Account(account),
|
|
&server.uri(),
|
|
"client-test",
|
|
context,
|
|
options,
|
|
)
|
|
.await?;
|
|
let read = || {
|
|
(
|
|
store.sdk.check_gate("probe"),
|
|
json!(store.sdk.get_dynamic_config("probe").value()),
|
|
)
|
|
};
|
|
assert_eq!(read(), (true, json!({"limit": 1})));
|
|
assert!(store.refresh().await.is_err());
|
|
assert_eq!(read(), (true, json!({"limit": 1})));
|
|
store.refresh().await?;
|
|
assert_eq!(read(), (false, json!({"limit": 2})));
|
|
server.verify().await;
|
|
store.shutdown().await;
|
|
Ok(())
|
|
}
|