forked from open-telemetry/opentelemetry-rust
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod.rs
208 lines (184 loc) · 6.68 KB
/
mod.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
mod model;
mod uploader;
use async_trait::async_trait;
use http::Uri;
use model::endpoint::Endpoint;
use opentelemetry::{
global, sdk,
sdk::export::{trace, ExportError},
sdk::trace::TraceRuntime,
trace::{TraceError, TracerProvider},
};
use opentelemetry_http::HttpClient;
use std::net::SocketAddr;
/// Default Zipkin collector endpoint
const DEFAULT_COLLECTOR_ENDPOINT: &str = "http://127.0.0.1:9411/api/v2/spans";
/// Default service name if no service is configured.
const DEFAULT_SERVICE_NAME: &str = "OpenTelemetry";
/// Zipkin span exporter
#[derive(Debug)]
pub struct Exporter {
local_endpoint: Endpoint,
uploader: uploader::Uploader,
}
impl Exporter {
fn new(local_endpoint: Endpoint, client: Box<dyn HttpClient>, collector_endpoint: Uri) -> Self {
Exporter {
local_endpoint,
uploader: uploader::Uploader::new(client, collector_endpoint),
}
}
}
/// Create a new Zipkin exporter pipeline builder.
pub fn new_pipeline() -> ZipkinPipelineBuilder {
ZipkinPipelineBuilder::default()
}
/// Builder for `ExporterConfig` struct.
#[derive(Debug)]
pub struct ZipkinPipelineBuilder {
service_name: String,
service_addr: Option<SocketAddr>,
collector_endpoint: String,
trace_config: Option<sdk::trace::Config>,
client: Option<Box<dyn HttpClient>>,
}
impl Default for ZipkinPipelineBuilder {
fn default() -> Self {
ZipkinPipelineBuilder {
#[cfg(feature = "reqwest-blocking-client")]
client: Some(Box::new(reqwest::blocking::Client::new())),
#[cfg(all(
not(feature = "reqwest-blocking-client"),
not(feature = "surf-client"),
feature = "reqwest-client"
))]
client: Some(Box::new(reqwest::Client::new())),
#[cfg(all(
not(feature = "reqwest-client"),
not(feature = "reqwest-blocking-client"),
feature = "surf-client"
))]
client: Some(Box::new(surf::Client::new())),
#[cfg(all(
not(feature = "reqwest-client"),
not(feature = "surf-client"),
not(feature = "reqwest-blocking-client")
))]
client: None,
service_name: DEFAULT_SERVICE_NAME.to_string(),
service_addr: None,
collector_endpoint: DEFAULT_COLLECTOR_ENDPOINT.to_string(),
trace_config: None,
}
}
}
impl ZipkinPipelineBuilder {
/// Initial a Zipkin span exporter.
///
/// Returns error if the endpoint is not valid or if no http client is provided.
pub fn init_exporter(self) -> Result<Exporter, TraceError> {
if let Some(client) = self.client {
let endpoint = Endpoint::new(self.service_name, self.service_addr);
let exporter = Exporter::new(
endpoint,
client,
self.collector_endpoint
.parse()
.map_err::<Error, _>(Into::into)?,
);
Ok(exporter)
} else {
Err(Error::NoHttpClient.into())
}
}
/// Install the Zipkin trace exporter pipeline with a simple span processor.
pub fn install_simple(mut self) -> Result<sdk::trace::Tracer, TraceError> {
let config = self.trace_config.take();
let exporter = self.init_exporter()?;
let mut provider_builder =
sdk::trace::TracerProvider::builder().with_simple_exporter(exporter);
if let Some(config) = config {
provider_builder = provider_builder.with_config(config);
}
let provider = provider_builder.build();
let tracer = provider.get_tracer("opentelemetry-zipkin", Some(env!("CARGO_PKG_VERSION")));
let _ = global::set_tracer_provider(provider);
Ok(tracer)
}
/// Install the Zipkin trace exporter pipeline with a batch span processor using the specified
/// runtime.
pub fn install_batch<R: TraceRuntime>(
mut self,
runtime: R,
) -> Result<sdk::trace::Tracer, TraceError> {
let config = self.trace_config.take();
let exporter = self.init_exporter()?;
let mut provider_builder =
sdk::trace::TracerProvider::builder().with_batch_exporter(exporter, runtime);
if let Some(config) = config {
provider_builder = provider_builder.with_config(config);
}
let provider = provider_builder.build();
let tracer = provider.get_tracer("opentelemetry-zipkin", Some(env!("CARGO_PKG_VERSION")));
let _ = global::set_tracer_provider(provider);
Ok(tracer)
}
/// Assign the service name under which to group traces.
pub fn with_service_name<T: Into<String>>(mut self, name: T) -> Self {
self.service_name = name.into();
self
}
/// Assign client implementation
pub fn with_http_client<T: HttpClient + 'static>(mut self, client: T) -> Self {
self.client = Some(Box::new(client));
self
}
/// Assign the service name under which to group traces.
pub fn with_service_address(mut self, addr: SocketAddr) -> Self {
self.service_addr = Some(addr);
self
}
/// Assign the Zipkin collector endpoint
pub fn with_collector_endpoint<T: Into<String>>(mut self, endpoint: T) -> Self {
self.collector_endpoint = endpoint.into();
self
}
/// Assign the SDK trace configuration.
pub fn with_trace_config(mut self, config: sdk::trace::Config) -> Self {
self.trace_config = Some(config);
self
}
}
#[async_trait]
impl trace::SpanExporter for Exporter {
/// Export spans to Zipkin collector.
async fn export(&mut self, batch: Vec<trace::SpanData>) -> trace::ExportResult {
let zipkin_spans = batch
.into_iter()
.map(|span| model::into_zipkin_span(self.local_endpoint.clone(), span))
.collect();
self.uploader.upload(zipkin_spans).await
}
}
/// Wrap type for errors from opentelemetry zipkin
#[derive(thiserror::Error, Debug)]
#[non_exhaustive]
pub enum Error {
/// No http client implementation found. User should provide one or enable features.
#[error("http client must be set, users can enable reqwest or surf feature to use http client implementation within create")]
NoHttpClient,
/// Http requests failed
#[error("http request failed with {0}")]
RequestFailed(#[from] http::Error),
/// The uri provided is invalid
#[error("invalid uri")]
InvalidUri(#[from] http::uri::InvalidUri),
/// Other errors
#[error("export error: {0}")]
Other(String),
}
impl ExportError for Error {
fn exporter_name(&self) -> &'static str {
"zipkin"
}
}