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
//! A configurator module for telemetry.
//!
//! # Usage
//!
//! The configurator creates a pipeline that by default registers
//! a noop implementation for events emitted by [tracing] crate.
//! The utility functions on the resultant pipeline add
//! logs, metrics and trace subscribers up until the `init` method is called.
//!
//! ## Setup telemetry collection
//!
//! ### Minimal setup for an AWS environment
//! ```no_run
//! use metry::telemetry;
//!
//! telemetry::new()
//! .with_stdout_logs()
//! .with_aws_metrics()
//! .with_aws_traces()
//! .init()
//! .await;
//! ```
//!
//! ### Setup with default settings and all data written to stdout
//! ```no_run
//! use metry::{log, metrics, telemetry, trace, Level};
//!
//! telemetry::new()
//! .with_level(Level::DEBUG)
//! .with_logs(log::LayerBuilder::Stdout(logs::LogOptions::default()))
//! .with_metrics(metrics::LayerBuilder::Stdout(metrics::MetricsOptions::default()))
//! .with_traces(trace::LayerBuilder::Stdout(trace::TraceOptions::default())
//! .init()
//! .await;
//! ```
//!
//! ### Provide custom configuration for each provider
//! ```no_run
//! use std::time::Duration;
//! use metry::{
//! telemetry,
//! log,
//! metrics::{self, CloudWatchMetricsOptionsConfigurator, MetricsOptionsConfigurator},
//! trace,
//! Level
//! };
//! use opentelemetry::KeyValue
//! use opentelemetry_aws::trace::XrayIdGenerator;
//! use opentelemetry_sdk::Resource;
//! use opentelemetry_semantic_conventions::resource::SERVICE_NAME;
//! use tracing_core::LevelFilter;
//!
//! let resource = Resource::new(vec![KeyValue::new(SERVICE_NAME, "telemetry-example-rust")]);
//!
//! telemetry::new()
//! .with_level(Level::DEBUG)
//! .with_logs(log::LayerBuilder::Stdout(
//! logs::LogOptions::with_log_config(opentelemetry_sdk::logs::config().with_resource(resource)),
//! ))
//! .with_metrics(metrics::LayerBuilder::CloudWatch(
//! metrics::CloudWatchMetricsOptions::default()
//! .with_namespace("rust-metrics".to_string())
//! .with_exporter_interval(Duration::from_secs(10))
//! .with_view(Box::new(|instrument: &Instrument| -> Option<Stream> {
//! let start = "s3";
//! if instrument.name.starts_with(start) {
//! let new_name = instrument.name.replace(start, "s3-bucket-listing");
//! Some(
//! Stream::new()
//! .name(new_name)
//! .allowed_attribute_keys([Key::from("allowed_key")]),
//! )
//! } else {
//! None
//! }
//! })),
//! ))
//! .with_traces(trace::LayerBuilder::Xray(
//! trace::TraceOptions::with_trace_config(
//! opentelemetry_sdk::trace::config()
//! .with_resource(resource)
//! .with_id_generator(XrayIdGenerator::default()),
//! ),
//! ))
//! .init()
//! .await;
//! ```
//!
//! ## Collect data in your code
//! To publish a log emit any [tracing] event.
//!
//! To publish a new metric follow instructions in [tracing_opentelemetry::MetricsLayer].
//!
//! To record trace create [tracing] spans and either bring them in scope manually or attach to futures.
//!
//! ### Examples:
//! ```no_run
//! use tracing::{info, info_span};
//!
//! // Emit a log event with level info
//! info!("Started execution");
//!
//! // Start a synchronous trace span
//! let span = info_span!("my sync span");
//! span.in_scope(|| {
//! info!("This event is recorder within the trace span");
//!
//! // Do work inside the span...
//!
//! // Collect metrics: count on foo
//! info!(monotonic_counter.foo = 1);
//! });
//!
//! // Run async code in a future with a span attached
//! let future_result: Vec<u64> = some_future
//! .instrument(tracing::info_span!("my async span"))
//! .await;
//!
//! // Collect metrics: put data into the bar histogram
//! info!(histogram.bar = future_result);
//! info!("Execution complete");
//! ```
//!
pub use Level;
pub use ;