mirror of
https://github.com/openai/codex.git
synced 2026-11-24 23:20:47 +00:00
## Why The code-mode host only exports traces, and Windows sandbox setup only exports metrics. Selecting the required exporter and HTTP client at compile time lets the linker drop unused exporter and custom client code. ## What changed Use `BlockingHttpService` for OTLP/HTTP JSON traces in the code-mode host and Statsig metrics during Windows sandbox setup. These providers use the OpenTelemetry SDK's default HTTP client. Traces use the dedicated batch span processor even inside a Tokio runtime. Move `codex_home` from `OtelSettings` to `OtelHeadersHelper`, where it supplies the working directory for user-configured header commands. ## Testing Exercise the blocking providers in HTTP loopback tests for trace export inside a current-thread Tokio runtime and cumulative metrics temporality. GitOrigin-RevId: 21bacbdb18b603f5f536532812ce7643e4eb36d6
108 lines
3.5 KiB
Rust
108 lines
3.5 KiB
Rust
use std::time::Duration;
|
|
|
|
use clap::Parser;
|
|
use codex_otel::BlockingHttpService;
|
|
use codex_otel::OtelProvider;
|
|
use codex_otel_trace_websocket::TraceWebSocket;
|
|
use tracing_subscriber::Layer;
|
|
use tracing_subscriber::layer::SubscriberExt;
|
|
use tracing_subscriber::util::SubscriberInitExt;
|
|
|
|
const OTEL_SHUTDOWN_TIMEOUT: Duration = Duration::from_secs(/*secs*/ 5);
|
|
|
|
#[derive(Debug, Parser)]
|
|
struct Cli {
|
|
#[arg(
|
|
long,
|
|
hide = true,
|
|
conflicts_with_all = ["listen", "otel_trace_listen", "otel_trace_exporter"]
|
|
)]
|
|
codex_ui_plugin_process: bool,
|
|
|
|
/// gRPC endpoint: `grpc+stdio://` (aliases `stdio` and `stdio://`) or `grpc://IP:PORT`.
|
|
#[arg(
|
|
long,
|
|
value_name = "URL",
|
|
default_value = codex_code_mode_host::DEFAULT_LISTEN_URL
|
|
)]
|
|
listen: String,
|
|
|
|
/// Optional WebSocket endpoint that streams only raw OTLP trace batches.
|
|
#[arg(long, value_name = "URL")]
|
|
otel_trace_listen: Option<String>,
|
|
|
|
/// Optional OTLP/HTTP JSON trace exporter endpoint, analogous to
|
|
/// `otel.trace_exporter` in app-server configuration.
|
|
#[arg(long, value_name = "URL", conflicts_with = "otel_trace_listen")]
|
|
otel_trace_exporter: Option<String>,
|
|
}
|
|
|
|
#[tokio::main(flavor = "current_thread")]
|
|
async fn run(cli: Cli) -> anyhow::Result<()> {
|
|
let mut trace_transport = if let Some(trace_listen) = cli.otel_trace_listen.as_deref() {
|
|
Some(TraceWebSocket::start(trace_listen).await?)
|
|
} else {
|
|
None
|
|
};
|
|
let trace_exporter_endpoint = trace_transport
|
|
.as_ref()
|
|
.map(TraceWebSocket::exporter_endpoint)
|
|
.or(cli.otel_trace_exporter.as_deref());
|
|
let otel = trace_exporter_endpoint
|
|
.map(build_trace_provider)
|
|
.transpose()?;
|
|
let otel_layer = otel.as_ref().and_then(OtelProvider::tracing_layer);
|
|
tracing_subscriber::registry()
|
|
.with(
|
|
tracing_subscriber::fmt::layer()
|
|
.with_writer(std::io::stderr)
|
|
.with_ansi(false)
|
|
.with_filter(tracing_subscriber::filter::LevelFilter::INFO),
|
|
)
|
|
.with(otel_layer)
|
|
.init();
|
|
if let Some(trace_transport) = trace_transport.as_ref() {
|
|
let listen_addr = trace_transport.listen_addr();
|
|
tracing::info!("codex-code-mode-host OTEL trace websocket listening on ws://{listen_addr}");
|
|
}
|
|
tracing::info_span!(
|
|
"code_mode_host.startup",
|
|
otel.name = "code_mode_host.startup"
|
|
)
|
|
.in_scope(|| {});
|
|
|
|
let main_transport = codex_code_mode_host::run_main(&cli.listen);
|
|
let result = match trace_transport.as_mut() {
|
|
Some(trace_transport) => tokio::select! {
|
|
result = main_transport => result,
|
|
result = trace_transport.wait_for_failure() => result,
|
|
},
|
|
None => main_transport.await,
|
|
};
|
|
if let Some(otel) = otel
|
|
&& let Err(error) = otel.shutdown_with_timeout(OTEL_SHUTDOWN_TIMEOUT).await
|
|
{
|
|
tracing::warn!(%error, "failed to finish code-mode host telemetry shutdown");
|
|
}
|
|
drop(trace_transport);
|
|
result
|
|
}
|
|
|
|
fn main() -> anyhow::Result<()> {
|
|
let cli = Cli::parse();
|
|
if cli.codex_ui_plugin_process {
|
|
return codex_code_mode_runtime::run_ui_sandbox().map_err(Into::into);
|
|
}
|
|
run(cli)
|
|
}
|
|
|
|
fn build_trace_provider(endpoint: &str) -> anyhow::Result<OtelProvider> {
|
|
BlockingHttpService {
|
|
environment: "code-mode-host",
|
|
name: "codex-code-mode-host",
|
|
version: env!("CARGO_PKG_VERSION"),
|
|
}
|
|
.traces(endpoint)
|
|
.map_err(|error| anyhow::anyhow!("failed to build code-mode host OTEL provider: {error}"))
|
|
}
|