forked from open-telemetry/opentelemetry-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
299 lines (266 loc) · 10 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
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
use crate::exporter::ModelConfig;
use http::uri;
use opentelemetry_sdk::export::{
trace::{self, SpanData},
ExportError,
};
use std::fmt::Debug;
use url::ParseError;
use self::unified_tags::UnifiedTags;
use super::Mapping;
pub mod unified_tags;
mod v03;
mod v05;
// todo: we should follow the same mapping defined in https://github.com/DataDog/datadog-agent/blob/main/pkg/trace/api/otlp.go
// https://github.com/DataDog/dd-trace-js/blob/c89a35f7d27beb4a60165409376e170eacb194c5/packages/dd-trace/src/constants.js#L4
static SAMPLING_PRIORITY_KEY: &str = "_sampling_priority_v1";
/// Custom mapping between opentelemetry spans and datadog spans.
///
/// User can provide custom function to change the mapping. It currently supports customizing the following
/// fields in Datadog span protocol.
///
/// |field name|default value|
/// |---------------|-------------|
/// |service name| service name configuration from [`ModelConfig`]|
/// |name | opentelemetry instrumentation library name |
/// |resource| opentelemetry name|
///
/// The function takes a reference to [`SpanData`]() and a reference to [`ModelConfig`]() as parameters.
/// It should return a `&str` which will be used as the value for the field.
///
/// If no custom mapping is provided. Default mapping detailed above will be used.
///
/// For example,
/// ```no_run
/// use opentelemetry_datadog::{ApiVersion, new_pipeline};
/// fn main() -> Result<(), opentelemetry::trace::TraceError> {
/// let tracer = new_pipeline()
/// .with_service_name("my_app")
/// .with_api_version(ApiVersion::Version05)
/// // the custom mapping below will change the all spans' name to datadog spans
/// .with_name_mapping(|span, model_config|{
/// "datadog spans"
/// })
/// .with_agent_endpoint("http://localhost:8126")
/// .install_batch(opentelemetry_sdk::runtime::Tokio)?;
///
/// Ok(())
/// }
/// ```
pub type FieldMappingFn = dyn for<'a> Fn(&'a SpanData, &'a ModelConfig) -> &'a str + Send + Sync;
pub(crate) type FieldMapping = std::sync::Arc<FieldMappingFn>;
// Datadog uses some magic tags in their models. There is no recommended mapping defined in
// opentelemetry spec. Below is default mapping we gonna uses. Users can override it by providing
// their own implementations.
fn default_service_name_mapping<'a>(_span: &'a SpanData, config: &'a ModelConfig) -> &'a str {
config.service_name.as_str()
}
fn default_name_mapping<'a>(span: &'a SpanData, _config: &'a ModelConfig) -> &'a str {
span.instrumentation_lib.name.as_ref()
}
fn default_resource_mapping<'a>(span: &'a SpanData, _config: &'a ModelConfig) -> &'a str {
span.name.as_ref()
}
/// Wrap type for errors from opentelemetry datadog exporter
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Message pack error
#[error("message pack error")]
MessagePackError,
/// No http client founded. 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 with following errors
#[error(transparent)]
RequestError(#[from] http::Error),
/// The Uri was invalid
#[error("invalid url {0}")]
InvalidUri(String),
/// Other errors
#[error("{0}")]
Other(String),
}
impl ExportError for Error {
fn exporter_name(&self) -> &'static str {
"datadog"
}
}
impl From<rmp::encode::ValueWriteError> for Error {
fn from(_: rmp::encode::ValueWriteError) -> Self {
Self::MessagePackError
}
}
impl From<url::ParseError> for Error {
fn from(err: ParseError) -> Self {
Self::InvalidUri(err.to_string())
}
}
impl From<uri::InvalidUri> for Error {
fn from(err: uri::InvalidUri) -> Self {
Self::InvalidUri(err.to_string())
}
}
/// Version of datadog trace ingestion API
#[derive(Debug, Copy, Clone)]
#[non_exhaustive]
pub enum ApiVersion {
/// Version 0.3
Version03,
/// Version 0.5 - requires datadog-agent v7.22.0 or above
Version05,
}
impl ApiVersion {
pub(crate) fn path(self) -> &'static str {
match self {
ApiVersion::Version03 => "/v0.3/traces",
ApiVersion::Version05 => "/v0.5/traces",
}
}
pub(crate) fn content_type(self) -> &'static str {
match self {
ApiVersion::Version03 => "application/msgpack",
ApiVersion::Version05 => "application/msgpack",
}
}
pub(crate) fn encode(
self,
model_config: &ModelConfig,
traces: Vec<Vec<trace::SpanData>>,
mapping: &Mapping,
unified_tags: &UnifiedTags,
) -> Result<Vec<u8>, Error> {
match self {
Self::Version03 => v03::encode(
model_config,
traces,
|span, config| match &mapping.service_name {
Some(f) => f(span, config),
None => default_service_name_mapping(span, config),
},
|span, config| match &mapping.name {
Some(f) => f(span, config),
None => default_name_mapping(span, config),
},
|span, config| match &mapping.resource {
Some(f) => f(span, config),
None => default_resource_mapping(span, config),
},
),
Self::Version05 => v05::encode(
model_config,
traces,
|span, config| match &mapping.service_name {
Some(f) => f(span, config),
None => default_service_name_mapping(span, config),
},
|span, config| match &mapping.name {
Some(f) => f(span, config),
None => default_name_mapping(span, config),
},
|span, config| match &mapping.resource {
Some(f) => f(span, config),
None => default_resource_mapping(span, config),
},
unified_tags,
),
}
}
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
use opentelemetry::{
trace::{SpanContext, SpanId, SpanKind, Status, TraceFlags, TraceId, TraceState},
KeyValue,
};
use opentelemetry_sdk::{
self,
trace::{EvictedQueue, SpanLinks},
InstrumentationLibrary, Resource,
};
use std::borrow::Cow;
use std::time::{Duration, SystemTime};
fn get_traces() -> Vec<Vec<trace::SpanData>> {
vec![vec![get_span(7, 1, 99)]]
}
pub(crate) fn get_span(trace_id: u128, parent_span_id: u64, span_id: u64) -> trace::SpanData {
let span_context = SpanContext::new(
TraceId::from_u128(trace_id),
SpanId::from_u64(span_id),
TraceFlags::default(),
false,
TraceState::default(),
);
let start_time = SystemTime::UNIX_EPOCH;
let end_time = start_time.checked_add(Duration::from_secs(1)).unwrap();
let capacity = 3;
let attributes = vec![KeyValue::new("span.type", "web")];
let events = EvictedQueue::new(capacity);
let links = SpanLinks::default();
let resource = Resource::new(vec![KeyValue::new("host.name", "test")]);
trace::SpanData {
span_context,
parent_span_id: SpanId::from_u64(parent_span_id),
span_kind: SpanKind::Client,
name: "resource".into(),
start_time,
end_time,
attributes,
dropped_attributes_count: 0,
events,
span_links: links,
status: Status::Ok,
resource: Cow::Owned(resource),
instrumentation_lib: InstrumentationLibrary::new(
"component",
None::<&'static str>,
None::<&'static str>,
None,
),
}
}
#[test]
fn test_encode_v03() -> Result<(), Box<dyn std::error::Error>> {
let traces = get_traces();
let model_config = ModelConfig {
service_name: "service_name".to_string(),
..Default::default()
};
let encoded = base64::encode(ApiVersion::Version03.encode(
&model_config,
traces,
&Mapping::empty(),
&UnifiedTags::new(),
)?);
assert_eq!(encoded.as_str(), "kZGMpHR5cGWjd2Vip3NlcnZpY2Wsc2VydmljZV9uYW1lpG5hbWWpY29tcG9uZW\
50qHJlc291cmNlqHJlc291cmNlqHRyYWNlX2lkzwAAAAAAAAAHp3NwYW5faWTPAAAAAAAAAGOpcGFyZW50X2lkzwAAAA\
AAAAABpXN0YXJ00wAAAAAAAAAAqGR1cmF0aW9u0wAAAAA7msoApWVycm9y0gAAAACkbWV0YYKpaG9zdC5uYW1lpHRlc3\
Spc3Bhbi50eXBlo3dlYqdtZXRyaWNzgbVfc2FtcGxpbmdfcHJpb3JpdHlfdjHLAAAAAAAAAAA=");
Ok(())
}
#[test]
fn test_encode_v05() -> Result<(), Box<dyn std::error::Error>> {
let traces = get_traces();
let model_config = ModelConfig {
service_name: "service_name".to_string(),
..Default::default()
};
let mut unified_tags = UnifiedTags::new();
unified_tags.set_env(Some(String::from("test-env")));
unified_tags.set_version(Some(String::from("test-version")));
unified_tags.set_service(Some(String::from("test-service")));
let _encoded = base64::encode(ApiVersion::Version05.encode(
&model_config,
traces,
&Mapping::empty(),
&unified_tags,
)?);
// TODO: Need someone to generate the expected result or instructions to do so.
// assert_eq!(encoded.as_str(), "kp6jd2VirHNlcnZpY2VfbmFtZaljb21wb25lbnSocmVzb3VyY2WpaG9zdC5uYW\
// 1lpHRlc3Snc2VydmljZax0ZXN0LXNlcnZpY2WjZW52qHRlc3QtZW52p3ZlcnNpb26sdGVzdC12ZXJzaW9uqXNwYW4udH\
// lwZbVfc2FtcGxpbmdfcHJpb3JpdHlfdjGRkZzOAAAAAc4AAAACzgAAAAPPAAAAAAAAAAfPAAAAAAAAAGPPAAAAAAAAAA\
// HTAAAAAAAAAADTAAAAADuaygDSAAAAAIXOAAAABM4AAAAFzgAAAAbOAAAAB84AAAAIzgAAAAnOAAAACs4AAAALzgAAAA\
// zOAAAAAIHOAAAADcsAAAAAAAAAAM4AAAAA");
Ok(())
}
}