Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

out_opentelemetry: make add_label have an effect on logs #7029

Closed
wants to merge 4 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions plugins/out_opentelemetry/opentelemetry.c
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,14 @@ static int flush_to_otel(struct opentelemetry_context *ctx,
Opentelemetry__Proto__Logs__V1__ResourceLogs resource_log;
Opentelemetry__Proto__Logs__V1__ResourceLogs *resource_logs[1];
Opentelemetry__Proto__Logs__V1__ScopeLogs *scope_logs[1];
Opentelemetry__Proto__Common__V1__KeyValue **attributes_list;
Opentelemetry__Proto__Common__V1__KeyValue *attributes;
size_t kv_size;
size_t kv_index;
struct mk_list *kv_head;
struct flb_kv *kv;
int index;

void *body;
unsigned len;
int res;
Expand All @@ -723,6 +731,37 @@ static int flush_to_otel(struct opentelemetry_context *ctx,
resource_log.n_scope_logs = 1;
resource_logs[0] = &resource_log;

kv_size = mk_list_size(&(ctx->kv_labels));
attributes_list = flb_calloc(kv_size, sizeof(Opentelemetry__Proto__Common__V1__KeyValue *));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code that depends on kv_size holding a value higher than zero should be conditional so it doesn't run unless necessary.

if (attributes_list == NULL) {
flb_errno();
return -1;
}
attributes = flb_calloc(kv_size, sizeof(Opentelemetry__Proto__Common__V1__KeyValue));
if (attributes == NULL) {
flb_errno();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

attributes_list is leaked in this code path.

return -1;
}
for(index = 0; index < kv_size; index++) {
attributes_list[index] = &attributes[index];
}


kv_index = 0;
mk_list_foreach(kv_head, &ctx->kv_labels) {
kv = mk_list_entry(kv_head, struct flb_kv, _head);
opentelemetry__proto__common__v1__key_value__init(&attributes[kv_index]);
attributes[kv_index].key = kv->key;
attributes[kv_index].value = otlp_any_value_initialize(MSGPACK_OBJECT_STR, 0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to verify the result of otlp_any_value_initialize here and have a proper cleanup sequence.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this loop could be merged with the one above.

attributes[kv_index].value->string_value = kv->val;
kv_index++;
}

resource_log.resource = flb_calloc(1, sizeof(Opentelemetry__Proto__Resource__V1__Resource));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to check the result of flb_calloc here and have the appropriate cleanup sequence as well.

opentelemetry__proto__resource__v1__resource__init(resource_log.resource);
resource_log.resource->n_attributes = kv_size;
resource_log.resource->attributes = attributes_list;

export_logs.resource_logs = resource_logs;
export_logs.n_resource_logs = 1;

Expand All @@ -742,6 +781,12 @@ static int flush_to_otel(struct opentelemetry_context *ctx,
ctx->logs_uri);

flb_free(body);
flb_free(resource_log.resource);
for (index = 0; index < kv_size; index++) {
flb_free(attributes[index].value);
}
flb_free(attributes);
flb_free(attributes_list);

return res;
}
Expand Down