Skip to content

out_opentelemetry: add option to preserve fields in log body #10304

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions plugins/out_opentelemetry/opentelemetry.c
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,12 @@ static struct flb_config_map config_map[] = {
"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_BOOL, "logs_body_preserve", "false",
0, FLB_TRUE, offsetof(struct opentelemetry_context, logs_body_preserve),
"Preserve fields in record when they're used in the log body, rather than removing them."
},

{
FLB_CONFIG_MAP_STR, "traces_uri", "/v1/traces",
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 @@ -141,6 +141,9 @@ struct opentelemetry_context {
/* boolean that defines if remaining keys of logs_body_key are set as attributes */
int logs_body_key_attributes;

/* Option to preserve fields in record when they're used in the log body */
int logs_body_preserve;

/* internal labels ready to append */
struct mk_list kv_labels;

Expand Down
38 changes: 27 additions & 11 deletions plugins/out_opentelemetry/opentelemetry_logs.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,12 @@ static inline int log_record_set_body(struct opentelemetry_context *ctx,

ret = flb_ra_get_kv_pair(bk->ra, *event->body, &s_key, &o_key, &o_val);
if (ret == 0) {
log_object = msgpack_object_to_otlp_any_value(o_val);
/* If we're preserving fields, use the whole record for OTLP body */
if (ctx->logs_body_preserve == FLB_TRUE) {
log_object = msgpack_object_to_otlp_any_value(event->body);
} else {
log_object = msgpack_object_to_otlp_any_value(o_val);
}

/* Link the record accessor pattern that matched */
*out_ra_match = bk->ra;
Expand Down Expand Up @@ -228,7 +233,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->logs_body_key_attributes == FLB_TRUE && ctx->mp_accessor && ra_match) {
if (ctx->logs_body_key_attributes == FLB_TRUE && ctx->mp_accessor && ra_match && ctx->logs_body_preserve == FLB_FALSE) {
/*
* 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 @@ -287,16 +292,27 @@ static int log_record_set_attributes(struct opentelemetry_context *ctx,
attr_count++;
}

/* remaining fields that were not added to log body */
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];
buf[attr_count] = msgpack_kv_to_otlp_any_value(kv);
attr_count++;
/* Handle remaining fields based on configuration */
if (ctx->logs_body_key_attributes == FLB_TRUE) {
if (unpacked) {
/* iterate the map and reference each element as an OTLP value */
for (i = 0; i < result.data.via.map.size; i++) {
kv = &result.data.via.map.ptr[i];
buf[attr_count] = msgpack_kv_to_otlp_any_value(kv);
attr_count++;
}
msgpack_unpacked_destroy(&result);
flb_free(out_buf);
}
else if (!unpacked && ctx->logs_body_preserve == FLB_TRUE) {
/* When logs_body_preserve is true, still add fields to attributes
* if logs_body_key_attributes is enabled */
for (i = 0; i < event->body->via.map.size; i++) {
kv = &event->body->via.map.ptr[i];
buf[attr_count] = msgpack_kv_to_otlp_any_value(kv);
attr_count++;
}
}
msgpack_unpacked_destroy(&result);
flb_free(out_buf);
}

log_record->attributes = buf;
Expand Down
Loading