1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//! Optional [`tracing`](https://docs.rs/tracing) integration helpers
//! (v0.9.5, behind the `tracing` Cargo feature).
//!
//! This module is intentionally minimal: it adds opt-in adapters that wrap
//! existing `Timer`/`MetricsCore` operations with a `tracing` span, so the
//! observation shows up in both the metric histogram **and** the user's
//! tracing subscriber. Hot paths in the metric types themselves are
//! unchanged — enabling the `tracing` feature does not slow down
//! `Counter::inc`, `Gauge::set`, `Timer::record`, etc.
//!
//! # When to use
//!
//! - You already use `tracing` for structured logging and want per-operation
//! timings to appear under a named span in addition to being recorded as
//! a metric.
//! - You're profiling a code path and want one call site to populate both
//! the metrics export and the tracing flamegraph.
//!
//! # Example
//!
//! ```
//! # #[cfg(all(feature = "timer", feature = "tracing"))]
//! # {
//! use metrics_lib::{init, metrics};
//! use metrics_lib::tracing_ext::time_in_span;
//!
//! init();
//! let timer = metrics().timer("db.query");
//! let result = time_in_span("db.query", &timer, || {
//! // ... work happens inside both the Timer and a tracing info_span!
//! 42
//! });
//! assert_eq!(result, 42);
//! # }
//! ```
use crateTimer;
use Instant;
/// Time `f` and record the elapsed duration into `timer`, simultaneously
/// emitting a `tracing::info_span!("metric.time", name = …)` for the
/// duration of the call.
///
/// Requires the `tracing` and `timer` features.
/// Time `f` and record the elapsed duration into the global registry's
/// timer named `name`. Wraps the call in a `tracing::info_span!`.
///
/// Equivalent to `time_in_span(name, &metrics().timer(name), f)` but avoids
/// looking up the timer manually. Requires the `tracing` and `timer`
/// features.