-
Notifications
You must be signed in to change notification settings - Fork 385
/
lib.rs
238 lines (221 loc) · 7.89 KB
/
lib.rs
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
//! The OTLP Exporter supports exporting trace and metric data in the OTLP
//! format to the OpenTelemetry collector. The OpenTelemetry Collector offers a
//! vendor-agnostic implementation on how to receive, process, and export
//! telemetry data. In addition, it removes the need to run, operate, and
//! maintain multiple agents/collectors in order to support open-source
//! telemetry data formats (e.g. Jaeger, Prometheus, etc.) sending to multiple
//! open-source or commercial back-ends.
//!
//! ## Quickstart
//!
//! First make sure you have a running version of the opentelemetry collector
//! you want to send data to:
//!
//! ```shell
//! $ docker run -p 55680:55680 otel/opentelemetry-collector-dev:latest
//! ```
//!
//! Then install a new pipeline with the recommended defaults to start exporting
//! telemetry:
//!
//! ```no_run
//! use opentelemetry::trace::Tracer;
//!
//! fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
//! let (tracer, _uninstall) = opentelemetry_otlp::new_pipeline().install()?;
//!
//! tracer.in_span("doing_work", |cx| {
//! // Traced app logic here...
//! });
//!
//! Ok(())
//! }
//! ```
//!
//! ## Performance
//!
//! For optimal performance, a batch exporter is recommended as the simple
//! exporter will export each span synchronously on drop. You can enable the
//! [`tokio`] or [`async-std`] features to have a batch exporter configured for
//! you automatically for either executor when you install the pipeline.
//!
//! ```toml
//! [dependencies]
//! opentelemetry = { version = "*", features = ["tokio"] }
//! opentelemetry-otlp = "*"
//! ```
//!
//! [`tokio`]: https://tokio.rs
//! [`async-std`]: https://async.rs
//!
//! ## Kitchen Sink Full Configuration
//!
//! Example showing how to override all configuration options. See the
//! [`OtlpPipelineBuilder`] docs for details of each option.
//!
//! [`OtlpPipelineBuilder`]: struct.OtlpPipelineBuilder.html
//!
//! ```no_run
//! use opentelemetry::{KeyValue, trace::Tracer};
//! use opentelemetry::sdk::{trace::{self, IdGenerator, Sampler}, Resource};
//! use opentelemetry_otlp::{Compression, Credentials, Protocol};
//! use std::time::Duration;
//!
//! fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
//! let headers = vec![("X-Custom".to_string(), "Custom-Value".to_string())]
//! .into_iter()
//! .collect();
//!
//! let (tracer, _uninstall) = opentelemetry_otlp::new_pipeline()
//! .with_endpoint("localhost:55680")
//! .with_protocol(Protocol::Grpc)
//! .with_headers(headers)
//! .with_compression(Compression::Gzip)
//! .with_timeout(Duration::from_secs(3))
//! .with_completion_queue_count(2)
//! .with_credentials(Credentials {
//! cert: "tls.cert".to_string(),
//! key: "tls.key".to_string(),
//! })
//! .with_trace_config(
//! trace::config()
//! .with_default_sampler(Sampler::AlwaysOn)
//! .with_id_generator(IdGenerator::default())
//! .with_max_events_per_span(64)
//! .with_max_attributes_per_span(16)
//! .with_max_events_per_span(16)
//! .with_resource(Resource::new(vec![KeyValue::new("key", "value")])),
//! )
//! .install()?;
//!
//! tracer.in_span("doing_work", |cx| {
//! // Traced app logic here...
//! });
//!
//! Ok(())
//! }
//! ```
//! ## Feature flags
//!
//! By default `opentelemetry-otlp` uses `boringssl` for grpc crypto. You can switch
//! this to use `openssl` by enabling the `openssl` feature:
//!
//! ```toml
//! [dependencies]
//! opentelemetry-otlp = { version = "*", features = ["openssl"] }
//! ```
//!
//! If you would like to use a vendored `openssl` version, use the `openssl-vendored` feature.
//! For more info, see https://github.com/tikv/grpc-rs#feature-openssl-and-openssl-vendored.
#![warn(
future_incompatible,
missing_debug_implementations,
missing_docs,
nonstandard_style,
rust_2018_idioms,
unreachable_pub,
unused
)]
#![allow(elided_lifetimes_in_paths)]
#![cfg_attr(docsrs, feature(doc_cfg), deny(broken_intra_doc_links))]
#![cfg_attr(test, deny(warnings))]
use opentelemetry::{global, sdk, trace::TracerProvider};
use std::collections::HashMap;
use std::error::Error;
use std::time::Duration;
#[allow(clippy::all, unreachable_pub, dead_code)]
#[rustfmt::skip]
mod proto;
mod span;
mod transform;
pub use crate::span::{Compression, Credentials, Exporter, ExporterConfig, Protocol};
/// Create a new pipeline builder with the recommended configuration.
///
/// ## Examples
///
/// ```no_run
/// fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// let (tracer, _uninstall) = opentelemetry_otlp::new_pipeline().install()?;
///
/// Ok(())
/// }
/// ```
pub fn new_pipeline() -> OtlpPipelineBuilder {
OtlpPipelineBuilder::default()
}
/// Recommended configuration for an Otlp exporter pipeline.
///
/// ## Examples
///
/// ```no_run
/// fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// let (tracer, _uninstall) = opentelemetry_otlp::new_pipeline().install()?;
///
/// Ok(())
/// }
/// ```
#[derive(Default, Debug)]
pub struct OtlpPipelineBuilder {
exporter_config: ExporterConfig,
trace_config: Option<sdk::trace::Config>,
}
impl OtlpPipelineBuilder {
/// Set the address of the OTLP collector. If not set, the default address is used.
pub fn with_endpoint<T: Into<String>>(mut self, endpoint: T) -> Self {
self.exporter_config.endpoint = endpoint.into();
self
}
/// Set the protocol to use when communicating with the collector.
pub fn with_protocol(mut self, protocol: Protocol) -> Self {
self.exporter_config.protocol = protocol;
self
}
/// Set the credentials to use when communicating with the collector.
pub fn with_credentials(mut self, credentials: Credentials) -> Self {
self.exporter_config.credentials = Some(credentials);
self
}
/// Set Additional headers to send to the collector.
pub fn with_headers(mut self, headers: HashMap<String, String>) -> Self {
self.exporter_config.headers = Some(headers);
self
}
/// Set the compression algorithm to use when communicating with the collector.
pub fn with_compression(mut self, compression: Compression) -> Self {
self.exporter_config.compression = Some(compression);
self
}
/// Set the timeout to the collector.
pub fn with_timeout(mut self, timeout: Duration) -> Self {
self.exporter_config.timeout = timeout;
self
}
/// Set the number of GRPC worker threads to poll queues.
pub fn with_completion_queue_count(mut self, count: usize) -> Self {
self.exporter_config.completion_queue_count = count;
self
}
/// Set the trace provider configuration.
pub fn with_trace_config(mut self, trace_config: sdk::trace::Config) -> Self {
self.trace_config = Some(trace_config);
self
}
/// Install the OTLP exporter pipeline with the recommended defaults.
pub fn install(
mut self,
) -> Result<(sdk::trace::Tracer, Uninstall), Box<dyn Error + Send + Sync + 'static>> {
let exporter = Exporter::new(self.exporter_config);
let mut provider_builder = sdk::trace::TracerProvider::builder().with_exporter(exporter);
if let Some(config) = self.trace_config.take() {
provider_builder = provider_builder.with_config(config);
}
let provider = provider_builder.build();
let tracer = provider.get_tracer("opentelemetry-otlp", Some(env!("CARGO_PKG_VERSION")));
let provider_guard = global::set_tracer_provider(provider);
Ok((tracer, Uninstall(provider_guard)))
}
}
/// Uninstalls the OTLP pipeline on drop
#[must_use]
#[derive(Debug)]
pub struct Uninstall(global::TracerProviderGuard);