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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
//! Common telemetry tools.
//!
//! We focus on supporting the following Rust APIs:
//!
//! - `tracing` for tracing, with support for fowarding from `log`.
//! - `metrics` for monitoring.
//!
//! We specifically try to integrate with OpenTelemetry and to support standard
//! `"traceparent"` and `"tracestate"` headers.
//!
//! ## Usage
//!
//! For a simple async CLI tool, you could use this library like this:
//! ```
//! use anyhow::Result;
//! use opinionated_telemetry::{
//! run_with_telemetry, set_parent_span_from_env, AppType,
//! };
//! use tracing::instrument;
//!
//! #[tokio::main]
//! async fn main() -> Result<()> {
//! run_with_telemetry(
//! AppType::Cli,
//! env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"),
//! main_helper(),
//! ).await
//! }
//!
//! // Note that `instrument` will only work correctly on functions called from
//! // inside `run_with_telemetry`.
//! #[instrument(
//! name = "my-app",
//! fields(version = env!("CARGO_PKG_VERSION"))
//! )]
//! async fn main_helper() -> Result<()> {
//! // Use TRACEPARENT and TRACESTATE from the environment to link into any
//! // existing trace. Or start a new trace if none are present.
//! set_parent_span_from_env();
//! Ok(())
//! }
//! ```
//!
//! For more complex applications, you can use [`TelemetryConfig`]. For
//! synchronous applications, see [`run_with_telemetry_sync`] and
//! [`TelemetryConfig::install_sync`], which are available if the `sync` feature
//! is enabled.
//!
//! ## Features
//!
//! - `sync`: Enable synchronous telemetry support. Use this for otherwise
//! synchronous applications. This is not enabled by default.
//!
//! ## Environment Variables
//!
//! The following variables can be used to configure telemetry:
//!
//! - `RUST_LOG` can be used to control our logging levels in the normal
//! fashion.
//! - `OPINIONATED_TELEMETRY_TRACER` can be set to `cloud_trace` or `debug` to
//! enable OpenTelelmetry tracing. If not set, we will log to stderr using
//! [`tracing`], honoring the filter specified by `RUST_LOG`.
//! - `OPINIONATED_TELEMETRY_METRICS` can be set to `prometheus` to enable
//! Prometheus metrics, or `debug` to log metrics. Otherwise metrics will not
//! be reported.
//! - `OPINIONATED_TELEMETRY_PROMETHEUS_LISTEN_ADDR` defaults to
//! `"0.0.0.0:9090".
//! - `OPINIONATED_TELEMETRY_PROMETHEUS_PUSHGATEWAY_URL` must be specified for
//! CLI tools using Prometheus. We strongly recommend using
//! [`prom-aggregation-gateway`](https://github.com/zapier/prom-aggregation-gateway)
//! instead of [Prometheus's default
//! `pushgateway`](https://github.com/prometheus/pushgateway/).
//! - `OTEL_SERVICE_NAME` and `OTEL_SERVICE_VERSION` can be used to identify
//! your service. If not set, we will use the `service_name` and
//! `service_version` parameters to [`TelemetryConfig`],
//! [`run_with_telemetry`] or [`run_with_telemetry_sync`]. Other `OTEL_`
//! variables supported by the [`opentelemetry`] crate may also be respected.
//!
//! For CLI tools, these variables will normally be set by the calling app:
//!
//! - `TRACEPARENT` and `TRACESTATE` can be passed to CLI tools to link them
//! into an existing trace. These follow the conventions of the [W3C Trace
//! Context](https://www.w3.org/TR/trace-context/).
//!
//! ## Metric naming conventions
//!
//! For best results across different metrics reporting systems, we recommend
//! following the [Prometheus metric naming
//! conventions](https://prometheus.io/docs/practices/naming/).
//!
//! Example metric names:
//!
//! - `myapp_requests_total`: Counter with no units.
//! - `myapp_processed_bytes_total`: Counter with units.
//! - `myapp_memory_usage_bytes`: Gauge with units.
//!
//! ## Label naming
//!
//! Labels should be "low-arity". Specifically, that means that labels should
//! have only a small number of possible values, because each possible label
//! value will require most backends to store a new time series.
use HashMap;
// We re-export `tracing` and `metrics`, but only so users can access
// traits and functions. The macros won't re-export correctly.
use Future;
pub use metrics;
use ;
pub use tracing;
use ;
use ;
pub use ;
pub use TelemetrySyncHandle;
pub use ;
/// What type of application should we configure telemetry for? This will affect
/// how various kinds of telemetry are configured. For example,
/// [`AppType::Server`] would create a Prometheus scape endpoint, but
/// [`AppType::Cli`] would push metrics to a Prometheus push gateway once on
/// shutdown.
/// Interface used to configure and install telemetry.
///
/// ```
/// use anyhow::Result;
/// use opinionated_telemetry::{
/// set_parent_span_from_env, AppType, TelemetryConfig,
/// };
/// use tracing::instrument;
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
/// // Configure and install our telemetry.
/// let handle = TelemetryConfig::new(
/// AppType::Cli, env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"),
/// ).install().await?;
///
/// // Call our real `main` function.
/// let result = main_helper().await;
///
/// // Flush telemetry data, shut down, and return our result.
/// handle.flush_and_shutdown().await;
/// result
/// }
///
/// // Note that `instrument` will only work correctly on functions called
/// // _after_ we call `TelemetryConfig::install`.
/// #[instrument(
/// name = "my-app",
/// fields(version = env!("CARGO_PKG_VERSION"))
/// )]
/// async fn main_helper() -> Result<()> {
/// // Use TRACEPARENT and TRACESTATE from the environment to link into any
/// // existing trace. Or start a new trace if none are present.
/// set_parent_span_from_env();
/// Ok(())
/// }
/// ```
/// A handle that can be used to shut down telemetry and flush any remaining
/// data. If you do not call [`TelemetryHandle::flush_and_shutdown`], telemetry data may be
/// lost.
/// Start all telemetry subsystems, run the given future, and then stop all
/// telemetry subsystems.
///
/// The error type returned by `fut` must support a conversion from
/// [`opinionated_telemetry::Error`].
///
/// ```
/// use anyhow::Result;
/// use opinionated_telemetry::{
/// run_with_telemetry, set_parent_span_from_env, AppType,
/// };
/// use tracing::instrument;
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
/// run_with_telemetry(
/// AppType::Cli,
/// env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"),
/// main_helper(),
/// ).await
/// }
///
/// // Note that `instrument` will only work correctly on functions called from
/// // inside `run_with_telemetry`.
/// #[instrument(
/// name = "my-app",
/// fields(version = env!("CARGO_PKG_VERSION"))
/// )]
/// async fn main_helper() -> Result<()> {
/// // Use TRACEPARENT and TRACESTATE from the environment to link into any
/// // existing trace. Or start a new trace if none are present.
/// set_parent_span_from_env();
/// Ok(())
/// }
/// ```
pub async
/// EXPERIMENTAL: Like [`run_with_telemetry`], but for synchronous telemetry.
///
/// ```
/// use anyhow::Result;
/// use opinionated_telemetry::{
/// run_with_telemetry_sync, set_parent_span_from_env, AppType,
/// };
/// use tracing::instrument;
///
/// fn main() -> Result<()> {
/// run_with_telemetry_sync(
/// AppType::Cli,
/// env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"),
/// main_helper,
/// )
/// }
///
/// // Note that `instrument` will only work correctly on functions called from
/// // inside `run_with_telemetry_sync`.
/// #[instrument(
/// name = "my-app",
/// fields(version = env!("CARGO_PKG_VERSION"))
/// )]
/// fn main_helper() -> Result<()> {
/// // Use TRACEPARENT and TRACESTATE from the environment to link into any
/// // existing trace. Or start a new trace if none are present.
/// set_parent_span_from_env();
/// Ok(())
/// }
/// ```