mirror of
https://github.com/openai/codex.git
synced 2026-11-20 22:40:37 +00:00
exec-server: cover metric export end to end
This commit is contained in:
@@ -2,6 +2,7 @@ use std::io::Read as _;
|
||||
use std::io::Write as _;
|
||||
use std::net::TcpListener;
|
||||
use std::path::Path;
|
||||
use std::sync::mpsc;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use std::time::Instant;
|
||||
@@ -55,6 +56,53 @@ fn local_exec_server_ignores_invalid_config_without_strict_config() -> Result<()
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn local_exec_server_exports_real_otel_metrics() -> Result<()> {
|
||||
let collector = TestCollector::start()?;
|
||||
let codex_home = TempDir::new()?;
|
||||
let base_url = &collector.base_url;
|
||||
std::fs::write(
|
||||
codex_home.path().join("config.toml"),
|
||||
format!(
|
||||
r#"
|
||||
[analytics]
|
||||
enabled = true
|
||||
|
||||
[otel]
|
||||
environment = "test"
|
||||
metrics_exporter = {{ otlp-http = {{ endpoint = "{base_url}/v1/metrics", protocol = "json" }} }}
|
||||
"#
|
||||
),
|
||||
)?;
|
||||
|
||||
let mut cmd = codex_command(codex_home.path())?;
|
||||
cmd.args(["exec-server", "--listen", "stdio"])
|
||||
.write_stdin(
|
||||
r#"{"id":1,"method":"initialize","params":{"clientName":"otel-test","resumeSessionId":null}}"#,
|
||||
)
|
||||
.assert()
|
||||
.success();
|
||||
|
||||
let requests = collector.finish()?;
|
||||
let metrics = requests
|
||||
.iter()
|
||||
.filter(|request| request.path == "/v1/metrics")
|
||||
.map(|request| request.body.as_str())
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
assert!(
|
||||
metrics.contains("exec_server_connections_active"),
|
||||
"{metrics}"
|
||||
);
|
||||
assert!(metrics.contains("exec_server_requests_total"), "{metrics}");
|
||||
assert!(metrics.contains("initialize"), "{metrics}");
|
||||
assert!(
|
||||
metrics.contains("success") || metrics.contains("disconnected"),
|
||||
"{metrics}"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remote_exec_server_preserves_websocket_error_in_stderr() -> Result<()> {
|
||||
let failed_websocket_listener = TcpListener::bind("127.0.0.1:0")?;
|
||||
@@ -109,6 +157,60 @@ struct CapturedRequest {
|
||||
body: String,
|
||||
}
|
||||
|
||||
struct TestCollector {
|
||||
base_url: String,
|
||||
requests: mpsc::Receiver<Vec<CapturedRequest>>,
|
||||
stop: mpsc::Sender<()>,
|
||||
server: thread::JoinHandle<()>,
|
||||
}
|
||||
|
||||
impl TestCollector {
|
||||
fn start() -> Result<Self> {
|
||||
let listener = TcpListener::bind("127.0.0.1:0")?;
|
||||
let addr = listener.local_addr()?;
|
||||
listener.set_nonblocking(true)?;
|
||||
let (tx, requests) = mpsc::channel();
|
||||
let (stop, stop_rx) = mpsc::channel();
|
||||
let server = thread::spawn(move || {
|
||||
let mut captured = Vec::new();
|
||||
loop {
|
||||
match listener.accept() {
|
||||
Ok((mut stream, _)) => {
|
||||
if let Ok(request) = read_http_request(&mut stream) {
|
||||
captured.push(request);
|
||||
}
|
||||
let _ = stream.write_all(
|
||||
b"HTTP/1.1 202 Accepted\r\nContent-Length: 0\r\nConnection: close\r\n\r\n",
|
||||
);
|
||||
}
|
||||
Err(err) if err.kind() == std::io::ErrorKind::WouldBlock => {
|
||||
if stop_rx.try_recv().is_ok() {
|
||||
break;
|
||||
}
|
||||
thread::sleep(Duration::from_millis(10));
|
||||
}
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
let _ = tx.send(captured);
|
||||
});
|
||||
Ok(Self {
|
||||
base_url: format!("http://{addr}"),
|
||||
requests,
|
||||
stop,
|
||||
server,
|
||||
})
|
||||
}
|
||||
|
||||
fn finish(self) -> Result<Vec<CapturedRequest>> {
|
||||
let _ = self.stop.send(());
|
||||
self.server
|
||||
.join()
|
||||
.map_err(|_| anyhow::anyhow!("collector thread panicked"))?;
|
||||
Ok(self.requests.recv_timeout(Duration::from_secs(1))?)
|
||||
}
|
||||
}
|
||||
|
||||
struct TestRegistry {
|
||||
base_url: String,
|
||||
server: thread::JoinHandle<Result<()>>,
|
||||
|
||||
Reference in New Issue
Block a user