Skip to main content

rust_ef/observability/
save_changes_guard.rs

1#[cfg(feature = "tracing")]
2use std::time::Instant;
3
4/// Guards a `save_changes` operation, emitting timing on drop.
5#[cfg(feature = "tracing")]
6pub struct SaveChangesGuard {
7    start: Instant,
8}
9
10#[cfg(feature = "tracing")]
11impl SaveChangesGuard {
12    pub fn new() -> Self {
13        Self {
14            start: Instant::now(),
15        }
16    }
17}
18
19#[cfg(feature = "tracing")]
20impl Default for SaveChangesGuard {
21    fn default() -> Self {
22        Self::new()
23    }
24}
25
26#[cfg(feature = "tracing")]
27impl Drop for SaveChangesGuard {
28    fn drop(&mut self) {
29        let elapsed = self.start.elapsed();
30        tracing::info!(
31            target: "rust_ef::save_changes",
32            elapsed_ms = elapsed.as_millis() as u64,
33            "save_changes completed"
34        );
35    }
36}
37
38#[cfg(not(feature = "tracing"))]
39pub struct SaveChangesGuard;
40
41#[cfg(not(feature = "tracing"))]
42impl Default for SaveChangesGuard {
43    fn default() -> Self {
44        Self
45    }
46}
47
48#[cfg(not(feature = "tracing"))]
49impl SaveChangesGuard {
50    pub fn new() -> Self {
51        Self
52    }
53}