Skip to main content

shared_framework/middleware/
monitoring.rs

1//! Monitoring helpers for logging request outcomes.
2//!
3//! [`RequestLogger`] records one `monitor`-target log line per completed request with path,
4//! method, status, and latency. Call [`RequestLogger::start`] before handling and
5//! [`RequestLogger::log`] after to emit the measurement.
6//! ```ignore
7//! let start = RequestLogger::start();
8//! // ... handle request, obtain `status` ...
9//! RequestLogger::log("/users/list", "GET", status, start.elapsed());
10//! ```
11
12use std::time::Instant;
13
14/// Emits one `monitor`-target log line per completed request with path, method, status, and latency.
15pub struct RequestLogger;
16
17impl RequestLogger {
18    /// Logs a completed request with its path, method, status code, and latency.
19    pub fn log(path: &str, method: &str, status: u16, latency: std::time::Duration) {
20        tracing::trace!(
21            target: "monitor",
22            path = %path,
23            method = %method,
24            status = status,
25            latency_ms = latency.as_millis() as u64,
26            "Request completed"
27        );
28    }
29
30    /// Captures the start instant for a latency measurement; call `elapsed()` on it when logging.
31    pub fn start() -> Instant { Instant::now() }
32}