Skip to content

Commit d80d420

Browse files
committed
opentelemetry: utils: on variants values handle NULL as empty strings
Signed-off-by: Eduardo Silva <[email protected]>
1 parent a5cd24b commit d80d420

File tree

2 files changed

+82
-11
lines changed

2 files changed

+82
-11
lines changed

src/opentelemetry/flb_opentelemetry_logs.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,13 @@ static int process_json_payload_resource_logs_entry (struct flb_log_event_encode
548548
result = flb_otel_utils_json_payload_append_converted_kvlist(tmp_encoder,
549549
FLB_LOG_EVENT_BODY,
550550
resource_attr);
551+
if (result < 0) {
552+
if (error_status) {
553+
*error_status = FLB_OTEL_RESOURCE_INVALID_ATTRIBUTE;
554+
}
555+
flb_log_event_encoder_destroy(tmp_encoder);
556+
return -FLB_OTEL_RESOURCE_INVALID_ATTRIBUTE;
557+
}
551558
}
552559

553560
/* resource dropped_attributers_count */

src/opentelemetry/flb_opentelemetry_utils.c

Lines changed: 75 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ int flb_otel_utils_json_payload_get_wrapped_value(msgpack_object *wrapper,
7676
msgpack_object **value,
7777
int *type)
7878
{
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;
8383

8484
if (wrapper->type != MSGPACK_OBJECT_MAP) {
8585
return -1;
@@ -95,27 +95,63 @@ int flb_otel_utils_json_payload_get_wrapped_value(msgpack_object *wrapper,
9595
kv_key = &map->ptr[0].key.via.str;
9696

9797
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+
}
98105
internal_type = MSGPACK_OBJECT_STR;
99106
}
100107
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+
}
101112
internal_type = MSGPACK_OBJECT_BOOLEAN;
102113
}
103114
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+
}
104120
internal_type = MSGPACK_OBJECT_POSITIVE_INTEGER;
105121
}
106122
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+
}
107128
internal_type = MSGPACK_OBJECT_FLOAT;
108129
}
109130
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+
}
110135
internal_type = MSGPACK_OBJECT_BIN;
111136
}
112137
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+
}
113142
internal_type = MSGPACK_OBJECT_ARRAY;
114143
}
115144
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+
}
116149
internal_type = MSGPACK_OBJECT_MAP;
117150
}
118151
}
152+
else {
153+
printf("Unsupported key type: %d\n", map->ptr[0].key.type);
154+
}
119155
}
120156

121157
if (internal_type != -1) {
@@ -191,14 +227,21 @@ int flb_otel_utils_json_payload_append_converted_value(
191227
break;
192228

193229
case MSGPACK_OBJECT_STR:
230+
/* If the string is empty or null, append an empty string */
194231
result = flb_log_event_encoder_append_string(
195232
encoder,
196233
target_field,
197234
(char *) object->via.str.ptr,
198235
object->via.str.size);
199236

200237
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;
202245
case MSGPACK_OBJECT_BIN:
203246
result = flb_log_event_encoder_append_binary(
204247
encoder,
@@ -221,7 +264,6 @@ int flb_otel_utils_json_payload_append_converted_value(
221264
object);
222265

223266
break;
224-
225267
default:
226268
break;
227269
}
@@ -336,6 +378,9 @@ int flb_otel_utils_json_payload_append_converted_map(
336378
if (result == 0 && encoder_result == FLB_EVENT_ENCODER_SUCCESS) {
337379
return result;
338380
}
381+
else if (result != 0) {
382+
return result;
383+
}
339384

340385
result = flb_log_event_encoder_begin_map(encoder, target_field);
341386

@@ -436,7 +481,10 @@ int flb_otel_utils_json_payload_append_converted_kvlist(
436481
}
437482

438483
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+
*/
440488
}
441489

442490
if (result == FLB_EVENT_ENCODER_SUCCESS) {
@@ -447,10 +495,26 @@ int flb_otel_utils_json_payload_append_converted_kvlist(
447495
}
448496

449497
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+
}
454518
}
455519
}
456520
}

0 commit comments

Comments
 (0)