pub struct TimingMetrics {
pub start_time: Instant,
pub first_byte_time: Option<Instant>,
pub custom_metrics: Vec<(String, f64, Option<String>)>,
}Expand description
Collected timing metrics for a single request.
This struct is stored in request extensions and can be read by interceptors or logging middleware to expose timing data.
§Usage
Handlers can access and modify timing metrics via request extensions:
ⓘ
// Add a custom timing metric
if let Some(metrics) = req.get_extension_mut::<TimingMetrics>() {
metrics.add_metric("db", db_time.as_secs_f64() * 1000.0);
}Fields§
§start_time: InstantWhen the request processing started.
first_byte_time: Option<Instant>When the first byte of the response was sent (if known).
custom_metrics: Vec<(String, f64, Option<String>)>Custom metrics added by handlers (name -> duration_ms).
Implementations§
Source§impl TimingMetrics
impl TimingMetrics
Sourcepub fn with_start_time(start_time: Instant) -> Self
pub fn with_start_time(start_time: Instant) -> Self
Create timing metrics with a specific start time.
Sourcepub fn mark_first_byte(&mut self)
pub fn mark_first_byte(&mut self)
Mark the time when the first byte of the response was sent.
Sourcepub fn add_metric(&mut self, name: impl Into<String>, duration_ms: f64)
pub fn add_metric(&mut self, name: impl Into<String>, duration_ms: f64)
Add a custom metric (e.g., database query time).
Sourcepub fn add_metric_with_desc(
&mut self,
name: impl Into<String>,
duration_ms: f64,
desc: impl Into<String>,
)
pub fn add_metric_with_desc( &mut self, name: impl Into<String>, duration_ms: f64, desc: impl Into<String>, )
Add a custom metric with a description.
Sourcepub fn ttfb_ms(&self) -> Option<f64>
pub fn ttfb_ms(&self) -> Option<f64>
Get the time-to-first-byte in milliseconds (if available).
Sourcepub fn to_server_timing(&self) -> ServerTimingBuilder
pub fn to_server_timing(&self) -> ServerTimingBuilder
Build a Server-Timing header from the collected metrics.
Trait Implementations§
Source§impl Clone for TimingMetrics
impl Clone for TimingMetrics
Source§fn clone(&self) -> TimingMetrics
fn clone(&self) -> TimingMetrics
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for TimingMetrics
impl Debug for TimingMetrics
Auto Trait Implementations§
impl Freeze for TimingMetrics
impl RefUnwindSafe for TimingMetrics
impl Send for TimingMetrics
impl Sync for TimingMetrics
impl Unpin for TimingMetrics
impl UnwindSafe for TimingMetrics
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, _span: NoopSpan) -> Self
fn instrument(self, _span: NoopSpan) -> Self
Instruments this future with a span (no-op when disabled).
Source§fn in_current_span(self) -> Self
fn in_current_span(self) -> Self
Instruments this future with the current span (no-op when disabled).