Skip to content

Commit c872957

Browse files
committed
out_opentelemetry: fix behavior of record attributes fix the merge process
The log attribute code now merges additional attributes into the existing array instead of overwriting it by calling otlp_kvarray_append. A new helper otlp_kvarray_append grows an array of OTLP key/value pairs and copies the new entries into it. Added a unit test test_kvarray_append validating that two key/value arrays are combined correctly. Signed-off-by: Eduardo Silva <[email protected]>
1 parent 4104e1a commit c872957

File tree

3 files changed

+61
-6
lines changed

3 files changed

+61
-6
lines changed

plugins/out_opentelemetry/opentelemetry_logs.c

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,9 @@ static int append_v1_logs_metadata_and_fields(struct opentelemetry_context *ctx,
392392
int severity_text_set = FLB_FALSE;
393393
int severity_number_set = FLB_FALSE;
394394
int trace_flags_set = FLB_FALSE;
395+
size_t attr_count = 0;
395396
struct flb_ra_value *ra_val;
397+
Opentelemetry__Proto__Common__V1__KeyValue **attrs = NULL;
396398

397399
if (ctx == NULL || event == NULL || log_record == NULL) {
398400
return -1;
@@ -519,21 +521,43 @@ static int append_v1_logs_metadata_and_fields(struct opentelemetry_context *ctx,
519521
ra_val = flb_ra_get_value_object(ctx->ra_log_meta_otlp_attr, *event->metadata);
520522
if (ra_val != NULL) {
521523
if (ra_val->o.type == MSGPACK_OBJECT_MAP) {
522-
if (log_record->attributes != NULL) {
523-
otlp_kvarray_destroy(log_record->attributes, log_record->n_attributes);
524+
attr_count = 0;
525+
attrs = msgpack_map_to_otlp_kvarray(&ra_val->o, &attr_count);
526+
if (attrs) {
527+
if (log_record->attributes != NULL) {
528+
if (otlp_kvarray_append(&log_record->attributes,
529+
&log_record->n_attributes,
530+
attrs, attr_count) != 0) {
531+
otlp_kvarray_destroy(attrs, attr_count);
532+
}
533+
}
534+
else {
535+
log_record->attributes = attrs;
536+
log_record->n_attributes = attr_count;
537+
}
524538
}
525-
log_record->attributes = msgpack_map_to_otlp_kvarray(&ra_val->o, &log_record->n_attributes);
526539
}
527540
flb_ra_key_value_destroy(ra_val);
528541
}
529542
else if (ctx->ra_attributes_metadata) {
530543
ra_val = flb_ra_get_value_object(ctx->ra_attributes_metadata, *event->metadata);
531544
if (ra_val != NULL) {
532545
if (ra_val->o.type == MSGPACK_OBJECT_MAP) {
533-
if (log_record->attributes != NULL) {
534-
otlp_kvarray_destroy(log_record->attributes, log_record->n_attributes);
546+
attr_count = 0;
547+
attrs = msgpack_map_to_otlp_kvarray(&ra_val->o, &attr_count);
548+
if (attrs) {
549+
if (log_record->attributes != NULL) {
550+
if (otlp_kvarray_append(&log_record->attributes,
551+
&log_record->n_attributes,
552+
attrs, attr_count) != 0) {
553+
otlp_kvarray_destroy(attrs, attr_count);
554+
}
555+
}
556+
else {
557+
log_record->attributes = attrs;
558+
log_record->n_attributes = attr_count;
559+
}
535560
}
536-
log_record->attributes = msgpack_map_to_otlp_kvarray(&ra_val->o, &log_record->n_attributes);
537561
}
538562
flb_ra_key_value_destroy(ra_val);
539563
}

plugins/out_opentelemetry/opentelemetry_utils.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,33 @@ void otlp_kvarray_destroy(Opentelemetry__Proto__Common__V1__KeyValue **kvarray,
167167
}
168168
}
169169

170+
int otlp_kvarray_append(Opentelemetry__Proto__Common__V1__KeyValue ***base,
171+
size_t *base_count,
172+
Opentelemetry__Proto__Common__V1__KeyValue **extra,
173+
size_t extra_count)
174+
{
175+
size_t new_count;
176+
Opentelemetry__Proto__Common__V1__KeyValue **tmp;
177+
178+
if (extra == NULL || extra_count == 0) {
179+
return 0;
180+
}
181+
182+
new_count = *base_count + extra_count;
183+
tmp = flb_realloc(*base, new_count * sizeof(Opentelemetry__Proto__Common__V1__KeyValue *));
184+
if (!tmp) {
185+
return -1;
186+
}
187+
188+
*base = tmp;
189+
memcpy(*base + *base_count, extra,
190+
extra_count * sizeof(Opentelemetry__Proto__Common__V1__KeyValue *));
191+
*base_count = new_count;
192+
flb_free(extra);
193+
194+
return 0;
195+
}
196+
170197
void otlp_kvpair_destroy(Opentelemetry__Proto__Common__V1__KeyValue *kvpair)
171198
{
172199
if (kvpair == NULL) {

plugins/out_opentelemetry/opentelemetry_utils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ Opentelemetry__Proto__Common__V1__KeyValueList *otlp_kvlist_value_initialize(siz
2626
Opentelemetry__Proto__Common__V1__AnyValue *otlp_any_value_initialize(int data_type, size_t entry_count);
2727

2828
void otlp_kvarray_destroy(Opentelemetry__Proto__Common__V1__KeyValue **kvarray, size_t entry_count);
29+
int otlp_kvarray_append(Opentelemetry__Proto__Common__V1__KeyValue ***base,
30+
size_t *base_count,
31+
Opentelemetry__Proto__Common__V1__KeyValue **extra,
32+
size_t extra_count);
2933
void otlp_kvpair_destroy(Opentelemetry__Proto__Common__V1__KeyValue *kvpair);
3034
void otlp_kvlist_destroy(Opentelemetry__Proto__Common__V1__KeyValueList *kvlist);
3135
void otlp_array_destroy(Opentelemetry__Proto__Common__V1__ArrayValue *array);

0 commit comments

Comments
 (0)