mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Add bounded plugin measurement analytics (#38239)
## What changed - Add `PluginMeasurementsInput` and `PluginMeasurementRow`, plus an analytics client method for recording batches and checking whether analytics are enabled. - Emit each valid row as a `codex_plugin_measurement_event` with thread, turn, item, plugin, execution, and operation context. - Bound batches at 100 rows and reject non-finite values, invalid identifiers, and rows with more than eight dimensions. - Treat measurement events as plugin events when routing API-key analytics. ## Testing - Cover batch limits, row filtering, serialized event payloads, and API-key plugin-event routing. GitOrigin-RevId: f949ed06246ee1e22466df2660076f2d4f556444
This commit is contained in:
1 parent
dc8562d672
commit
d6eefb26a6
7 files changed
+318
No files matched your search
@@ -77,6 +77,8 @@ use crate::facts::PluginInstallRequested;
|
||||
use crate::facts::PluginInstallRequestedInput;
|
||||
use crate::facts::PluginInstallRequestedPlugin;
|
||||
use crate::facts::PluginInstallSource;
|
||||
use crate::facts::PluginMeasurementRow;
|
||||
use crate::facts::PluginMeasurementsInput;
|
||||
use crate::facts::PluginState;
|
||||
use crate::facts::PluginStateChangedInput;
|
||||
use crate::facts::PluginUsedInput;
|
||||
@@ -182,6 +184,7 @@ use codex_utils_absolute_path::test_support::PathBufExt;
|
||||
use codex_utils_absolute_path::test_support::test_path_buf;
|
||||
use pretty_assertions::assert_eq;
|
||||
use serde_json::json;
|
||||
use std::collections::BTreeMap;
|
||||
use std::collections::HashSet;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
@@ -813,6 +816,18 @@ async fn ingest_completed_command_execution_item(
|
||||
.await;
|
||||
}
|
||||
|
||||
fn plugin_measurements(rows: Vec<PluginMeasurementRow>) -> PluginMeasurementsInput {
|
||||
PluginMeasurementsInput {
|
||||
thread_id: "thread-1".to_string(),
|
||||
turn_id: "turn-1".to_string(),
|
||||
item_id: "item-1".to_string(),
|
||||
plugin_id: "sample@openai-curated".to_string(),
|
||||
execution_id: "execution-1".to_string(),
|
||||
operation: "security_scan".to_string(),
|
||||
rows,
|
||||
}
|
||||
}
|
||||
|
||||
fn sample_initialize_fact(connection_id: u64) -> AnalyticsFact {
|
||||
AnalyticsFact::Initialize {
|
||||
connection_id,
|
||||
@@ -2597,6 +2612,78 @@ async fn item_lifecycle_notifications_publish_command_execution_event() {
|
||||
assert_eq!(payload[0]["event_params"]["thread_source"], "user");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn plugin_measurement_batch_emits_directly_and_filters_invalid_rows() {
|
||||
let mut reducer = AnalyticsReducer::default();
|
||||
let mut events = Vec::new();
|
||||
let mut too_many_dimensions = BTreeMap::new();
|
||||
for index in 0..9 {
|
||||
too_many_dimensions.insert(format!("dimension_{index}"), "allowed".to_string());
|
||||
}
|
||||
let measurements = plugin_measurements(vec![
|
||||
PluginMeasurementRow {
|
||||
measurement_name: "finding_count".to_string(),
|
||||
number_value: 3.0,
|
||||
dimensions: BTreeMap::from([("severity".to_string(), "high".to_string())]),
|
||||
},
|
||||
PluginMeasurementRow {
|
||||
measurement_name: "non_finite".to_string(),
|
||||
number_value: f64::NAN,
|
||||
dimensions: BTreeMap::new(),
|
||||
},
|
||||
PluginMeasurementRow {
|
||||
measurement_name: "too_many_dimensions".to_string(),
|
||||
number_value: 1.0,
|
||||
dimensions: too_many_dimensions,
|
||||
},
|
||||
PluginMeasurementRow {
|
||||
measurement_name: "files_scanned".to_string(),
|
||||
number_value: 17.0,
|
||||
dimensions: BTreeMap::new(),
|
||||
},
|
||||
]);
|
||||
reducer
|
||||
.ingest(
|
||||
AnalyticsFact::Custom(CustomAnalyticsFact::PluginMeasurements(measurements)),
|
||||
&mut events,
|
||||
)
|
||||
.await;
|
||||
let payload = serde_json::to_value(&events).expect("serialize events");
|
||||
assert_eq!(
|
||||
payload,
|
||||
json!([
|
||||
{
|
||||
"event_type": "codex_plugin_measurement_event",
|
||||
"event_params": {
|
||||
"thread_id": "thread-1",
|
||||
"turn_id": "turn-1",
|
||||
"item_id": "item-1",
|
||||
"plugin_id": "sample@openai-curated",
|
||||
"execution_id": "execution-1",
|
||||
"operation": "security_scan",
|
||||
"measurement_name": "finding_count",
|
||||
"number_value": 3.0,
|
||||
"dimensions": {"severity": "high"},
|
||||
},
|
||||
},
|
||||
{
|
||||
"event_type": "codex_plugin_measurement_event",
|
||||
"event_params": {
|
||||
"thread_id": "thread-1",
|
||||
"turn_id": "turn-1",
|
||||
"item_id": "item-1",
|
||||
"plugin_id": "sample@openai-curated",
|
||||
"execution_id": "execution-1",
|
||||
"operation": "security_scan",
|
||||
"measurement_name": "files_scanned",
|
||||
"number_value": 17.0,
|
||||
"dimensions": null,
|
||||
},
|
||||
},
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn command_execution_approval_response_publishes_user_review_event() {
|
||||
let mut reducer = AnalyticsReducer::default();
|
||||
|
||||
Reference in new issue
Block a user