mirror of
https://github.com/openai/codex.git
synced 2026-11-18 22:20:48 +00:00
Add optional process metrics sampling for code-mode hosts (#57841)
## What changed - Add `GetProcessMetrics` and provider sampling APIs for process ID, start time, cumulative CPU time, and resident memory, exposed through `ThreadManager` without creating a thread or session. - Distinguish unsupported collection, a locally absent host, and sampling failures. Process-owned providers never start or replace a host when sampling. - Bound gRPC sampling to one second without retries or explicit connection resets. Run optional collectors on a detached worker with at most one collection in flight, so stalled collection cannot block the HTTP/2 driver or runtime shutdown. ## Testing Add coverage for snapshots, collector errors and panics, deadlines, overload, and shutdown with a stuck collector. Extend lifecycle tests to check that sampling preserves session state and keepalive behavior without starting or restarting local hosts. GitOrigin-RevId: eeafe275891d0464636f28a29345a87aed1a12dc
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
//! A bounded, non-retrying host observation. No session binding or recovery is
|
||||
//! involved: a failed RPC never explicitly retires the execution connection.
|
||||
//! Connection establishment and reconnection remain the transport's policy.
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use codex_code_mode_protocol::CodeModeProcessMetrics;
|
||||
use codex_code_mode_protocol::CodeModeProcessMetricsSample;
|
||||
use codex_code_mode_protocol::grpc;
|
||||
use tonic::Code;
|
||||
|
||||
use super::GrpcCodeModeSessionProvider;
|
||||
|
||||
pub(super) async fn sample(
|
||||
provider: &GrpcCodeModeSessionProvider,
|
||||
) -> Result<CodeModeProcessMetricsSample, String> {
|
||||
let deadline = Duration::from_secs(/*secs*/ 1);
|
||||
tokio::time::timeout(deadline, async {
|
||||
let mut client = provider.transport.client().await?;
|
||||
let mut request = tonic::Request::new(grpc::GetProcessMetricsRequest {});
|
||||
request.set_timeout(deadline);
|
||||
match client.get_process_metrics(request).await {
|
||||
Ok(response) => {
|
||||
let snapshot = response.into_inner();
|
||||
if snapshot.process_id == 0
|
||||
|| !snapshot.process_start_time_seconds.is_finite()
|
||||
|| snapshot.process_start_time_seconds <= 0.0
|
||||
|| !snapshot.cpu_seconds_total.is_finite()
|
||||
|| snapshot.cpu_seconds_total < 0.0
|
||||
{
|
||||
return Err("invalid code-mode process metrics snapshot".to_string());
|
||||
}
|
||||
Ok(CodeModeProcessMetricsSample::Snapshot(
|
||||
CodeModeProcessMetrics {
|
||||
process_id: snapshot.process_id,
|
||||
process_start_time_seconds: snapshot.process_start_time_seconds,
|
||||
cpu_seconds_total: snapshot.cpu_seconds_total,
|
||||
resident_memory_bytes: snapshot.resident_memory_bytes,
|
||||
},
|
||||
))
|
||||
}
|
||||
Err(status) if status.code() == Code::Unimplemented => {
|
||||
Ok(CodeModeProcessMetricsSample::Unsupported)
|
||||
}
|
||||
Err(status) => Err(format!("code-mode process metrics failed: {status}")),
|
||||
}
|
||||
})
|
||||
.await
|
||||
.map_err(|_| "code-mode process metrics timed out".to_string())?
|
||||
}
|
||||
Reference in New Issue
Block a user