|
| 1 | +use crate::tracing::{Trace, Traces}; |
| 2 | +use super::Exporter; |
| 3 | +use async_trait::async_trait; |
| 4 | +use rmp::encode; |
| 5 | +use rmp::encode::ByteBuf; |
| 6 | +use hashbrown::HashMap; |
| 7 | + |
| 8 | +pub struct AgentExporter {} |
| 9 | + |
| 10 | +#[async_trait] |
| 11 | +impl Exporter for AgentExporter { |
| 12 | + async fn export(&self, traces: Traces) { |
| 13 | + let mut wr = ByteBuf::new(); |
| 14 | + let trace_count = traces.len(); |
| 15 | + |
| 16 | + if trace_count > 0 { |
| 17 | + // println!("{:#?}", traces); |
| 18 | + |
| 19 | + self.encode_traces(&mut wr, traces); |
| 20 | + |
| 21 | + let client = hyper::Client::new(); |
| 22 | + let data: Vec<u8> = wr.as_vec().to_vec(); |
| 23 | + let req = hyper::Request::builder() |
| 24 | + .method(hyper::Method::PUT) |
| 25 | + .uri("http://localhost:8126/v0.4/traces") |
| 26 | + .header("Content-Type", "application/msgpack") |
| 27 | + .header("X-Datadog-Trace-Count", trace_count.to_string()) |
| 28 | + // .header("Datadog-Meta-Tracer-Version", "") |
| 29 | + // .header("Datadog-Meta-Lang", "") |
| 30 | + // .header("Datadog-Meta-Lang-Version", "") |
| 31 | + // .header("Datadog-Meta-Lang-Interpreter", "") |
| 32 | + .body(hyper::Body::from(data)) |
| 33 | + .unwrap(); |
| 34 | + |
| 35 | + client.request(req).await.unwrap(); |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +impl Default for AgentExporter { |
| 41 | + fn default() -> Self { |
| 42 | + Self::new() |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl AgentExporter { |
| 47 | + pub fn new() -> Self { |
| 48 | + Self {} |
| 49 | + } |
| 50 | + |
| 51 | + fn encode_traces(&self, wr: &mut ByteBuf, traces: Traces) { |
| 52 | + encode::write_array_len(wr, traces.len() as u32).unwrap(); |
| 53 | + |
| 54 | + for trace in traces.values() { |
| 55 | + self.encode_trace(wr, trace); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + fn encode_trace(&self, wr: &mut ByteBuf, trace: &Trace) { |
| 60 | + encode::write_array_len(wr, trace.spans.len() as u32).unwrap(); |
| 61 | + |
| 62 | + for span in trace.spans.values() { |
| 63 | + match &span.span_type { |
| 64 | + Some(span_type) => { |
| 65 | + encode::write_map_len(wr, 12).unwrap(); |
| 66 | + encode::write_str(wr, "type").unwrap(); |
| 67 | + encode::write_str(wr, span_type.as_str()).unwrap(); |
| 68 | + }, |
| 69 | + None => { |
| 70 | + encode::write_map_len(wr, 11).unwrap(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + encode::write_str(wr, "trace_id").unwrap(); |
| 75 | + encode::write_uint(wr, span.trace_id).unwrap(); |
| 76 | + encode::write_str(wr, "span_id").unwrap(); |
| 77 | + encode::write_uint(wr, span.span_id).unwrap(); |
| 78 | + encode::write_str(wr, "parent_id").unwrap(); |
| 79 | + encode::write_uint(wr, span.parent_id).unwrap(); |
| 80 | + encode::write_str(wr, "name").unwrap(); |
| 81 | + encode::write_str(wr, span.name.as_str()).unwrap(); |
| 82 | + encode::write_str(wr, "resource").unwrap(); |
| 83 | + encode::write_str(wr, span.resource.as_str()).unwrap(); |
| 84 | + encode::write_str(wr, "service").unwrap(); |
| 85 | + encode::write_str(wr, span.service.as_str()).unwrap(); |
| 86 | + encode::write_str(wr, "error").unwrap(); |
| 87 | + encode::write_uint(wr, span.error).unwrap(); |
| 88 | + encode::write_str(wr, "start").unwrap(); |
| 89 | + encode::write_uint(wr, span.start).unwrap(); |
| 90 | + encode::write_str(wr, "duration").unwrap(); |
| 91 | + encode::write_uint(wr, span.duration + 1).unwrap(); |
| 92 | + |
| 93 | + self.encode_meta(wr, &span.meta); |
| 94 | + self.encode_metrics(wr, &span.metrics); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + fn encode_meta(&self, wr: &mut ByteBuf, map: &HashMap<String, String>) { |
| 99 | + encode::write_str(wr, "meta").unwrap(); |
| 100 | + encode::write_map_len(wr, map.len() as u32).unwrap(); |
| 101 | + |
| 102 | + for (k, v) in map { |
| 103 | + encode::write_str(wr, k.as_str()).unwrap(); |
| 104 | + encode::write_str(wr, v.as_str()).unwrap(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + fn encode_metrics(&self, wr: &mut ByteBuf, map: &HashMap<String, f64>) { |
| 109 | + encode::write_str(wr, "metrics").unwrap(); |
| 110 | + encode::write_map_len(wr, map.len() as u32).unwrap(); |
| 111 | + |
| 112 | + for (k, v) in map { |
| 113 | + encode::write_str(wr, k.as_str()).unwrap(); |
| 114 | + encode::write_f64(wr, *v).unwrap(); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments