2025-10-07 01:56:39 -07:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
use std::sync::Mutex;
|
|
|
|
|
|
2025-09-18 17:53:14 -07:00
|
|
|
use serde_json::Value;
|
|
|
|
|
use wiremock::BodyPrintLimit;
|
2025-10-07 01:56:39 -07:00
|
|
|
use wiremock::Match;
|
2025-09-18 17:53:14 -07:00
|
|
|
use wiremock::Mock;
|
2025-10-07 01:56:39 -07:00
|
|
|
use wiremock::MockBuilder;
|
2025-09-18 17:53:14 -07:00
|
|
|
use wiremock::MockServer;
|
2025-09-25 17:12:45 -07:00
|
|
|
use wiremock::Respond;
|
2025-09-18 17:53:14 -07:00
|
|
|
use wiremock::ResponseTemplate;
|
|
|
|
|
use wiremock::matchers::method;
|
2025-10-07 01:56:39 -07:00
|
|
|
use wiremock::matchers::path_regex;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct ResponseMock {
|
|
|
|
|
requests: Arc<Mutex<Vec<ResponsesRequest>>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ResponseMock {
|
|
|
|
|
fn new() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
requests: Arc::new(Mutex::new(Vec::new())),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn single_request(&self) -> ResponsesRequest {
|
|
|
|
|
let requests = self.requests.lock().unwrap();
|
|
|
|
|
if requests.len() != 1 {
|
|
|
|
|
panic!("expected 1 request, got {}", requests.len());
|
|
|
|
|
}
|
|
|
|
|
requests.first().unwrap().clone()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn requests(&self) -> Vec<ResponsesRequest> {
|
|
|
|
|
self.requests.lock().unwrap().clone()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct ResponsesRequest(wiremock::Request);
|
|
|
|
|
|
|
|
|
|
impl ResponsesRequest {
|
|
|
|
|
pub fn body_json(&self) -> Value {
|
|
|
|
|
self.0.body_json().unwrap()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn input(&self) -> Vec<Value> {
|
|
|
|
|
self.0.body_json::<Value>().unwrap()["input"]
|
|
|
|
|
.as_array()
|
|
|
|
|
.expect("input array not found in request")
|
|
|
|
|
.clone()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn function_call_output(&self, call_id: &str) -> Value {
|
|
|
|
|
self.call_output(call_id, "function_call_output")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn custom_tool_call_output(&self, call_id: &str) -> Value {
|
|
|
|
|
self.call_output(call_id, "custom_tool_call_output")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn call_output(&self, call_id: &str, call_type: &str) -> Value {
|
|
|
|
|
self.input()
|
|
|
|
|
.iter()
|
|
|
|
|
.find(|item| {
|
|
|
|
|
item.get("type").unwrap() == call_type && item.get("call_id").unwrap() == call_id
|
|
|
|
|
})
|
|
|
|
|
.cloned()
|
|
|
|
|
.unwrap_or_else(|| panic!("function call output {call_id} item not found in request"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn header(&self, name: &str) -> Option<String> {
|
|
|
|
|
self.0
|
|
|
|
|
.headers
|
|
|
|
|
.get(name)
|
|
|
|
|
.and_then(|v| v.to_str().ok())
|
|
|
|
|
.map(str::to_string)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn path(&self) -> String {
|
|
|
|
|
self.0.url.path().to_string()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn query_param(&self, name: &str) -> Option<String> {
|
|
|
|
|
self.0
|
|
|
|
|
.url
|
|
|
|
|
.query_pairs()
|
|
|
|
|
.find(|(k, _)| k == name)
|
|
|
|
|
.map(|(_, v)| v.to_string())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Match for ResponseMock {
|
|
|
|
|
fn matches(&self, request: &wiremock::Request) -> bool {
|
|
|
|
|
self.requests
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.push(ResponsesRequest(request.clone()));
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-18 17:53:14 -07:00
|
|
|
|
|
|
|
|
/// Build an SSE stream body from a list of JSON events.
|
|
|
|
|
pub fn sse(events: Vec<Value>) -> String {
|
|
|
|
|
use std::fmt::Write as _;
|
|
|
|
|
let mut out = String::new();
|
|
|
|
|
for ev in events {
|
|
|
|
|
let kind = ev.get("type").and_then(|v| v.as_str()).unwrap();
|
|
|
|
|
writeln!(&mut out, "event: {kind}").unwrap();
|
|
|
|
|
if !ev.as_object().map(|o| o.len() == 1).unwrap_or(false) {
|
|
|
|
|
write!(&mut out, "data: {ev}\n\n").unwrap();
|
|
|
|
|
} else {
|
|
|
|
|
out.push('\n');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Convenience: SSE event for a completed response with a specific id.
|
|
|
|
|
pub fn ev_completed(id: &str) -> Value {
|
|
|
|
|
serde_json::json!({
|
|
|
|
|
"type": "response.completed",
|
|
|
|
|
"response": {
|
|
|
|
|
"id": id,
|
|
|
|
|
"usage": {"input_tokens":0,"input_tokens_details":null,"output_tokens":0,"output_tokens_details":null,"total_tokens":0}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-05 14:11:43 -07:00
|
|
|
/// Convenience: SSE event for a created response with a specific id.
|
|
|
|
|
pub fn ev_response_created(id: &str) -> Value {
|
|
|
|
|
serde_json::json!({
|
|
|
|
|
"type": "response.created",
|
|
|
|
|
"response": {
|
|
|
|
|
"id": id,
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-20 11:29:49 -07:00
|
|
|
pub fn ev_completed_with_tokens(id: &str, total_tokens: i64) -> Value {
|
2025-09-18 17:53:14 -07:00
|
|
|
serde_json::json!({
|
|
|
|
|
"type": "response.completed",
|
|
|
|
|
"response": {
|
|
|
|
|
"id": id,
|
|
|
|
|
"usage": {
|
|
|
|
|
"input_tokens": total_tokens,
|
|
|
|
|
"input_tokens_details": null,
|
|
|
|
|
"output_tokens": 0,
|
|
|
|
|
"output_tokens_details": null,
|
|
|
|
|
"total_tokens": total_tokens
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Convenience: SSE event for a single assistant message output item.
|
|
|
|
|
pub fn ev_assistant_message(id: &str, text: &str) -> Value {
|
|
|
|
|
serde_json::json!({
|
|
|
|
|
"type": "response.output_item.done",
|
|
|
|
|
"item": {
|
|
|
|
|
"type": "message",
|
|
|
|
|
"role": "assistant",
|
|
|
|
|
"id": id,
|
|
|
|
|
"content": [{"type": "output_text", "text": text}]
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn ev_function_call(call_id: &str, name: &str, arguments: &str) -> Value {
|
|
|
|
|
serde_json::json!({
|
|
|
|
|
"type": "response.output_item.done",
|
|
|
|
|
"item": {
|
|
|
|
|
"type": "function_call",
|
|
|
|
|
"call_id": call_id,
|
|
|
|
|
"name": name,
|
|
|
|
|
"arguments": arguments
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
OpenTelemetry events (#2103)
### Title
## otel
Codex can emit [OpenTelemetry](https://opentelemetry.io/) **log events**
that
describe each run: outbound API requests, streamed responses, user
input,
tool-approval decisions, and the result of every tool invocation. Export
is
**disabled by default** so local runs remain self-contained. Opt in by
adding an
`[otel]` table and choosing an exporter.
```toml
[otel]
environment = "staging" # defaults to "dev"
exporter = "none" # defaults to "none"; set to otlp-http or otlp-grpc to send events
log_user_prompt = false # defaults to false; redact prompt text unless explicitly enabled
```
Codex tags every exported event with `service.name = "codex-cli"`, the
CLI
version, and an `env` attribute so downstream collectors can distinguish
dev/staging/prod traffic. Only telemetry produced inside the
`codex_otel`
crate—the events listed below—is forwarded to the exporter.
### Event catalog
Every event shares a common set of metadata fields: `event.timestamp`,
`conversation.id`, `app.version`, `auth_mode` (when available),
`user.account_id` (when available), `terminal.type`, `model`, and
`slug`.
With OTEL enabled Codex emits the following event types (in addition to
the
metadata above):
- `codex.api_request`
- `cf_ray` (optional)
- `attempt`
- `duration_ms`
- `http.response.status_code` (optional)
- `error.message` (failures)
- `codex.sse_event`
- `event.kind`
- `duration_ms`
- `error.message` (failures)
- `input_token_count` (completion only)
- `output_token_count` (completion only)
- `cached_token_count` (completion only, optional)
- `reasoning_token_count` (completion only, optional)
- `tool_token_count` (completion only)
- `codex.user_prompt`
- `prompt_length`
- `prompt` (redacted unless `log_user_prompt = true`)
- `codex.tool_decision`
- `tool_name`
- `call_id`
- `decision` (`approved`, `approved_for_session`, `denied`, or `abort`)
- `source` (`config` or `user`)
- `codex.tool_result`
- `tool_name`
- `call_id`
- `arguments`
- `duration_ms` (execution time for the tool)
- `success` (`"true"` or `"false"`)
- `output`
### Choosing an exporter
Set `otel.exporter` to control where events go:
- `none` – leaves instrumentation active but skips exporting. This is
the
default.
- `otlp-http` – posts OTLP log records to an OTLP/HTTP collector.
Specify the
endpoint, protocol, and headers your collector expects:
```toml
[otel]
exporter = { otlp-http = {
endpoint = "https://otel.example.com/v1/logs",
protocol = "binary",
headers = { "x-otlp-api-key" = "${OTLP_TOKEN}" }
}}
```
- `otlp-grpc` – streams OTLP log records over gRPC. Provide the endpoint
and any
metadata headers:
```toml
[otel]
exporter = { otlp-grpc = {
endpoint = "https://otel.example.com:4317",
headers = { "x-otlp-meta" = "abc123" }
}}
```
If the exporter is `none` nothing is written anywhere; otherwise you
must run or point to your
own collector. All exporters run on a background batch worker that is
flushed on
shutdown.
If you build Codex from source the OTEL crate is still behind an `otel`
feature
flag; the official prebuilt binaries ship with the feature enabled. When
the
feature is disabled the telemetry hooks become no-ops so the CLI
continues to
function without the extra dependencies.
---------
Co-authored-by: Anton Panasenko <apanasenko@openai.com>
2025-09-29 19:30:55 +01:00
|
|
|
pub fn ev_custom_tool_call(call_id: &str, name: &str, input: &str) -> Value {
|
|
|
|
|
serde_json::json!({
|
|
|
|
|
"type": "response.output_item.done",
|
|
|
|
|
"item": {
|
|
|
|
|
"type": "custom_tool_call",
|
|
|
|
|
"call_id": call_id,
|
|
|
|
|
"name": name,
|
|
|
|
|
"input": input
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn ev_local_shell_call(call_id: &str, status: &str, command: Vec<&str>) -> Value {
|
|
|
|
|
serde_json::json!({
|
|
|
|
|
"type": "response.output_item.done",
|
|
|
|
|
"item": {
|
|
|
|
|
"type": "local_shell_call",
|
|
|
|
|
"call_id": call_id,
|
|
|
|
|
"status": status,
|
|
|
|
|
"action": {
|
|
|
|
|
"type": "exec",
|
|
|
|
|
"command": command,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-19 06:46:25 -07:00
|
|
|
/// Convenience: SSE event for an `apply_patch` custom tool call with raw patch
|
|
|
|
|
/// text. This mirrors the payload produced by the Responses API when the model
|
|
|
|
|
/// invokes `apply_patch` directly (before we convert it to a function call).
|
|
|
|
|
pub fn ev_apply_patch_custom_tool_call(call_id: &str, patch: &str) -> Value {
|
|
|
|
|
serde_json::json!({
|
|
|
|
|
"type": "response.output_item.done",
|
|
|
|
|
"item": {
|
|
|
|
|
"type": "custom_tool_call",
|
|
|
|
|
"name": "apply_patch",
|
|
|
|
|
"input": patch,
|
|
|
|
|
"call_id": call_id
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Convenience: SSE event for an `apply_patch` function call. The Responses API
|
|
|
|
|
/// wraps the patch content in a JSON string under the `input` key; we recreate
|
|
|
|
|
/// the same structure so downstream code exercises the full parsing path.
|
|
|
|
|
pub fn ev_apply_patch_function_call(call_id: &str, patch: &str) -> Value {
|
|
|
|
|
let arguments = serde_json::json!({ "input": patch });
|
|
|
|
|
let arguments = serde_json::to_string(&arguments).expect("serialize apply_patch arguments");
|
|
|
|
|
|
|
|
|
|
serde_json::json!({
|
|
|
|
|
"type": "response.output_item.done",
|
|
|
|
|
"item": {
|
|
|
|
|
"type": "function_call",
|
|
|
|
|
"name": "apply_patch",
|
|
|
|
|
"arguments": arguments,
|
|
|
|
|
"call_id": call_id
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-04 18:40:06 -07:00
|
|
|
pub fn sse_failed(id: &str, code: &str, message: &str) -> String {
|
|
|
|
|
sse(vec![serde_json::json!({
|
|
|
|
|
"type": "response.failed",
|
|
|
|
|
"response": {
|
|
|
|
|
"id": id,
|
|
|
|
|
"error": {"code": code, "message": message}
|
|
|
|
|
}
|
|
|
|
|
})])
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-18 17:53:14 -07:00
|
|
|
pub fn sse_response(body: String) -> ResponseTemplate {
|
|
|
|
|
ResponseTemplate::new(200)
|
|
|
|
|
.insert_header("content-type", "text/event-stream")
|
|
|
|
|
.set_body_raw(body, "text/event-stream")
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-07 01:56:39 -07:00
|
|
|
fn base_mock() -> (MockBuilder, ResponseMock) {
|
|
|
|
|
let response_mock = ResponseMock::new();
|
|
|
|
|
let mock = Mock::given(method("POST"))
|
|
|
|
|
.and(path_regex(".*/responses$"))
|
|
|
|
|
.and(response_mock.clone());
|
|
|
|
|
(mock, response_mock)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn mount_sse_once_match<M>(server: &MockServer, matcher: M, body: String) -> ResponseMock
|
2025-09-18 17:53:14 -07:00
|
|
|
where
|
|
|
|
|
M: wiremock::Match + Send + Sync + 'static,
|
|
|
|
|
{
|
2025-10-07 01:56:39 -07:00
|
|
|
let (mock, response_mock) = base_mock();
|
|
|
|
|
mock.and(matcher)
|
2025-09-18 17:53:14 -07:00
|
|
|
.respond_with(sse_response(body))
|
2025-09-25 18:17:51 -07:00
|
|
|
.up_to_n_times(1)
|
2025-09-18 17:53:14 -07:00
|
|
|
.mount(server)
|
|
|
|
|
.await;
|
2025-10-07 01:56:39 -07:00
|
|
|
response_mock
|
2025-09-18 17:53:14 -07:00
|
|
|
}
|
|
|
|
|
|
2025-10-07 01:56:39 -07:00
|
|
|
pub async fn mount_sse_once(server: &MockServer, body: String) -> ResponseMock {
|
|
|
|
|
let (mock, response_mock) = base_mock();
|
|
|
|
|
mock.respond_with(sse_response(body))
|
|
|
|
|
.up_to_n_times(1)
|
OpenTelemetry events (#2103)
### Title
## otel
Codex can emit [OpenTelemetry](https://opentelemetry.io/) **log events**
that
describe each run: outbound API requests, streamed responses, user
input,
tool-approval decisions, and the result of every tool invocation. Export
is
**disabled by default** so local runs remain self-contained. Opt in by
adding an
`[otel]` table and choosing an exporter.
```toml
[otel]
environment = "staging" # defaults to "dev"
exporter = "none" # defaults to "none"; set to otlp-http or otlp-grpc to send events
log_user_prompt = false # defaults to false; redact prompt text unless explicitly enabled
```
Codex tags every exported event with `service.name = "codex-cli"`, the
CLI
version, and an `env` attribute so downstream collectors can distinguish
dev/staging/prod traffic. Only telemetry produced inside the
`codex_otel`
crate—the events listed below—is forwarded to the exporter.
### Event catalog
Every event shares a common set of metadata fields: `event.timestamp`,
`conversation.id`, `app.version`, `auth_mode` (when available),
`user.account_id` (when available), `terminal.type`, `model`, and
`slug`.
With OTEL enabled Codex emits the following event types (in addition to
the
metadata above):
- `codex.api_request`
- `cf_ray` (optional)
- `attempt`
- `duration_ms`
- `http.response.status_code` (optional)
- `error.message` (failures)
- `codex.sse_event`
- `event.kind`
- `duration_ms`
- `error.message` (failures)
- `input_token_count` (completion only)
- `output_token_count` (completion only)
- `cached_token_count` (completion only, optional)
- `reasoning_token_count` (completion only, optional)
- `tool_token_count` (completion only)
- `codex.user_prompt`
- `prompt_length`
- `prompt` (redacted unless `log_user_prompt = true`)
- `codex.tool_decision`
- `tool_name`
- `call_id`
- `decision` (`approved`, `approved_for_session`, `denied`, or `abort`)
- `source` (`config` or `user`)
- `codex.tool_result`
- `tool_name`
- `call_id`
- `arguments`
- `duration_ms` (execution time for the tool)
- `success` (`"true"` or `"false"`)
- `output`
### Choosing an exporter
Set `otel.exporter` to control where events go:
- `none` – leaves instrumentation active but skips exporting. This is
the
default.
- `otlp-http` – posts OTLP log records to an OTLP/HTTP collector.
Specify the
endpoint, protocol, and headers your collector expects:
```toml
[otel]
exporter = { otlp-http = {
endpoint = "https://otel.example.com/v1/logs",
protocol = "binary",
headers = { "x-otlp-api-key" = "${OTLP_TOKEN}" }
}}
```
- `otlp-grpc` – streams OTLP log records over gRPC. Provide the endpoint
and any
metadata headers:
```toml
[otel]
exporter = { otlp-grpc = {
endpoint = "https://otel.example.com:4317",
headers = { "x-otlp-meta" = "abc123" }
}}
```
If the exporter is `none` nothing is written anywhere; otherwise you
must run or point to your
own collector. All exporters run on a background batch worker that is
flushed on
shutdown.
If you build Codex from source the OTEL crate is still behind an `otel`
feature
flag; the official prebuilt binaries ship with the feature enabled. When
the
feature is disabled the telemetry hooks become no-ops so the CLI
continues to
function without the extra dependencies.
---------
Co-authored-by: Anton Panasenko <apanasenko@openai.com>
2025-09-29 19:30:55 +01:00
|
|
|
.mount(server)
|
|
|
|
|
.await;
|
2025-10-07 01:56:39 -07:00
|
|
|
response_mock
|
OpenTelemetry events (#2103)
### Title
## otel
Codex can emit [OpenTelemetry](https://opentelemetry.io/) **log events**
that
describe each run: outbound API requests, streamed responses, user
input,
tool-approval decisions, and the result of every tool invocation. Export
is
**disabled by default** so local runs remain self-contained. Opt in by
adding an
`[otel]` table and choosing an exporter.
```toml
[otel]
environment = "staging" # defaults to "dev"
exporter = "none" # defaults to "none"; set to otlp-http or otlp-grpc to send events
log_user_prompt = false # defaults to false; redact prompt text unless explicitly enabled
```
Codex tags every exported event with `service.name = "codex-cli"`, the
CLI
version, and an `env` attribute so downstream collectors can distinguish
dev/staging/prod traffic. Only telemetry produced inside the
`codex_otel`
crate—the events listed below—is forwarded to the exporter.
### Event catalog
Every event shares a common set of metadata fields: `event.timestamp`,
`conversation.id`, `app.version`, `auth_mode` (when available),
`user.account_id` (when available), `terminal.type`, `model`, and
`slug`.
With OTEL enabled Codex emits the following event types (in addition to
the
metadata above):
- `codex.api_request`
- `cf_ray` (optional)
- `attempt`
- `duration_ms`
- `http.response.status_code` (optional)
- `error.message` (failures)
- `codex.sse_event`
- `event.kind`
- `duration_ms`
- `error.message` (failures)
- `input_token_count` (completion only)
- `output_token_count` (completion only)
- `cached_token_count` (completion only, optional)
- `reasoning_token_count` (completion only, optional)
- `tool_token_count` (completion only)
- `codex.user_prompt`
- `prompt_length`
- `prompt` (redacted unless `log_user_prompt = true`)
- `codex.tool_decision`
- `tool_name`
- `call_id`
- `decision` (`approved`, `approved_for_session`, `denied`, or `abort`)
- `source` (`config` or `user`)
- `codex.tool_result`
- `tool_name`
- `call_id`
- `arguments`
- `duration_ms` (execution time for the tool)
- `success` (`"true"` or `"false"`)
- `output`
### Choosing an exporter
Set `otel.exporter` to control where events go:
- `none` – leaves instrumentation active but skips exporting. This is
the
default.
- `otlp-http` – posts OTLP log records to an OTLP/HTTP collector.
Specify the
endpoint, protocol, and headers your collector expects:
```toml
[otel]
exporter = { otlp-http = {
endpoint = "https://otel.example.com/v1/logs",
protocol = "binary",
headers = { "x-otlp-api-key" = "${OTLP_TOKEN}" }
}}
```
- `otlp-grpc` – streams OTLP log records over gRPC. Provide the endpoint
and any
metadata headers:
```toml
[otel]
exporter = { otlp-grpc = {
endpoint = "https://otel.example.com:4317",
headers = { "x-otlp-meta" = "abc123" }
}}
```
If the exporter is `none` nothing is written anywhere; otherwise you
must run or point to your
own collector. All exporters run on a background batch worker that is
flushed on
shutdown.
If you build Codex from source the OTEL crate is still behind an `otel`
feature
flag; the official prebuilt binaries ship with the feature enabled. When
the
feature is disabled the telemetry hooks become no-ops so the CLI
continues to
function without the extra dependencies.
---------
Co-authored-by: Anton Panasenko <apanasenko@openai.com>
2025-09-29 19:30:55 +01:00
|
|
|
}
|
|
|
|
|
|
2025-10-07 01:56:39 -07:00
|
|
|
pub async fn mount_sse(server: &MockServer, body: String) -> ResponseMock {
|
|
|
|
|
let (mock, response_mock) = base_mock();
|
|
|
|
|
mock.respond_with(sse_response(body)).mount(server).await;
|
|
|
|
|
response_mock
|
OpenTelemetry events (#2103)
### Title
## otel
Codex can emit [OpenTelemetry](https://opentelemetry.io/) **log events**
that
describe each run: outbound API requests, streamed responses, user
input,
tool-approval decisions, and the result of every tool invocation. Export
is
**disabled by default** so local runs remain self-contained. Opt in by
adding an
`[otel]` table and choosing an exporter.
```toml
[otel]
environment = "staging" # defaults to "dev"
exporter = "none" # defaults to "none"; set to otlp-http or otlp-grpc to send events
log_user_prompt = false # defaults to false; redact prompt text unless explicitly enabled
```
Codex tags every exported event with `service.name = "codex-cli"`, the
CLI
version, and an `env` attribute so downstream collectors can distinguish
dev/staging/prod traffic. Only telemetry produced inside the
`codex_otel`
crate—the events listed below—is forwarded to the exporter.
### Event catalog
Every event shares a common set of metadata fields: `event.timestamp`,
`conversation.id`, `app.version`, `auth_mode` (when available),
`user.account_id` (when available), `terminal.type`, `model`, and
`slug`.
With OTEL enabled Codex emits the following event types (in addition to
the
metadata above):
- `codex.api_request`
- `cf_ray` (optional)
- `attempt`
- `duration_ms`
- `http.response.status_code` (optional)
- `error.message` (failures)
- `codex.sse_event`
- `event.kind`
- `duration_ms`
- `error.message` (failures)
- `input_token_count` (completion only)
- `output_token_count` (completion only)
- `cached_token_count` (completion only, optional)
- `reasoning_token_count` (completion only, optional)
- `tool_token_count` (completion only)
- `codex.user_prompt`
- `prompt_length`
- `prompt` (redacted unless `log_user_prompt = true`)
- `codex.tool_decision`
- `tool_name`
- `call_id`
- `decision` (`approved`, `approved_for_session`, `denied`, or `abort`)
- `source` (`config` or `user`)
- `codex.tool_result`
- `tool_name`
- `call_id`
- `arguments`
- `duration_ms` (execution time for the tool)
- `success` (`"true"` or `"false"`)
- `output`
### Choosing an exporter
Set `otel.exporter` to control where events go:
- `none` – leaves instrumentation active but skips exporting. This is
the
default.
- `otlp-http` – posts OTLP log records to an OTLP/HTTP collector.
Specify the
endpoint, protocol, and headers your collector expects:
```toml
[otel]
exporter = { otlp-http = {
endpoint = "https://otel.example.com/v1/logs",
protocol = "binary",
headers = { "x-otlp-api-key" = "${OTLP_TOKEN}" }
}}
```
- `otlp-grpc` – streams OTLP log records over gRPC. Provide the endpoint
and any
metadata headers:
```toml
[otel]
exporter = { otlp-grpc = {
endpoint = "https://otel.example.com:4317",
headers = { "x-otlp-meta" = "abc123" }
}}
```
If the exporter is `none` nothing is written anywhere; otherwise you
must run or point to your
own collector. All exporters run on a background batch worker that is
flushed on
shutdown.
If you build Codex from source the OTEL crate is still behind an `otel`
feature
flag; the official prebuilt binaries ship with the feature enabled. When
the
feature is disabled the telemetry hooks become no-ops so the CLI
continues to
function without the extra dependencies.
---------
Co-authored-by: Anton Panasenko <apanasenko@openai.com>
2025-09-29 19:30:55 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-18 17:53:14 -07:00
|
|
|
pub async fn start_mock_server() -> MockServer {
|
|
|
|
|
MockServer::builder()
|
|
|
|
|
.body_print_limit(BodyPrintLimit::Limited(80_000))
|
|
|
|
|
.start()
|
|
|
|
|
.await
|
|
|
|
|
}
|
2025-09-25 17:12:45 -07:00
|
|
|
|
|
|
|
|
/// Mounts a sequence of SSE response bodies and serves them in order for each
|
|
|
|
|
/// POST to `/v1/responses`. Panics if more requests are received than bodies
|
|
|
|
|
/// provided. Also asserts the exact number of expected calls.
|
2025-10-07 01:56:39 -07:00
|
|
|
pub async fn mount_sse_sequence(server: &MockServer, bodies: Vec<String>) -> ResponseMock {
|
2025-09-25 17:12:45 -07:00
|
|
|
use std::sync::atomic::AtomicUsize;
|
|
|
|
|
use std::sync::atomic::Ordering;
|
|
|
|
|
|
|
|
|
|
struct SeqResponder {
|
|
|
|
|
num_calls: AtomicUsize,
|
|
|
|
|
responses: Vec<String>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Respond for SeqResponder {
|
|
|
|
|
fn respond(&self, _: &wiremock::Request) -> ResponseTemplate {
|
|
|
|
|
let call_num = self.num_calls.fetch_add(1, Ordering::SeqCst);
|
|
|
|
|
match self.responses.get(call_num) {
|
|
|
|
|
Some(body) => ResponseTemplate::new(200)
|
|
|
|
|
.insert_header("content-type", "text/event-stream")
|
|
|
|
|
.set_body_string(body.clone()),
|
|
|
|
|
None => panic!("no response for {call_num}"),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let num_calls = bodies.len();
|
|
|
|
|
let responder = SeqResponder {
|
|
|
|
|
num_calls: AtomicUsize::new(0),
|
|
|
|
|
responses: bodies,
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-07 01:56:39 -07:00
|
|
|
let (mock, response_mock) = base_mock();
|
|
|
|
|
mock.respond_with(responder)
|
2025-09-25 17:12:45 -07:00
|
|
|
.expect(num_calls as u64)
|
|
|
|
|
.mount(server)
|
|
|
|
|
.await;
|
2025-10-07 01:56:39 -07:00
|
|
|
|
|
|
|
|
response_mock
|
2025-09-25 17:12:45 -07:00
|
|
|
}
|