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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/// Prelude module for convenient imports.
///
/// ```rust
/// use sideways_otel::prelude::*;
/// ```
///
/// Unlike vendor-specific metrics libraries, OpenTelemetry metrics don't need
/// macros - instruments are created once and then recorded against directly.
/// This prelude re-exports the pieces needed to do that, plus a handful of
/// `counter`/`histogram`/`up_down_counter`/`gauge` shortcuts for the common
/// synchronous case, and `observable_counter`/`observable_up_down_counter`/
/// `observable_gauge` for the async/callback-based case (a value that's
/// cheaper or more natural to sample at collection time - queue depth, open
/// file descriptors - than to push on every change). There's no
/// `observable_histogram`: the `OTel` spec doesn't define one, since a
/// histogram's whole point is recording a distribution of individual
/// measurements as they happen.
///
/// TODO: these shortcuts only cover the default numeric type per instrument
/// kind (`u64` counter, `f64` histogram/gauge, `i64` up/down counter) and
/// skip the alternate numeric types (`f64_counter`, `u64_histogram`,
/// `i64_gauge`, `f64_up_down_counter`). Add wrappers for these as real use
/// cases show up, rather than pre-building the full matrix speculatively.
pub use ;
pub use ;
pub use OpenTelemetrySpanExt;
pub use crate;
use Cow;
/// Get (or create) the global `Meter` scoped to the service name configured
/// via [`crate::init_telemetry`].
///
/// ```rust,no_run
/// use sideways_otel::prelude::*;
///
/// let requests = meter().u64_counter("requests.handled").build();
/// requests.add(1, &[KeyValue::new("status", "success")]);
/// ```
/// Create a `u64` counter instrument for recording increasing values.
///
/// ```rust,no_run
/// use sideways_otel::prelude::*;
///
/// let requests = counter("requests.handled");
/// requests.add(1, &[KeyValue::new("status", "success")]);
/// ```
/// Create an `f64` histogram instrument for recording a distribution of
/// values (e.g. request durations).
///
/// ```rust,no_run
/// use sideways_otel::prelude::*;
///
/// let latency = histogram("request.duration_ms");
/// latency.record(42.5, &[KeyValue::new("endpoint", "/users")]);
/// ```
/// Create an `i64` up/down counter instrument for values that both increase
/// and decrease (e.g. a queue depth).
///
/// ```rust,no_run
/// use sideways_otel::prelude::*;
///
/// let queue_depth = up_down_counter("queue.depth");
/// queue_depth.add(-1, &[KeyValue::new("queue", "emails")]);
/// ```
/// Create an `f64` gauge instrument for recording the current value of
/// something that isn't a sum (e.g. CPU usage, temperature).
///
/// ```rust,no_run
/// use sideways_otel::prelude::*;
///
/// let cpu_usage = gauge("cpu.usage_percent");
/// cpu_usage.record(45.2, &[KeyValue::new("host", "web-01")]);
/// ```
/// Create a `u64` observable (async) counter: `callback` is invoked by the
/// SDK once per collection cycle rather than being called directly, and
/// should call `observer.observe(value, attributes)` for each value it wants
/// to report (more than once, with different attributes, if applicable).
///
/// ```rust,no_run
/// use sideways_otel::prelude::*;
///
/// let _requests_total = observable_counter("requests.total", |observer| {
/// observer.observe(current_request_count(), &[]);
/// });
/// # fn current_request_count() -> u64 { 0 }
/// ```
/// Create an `i64` observable (async) up/down counter: `callback` is invoked
/// by the SDK once per collection cycle and should call
/// `observer.observe(value, attributes)` for each value it wants to report.
///
/// ```rust,no_run
/// use sideways_otel::prelude::*;
///
/// let _queue_depth = observable_up_down_counter("queue.depth", |observer| {
/// observer.observe(current_queue_depth(), &[KeyValue::new("queue", "emails")]);
/// });
/// # fn current_queue_depth() -> i64 { 0 }
/// ```
/// Create an `f64` observable (async) gauge: `callback` is invoked by the
/// SDK once per collection cycle and should call
/// `observer.observe(value, attributes)` for each value it wants to report.
/// Prefer this over [`gauge`] whenever the current value is cheaper or more
/// natural to sample on demand (e.g. reading OS/process stats) than to push
/// on every change.
///
/// ```rust,no_run
/// use sideways_otel::prelude::*;
///
/// let _cpu_usage = observable_gauge("cpu.usage_percent", |observer| {
/// observer.observe(current_cpu_usage(), &[KeyValue::new("host", "web-01")]);
/// });
/// # fn current_cpu_usage() -> f64 { 0.0 }
/// ```