Skip to main content

valence_core/instrumentation/
timing.rs

1//! Wall-time helpers for mutation and query instrumentation.
2
3use std::time::Instant;
4
5use super::metrics;
6
7/// Timer for mutation wall time (L1).
8pub struct MutationTimer {
9    operation: &'static str,
10    start: Instant,
11}
12
13impl MutationTimer {
14    #[must_use]
15    pub fn start(operation: &'static str) -> Self {
16        Self {
17            operation,
18            start: Instant::now(),
19        }
20    }
21
22    pub fn finish(self) {
23        let ms = self.start.elapsed().as_secs_f64() * 1000.0;
24        metrics::record_mutation_wall_ms(self.operation, ms);
25    }
26}
27
28pub struct QueryTimer {
29    start: Instant,
30}
31
32impl QueryTimer {
33    #[must_use]
34    pub fn start(_primary_table: impl Into<String>, _query_target: impl Into<String>) -> Self {
35        Self {
36            start: Instant::now(),
37        }
38    }
39
40    #[allow(
41        clippy::cast_possible_truncation,
42        reason = "telemetry duration is intentionally bounded by i64 milliseconds"
43    )]
44    pub fn elapsed_ms(&self) -> i64 {
45        self.start.elapsed().as_millis() as i64
46    }
47}