|
1 | 1 | use crate::client::Client;
|
2 |
| -use crate::tracing::{Trace, Traces}; |
| 2 | +use crate::tracing::{Trace, Traces, Span, Meta, Metrics}; |
3 | 3 | use super::Exporter;
|
| 4 | +use hashbrown::HashMap; |
4 | 5 | use rmp::encode;
|
5 | 6 | use rmp::encode::ByteBuf;
|
6 |
| -use hashbrown::HashMap; |
| 7 | +use std::rc::Rc; |
7 | 8 |
|
8 | 9 | pub struct AgentExporter {
|
9 | 10 | client: Box<dyn Client + Send + Sync>
|
@@ -35,69 +36,104 @@ impl AgentExporter {
|
35 | 36 | }
|
36 | 37 | }
|
37 | 38 |
|
| 39 | + fn cache_strings(&self, strings: &mut Vec<Rc<str>>, positions: &mut HashMap<Rc<str>, u32>, trace: &Trace) { |
| 40 | + for span in trace.spans.values() { |
| 41 | + self.cache_string(strings, positions, &span.service); |
| 42 | + self.cache_string(strings, positions, &span.name); |
| 43 | + self.cache_string(strings, positions, &span.resource); |
| 44 | + self.cache_string(strings, positions, &span.span_type); |
| 45 | + |
| 46 | + for (k, v) in &span.meta { |
| 47 | + self.cache_string(strings, positions, &k); |
| 48 | + self.cache_string(strings, positions, &v); |
| 49 | + } |
| 50 | + |
| 51 | + for (k, _) in &span.metrics { |
| 52 | + self.cache_string(strings, positions, &k); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + fn cache_string(&self, strings: &mut Vec<Rc<str>>, positions: &mut HashMap<Rc<str>, u32>, s: &Rc<str>) { |
| 58 | + if !positions.contains_key(s) { |
| 59 | + let len = strings.len() as u32; |
| 60 | + |
| 61 | + positions.insert(s.clone(), len); |
| 62 | + strings.push(s.clone()); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + fn encode_strings(&self, wr: &mut ByteBuf, strings: &mut Vec<Rc<str>>) { |
| 67 | + encode::write_array_len(wr, strings.len() as u32).unwrap(); |
| 68 | + |
| 69 | + for s in strings { |
| 70 | + encode::write_str(wr, s).unwrap(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
38 | 74 | fn encode_traces(&self, wr: &mut ByteBuf, traces: Traces) {
|
| 75 | + encode::write_array_len(wr, 2).unwrap(); |
| 76 | + |
| 77 | + let empty_string: Rc<str> = Rc::from(""); |
| 78 | + let mut strings = Vec::new(); |
| 79 | + let mut positions = HashMap::new(); |
| 80 | + |
| 81 | + strings.push(empty_string.clone()); |
| 82 | + positions.insert(empty_string.clone(), 0u32); |
| 83 | + |
| 84 | + // TODO: Avoid looping twice over traces/strings. |
| 85 | + for trace in traces.values() { |
| 86 | + self.cache_strings(&mut strings, &mut positions, trace); |
| 87 | + } |
| 88 | + |
| 89 | + self.encode_strings(wr, &mut strings); |
| 90 | + |
39 | 91 | encode::write_array_len(wr, traces.len() as u32).unwrap();
|
40 | 92 |
|
41 | 93 | for trace in traces.values() {
|
42 |
| - self.encode_trace(wr, trace); |
| 94 | + self.encode_trace(wr, trace, &positions); |
43 | 95 | }
|
44 | 96 | }
|
45 | 97 |
|
46 |
| - fn encode_trace(&self, wr: &mut ByteBuf, trace: &Trace) { |
| 98 | + fn encode_trace(&self, wr: &mut ByteBuf, trace: &Trace, positions: &HashMap<Rc<str>, u32>) { |
47 | 99 | encode::write_array_len(wr, trace.spans.len() as u32).unwrap();
|
48 | 100 |
|
49 | 101 | for span in trace.spans.values() {
|
50 |
| - match &span.span_type { |
51 |
| - Some(span_type) => { |
52 |
| - encode::write_map_len(wr, 12).unwrap(); |
53 |
| - encode::write_str(wr, "type").unwrap(); |
54 |
| - encode::write_str(wr, span_type.as_str()).unwrap(); |
55 |
| - }, |
56 |
| - None => { |
57 |
| - encode::write_map_len(wr, 11).unwrap(); |
58 |
| - } |
59 |
| - } |
60 |
| - |
61 |
| - encode::write_str(wr, "trace_id").unwrap(); |
62 |
| - encode::write_uint(wr, span.trace_id).unwrap(); |
63 |
| - encode::write_str(wr, "span_id").unwrap(); |
64 |
| - encode::write_uint(wr, span.span_id).unwrap(); |
65 |
| - encode::write_str(wr, "parent_id").unwrap(); |
66 |
| - encode::write_uint(wr, span.parent_id).unwrap(); |
67 |
| - encode::write_str(wr, "name").unwrap(); |
68 |
| - encode::write_str(wr, span.name.as_str()).unwrap(); |
69 |
| - encode::write_str(wr, "resource").unwrap(); |
70 |
| - encode::write_str(wr, span.resource.as_str()).unwrap(); |
71 |
| - encode::write_str(wr, "service").unwrap(); |
72 |
| - encode::write_str(wr, span.service.as_str()).unwrap(); |
73 |
| - encode::write_str(wr, "error").unwrap(); |
74 |
| - encode::write_uint(wr, span.error).unwrap(); |
75 |
| - encode::write_str(wr, "start").unwrap(); |
76 |
| - encode::write_uint(wr, span.start).unwrap(); |
77 |
| - encode::write_str(wr, "duration").unwrap(); |
78 |
| - encode::write_uint(wr, span.duration + 1).unwrap(); |
79 |
| - |
80 |
| - self.encode_meta(wr, &span.meta); |
81 |
| - self.encode_metrics(wr, &span.metrics); |
| 102 | + self.encode_span(wr, span, positions); |
82 | 103 | }
|
83 | 104 | }
|
84 | 105 |
|
85 |
| - fn encode_meta(&self, wr: &mut ByteBuf, map: &HashMap<String, String>) { |
86 |
| - encode::write_str(wr, "meta").unwrap(); |
87 |
| - encode::write_map_len(wr, map.len() as u32).unwrap(); |
| 106 | + fn encode_span(&self, wr: &mut ByteBuf, span: &Span, positions: &HashMap<Rc<str>, u32>) { |
| 107 | + encode::write_array_len(wr, 12).unwrap(); |
| 108 | + |
| 109 | + encode::write_uint(wr, positions[&span.service] as u64).unwrap(); |
| 110 | + encode::write_uint(wr, positions[&span.name] as u64).unwrap(); |
| 111 | + encode::write_uint(wr, positions[&span.resource] as u64).unwrap(); |
| 112 | + encode::write_uint(wr, span.trace_id).unwrap(); |
| 113 | + encode::write_uint(wr, span.span_id).unwrap(); |
| 114 | + encode::write_uint(wr, span.parent_id).unwrap(); |
| 115 | + encode::write_uint(wr, span.start).unwrap(); |
| 116 | + encode::write_uint(wr, span.duration + 1).unwrap(); |
| 117 | + encode::write_uint(wr, span.error).unwrap(); |
| 118 | + self.encode_meta(wr, &span.meta, positions); |
| 119 | + self.encode_metrics(wr, &span.metrics, positions); |
| 120 | + encode::write_uint(wr, positions[&span.span_type] as u64).unwrap(); |
| 121 | + } |
| 122 | + |
| 123 | + fn encode_meta(&self, wr: &mut ByteBuf, meta: &Meta, positions: &HashMap<Rc<str>, u32>) { |
| 124 | + encode::write_map_len(wr, meta.len() as u32).unwrap(); |
88 | 125 |
|
89 |
| - for (k, v) in map { |
90 |
| - encode::write_str(wr, k.as_str()).unwrap(); |
91 |
| - encode::write_str(wr, v.as_str()).unwrap(); |
| 126 | + for (k, v) in meta { |
| 127 | + encode::write_uint(wr, positions[k] as u64).unwrap(); |
| 128 | + encode::write_uint(wr, positions[v] as u64).unwrap(); |
92 | 129 | }
|
93 | 130 | }
|
94 | 131 |
|
95 |
| - fn encode_metrics(&self, wr: &mut ByteBuf, map: &HashMap<String, f64>) { |
96 |
| - encode::write_str(wr, "metrics").unwrap(); |
97 |
| - encode::write_map_len(wr, map.len() as u32).unwrap(); |
| 132 | + fn encode_metrics(&self, wr: &mut ByteBuf, metrics: &Metrics, positions: &HashMap<Rc<str>, u32>) { |
| 133 | + encode::write_map_len(wr, metrics.len() as u32).unwrap(); |
98 | 134 |
|
99 |
| - for (k, v) in map { |
100 |
| - encode::write_str(wr, k.as_str()).unwrap(); |
| 135 | + for (k, v) in metrics { |
| 136 | + encode::write_uint(wr, positions[k] as u64).unwrap(); |
101 | 137 | encode::write_f64(wr, *v).unwrap();
|
102 | 138 | }
|
103 | 139 | }
|
|
0 commit comments