Files
codex/codex-rs/analytics/src/lib.rs
T
ding-oai b059ce11f9 Allow hosts to route selected analytics events (#62684)
## Why

The analytics client sends eligible events through HTTP. Hosts that
embed Codex need a way to deliver specific event types themselves while
keeping HTTP delivery for the rest of the batch.

## What changed

Add `AnalyticsEventsClient::new_with_event_transport` so hosts can opt
in and select eligible events by wire type. The transport receives the
complete wire payload and the event's product SKU, when available.
Events stay on the HTTP path unless the transport returns `Handled`,
which suppresses a duplicate HTTP send.

Disabled analytics and local file capture bypass the transport.
Rebinding the client to a new auth context restores HTTP delivery.

## Testing

Add tests for mixed batches that verify HTTP payloads and product
attribution, plus coverage for auth eligibility, local capture,
analytics opt-out, and auth rebinding.

GitOrigin-RevId: 27ba3fe54050754b5f844f00e7e761a4d7bcb8c0
2026-11-20 06:37:15 +00:00

127 lines
4.0 KiB
Rust

mod accepted_lines;
#[cfg(debug_assertions)]
mod analytics_capture;
mod client;
mod event_transport;
mod events;
mod facts;
mod guardian_v2;
mod product_attribution;
mod reducer;
mod thread_hint;
mod turn_critical_path;
use std::time::SystemTime;
use std::time::UNIX_EPOCH;
pub use accepted_lines::fingerprint_hash;
pub use client::AnalyticsEventsClient;
pub use event_transport::AnalyticsEventDelivery;
pub use event_transport::AnalyticsEventTransport;
pub use event_transport::AnalyticsTransportEvent;
pub use events::AppServerRpcTransport;
pub use events::GuardianAdditionalPermissions;
pub use events::GuardianApprovalRequestSource;
pub use events::GuardianReviewAnalyticsResult;
pub use events::GuardianReviewDecision;
pub use events::GuardianReviewEventParams;
pub use events::GuardianReviewFailureReason;
pub use events::GuardianReviewSessionAnalyticsParams;
pub use events::GuardianReviewSessionKind;
pub use events::GuardianReviewTerminalStatus;
pub use events::GuardianReviewTrackContext;
pub use events::GuardianReviewedAction;
pub use facts::AnalyticsJsonRpcError;
pub use facts::AppInvocation;
pub use facts::ArtifactOperation;
pub use facts::ArtifactOperationLifecycle;
pub use facts::CodeModeToolCallFact;
pub use facts::CodeModeToolCallStatus;
pub use facts::CodexCompactionEvent;
pub use facts::CodexErrKind;
pub use facts::CodexGoalEvent;
pub use facts::CodexTurnSteerEvent;
pub use facts::CompactionImplementation;
pub use facts::CompactionPhase;
pub use facts::CompactionReason;
pub use facts::CompactionStatus;
pub use facts::CompactionStrategy;
pub use facts::CompactionTrigger;
pub use facts::ControlToolCallFact;
pub use facts::ControlToolCallStatus;
pub use facts::ElicitationType;
pub use facts::ExternalAgentConfigImportCompletedInput;
pub use facts::ExternalAgentConfigImportFailureInput;
pub use facts::GoalEventKind;
pub use facts::HookRunFact;
pub use facts::ImageDetailSetting;
pub use facts::ImagePreparationFact;
pub use facts::ImagePreparationMetadata;
pub use facts::InputError;
pub use facts::InvocationType;
pub use facts::McpToolCallElicitation;
pub use facts::PluginInstallRequestSource;
pub use facts::PluginInstallRequested;
pub use facts::PluginInstallRequestedPlugin;
pub use facts::PluginInstallSource;
pub use facts::PluginMeasurementRow;
pub use facts::PluginMeasurementsInput;
pub use facts::SkillInvocation;
pub use facts::SkillInvocationLocation;
pub use facts::SubAgentThreadStartedInput;
pub use facts::ThreadInitializationMode;
pub use facts::TrackEventsContext;
pub use facts::TurnAnalyticsMetadata;
pub use facts::TurnCodexErrorFact;
pub use facts::TurnProfile;
pub use facts::TurnProfileFact;
pub use facts::TurnResolvedConfigFact;
pub use facts::TurnStatus;
pub use facts::TurnSteerRejectionReason;
pub use facts::TurnSteerRequestError;
pub use facts::TurnSteerResult;
pub use facts::TurnTokenUsageFact;
pub use facts::build_track_events_context;
pub use guardian_v2::GuardianV2Event;
pub use guardian_v2::GuardianV2EventKind;
pub use product_attribution::ThreadProductUpdate;
pub use thread_hint::ThreadHintStatus;
pub use thread_hint::ThreadHintStatusEvent;
pub use turn_critical_path::IncompleteReason;
pub use turn_critical_path::TurnCriticalPath;
pub use turn_critical_path::TurnCriticalPathDurations;
#[cfg(test)]
mod tests;
pub fn now_unix_seconds() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs()
}
pub fn now_unix_millis() -> u64 {
u64::try_from(
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_millis(),
)
.unwrap_or(u64::MAX)
}
pub(crate) fn serialize_enum_as_string<T: serde::Serialize>(value: &T) -> Option<String> {
serde_json::to_value(value)
.ok()
.and_then(|value| value.as_str().map(str::to_string))
}
pub(crate) fn usize_to_u64(value: usize) -> u64 {
u64::try_from(value).unwrap_or(u64::MAX)
}
pub(crate) fn option_i64_to_u64(value: Option<i64>) -> Option<u64> {
value.and_then(|value| u64::try_from(value).ok())
}