chore(otel): reorganize codex-otel crate (#13800)

## Summary
This is a structural cleanup of `codex-otel` to make the ownership
boundaries a lot clearer.

For example, previously it was quite confusing that `OtelManager` which
emits log + trace event telemetry lived under
`codex-rs/otel/src/traces/`. Also, there were two places that defined
methods on OtelManager via `impl OtelManager` (`lib.rs` and
`otel_manager.rs`).

What changed:
- move the `OtelProvider` implementation into `src/provider.rs`
- move `OtelManager` and session-scoped event emission into
`src/events/otel_manager.rs`
- collapse the shared log/trace event helpers into
`src/events/shared.rs`
- pull target classification into `src/targets.rs`
- move `traceparent_context_from_env()` into `src/trace_context.rs`
- keep `src/otel_provider.rs` as a compatibility shim for existing
imports
- update the `codex-otel` README to reflect the new layout

## Why
`lib.rs` and `otel_provider.rs` were doing too many different jobs at
once: provider setup, export routing, trace-context helpers, and session
event emission all lived together.

This refactor separates those concerns without trying to change the
behavior of the crate. The goal is to make future OTEL work easier to
reason about and easier to review.

## Notes
- no intended behavior change
- `OtelManager` remains the session-scoped event emitter in this PR
- the `otel_provider` shim keeps downstream churn low while the
internals move around

## Validation
- `just fmt`
- `cargo test -p codex-otel`
- `just fix -p codex-otel`
This commit is contained in:
Owen Lin
2026-03-06 14:58:18 -08:00
committed by GitHub
parent 8ede18011a
commit dd4a5216c9
10 changed files with 782 additions and 813 deletions

View File

@@ -2,10 +2,11 @@
`codex-otel` is the OpenTelemetry integration crate for Codex. It provides:
- Trace/log/metrics exporters and tracing subscriber layers (`codex_otel::otel_provider`).
- A structured event helper (`codex_otel::OtelManager`).
- OpenTelemetry metrics support via OTLP exporters (`codex_otel::metrics`).
- A metrics facade on `OtelManager` so tracing + metrics share metadata.
- Provider wiring for log/trace/metric exporters (`codex_otel::OtelProvider`,
`codex_otel::provider`, and the compatibility shim `codex_otel::otel_provider`).
- Session-scoped business event emission via `codex_otel::OtelManager`.
- Low-level metrics APIs via `codex_otel::metrics`.
- Trace-context helpers via `codex_otel::trace_context` and crate-root re-exports.
## Tracing and logs
@@ -17,7 +18,7 @@ registry:
use codex_otel::config::OtelExporter;
use codex_otel::config::OtelHttpProtocol;
use codex_otel::config::OtelSettings;
use codex_otel::otel_provider::OtelProvider;
use codex_otel::OtelProvider;
use tracing_subscriber::prelude::*;
let settings = OtelSettings {
@@ -51,7 +52,8 @@ if let Some(provider) = OtelProvider::from(&settings)? {
## OtelManager (events)
`OtelManager` adds consistent metadata to tracing events and helps record
Codex-specific events.
Codex-specific session events. Rich session/business events should go through
`OtelManager`; subsystem-owned audit events can stay with the owning subsystem.
```rust
use codex_otel::OtelManager;
@@ -63,6 +65,7 @@ let manager = OtelManager::new(
account_id,
account_email,
auth_mode,
originator,
log_user_prompts,
terminal_type,
session_source,
@@ -119,6 +122,15 @@ metrics.counter("codex.turns", 1, &[("model", "gpt-5.1")])?;
metrics.shutdown()?; // flushes in-memory exporter
```
## Trace context
Trace propagation helpers remain separate from the session event emitter:
```rust
use codex_otel::current_span_w3c_trace_context;
use codex_otel::set_parent_from_w3c_trace_context;
```
## Shutdown
- `OtelProvider::shutdown()` stops the OTEL exporter.