Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions bin/debug-trace-server/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,26 @@ impl ResponseSizeMetrics {
}
}

/// CPU execution time per request (global, no method label).
#[derive(Clone, Metrics)]
#[metrics(scope = "debug_trace")]
pub struct CpuTimeMetrics {
/// CPU execution time per request in seconds
cpu_time_seconds: Histogram,
}

impl CpuTimeMetrics {
/// Creates global CPU time metrics.
pub fn create() -> Self {
Self::new_with_labels(&[] as &[(&str, &str)])
}

/// Records a CPU time measurement.
pub fn record(&self, seconds: f64) {
self.cpu_time_seconds.record(seconds);
}
}

// ---------------------------------------------------------------------------
// ── Cache Layer ────────────────────────────────
// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -427,6 +447,9 @@ fn pre_register_all_metrics() {
let _ = ResponseSizeMetrics::new_for_method(METHOD_TRACE_BLOCK);
let _ = ResponseSizeMetrics::new_for_method(METHOD_TRACE_TRANSACTION);

// Request Layer: CPU time (global)
let _ = CpuTimeMetrics::create();

// Cache Layer
let _ = CacheMetrics::new_for_cache(CACHE_TYPE_DEBUG_TRACE);
let _ = CacheMetrics::new_for_cache(CACHE_TYPE_TRACE);
Expand Down
4 changes: 4 additions & 0 deletions bin/debug-trace-server/src/timing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use http::{HeaderName, HeaderValue};
use pin_project_lite::pin_project;
use tower::{Layer, Service};

use crate::metrics::CpuTimeMetrics;

/// Header name for execution time in nanoseconds.
pub const TIMING_HEADER_NAME: &str = "x-execution-time-ns";

Expand Down Expand Up @@ -111,6 +113,8 @@ where
response.headers_mut().insert(header_name, header_value);
}
}
// Record as Prometheus metric
CpuTimeMetrics::create().record(Duration::from_nanos(cpu_ns).as_secs_f64());
Poll::Ready(Ok(response))
}
Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
Expand Down