@@ -65,60 +65,65 @@ impl UserEventsExporter {
65
65
options = * options. group_name ( provider_name) ;
66
66
let mut eventheader_provider: eventheader_dynamic:: Provider =
67
67
eventheader_dynamic:: Provider :: new ( provider_name, & options) ;
68
- if exporter_config. keywords_map . is_empty ( ) {
69
- println ! (
70
- "Register default keyword {}" ,
71
- exporter_config. default_keyword
72
- ) ;
73
- Self :: register_events ( & mut eventheader_provider, exporter_config. default_keyword )
74
- }
75
- for keyword in exporter_config. keywords_map . values ( ) {
76
- Self :: register_events ( & mut eventheader_provider, * keyword)
77
- }
68
+ Self :: register_keywords ( & mut eventheader_provider, & exporter_config) ;
78
69
UserEventsExporter {
79
70
provider : Arc :: new ( eventheader_provider) ,
80
71
exporter_config,
81
72
}
82
73
}
83
74
84
75
fn register_events ( eventheader_provider : & mut eventheader_dynamic:: Provider , keyword : u64 ) {
85
- eventheader_provider. register_set ( eventheader:: Level :: Informational , keyword) ;
86
- eventheader_provider. register_set ( eventheader:: Level :: Verbose , keyword) ;
87
- eventheader_provider. register_set ( eventheader:: Level :: Warning , keyword) ;
88
- eventheader_provider. register_set ( eventheader:: Level :: Error , keyword) ;
89
- eventheader_provider. register_set ( eventheader:: Level :: CriticalError , keyword) ;
76
+ let levels = [
77
+ eventheader:: Level :: Informational ,
78
+ eventheader:: Level :: Verbose ,
79
+ eventheader:: Level :: Warning ,
80
+ eventheader:: Level :: Error ,
81
+ eventheader:: Level :: CriticalError ,
82
+ ] ;
83
+
84
+ for & level in levels. iter ( ) {
85
+ eventheader_provider. register_set ( level, keyword) ;
86
+ }
90
87
}
91
88
92
- fn add_attributes_to_event (
93
- & self ,
94
- eb : & mut EventBuilder ,
95
- attribs : & mut dyn Iterator < Item = & ( Key , AnyValue ) > ,
89
+ fn register_keywords (
90
+ eventheader_provider : & mut eventheader_dynamic:: Provider ,
91
+ exporter_config : & ExporterConfig ,
96
92
) {
97
- for attrib in attribs {
98
- if attrib. 0 . to_string ( ) == EVENT_ID || attrib. 0 . to_string ( ) == EVENT_NAME {
99
- continue ;
93
+ if exporter_config. keywords_map . is_empty ( ) {
94
+ println ! (
95
+ "Register default keyword {}" ,
96
+ exporter_config. default_keyword
97
+ ) ;
98
+ Self :: register_events ( eventheader_provider, exporter_config. default_keyword ) ;
99
+ }
100
+
101
+ for keyword in exporter_config. keywords_map . values ( ) {
102
+ Self :: register_events ( eventheader_provider, * keyword) ;
103
+ }
104
+ }
105
+
106
+ fn add_attribute_to_event ( & self , eb : & mut EventBuilder , attrib : & ( Key , AnyValue ) ) {
107
+ let field_name = & attrib. 0 . to_string ( ) ;
108
+ match attrib. 1 . to_owned ( ) {
109
+ AnyValue :: Boolean ( b) => {
110
+ eb. add_value ( field_name, b, FieldFormat :: Boolean , 0 ) ;
100
111
}
101
- let field_name = & attrib. 0 . to_string ( ) ;
102
- match attrib. 1 . to_owned ( ) {
103
- AnyValue :: Boolean ( b) => {
104
- eb. add_value ( field_name, b, FieldFormat :: Boolean , 0 ) ;
105
- }
106
- AnyValue :: Int ( i) => {
107
- eb. add_value ( field_name, i, FieldFormat :: SignedInt , 0 ) ;
108
- }
109
- AnyValue :: Double ( f) => {
110
- eb. add_value ( field_name, f, FieldFormat :: Float , 0 ) ;
111
- }
112
- AnyValue :: String ( s) => {
113
- eb. add_str ( field_name, & s. to_string ( ) , FieldFormat :: Default , 0 ) ;
114
- }
115
- _ => ( ) ,
112
+ AnyValue :: Int ( i) => {
113
+ eb. add_value ( field_name, i, FieldFormat :: SignedInt , 0 ) ;
114
+ }
115
+ AnyValue :: Double ( f) => {
116
+ eb. add_value ( field_name, f, FieldFormat :: Float , 0 ) ;
116
117
}
118
+ AnyValue :: String ( s) => {
119
+ eb. add_str ( field_name, & s. to_string ( ) , FieldFormat :: Default , 0 ) ;
120
+ }
121
+ _ => ( ) ,
117
122
}
118
123
}
119
124
120
125
fn get_severity_level ( & self , severity : Severity ) -> Level {
121
- let level : Level = match severity {
126
+ match severity {
122
127
Severity :: Debug
123
128
| Severity :: Debug2
124
129
| Severity :: Debug3
@@ -143,8 +148,7 @@ impl UserEventsExporter {
143
148
Severity :: Warn | Severity :: Warn2 | Severity :: Warn3 | Severity :: Warn4 => {
144
149
eventheader:: Level :: Warning
145
150
}
146
- } ;
147
- level
151
+ }
148
152
}
149
153
150
154
#[ allow( dead_code) ]
@@ -184,7 +188,6 @@ impl UserEventsExporter {
184
188
} ;
185
189
if log_es. enabled ( ) {
186
190
EBW . with ( |eb| {
187
- let [ mut cs_a_count, mut cs_b_count, mut cs_c_count] = [ 0 ; 3 ] ;
188
191
let mut eb = eb. borrow_mut ( ) ;
189
192
let event_tags: u32 = 0 ; // TBD name and event_tag values
190
193
eb. reset ( log_data. instrumentation . name . as_ref ( ) , event_tags as u16 ) ;
@@ -193,6 +196,7 @@ impl UserEventsExporter {
193
196
eb. add_value ( "__csver__" , 0x0401u16 , FieldFormat :: HexInt , 0 ) ;
194
197
195
198
// populate CS PartA
199
+ let mut cs_a_count = 0 ;
196
200
let event_time: SystemTime = log_data
197
201
. record
198
202
. timestamp
@@ -205,107 +209,85 @@ impl UserEventsExporter {
205
209
) ;
206
210
eb. add_str ( "time" , time, FieldFormat :: Default , 0 ) ;
207
211
}
212
+ //populate CS PartC
213
+ let ( mut is_event_id, mut event_id) = ( false , 0 ) ;
214
+ let ( mut is_event_name, mut event_name) = ( false , "" ) ;
208
215
209
- // populate CS PartB
210
- // Get Event_Id and Event_Name if present.
211
- let ( mut event_id, mut event_name) = ( 0 , "" ) ;
212
- let mut event_count = 0 ;
213
- if log_data. record . attributes . is_some ( ) {
214
- for ( k, v) in log_data. record . attributes . as_ref ( ) . unwrap ( ) . iter ( ) {
215
- if k. as_str ( ) == EVENT_ID {
216
- event_id = match v {
217
- AnyValue :: Int ( value) => {
218
- event_count += 1 ;
219
- * value
220
- }
221
- _ => 0 ,
216
+ if let Some ( attr_list) = & log_data. record . attributes {
217
+ let ( mut is_part_c_present, mut cs_c_bookmark, mut cs_c_count) = ( false , 0 , 0 ) ;
218
+ for attrib in attr_list. iter ( ) {
219
+ match ( attrib. 0 . as_str ( ) , & attrib. 1 ) {
220
+ ( EVENT_ID , AnyValue :: Int ( value) ) => {
221
+ is_event_id = true ;
222
+ event_id = * value;
223
+ continue ;
222
224
}
223
- }
224
- if k. as_str ( ) == EVENT_NAME {
225
- event_name = match v {
226
- AnyValue :: String ( value) => {
227
- event_count += 1 ;
228
- value. as_ref ( )
225
+ ( EVENT_NAME , AnyValue :: String ( value) ) => {
226
+ is_event_name = true ;
227
+ event_name = value. as_str ( ) ;
228
+ continue ;
229
+ }
230
+ _ => {
231
+ if !is_part_c_present {
232
+ eb. add_struct_with_bookmark ( "PartC" , 1 , 0 , & mut cs_c_bookmark) ;
233
+ is_part_c_present = true ;
229
234
}
230
- _ => "" ,
235
+ self . add_attribute_to_event ( & mut eb, attrib) ;
236
+ cs_c_count += 1 ;
231
237
}
232
238
}
233
239
}
240
+ if is_part_c_present {
241
+ eb. set_struct_field_count ( cs_c_bookmark, cs_c_count) ;
242
+ }
234
243
}
235
- cs_b_count += event_count;
236
- // check body, severity number and severity text
237
- let ( mut is_body_present, mut is_severity_text_present) = ( false , false ) ;
244
+ // populate CS PartB
245
+ let mut cs_b_bookmark: usize = 0 ;
246
+ let mut cs_b_count = 0 ;
247
+ eb. add_struct_with_bookmark ( "PartB" , 1 , 0 , & mut cs_b_bookmark) ;
248
+ eb. add_str ( "_typename" , "Logs" , FieldFormat :: Default , 0 ) ;
249
+ cs_b_count += 1 ;
250
+
238
251
if log_data. record . body . is_some ( ) {
252
+ eb. add_str (
253
+ "body" ,
254
+ match log_data. record . body . as_ref ( ) . unwrap ( ) {
255
+ AnyValue :: Int ( value) => value. to_string ( ) ,
256
+ AnyValue :: String ( value) => value. to_string ( ) ,
257
+ AnyValue :: Boolean ( value) => value. to_string ( ) ,
258
+ AnyValue :: Double ( value) => value. to_string ( ) ,
259
+ AnyValue :: Bytes ( value) => String :: from_utf8_lossy ( value) . to_string ( ) ,
260
+ AnyValue :: ListAny ( _value) => "" . to_string ( ) ,
261
+ AnyValue :: Map ( _value) => "" . to_string ( ) ,
262
+ } ,
263
+ FieldFormat :: Default ,
264
+ 0 ,
265
+ ) ;
239
266
cs_b_count += 1 ;
240
- is_body_present = true ;
241
267
}
242
268
if level != Level :: Invalid {
269
+ eb. add_value ( "severityNumber" , level. as_int ( ) , FieldFormat :: SignedInt , 0 ) ;
243
270
cs_b_count += 1 ;
244
271
}
245
272
if log_data. record . severity_text . is_some ( ) {
273
+ eb. add_str (
274
+ "severityText" ,
275
+ log_data. record . severity_text . as_ref ( ) . unwrap ( ) . as_ref ( ) ,
276
+ FieldFormat :: SignedInt ,
277
+ 0 ,
278
+ ) ;
246
279
cs_b_count += 1 ;
247
- is_severity_text_present = true ;
248
- }
249
- if cs_b_count > 0 {
250
- eb. add_struct ( "PartB" , cs_b_count, 0 ) ;
251
- {
252
- if level != Level :: Invalid {
253
- eb. add_value (
254
- "severityNumber" ,
255
- level. as_int ( ) ,
256
- FieldFormat :: SignedInt ,
257
- 0 ,
258
- ) ;
259
- }
260
- if is_severity_text_present {
261
- eb. add_str (
262
- "severityText" ,
263
- log_data. record . severity_text . as_ref ( ) . unwrap ( ) . as_ref ( ) ,
264
- FieldFormat :: SignedInt ,
265
- 0 ,
266
- ) ;
267
- }
268
- if is_body_present {
269
- eb. add_str (
270
- "body" ,
271
- match log_data. record . body . as_ref ( ) . unwrap ( ) {
272
- AnyValue :: Int ( value) => value. to_string ( ) ,
273
- AnyValue :: String ( value) => value. to_string ( ) ,
274
- AnyValue :: Boolean ( value) => value. to_string ( ) ,
275
- AnyValue :: Double ( value) => value. to_string ( ) ,
276
- AnyValue :: Bytes ( value) => {
277
- String :: from_utf8_lossy ( value) . to_string ( )
278
- }
279
- AnyValue :: ListAny ( _value) => "" . to_string ( ) ,
280
- AnyValue :: Map ( _value) => "" . to_string ( ) ,
281
- } ,
282
- FieldFormat :: Default ,
283
- 0 ,
284
- ) ;
285
- }
286
- if event_id > 0 {
287
- eb. add_value ( "eventId" , event_id, FieldFormat :: SignedInt , 0 ) ;
288
- }
289
- if !event_name. is_empty ( ) {
290
- eb. add_str ( "name" , event_name, FieldFormat :: Default , 0 ) ;
291
- }
292
- } ;
293
280
}
294
-
295
- // populate CS PartC
296
- if log_data. record . attributes . is_some ( ) {
297
- cs_c_count =
298
- log_data. record . attributes . as_ref ( ) . unwrap ( ) . len ( ) as u8 - event_count;
281
+ if is_event_id {
282
+ eb. add_value ( "eventId" , event_id, FieldFormat :: SignedInt , 0 ) ;
283
+ cs_b_count += 1 ;
299
284
}
300
- if cs_c_count > 0 {
301
- eb. add_struct ( "PartC" , cs_c_count, 0 ) ;
302
- {
303
- self . add_attributes_to_event (
304
- & mut eb,
305
- & mut log_data. record . attributes . as_ref ( ) . unwrap ( ) . iter ( ) ,
306
- ) ;
307
- }
285
+ if is_event_name {
286
+ eb. add_str ( "name" , event_name, FieldFormat :: Default , 0 ) ;
287
+ cs_b_count += 1 ;
308
288
}
289
+ eb. set_struct_field_count ( cs_b_bookmark, cs_b_count) ;
290
+
309
291
eb. write ( & log_es, None , None ) ;
310
292
} ) ;
311
293
return Ok ( ( ) ) ;
@@ -352,7 +334,6 @@ impl opentelemetry_sdk::export::logs::LogExporter for UserEventsExporter {
352
334
match es {
353
335
Some ( x) => x. enabled ( ) ,
354
336
_ => false ,
355
- } ;
356
- false
337
+ }
357
338
}
358
339
}
0 commit comments