1
1
use chrono:: SecondsFormat ;
2
- use std:: ops:: SubAssign ;
2
+ use std:: { ops:: SubAssign , str :: FromStr } ;
3
3
4
4
use std:: collections:: HashMap ;
5
5
use vector_lib:: {
@@ -13,9 +13,11 @@ use super::{
13
13
traces:: model:: OpentelemetryTracesModel ,
14
14
} ;
15
15
use opentelemetry:: {
16
- logs:: AnyValue as OtlpAnyValue , Array as OtlpArray , InstrumentationLibrary , Key , KeyValue ,
17
- StringValue , Value as OtlpValue ,
16
+ logs:: AnyValue as OtlpAnyValue ,
17
+ trace:: { SpanId , TraceFlags , TraceId , TraceState } ,
18
+ Array as OtlpArray , InstrumentationLibrary , Key , KeyValue , StringValue , Value as OtlpValue ,
18
19
} ;
20
+
19
21
use opentelemetry_sdk:: Resource ;
20
22
use std:: {
21
23
borrow:: Cow ,
@@ -119,10 +121,119 @@ pub fn value_to_system_time(value: &Value) -> SystemTime {
119
121
120
122
#[ derive( Debug ) ]
121
123
pub struct OpentelemetryResource {
122
- pub attributes : Vec < KeyValue > ,
124
+ pub attributes : OpentelemetryAttributes ,
123
125
pub schema_url : Cow < ' static , str > ,
124
126
}
125
127
128
+ pub struct OpentelemetryTraceId ( TraceId ) ;
129
+
130
+ impl From < Option < & Value > > for OpentelemetryTraceId {
131
+ fn from ( bytes : Option < & Value > ) -> Self {
132
+ match bytes {
133
+ Some ( Value :: Bytes ( bytes) ) => {
134
+ let mut trace_id = [ 0 ; 16 ] ;
135
+ match faster_hex:: hex_decode ( bytes, & mut trace_id) {
136
+ Ok ( _) => Self ( TraceId :: from_bytes ( trace_id) ) ,
137
+ Err ( _) => Self ( TraceId :: INVALID ) ,
138
+ }
139
+ }
140
+ _ => Self ( TraceId :: INVALID ) ,
141
+ }
142
+ }
143
+ }
144
+
145
+ impl From < OpentelemetryTraceId > for TraceId {
146
+ fn from ( trace_id : OpentelemetryTraceId ) -> TraceId {
147
+ trace_id. 0
148
+ }
149
+ }
150
+
151
+ pub struct OpentelemetrySpanId ( SpanId ) ;
152
+
153
+ impl From < Option < & Value > > for OpentelemetrySpanId {
154
+ fn from ( bytes : Option < & Value > ) -> Self {
155
+ match bytes {
156
+ Some ( Value :: Bytes ( bytes) ) => {
157
+ let mut span_id = [ 0 ; 8 ] ;
158
+ match faster_hex:: hex_decode ( bytes, & mut span_id) {
159
+ Ok ( _) => Self ( SpanId :: from_bytes ( span_id) ) ,
160
+ Err ( _) => Self ( SpanId :: INVALID ) ,
161
+ }
162
+ }
163
+ _ => Self ( SpanId :: INVALID ) ,
164
+ }
165
+ }
166
+ }
167
+
168
+ impl From < OpentelemetrySpanId > for SpanId {
169
+ fn from ( span_id : OpentelemetrySpanId ) -> Self {
170
+ span_id. 0
171
+ }
172
+ }
173
+
174
+ pub struct OpentelemetryTraceState ( TraceState ) ;
175
+
176
+ impl From < Option < & Value > > for OpentelemetryTraceState {
177
+ fn from ( bytes : Option < & Value > ) -> Self {
178
+ match bytes {
179
+ Some ( Value :: Bytes ( bytes) ) => {
180
+ let str = String :: from_utf8_lossy ( bytes) ;
181
+ Self ( TraceState :: from_str ( & str) . unwrap_or_default ( ) )
182
+ }
183
+ _ => Self ( TraceState :: NONE ) ,
184
+ }
185
+ }
186
+ }
187
+
188
+ impl From < OpentelemetryTraceState > for TraceState {
189
+ fn from ( state : OpentelemetryTraceState ) -> Self {
190
+ state. 0
191
+ }
192
+ }
193
+
194
+ pub struct OpentelemetryTraceFlags ( TraceFlags ) ;
195
+
196
+ impl From < Option < & Value > > for OpentelemetryTraceFlags {
197
+ fn from ( bytes : Option < & Value > ) -> Self {
198
+ match bytes {
199
+ Some ( Value :: Integer ( flag) ) => Self ( TraceFlags :: new (
200
+ u8:: try_from ( * flag) . unwrap_or ( TraceFlags :: NOT_SAMPLED . to_u8 ( ) ) ,
201
+ ) ) ,
202
+ _ => Self ( TraceFlags :: NOT_SAMPLED ) ,
203
+ }
204
+ }
205
+ }
206
+
207
+ impl From < OpentelemetryTraceFlags > for TraceFlags {
208
+ fn from ( flags : OpentelemetryTraceFlags ) -> Self {
209
+ flags. 0
210
+ }
211
+ }
212
+
213
+ #[ derive( Default , Debug ) ]
214
+ pub struct OpentelemetryAttributes ( Vec < KeyValue > ) ;
215
+
216
+ impl From < Option < & Value > > for OpentelemetryAttributes {
217
+ fn from ( value : Option < & Value > ) -> Self {
218
+ match value {
219
+ Some ( Value :: Object ( obj) ) => Self (
220
+ obj. iter ( )
221
+ . map ( |( key, value) | {
222
+ KeyValue :: new ( key. to_string ( ) , value_to_otlp_value ( value. clone ( ) ) )
223
+ } )
224
+ . collect ( ) ,
225
+ ) ,
226
+ _ => Self ( vec ! [ ] ) ,
227
+ }
228
+ }
229
+ }
230
+
231
+ impl From < OpentelemetryAttributes > for Vec < KeyValue > {
232
+ fn from ( attrs : OpentelemetryAttributes ) -> Self {
233
+ attrs. 0
234
+ }
235
+ }
236
+
126
237
impl From < & LogEvent > for OpentelemetryResource {
127
238
fn from ( log : & LogEvent ) -> Self {
128
239
let mut attributes = vec ! [ ] ;
@@ -143,15 +254,15 @@ impl From<&LogEvent> for OpentelemetryResource {
143
254
}
144
255
145
256
OpentelemetryResource {
146
- attributes,
257
+ attributes : OpentelemetryAttributes ( attributes ) ,
147
258
schema_url,
148
259
}
149
260
}
150
261
}
151
262
152
263
impl From < OpentelemetryResource > for Resource {
153
264
fn from ( val : OpentelemetryResource ) -> Self {
154
- Resource :: from_schema_url ( val. attributes , val. schema_url )
265
+ Resource :: from_schema_url ( Into :: < Vec < KeyValue > > :: into ( val. attributes ) , val. schema_url )
155
266
}
156
267
}
157
268
@@ -160,7 +271,7 @@ pub struct OpentelemetryScope {
160
271
pub name : Cow < ' static , str > ,
161
272
pub version : Option < Cow < ' static , str > > ,
162
273
pub schema_url : Option < Cow < ' static , str > > ,
163
- pub attributes : Vec < KeyValue > ,
274
+ pub attributes : OpentelemetryAttributes ,
164
275
}
165
276
166
277
impl From < & LogEvent > for OpentelemetryScope {
@@ -205,14 +316,19 @@ impl From<&LogEvent> for OpentelemetryScope {
205
316
name,
206
317
version,
207
318
schema_url,
208
- attributes,
319
+ attributes : OpentelemetryAttributes ( attributes ) ,
209
320
}
210
321
}
211
322
}
212
323
213
324
impl From < OpentelemetryScope > for InstrumentationLibrary {
214
325
fn from ( val : OpentelemetryScope ) -> Self {
215
- InstrumentationLibrary :: new ( val. name , val. version , val. schema_url , Some ( val. attributes ) )
326
+ InstrumentationLibrary :: new (
327
+ val. name ,
328
+ val. version ,
329
+ val. schema_url ,
330
+ Some ( val. attributes . into ( ) ) ,
331
+ )
216
332
}
217
333
}
218
334
0 commit comments