From 202da1374dffd98984b2aa56dd444d49bef1dbd5 Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Sun, 18 Feb 2024 13:09:18 -0600 Subject: [PATCH] out_opentelemetry: add new option 'logs_body_key_attributes' (default: false) When logs_body_key was added, by default it added all the unmatched keys as attributes, there are many cases where this is not desired. In order to keep the flexibility to the user, this patch adds a new config option called 'logs_body_key_attributes' which takes a boolean value. When it's enabled, the remaining keys are added as attributes; by default this is disabled. Signed-off-by: Eduardo Silva --- plugins/out_opentelemetry/opentelemetry.c | 13 ++++++++++--- plugins/out_opentelemetry/opentelemetry.h | 3 +++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/plugins/out_opentelemetry/opentelemetry.c b/plugins/out_opentelemetry/opentelemetry.c index 784b1d95a69..140d676e86f 100644 --- a/plugins/out_opentelemetry/opentelemetry.c +++ b/plugins/out_opentelemetry/opentelemetry.c @@ -819,7 +819,7 @@ static int log_record_set_attributes(struct opentelemetry_context *ctx, * buffer. If there are matches, meaning that a new output buffer was created, ret will * be FLB_TRUE, if no matches exists it returns FLB_FALSE. */ - if (ctx->mp_accessor && ra_match) { + if (ctx->logs_body_key_attributes == FLB_TRUE && ctx->mp_accessor && ra_match) { /* * if ra_match is not NULL, it means that the log body was populated with a key from the record * and the variable holds a reference to the record accessor that matched the key. @@ -860,7 +860,7 @@ static int log_record_set_attributes(struct opentelemetry_context *ctx, return -1; } - /* pack metadata */ + /* pack log metadata */ for (i = 0; i < metadata->via.map.size; i++) { kv = &metadata->via.map.ptr[i]; buf[i] = msgpack_kv_to_otlp_any_value(kv); @@ -868,7 +868,7 @@ static int log_record_set_attributes(struct opentelemetry_context *ctx, } /* remaining fields that were not added to log body */ - if (unpacked) { + if (ctx->logs_body_key_attributes == FLB_TRUE && unpacked) { /* iterate the map and reference each elemento as an OTLP value */ for (i = 0; i < result.data.via.map.size; i++) { kv = &result.data.via.map.ptr[i]; @@ -1331,6 +1331,13 @@ static struct flb_config_map config_map[] = { "Specify an optional HTTP URI for the target OTel endpoint." }, + { + FLB_CONFIG_MAP_BOOL, "logs_body_key_attributes", "false", + 0, FLB_TRUE, offsetof(struct opentelemetry_context, logs_body_key_attributes), + "If logs_body_key is set and it matched a pattern, this option will include the " + "remaining fields in the record as attributes." + }, + { FLB_CONFIG_MAP_STR, "traces_uri", "/v1/traces", 0, FLB_TRUE, offsetof(struct opentelemetry_context, traces_uri), diff --git a/plugins/out_opentelemetry/opentelemetry.h b/plugins/out_opentelemetry/opentelemetry.h index 6ef6b2bab87..7953088d9f5 100644 --- a/plugins/out_opentelemetry/opentelemetry.h +++ b/plugins/out_opentelemetry/opentelemetry.h @@ -77,6 +77,9 @@ struct opentelemetry_context { /* head of linked list body keys populated once log_body_key_list_str is parsed */ struct mk_list log_body_key_list; + /* boolean that defines if remaining keys of logs_body_key are set as attributes */ + int logs_body_key_attributes; + /* internal labels ready to append */ struct mk_list kv_labels;