From 5ad2d8be222c0d8094d983289a90884ff491b4a4 Mon Sep 17 00:00:00 2001 From: Richard Treu Date: Tue, 10 Dec 2024 11:36:12 +0100 Subject: [PATCH] filter_nest: Print log tag in warning Signed-off-by: Richard Treu --- plugins/filter_nest/nest.c | 80 +++++++++++++++++++++++--------------- 1 file changed, 48 insertions(+), 32 deletions(-) diff --git a/plugins/filter_nest/nest.c b/plugins/filter_nest/nest.c index 51b3fbdfe18..3243dc3030b 100644 --- a/plugins/filter_nest/nest.c +++ b/plugins/filter_nest/nest.c @@ -222,7 +222,10 @@ static inline void map_pack_each_fn(struct flb_log_event_encoder *log_encoder, msgpack_object * map, struct filter_nest_ctx *ctx, bool(*f) (msgpack_object_kv * kv, - struct filter_nest_ctx * ctx)) + struct filter_nest_ctx * ctx, + const char * tag), + const char *tag + ) { int i; int ret; @@ -232,7 +235,7 @@ static inline void map_pack_each_fn(struct flb_log_event_encoder *log_encoder, i < map->via.map.size && ret == FLB_EVENT_ENCODER_SUCCESS; i++) { - if ((*f) (&map->via.map.ptr[i], ctx)) { + if ((*f) (&map->via.map.ptr[i], ctx, tag)) { ret = flb_log_event_encoder_append_body_values( log_encoder, FLB_LOG_EVENT_MSGPACK_OBJECT_VALUE( @@ -247,7 +250,9 @@ static inline void map_transform_and_pack_each_fn(struct flb_log_event_encoder * msgpack_object * map, struct filter_nest_ctx *ctx, bool(*f) (msgpack_object_kv * kv, - struct filter_nest_ctx * ctx) + struct filter_nest_ctx * ctx, + const char * tag), + const char *tag ) { int i; @@ -259,7 +264,7 @@ static inline void map_transform_and_pack_each_fn(struct flb_log_event_encoder * i < map->via.map.size && ret == FLB_EVENT_ENCODER_SUCCESS ; i++) { - if ((*f) (&map->via.map.ptr[i], ctx)) { + if ((*f) (&map->via.map.ptr[i], ctx, tag)) { key = &map->via.map.ptr[i].key; if (ctx->add_prefix) { @@ -284,14 +289,15 @@ static inline void map_transform_and_pack_each_fn(struct flb_log_event_encoder * static inline int map_count_fn(msgpack_object * map, struct filter_nest_ctx *ctx, bool(*f) (msgpack_object_kv * kv, - struct filter_nest_ctx * ctx) - ) + struct filter_nest_ctx * ctx, + const char * tag), + const char *tag) { int i; int count = 0; for (i = 0; i < map->via.map.size; i++) { - if ((*f) (&map->via.map.ptr[i], ctx)) { + if ((*f) (&map->via.map.ptr[i], ctx, tag)) { count++; } } @@ -299,7 +305,8 @@ static inline int map_count_fn(msgpack_object * map, } static inline bool is_kv_to_nest(msgpack_object_kv * kv, - struct filter_nest_ctx *ctx) + struct filter_nest_ctx *ctx, + const char *tag) { const char *key; @@ -348,13 +355,15 @@ static inline bool is_kv_to_nest(msgpack_object_kv * kv, } static inline bool is_not_kv_to_nest(msgpack_object_kv * kv, - struct filter_nest_ctx *ctx) + struct filter_nest_ctx *ctx, + const char *tag) { - return !is_kv_to_nest(kv, ctx); + return !is_kv_to_nest(kv, ctx, tag); } static inline bool is_kv_to_lift(msgpack_object_kv * kv, - struct filter_nest_ctx *ctx) + struct filter_nest_ctx *ctx, + const char *tag) { const char *key; @@ -388,10 +397,10 @@ static inline bool is_kv_to_lift(msgpack_object_kv * kv, } memcpy(tmp, key, klen); tmp[klen] = '\0'; - if (ctx->suppress_warnings == false) { + if (!ctx->suppress_warnings) { flb_plg_warn(ctx->ins, "Value of key '%s' is not a map. " - "Will not attempt to lift from here", - tmp); + "Will not attempt to lift from here. Tag: %s", + tmp, tag); } flb_free(tmp); return false; @@ -402,13 +411,15 @@ static inline bool is_kv_to_lift(msgpack_object_kv * kv, } static inline bool is_not_kv_to_lift(msgpack_object_kv * kv, - struct filter_nest_ctx *ctx) + struct filter_nest_ctx *ctx, + const char *tag) { - return !is_kv_to_lift(kv, ctx); + return !is_kv_to_lift(kv, ctx, tag); } static inline int count_items_to_lift(msgpack_object * map, - struct filter_nest_ctx *ctx) + struct filter_nest_ctx *ctx, + const char *tag) { int i; int count = 0; @@ -416,7 +427,7 @@ static inline int count_items_to_lift(msgpack_object * map, for (i = 0; i < map->via.map.size; i++) { kv = &map->via.map.ptr[i]; - if (is_kv_to_lift(kv, ctx)) { + if (is_kv_to_lift(kv, ctx, tag)) { count = count + kv->val.via.map.size; } } @@ -426,7 +437,8 @@ static inline int count_items_to_lift(msgpack_object * map, static inline void pack_map( struct flb_log_event_encoder *log_encoder, msgpack_object * map, - struct filter_nest_ctx *ctx) + struct filter_nest_ctx *ctx + ) { int i; int ret; @@ -461,7 +473,9 @@ static inline void map_lift_each_fn(struct flb_log_event_encoder *log_encoder, msgpack_object * map, struct filter_nest_ctx *ctx, bool(*f) (msgpack_object_kv * kv, - struct filter_nest_ctx * ctx) + struct filter_nest_ctx * ctx, + const char * tag), + const char *tag ) { int i; @@ -469,7 +483,7 @@ static inline void map_lift_each_fn(struct flb_log_event_encoder *log_encoder, for (i = 0; i < map->via.map.size; i++) { kv = &map->via.map.ptr[i]; - if ((*f) (kv, ctx)) { + if ((*f) (kv, ctx, tag)) { pack_map(log_encoder, &kv->val, ctx); } } @@ -477,12 +491,13 @@ static inline void map_lift_each_fn(struct flb_log_event_encoder *log_encoder, static inline int apply_lifting_rules(struct flb_log_event_encoder *log_encoder, struct flb_log_event *log_event, - struct filter_nest_ctx *ctx) + struct filter_nest_ctx *ctx, + const char *tag) { int ret; msgpack_object map = *log_event->body; - int items_to_lift = map_count_fn(&map, ctx, &is_kv_to_lift); + int items_to_lift = map_count_fn(&map, ctx, &is_kv_to_lift, tag); if (items_to_lift == 0) { flb_plg_debug(ctx->ins, "Lift : No match found for %s", ctx->key); @@ -496,7 +511,7 @@ static inline int apply_lifting_rules(struct flb_log_event_encoder *log_encoder, * + number of element inside maps to lift */ int toplevel_items = - (map.via.map.size - items_to_lift) + count_items_to_lift(&map, ctx); + (map.via.map.size - items_to_lift) + count_items_to_lift(&map, ctx, tag); flb_plg_debug(ctx->ins, "Lift : Outer map size is %d, will be %d, " "lifting %d record(s)", @@ -523,10 +538,10 @@ static inline int apply_lifting_rules(struct flb_log_event_encoder *log_encoder, } /* Pack all current top-level items excluding the key keys */ - map_pack_each_fn(log_encoder, &map, ctx, &is_not_kv_to_lift); + map_pack_each_fn(log_encoder, &map, ctx, &is_not_kv_to_lift, tag); /* Lift and pack all elements in key keys */ - map_lift_each_fn(log_encoder, &map, ctx, &is_kv_to_lift); + map_lift_each_fn(log_encoder, &map, ctx, &is_kv_to_lift, tag); ret = flb_log_event_encoder_commit_record(log_encoder); @@ -539,12 +554,13 @@ static inline int apply_lifting_rules(struct flb_log_event_encoder *log_encoder, static inline int apply_nesting_rules(struct flb_log_event_encoder *log_encoder, struct flb_log_event *log_event, - struct filter_nest_ctx *ctx) + struct filter_nest_ctx *ctx, + const char *tag) { int ret; msgpack_object map = *log_event->body; - size_t items_to_nest = map_count_fn(&map, ctx, &is_kv_to_nest); + size_t items_to_nest = map_count_fn(&map, ctx, &is_kv_to_nest, tag); if (items_to_nest == 0) { flb_plg_debug(ctx->ins, "no match found for %s", ctx->prefix); @@ -581,7 +597,7 @@ static inline int apply_nesting_rules(struct flb_log_event_encoder *log_encoder, * Record array item 2/2 * Create a new map with toplevel items +1 for nested map */ - map_pack_each_fn(log_encoder, &map, ctx, &is_not_kv_to_nest); + map_pack_each_fn(log_encoder, &map, ctx, &is_not_kv_to_nest, tag); /* Pack the nested map key */ ret = flb_log_event_encoder_append_body_string( @@ -599,7 +615,7 @@ static inline int apply_nesting_rules(struct flb_log_event_encoder *log_encoder, } /* Pack the nested items */ - map_transform_and_pack_each_fn(log_encoder, &map, ctx, &is_kv_to_nest); + map_transform_and_pack_each_fn(log_encoder, &map, ctx, &is_kv_to_nest, tag); ret = flb_log_event_encoder_commit_record(log_encoder); @@ -680,11 +696,11 @@ static int cb_nest_filter(const void *data, size_t bytes, if (ctx->operation == NEST) { modified_records = - apply_nesting_rules(&log_encoder, &log_event, ctx); + apply_nesting_rules(&log_encoder, &log_event, ctx, tag); } else { modified_records = - apply_lifting_rules(&log_encoder, &log_event, ctx); + apply_lifting_rules(&log_encoder, &log_event, ctx, tag); } if (modified_records == 0) {