Skip to content

Commit a327812

Browse files
committed
opentelemetry: fix value handling for empty maps and missing values
- Add proper NULL handling for empty map values (unset values) - Fix missing value handling to pack as empty string - Complete logic flow to handle all OpenTelemetry value states Signed-off-by: Eduardo Silva <[email protected]>
1 parent b659c68 commit a327812

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

src/opentelemetry/flb_opentelemetry_utils.c

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,9 @@ int flb_otel_utils_json_payload_append_converted_kvlist(
451451
int value_index;
452452
int key_index;
453453
int result;
454+
int pack_null_value = FLB_FALSE;
455+
int pack_string_value = FLB_FALSE;
456+
int pack_value = FLB_FALSE;
454457
size_t index;
455458
msgpack_object_array *array;
456459
msgpack_object_map *entry;
@@ -476,15 +479,33 @@ int flb_otel_utils_json_payload_append_converted_kvlist(
476479
result = FLB_EVENT_ENCODER_ERROR_INVALID_ARGUMENT;
477480
}
478481

482+
value_index = -1;
483+
pack_null_value = FLB_FALSE;
484+
pack_string_value = FLB_FALSE;
485+
pack_value = FLB_FALSE;
486+
479487
if (result == FLB_EVENT_ENCODER_SUCCESS) {
480488
value_index = flb_otel_utils_find_map_entry_by_key(entry, "value", 0, FLB_TRUE);
489+
490+
if (value_index >= 0 &&
491+
entry->ptr[value_index].val.type == MSGPACK_OBJECT_MAP &&
492+
entry->ptr[value_index].val.via.map.size == 0) {
493+
/*
494+
* if value is an empty map it represents an unset value, pack as NULL
495+
*/
496+
pack_null_value = FLB_TRUE;
497+
}
481498
}
482499

483500
if (value_index == -1) {
484501
/*
485502
* if value is missing basically is 'unset' and handle as Empty() in OTel world, in
486503
* this case we just pack an empty string value
487504
*/
505+
pack_string_value = FLB_TRUE;
506+
}
507+
else if (!pack_null_value) {
508+
pack_value = FLB_TRUE;
488509
}
489510

490511
if (result == FLB_EVENT_ENCODER_SUCCESS) {
@@ -495,14 +516,18 @@ int flb_otel_utils_json_payload_append_converted_kvlist(
495516
}
496517

497518
if (result == FLB_EVENT_ENCODER_SUCCESS) {
498-
/* if the value is not set, register an empty string as value */
499-
if (value_index == -1) {
519+
if (pack_null_value) {
520+
/* pack NULL for unset values (empty maps) */
521+
result = flb_log_event_encoder_append_null(encoder, target_field);
522+
}
523+
else if (pack_string_value) {
524+
/* if the value is not set, register an empty string as value */
500525
result = flb_log_event_encoder_append_string(
501526
encoder,
502527
target_field,
503528
"", 0);
504529
}
505-
else {
530+
else if (pack_value) {
506531
/* expected value must come in a map */
507532
if (entry->ptr[value_index].val.type != MSGPACK_OBJECT_MAP) {
508533
result = -1;

0 commit comments

Comments
 (0)