-
Notifications
You must be signed in to change notification settings - Fork 384
/
v05.rs
221 lines (203 loc) · 7.92 KB
/
v05.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
use crate::exporter::intern::StringInterner;
use crate::exporter::model::SAMPLING_PRIORITY_KEY;
use crate::exporter::{Error, ModelConfig};
use opentelemetry::sdk::export::trace;
use opentelemetry::sdk::export::trace::SpanData;
use opentelemetry::trace::Status;
use opentelemetry::{Key, Value};
use std::time::SystemTime;
use super::unified_tags::{UnifiedTagField, UnifiedTags};
const SPAN_NUM_ELEMENTS: u32 = 12;
// Protocol documentation sourced from https://github.com/DataDog/datadog-agent/blob/c076ea9a1ffbde4c76d35343dbc32aecbbf99cb9/pkg/trace/api/version.go
//
// The payload is an array containing exactly 12 elements:
//
// 1. An array of all unique strings present in the payload (a dictionary referred to by index).
// 2. An array of traces, where each trace is an array of spans. A span is encoded as an array having
// exactly 12 elements, representing all span properties, in this exact order:
//
// 0: Service (uint32)
// 1: Name (uint32)
// 2: Resource (uint32)
// 3: TraceID (uint64)
// 4: SpanID (uint64)
// 5: ParentID (uint64)
// 6: Start (int64)
// 7: Duration (int64)
// 8: Error (int32)
// 9: Meta (map[uint32]uint32)
// 10: Metrics (map[uint32]float64)
// 11: Type (uint32)
//
// Considerations:
//
// - The "uint32" typed values in "Service", "Name", "Resource", "Type", "Meta" and "Metrics" represent
// the index at which the corresponding string is found in the dictionary. If any of the values are the
// empty string, then the empty string must be added into the dictionary.
//
// - None of the elements can be nil. If any of them are unset, they should be given their "zero-value". Here
// is an example of a span with all unset values:
//
// 0: 0 // Service is "" (index 0 in dictionary)
// 1: 0 // Name is ""
// 2: 0 // Resource is ""
// 3: 0 // TraceID
// 4: 0 // SpanID
// 5: 0 // ParentID
// 6: 0 // Start
// 7: 0 // Duration
// 8: 0 // Error
// 9: map[uint32]uint32{} // Meta (empty map)
// 10: map[uint32]float64{} // Metrics (empty map)
// 11: 0 // Type is ""
//
// The dictionary in this case would be []string{""}, having only the empty string at index 0.
//
pub(crate) fn encode<S, N, R>(
model_config: &ModelConfig,
traces: Vec<Vec<trace::SpanData>>,
get_service_name: S,
get_name: N,
get_resource: R,
unified_tags: &UnifiedTags,
) -> Result<Vec<u8>, Error>
where
for<'a> S: Fn(&'a SpanData, &'a ModelConfig) -> &'a str,
for<'a> N: Fn(&'a SpanData, &'a ModelConfig) -> &'a str,
for<'a> R: Fn(&'a SpanData, &'a ModelConfig) -> &'a str,
{
let mut interner = StringInterner::new();
let mut encoded_traces = encode_traces(
&mut interner,
model_config,
get_service_name,
get_name,
get_resource,
traces,
unified_tags,
)?;
let mut payload = Vec::new();
rmp::encode::write_array_len(&mut payload, 2)?;
rmp::encode::write_array_len(&mut payload, interner.len())?;
for data in interner.iter() {
rmp::encode::write_str(&mut payload, data)?;
}
payload.append(&mut encoded_traces);
Ok(payload)
}
fn write_unified_tags(
encoded: &mut Vec<u8>,
interner: &mut StringInterner,
unified_tags: &UnifiedTags,
) -> Result<(), Error> {
write_unified_tag(encoded, interner, &unified_tags.service)?;
write_unified_tag(encoded, interner, &unified_tags.env)?;
write_unified_tag(encoded, interner, &unified_tags.version)?;
Ok(())
}
fn write_unified_tag(
encoded: &mut Vec<u8>,
interner: &mut StringInterner,
tag: &UnifiedTagField,
) -> Result<(), Error> {
if let Some(tag_value) = &tag.value {
rmp::encode::write_u32(encoded, interner.intern(tag.get_tag_name()))?;
rmp::encode::write_u32(encoded, interner.intern(tag_value.as_str().as_ref()))?;
}
Ok(())
}
fn encode_traces<S, N, R>(
interner: &mut StringInterner,
model_config: &ModelConfig,
get_service_name: S,
get_name: N,
get_resource: R,
traces: Vec<Vec<trace::SpanData>>,
unified_tags: &UnifiedTags,
) -> Result<Vec<u8>, Error>
where
for<'a> S: Fn(&'a SpanData, &'a ModelConfig) -> &'a str,
for<'a> N: Fn(&'a SpanData, &'a ModelConfig) -> &'a str,
for<'a> R: Fn(&'a SpanData, &'a ModelConfig) -> &'a str,
{
let mut encoded = Vec::new();
rmp::encode::write_array_len(&mut encoded, traces.len() as u32)?;
for trace in traces.into_iter() {
rmp::encode::write_array_len(&mut encoded, trace.len() as u32)?;
for span in trace.into_iter() {
// Safe until the year 2262 when Datadog will need to change their API
let start = span
.start_time
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_nanos() as i64;
let duration = span
.end_time
.duration_since(span.start_time)
.map(|x| x.as_nanos() as i64)
.unwrap_or(0);
let span_type = match span.attributes.get(&Key::new("span.type")) {
Some(Value::String(s)) => interner.intern(s.as_str()),
_ => interner.intern(""),
};
// Datadog span name is OpenTelemetry component name - see module docs for more information
rmp::encode::write_array_len(&mut encoded, SPAN_NUM_ELEMENTS)?;
rmp::encode::write_u32(
&mut encoded,
interner.intern(get_service_name(&span, model_config)),
)?;
rmp::encode::write_u32(&mut encoded, interner.intern(get_name(&span, model_config)))?;
rmp::encode::write_u32(
&mut encoded,
interner.intern(get_resource(&span, model_config)),
)?;
rmp::encode::write_u64(
&mut encoded,
u128::from_be_bytes(span.span_context.trace_id().to_bytes()) as u64,
)?;
rmp::encode::write_u64(
&mut encoded,
u64::from_be_bytes(span.span_context.span_id().to_bytes()),
)?;
rmp::encode::write_u64(
&mut encoded,
u64::from_be_bytes(span.parent_span_id.to_bytes()),
)?;
rmp::encode::write_i64(&mut encoded, start)?;
rmp::encode::write_i64(&mut encoded, duration)?;
rmp::encode::write_i32(
&mut encoded,
match span.status {
Status::Error { .. } => 1,
_ => 0,
},
)?;
rmp::encode::write_map_len(
&mut encoded,
(span.attributes.len() + span.resource.len()) as u32
+ unified_tags.compute_attribute_size(),
)?;
for (key, value) in span.resource.iter() {
rmp::encode::write_u32(&mut encoded, interner.intern(key.as_str()))?;
rmp::encode::write_u32(&mut encoded, interner.intern(value.as_str().as_ref()))?;
}
write_unified_tags(&mut encoded, interner, unified_tags)?;
for (key, value) in span.attributes.iter() {
rmp::encode::write_u32(&mut encoded, interner.intern(key.as_str()))?;
rmp::encode::write_u32(&mut encoded, interner.intern(value.as_str().as_ref()))?;
}
rmp::encode::write_map_len(&mut encoded, 1)?;
rmp::encode::write_u32(&mut encoded, interner.intern(SAMPLING_PRIORITY_KEY))?;
rmp::encode::write_f64(
&mut encoded,
if span.span_context.is_sampled() {
1.0
} else {
0.0
},
)?;
rmp::encode::write_u32(&mut encoded, span_type)?;
}
}
Ok(encoded)
}