-
Notifications
You must be signed in to change notification settings - Fork 385
/
jaeger_json.rs
299 lines (265 loc) · 9.64 KB
/
jaeger_json.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
//! # Jaeger JSON file Exporter
//!
use async_trait::async_trait;
use futures::{future::BoxFuture, FutureExt};
use opentelemetry::sdk::export::trace::{ExportResult, SpanData, SpanExporter};
use opentelemetry::sdk::trace::{TraceRuntime, Tracer};
use opentelemetry::trace::TraceError;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::time::SystemTime;
/// An exporter for jaeger comptible json files containing trace data
#[derive(Debug)]
pub struct JaegerJsonExporter<R> {
out_path: PathBuf,
file_prefix: String,
service_name: String,
runtime: R,
}
impl<R: JaegerJsonRuntime> JaegerJsonExporter<R> {
/// Configure a new jaeger-json exporter
///
/// * `out_path` refers to an directory where span data are written. If it does not exist, it is created by the exporter
/// * `file_prefix` refers to a prefix prependend to each span file
/// * `service_name` is used to identify the corresponding service in jaeger
/// * `runtime` specifies the used async runtime to write the trace data
pub fn new(out_path: PathBuf, file_prefix: String, service_name: String, runtime: R) -> Self {
Self {
out_path,
file_prefix,
service_name,
runtime,
}
}
/// Install the exporter using the internal provided runtime
pub fn install_batch(self) -> Tracer {
use opentelemetry::trace::TracerProvider;
let runtime = self.runtime.clone();
let provider_builder =
opentelemetry::sdk::trace::TracerProvider::builder().with_batch_exporter(self, runtime);
let provider = provider_builder.build();
let tracer =
provider.versioned_tracer("opentelemetry", Some(env!("CARGO_PKG_VERSION")), None);
let _ = opentelemetry::global::set_tracer_provider(provider);
tracer
}
}
impl<R: JaegerJsonRuntime> SpanExporter for JaegerJsonExporter<R> {
fn export(&mut self, batch: Vec<SpanData>) -> BoxFuture<'static, ExportResult> {
let mut trace_map = HashMap::new();
for span in batch {
let ctx = &span.span_context;
trace_map
.entry(ctx.trace_id())
.or_insert_with(Vec::new)
.push(span_data_to_jaeger_json(span));
}
let data = trace_map
.into_iter()
.map(|(trace_id, spans)| {
serde_json::json!({
"traceID": trace_id.to_string(),
"spans": spans,
"processes": {
"p1": {
"serviceName": self.service_name,
"tags": []
}
}
})
})
.collect::<Vec<_>>();
let json = serde_json::json!({
"data": data,
});
let runtime = self.runtime.clone();
let out_path = self.out_path.clone();
let file_prefix = self.file_prefix.clone();
async move {
runtime.create_dir(&out_path).await?;
let file_name = out_path.join(format!(
"{}-{}.json",
file_prefix,
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.expect("This does not fail")
.as_secs()
));
runtime
.write_to_file(
&file_name,
&serde_json::to_vec(&json).expect("This is a valid json value"),
)
.await?;
Ok(())
}
.boxed()
}
}
fn span_data_to_jaeger_json(
span: opentelemetry::sdk::export::trace::SpanData,
) -> serde_json::Value {
let events = span
.events
.iter()
.map(|e| {
let mut fields = e
.attributes
.iter()
.map(|a| {
let (tpe, value) = opentelemetry_value_to_json(&a.value);
serde_json::json!({
"key": a.key.as_str(),
"type": tpe,
"value": value,
})
})
.collect::<Vec<_>>();
fields.push(serde_json::json!({
"key": "event",
"type": "string",
"value": e.name,
}));
serde_json::json!({
"timestamp": e.timestamp.duration_since(SystemTime::UNIX_EPOCH).expect("This does not fail").as_micros() as i64,
"fields": fields,
})
})
.collect::<Vec<_>>();
let tags = span
.attributes
.iter()
.map(|(key, value)| {
let (tpe, value) = opentelemetry_value_to_json(value);
serde_json::json!({
"key": key.as_str(),
"type": tpe,
"value": value,
})
})
.collect::<Vec<_>>();
let references = if span.links.is_empty() {
None
} else {
Some(
span.links
.iter()
.map(|link| {
let span_context = &link.span_context;
serde_json::json!({
"refType": "FOLLOWS_FROM",
"traceID": span_context.trace_id().to_string(),
"spanID": span_context.span_id().to_string(),
})
})
.collect::<Vec<_>>(),
)
};
serde_json::json!({
"traceID": span.span_context.trace_id().to_string(),
"spanID": span.span_context.span_id().to_string(),
"startTime": span.start_time.duration_since(SystemTime::UNIX_EPOCH).expect("This does not fail").as_micros() as i64,
"duration": span.end_time.duration_since(span.start_time).expect("This does not fail").as_micros() as i64,
"operationName": span.name,
"tags": tags,
"logs": events,
"flags": span.span_context.trace_flags().to_u8(),
"processID": "p1",
"warnings": None::<String>,
"references": references,
})
}
fn opentelemetry_value_to_json(value: &opentelemetry::Value) -> (&str, serde_json::Value) {
match value {
opentelemetry::Value::Bool(b) => ("bool", serde_json::json!(b)),
opentelemetry::Value::I64(i) => ("int64", serde_json::json!(i)),
opentelemetry::Value::F64(f) => ("float64", serde_json::json!(f)),
opentelemetry::Value::String(s) => ("string", serde_json::json!(s.as_str())),
v @ opentelemetry::Value::Array(_) => ("string", serde_json::json!(v.to_string())),
}
}
/// Jaeger Json Runtime is an extension to [`TraceRuntime`].
///
/// [`TraceRuntime`]: opentelemetry::sdk::trace::TraceRuntime
#[async_trait]
pub trait JaegerJsonRuntime: TraceRuntime + std::fmt::Debug {
/// Create a new directory if the given path does not exist yet
async fn create_dir(&self, path: &Path) -> ExportResult;
/// Write the provided content to a new file at the given path
async fn write_to_file(&self, path: &Path, content: &[u8]) -> ExportResult;
}
#[cfg(feature = "rt-tokio")]
#[async_trait]
impl JaegerJsonRuntime for opentelemetry::runtime::Tokio {
async fn create_dir(&self, path: &Path) -> ExportResult {
if tokio::fs::metadata(path).await.is_err() {
tokio::fs::create_dir_all(path)
.await
.map_err(|e| TraceError::Other(Box::new(e)))?
}
Ok(())
}
async fn write_to_file(&self, path: &Path, content: &[u8]) -> ExportResult {
use tokio::io::AsyncWriteExt;
let mut file = tokio::fs::File::create(path)
.await
.map_err(|e| TraceError::Other(Box::new(e)))?;
file.write_all(content)
.await
.map_err(|e| TraceError::Other(Box::new(e)))?;
file.sync_data()
.await
.map_err(|e| TraceError::Other(Box::new(e)))?;
Ok(())
}
}
#[cfg(feature = "rt-tokio-current-thread")]
#[async_trait]
impl JaegerJsonRuntime for opentelemetry::runtime::TokioCurrentThread {
async fn create_dir(&self, path: &Path) -> ExportResult {
if tokio::fs::metadata(path).await.is_err() {
tokio::fs::create_dir_all(path)
.await
.map_err(|e| TraceError::Other(Box::new(e)))?
}
Ok(())
}
async fn write_to_file(&self, path: &Path, content: &[u8]) -> ExportResult {
use tokio::io::AsyncWriteExt;
let mut file = tokio::fs::File::create(path)
.await
.map_err(|e| TraceError::Other(Box::new(e)))?;
file.write_all(content)
.await
.map_err(|e| TraceError::Other(Box::new(e)))?;
file.sync_data()
.await
.map_err(|e| TraceError::Other(Box::new(e)))?;
Ok(())
}
}
#[cfg(feature = "rt-async-std")]
#[async_trait]
impl JaegerJsonRuntime for opentelemetry::runtime::AsyncStd {
async fn create_dir(&self, path: &Path) -> ExportResult {
if async_std::fs::metadata(path).await.is_err() {
async_std::fs::create_dir_all(path)
.await
.map_err(|e| TraceError::Other(Box::new(e)))?;
}
Ok(())
}
async fn write_to_file(&self, path: &Path, content: &[u8]) -> ExportResult {
use async_std::io::WriteExt;
let mut file = async_std::fs::File::create(path)
.await
.map_err(|e| TraceError::Other(Box::new(e)))?;
file.write_all(content)
.await
.map_err(|e| TraceError::Other(Box::new(e)))?;
file.sync_data()
.await
.map_err(|e| TraceError::Other(Box::new(e)))?;
Ok(())
}
}