Skip to content

Commit

Permalink
out_opentelemetry: add new option 'logs_body_key_attributes' (default…
Browse files Browse the repository at this point in the history
…: 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 <[email protected]>
  • Loading branch information
edsiper committed Feb 19, 2024
1 parent ee7bbc0 commit 202da13
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
13 changes: 10 additions & 3 deletions plugins/out_opentelemetry/opentelemetry.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -860,15 +860,15 @@ 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);
attr_count++;
}

/* 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];
Expand Down Expand Up @@ -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),
Expand Down
3 changes: 3 additions & 0 deletions plugins/out_opentelemetry/opentelemetry.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 202da13

Please sign in to comment.