Skip to main content

rust_ef/observability/
pool_guard.rs

1#[cfg(feature = "tracing")]
2use std::time::Instant;
3
4/// Guards a connection pool acquisition, emitting acquire timing on drop.
5#[cfg(feature = "tracing")]
6pub struct PoolAcquireGuard {
7    provider: &'static str,
8    start: Instant,
9}
10
11#[cfg(feature = "tracing")]
12impl PoolAcquireGuard {
13    pub fn new(provider: &'static str) -> Self {
14        Self {
15            provider,
16            start: Instant::now(),
17        }
18    }
19}
20
21#[cfg(feature = "tracing")]
22impl Drop for PoolAcquireGuard {
23    fn drop(&mut self) {
24        let elapsed = self.start.elapsed();
25        tracing::info!(
26            target: "rust_ef::pool",
27            provider = self.provider,
28            acquire_ms = elapsed.as_millis() as u64,
29            "connection acquired"
30        );
31    }
32}
33
34#[cfg(not(feature = "tracing"))]
35pub struct PoolAcquireGuard;
36
37#[cfg(not(feature = "tracing"))]
38impl PoolAcquireGuard {
39    pub fn new(_provider: &'static str) -> Self {
40        Self
41    }
42}