@@ -3,68 +3,72 @@ use opentelemetry::sdk::export::trace;
3
3
use opentelemetry:: { Key , Value } ;
4
4
use std:: time:: SystemTime ;
5
5
6
- pub ( crate ) fn encode ( service_name : & str , spans : Vec < trace:: SpanData > ) -> Result < Vec < u8 > , Error > {
6
+ pub ( crate ) fn encode (
7
+ service_name : & str ,
8
+ traces : Vec < Vec < trace:: SpanData > > ,
9
+ ) -> Result < Vec < u8 > , Error > {
7
10
let mut encoded = Vec :: new ( ) ;
8
- rmp:: encode:: write_array_len ( & mut encoded, spans. len ( ) as u32 ) ?;
9
-
10
- for span in spans. into_iter ( ) {
11
- // API supports but doesn't mandate grouping spans with the same trace ID
12
- rmp:: encode:: write_array_len ( & mut encoded, 1 ) ?;
13
-
14
- // Safe until the year 2262 when Datadog will need to change their API
15
- let start = span
16
- . start_time
17
- . duration_since ( SystemTime :: UNIX_EPOCH )
18
- . unwrap ( )
19
- . as_nanos ( ) as i64 ;
20
-
21
- let duration = span
22
- . end_time
23
- . duration_since ( span. start_time )
24
- . map ( |x| x. as_nanos ( ) as i64 )
25
- . unwrap_or ( 0 ) ;
26
-
27
- if let Some ( Value :: String ( s) ) = span. attributes . get ( & Key :: new ( "span.type" ) ) {
28
- rmp:: encode:: write_map_len ( & mut encoded, 11 ) ?;
29
- rmp:: encode:: write_str ( & mut encoded, "type" ) ?;
30
- rmp:: encode:: write_str ( & mut encoded, s. as_ref ( ) ) ?;
31
- } else {
32
- rmp:: encode:: write_map_len ( & mut encoded, 10 ) ?;
33
- }
34
-
35
- // Datadog span name is OpenTelemetry component name - see module docs for more information
36
- rmp:: encode:: write_str ( & mut encoded, "service" ) ?;
37
- rmp:: encode:: write_str ( & mut encoded, service_name) ?;
38
-
39
- rmp:: encode:: write_str ( & mut encoded, "name" ) ?;
40
- rmp:: encode:: write_str ( & mut encoded, span. instrumentation_lib . name ) ?;
41
-
42
- rmp:: encode:: write_str ( & mut encoded, "resource" ) ?;
43
- rmp:: encode:: write_str ( & mut encoded, & span. name ) ?;
44
-
45
- rmp:: encode:: write_str ( & mut encoded, "trace_id" ) ?;
46
- rmp:: encode:: write_u64 ( & mut encoded, span. span_context . trace_id ( ) . to_u128 ( ) as u64 ) ?;
47
-
48
- rmp:: encode:: write_str ( & mut encoded, "span_id" ) ?;
49
- rmp:: encode:: write_u64 ( & mut encoded, span. span_context . span_id ( ) . to_u64 ( ) ) ?;
50
-
51
- rmp:: encode:: write_str ( & mut encoded, "parent_id" ) ?;
52
- rmp:: encode:: write_u64 ( & mut encoded, span. parent_span_id . to_u64 ( ) ) ?;
53
-
54
- rmp:: encode:: write_str ( & mut encoded, "start" ) ?;
55
- rmp:: encode:: write_i64 ( & mut encoded, start) ?;
56
-
57
- rmp:: encode:: write_str ( & mut encoded, "duration" ) ?;
58
- rmp:: encode:: write_i64 ( & mut encoded, duration) ?;
59
-
60
- rmp:: encode:: write_str ( & mut encoded, "error" ) ?;
61
- rmp:: encode:: write_i32 ( & mut encoded, span. status_code as i32 ) ?;
62
-
63
- rmp:: encode:: write_str ( & mut encoded, "meta" ) ?;
64
- rmp:: encode:: write_map_len ( & mut encoded, span. attributes . len ( ) as u32 ) ?;
65
- for ( key, value) in span. attributes . iter ( ) {
66
- rmp:: encode:: write_str ( & mut encoded, key. as_str ( ) ) ?;
67
- rmp:: encode:: write_str ( & mut encoded, value. as_str ( ) . as_ref ( ) ) ?;
11
+ rmp:: encode:: write_array_len ( & mut encoded, traces. len ( ) as u32 ) ?;
12
+
13
+ for trace in traces. into_iter ( ) {
14
+ rmp:: encode:: write_array_len ( & mut encoded, trace. len ( ) as u32 ) ?;
15
+
16
+ for span in trace. into_iter ( ) {
17
+ // Safe until the year 2262 when Datadog will need to change their API
18
+ let start = span
19
+ . start_time
20
+ . duration_since ( SystemTime :: UNIX_EPOCH )
21
+ . unwrap ( )
22
+ . as_nanos ( ) as i64 ;
23
+
24
+ let duration = span
25
+ . end_time
26
+ . duration_since ( span. start_time )
27
+ . map ( |x| x. as_nanos ( ) as i64 )
28
+ . unwrap_or ( 0 ) ;
29
+
30
+ if let Some ( Value :: String ( s) ) = span. attributes . get ( & Key :: new ( "span.type" ) ) {
31
+ rmp:: encode:: write_map_len ( & mut encoded, 11 ) ?;
32
+ rmp:: encode:: write_str ( & mut encoded, "type" ) ?;
33
+ rmp:: encode:: write_str ( & mut encoded, s. as_ref ( ) ) ?;
34
+ } else {
35
+ rmp:: encode:: write_map_len ( & mut encoded, 10 ) ?;
36
+ }
37
+
38
+ // Datadog span name is OpenTelemetry component name - see module docs for more information
39
+ rmp:: encode:: write_str ( & mut encoded, "service" ) ?;
40
+ rmp:: encode:: write_str ( & mut encoded, service_name) ?;
41
+
42
+ rmp:: encode:: write_str ( & mut encoded, "name" ) ?;
43
+ rmp:: encode:: write_str ( & mut encoded, span. instrumentation_lib . name ) ?;
44
+
45
+ rmp:: encode:: write_str ( & mut encoded, "resource" ) ?;
46
+ rmp:: encode:: write_str ( & mut encoded, & span. name ) ?;
47
+
48
+ rmp:: encode:: write_str ( & mut encoded, "trace_id" ) ?;
49
+ rmp:: encode:: write_u64 ( & mut encoded, span. span_context . trace_id ( ) . to_u128 ( ) as u64 ) ?;
50
+
51
+ rmp:: encode:: write_str ( & mut encoded, "span_id" ) ?;
52
+ rmp:: encode:: write_u64 ( & mut encoded, span. span_context . span_id ( ) . to_u64 ( ) ) ?;
53
+
54
+ rmp:: encode:: write_str ( & mut encoded, "parent_id" ) ?;
55
+ rmp:: encode:: write_u64 ( & mut encoded, span. parent_span_id . to_u64 ( ) ) ?;
56
+
57
+ rmp:: encode:: write_str ( & mut encoded, "start" ) ?;
58
+ rmp:: encode:: write_i64 ( & mut encoded, start) ?;
59
+
60
+ rmp:: encode:: write_str ( & mut encoded, "duration" ) ?;
61
+ rmp:: encode:: write_i64 ( & mut encoded, duration) ?;
62
+
63
+ rmp:: encode:: write_str ( & mut encoded, "error" ) ?;
64
+ rmp:: encode:: write_i32 ( & mut encoded, span. status_code as i32 ) ?;
65
+
66
+ rmp:: encode:: write_str ( & mut encoded, "meta" ) ?;
67
+ rmp:: encode:: write_map_len ( & mut encoded, span. attributes . len ( ) as u32 ) ?;
68
+ for ( key, value) in span. attributes . iter ( ) {
69
+ rmp:: encode:: write_str ( & mut encoded, key. as_str ( ) ) ?;
70
+ rmp:: encode:: write_str ( & mut encoded, value. as_str ( ) . as_ref ( ) ) ?;
71
+ }
68
72
}
69
73
}
70
74
0 commit comments