@@ -76,10 +76,10 @@ int flb_otel_utils_json_payload_get_wrapped_value(msgpack_object *wrapper,
76
76
msgpack_object * * value ,
77
77
int * type )
78
78
{
79
- int internal_type ;
80
- msgpack_object * kv_value ;
81
- msgpack_object_str * kv_key ;
82
- msgpack_object_map * map ;
79
+ int internal_type ;
80
+ msgpack_object * kv_value = NULL ;
81
+ msgpack_object_str * kv_key = NULL ;
82
+ msgpack_object_map * map = NULL ;
83
83
84
84
if (wrapper -> type != MSGPACK_OBJECT_MAP ) {
85
85
return -1 ;
@@ -95,27 +95,63 @@ int flb_otel_utils_json_payload_get_wrapped_value(msgpack_object *wrapper,
95
95
kv_key = & map -> ptr [0 ].key .via .str ;
96
96
97
97
if (strncasecmp (kv_key -> ptr , "stringValue" , kv_key -> size ) == 0 ) {
98
+ if (kv_value -> type == MSGPACK_OBJECT_NIL ) {
99
+ internal_type = MSGPACK_OBJECT_NIL ;
100
+ }
101
+ else if (kv_value -> type != MSGPACK_OBJECT_STR ) {
102
+ /* If the value is not a string, we cannot process it */
103
+ return -2 ;
104
+ }
98
105
internal_type = MSGPACK_OBJECT_STR ;
99
106
}
100
107
else if (strncasecmp (kv_key -> ptr , "boolValue" , kv_key -> size ) == 0 ) {
108
+ if (kv_value -> type != MSGPACK_OBJECT_BOOLEAN ) {
109
+ /* If the value is not a boolean, we cannot process it */
110
+ return -2 ;
111
+ }
101
112
internal_type = MSGPACK_OBJECT_BOOLEAN ;
102
113
}
103
114
else if (strncasecmp (kv_key -> ptr , "intValue" , kv_key -> size ) == 0 ) {
115
+ if (kv_value -> type != MSGPACK_OBJECT_POSITIVE_INTEGER &&
116
+ kv_value -> type != MSGPACK_OBJECT_NEGATIVE_INTEGER ) {
117
+ /* If the value is not an integer, we cannot process it */
118
+ return -2 ;
119
+ }
104
120
internal_type = MSGPACK_OBJECT_POSITIVE_INTEGER ;
105
121
}
106
122
else if (strncasecmp (kv_key -> ptr , "doubleValue" , kv_key -> size ) == 0 ) {
123
+ if (kv_value -> type != MSGPACK_OBJECT_FLOAT32 &&
124
+ kv_value -> type != MSGPACK_OBJECT_FLOAT64 ) {
125
+ /* If the value is not a float, we cannot process it */
126
+ return -2 ;
127
+ }
107
128
internal_type = MSGPACK_OBJECT_FLOAT ;
108
129
}
109
130
else if (strncasecmp (kv_key -> ptr , "bytesValue" , kv_key -> size ) == 0 ) {
131
+ if (kv_value -> type != MSGPACK_OBJECT_BIN ) {
132
+ /* If the value is not binary, we cannot process it */
133
+ return -2 ;
134
+ }
110
135
internal_type = MSGPACK_OBJECT_BIN ;
111
136
}
112
137
else if (strncasecmp (kv_key -> ptr , "arrayValue" , kv_key -> size ) == 0 ) {
138
+ if (kv_value -> type != MSGPACK_OBJECT_ARRAY ) {
139
+ /* If the value is not an array, we cannot process it */
140
+ return -2 ;
141
+ }
113
142
internal_type = MSGPACK_OBJECT_ARRAY ;
114
143
}
115
144
else if (strncasecmp (kv_key -> ptr , "kvlistValue" , kv_key -> size ) == 0 ) {
145
+ if (kv_value -> type != MSGPACK_OBJECT_MAP ) {
146
+ /* If the value is not a map, we cannot process it */
147
+ return -2 ;
148
+ }
116
149
internal_type = MSGPACK_OBJECT_MAP ;
117
150
}
118
151
}
152
+ else {
153
+ printf ("Unsupported key type: %d\n" , map -> ptr [0 ].key .type );
154
+ }
119
155
}
120
156
121
157
if (internal_type != -1 ) {
@@ -191,14 +227,21 @@ int flb_otel_utils_json_payload_append_converted_value(
191
227
break ;
192
228
193
229
case MSGPACK_OBJECT_STR :
230
+ /* If the string is empty or null, append an empty string */
194
231
result = flb_log_event_encoder_append_string (
195
232
encoder ,
196
233
target_field ,
197
234
(char * ) object -> via .str .ptr ,
198
235
object -> via .str .size );
199
236
200
237
break ;
201
-
238
+ case MSGPACK_OBJECT_NIL :
239
+ /* Append a null value */
240
+ result = flb_log_event_encoder_append_string (
241
+ encoder ,
242
+ target_field ,
243
+ "" , 0 );
244
+ break ;
202
245
case MSGPACK_OBJECT_BIN :
203
246
result = flb_log_event_encoder_append_binary (
204
247
encoder ,
@@ -221,7 +264,6 @@ int flb_otel_utils_json_payload_append_converted_value(
221
264
object );
222
265
223
266
break ;
224
-
225
267
default :
226
268
break ;
227
269
}
@@ -336,6 +378,9 @@ int flb_otel_utils_json_payload_append_converted_map(
336
378
if (result == 0 && encoder_result == FLB_EVENT_ENCODER_SUCCESS ) {
337
379
return result ;
338
380
}
381
+ else if (result != 0 ) {
382
+ return result ;
383
+ }
339
384
340
385
result = flb_log_event_encoder_begin_map (encoder , target_field );
341
386
@@ -436,7 +481,10 @@ int flb_otel_utils_json_payload_append_converted_kvlist(
436
481
}
437
482
438
483
if (value_index == -1 ) {
439
- result = FLB_EVENT_ENCODER_ERROR_INVALID_ARGUMENT ;
484
+ /*
485
+ * if value is missing basically is 'unset' and handle as Empty() in OTel world, in
486
+ * this case we just pack an empty string value
487
+ */
440
488
}
441
489
442
490
if (result == FLB_EVENT_ENCODER_SUCCESS ) {
@@ -447,10 +495,26 @@ int flb_otel_utils_json_payload_append_converted_kvlist(
447
495
}
448
496
449
497
if (result == FLB_EVENT_ENCODER_SUCCESS ) {
450
- result = flb_otel_utils_json_payload_append_converted_value (
451
- encoder ,
452
- target_field ,
453
- & entry -> ptr [value_index ].val );
498
+ /* if the value is not set, register an empty string as value */
499
+ if (value_index == -1 ) {
500
+ result = flb_log_event_encoder_append_string (
501
+ encoder ,
502
+ target_field ,
503
+ "" , 0 );
504
+ }
505
+ else {
506
+ /* expected value must come in a map */
507
+ if (entry -> ptr [value_index ].val .type != MSGPACK_OBJECT_MAP ) {
508
+ result = -1 ;
509
+ break ;
510
+ }
511
+ else {
512
+ result = flb_otel_utils_json_payload_append_converted_value (
513
+ encoder ,
514
+ target_field ,
515
+ & entry -> ptr [value_index ].val );
516
+ }
517
+ }
454
518
}
455
519
}
456
520
}
0 commit comments