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

filter_nest: Lift warning log switch #9703

Open
wants to merge 2 commits 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
90 changes: 58 additions & 32 deletions plugins/filter_nest/nest.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ static int configure(struct filter_nest_ctx *ctx,
ctx->prefix = flb_strdup(kv->val);
ctx->prefix_len = flb_sds_len(kv->val);
ctx->remove_prefix = true;
}
else if (strcasecmp(kv->key, "suppress_warnings") == 0) {
continue;
} else {
flb_plg_error(ctx->ins, "Invalid configuration key '%s'", kv->key);
return -1;
Expand Down Expand Up @@ -219,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;
Expand All @@ -229,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(
Expand All @@ -244,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;
Expand All @@ -256,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) {
Expand All @@ -281,22 +289,24 @@ 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++;
}
}
return count;
}

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;
Expand Down Expand Up @@ -345,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;
Expand Down Expand Up @@ -385,9 +397,11 @@ static inline bool is_kv_to_lift(msgpack_object_kv * kv,
}
memcpy(tmp, key, klen);
tmp[klen] = '\0';
flb_plg_warn(ctx->ins, "Value of key '%s' is not a map. "
"Will not attempt to lift from here",
tmp);
if (!ctx->suppress_warnings) {
flb_plg_warn(ctx->ins, "Value of key '%s' is not a map. "
"Will not attempt to lift from here. Tag: %s",
tmp, tag);
}
flb_free(tmp);
return false;
}
Expand All @@ -397,21 +411,23 @@ 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;
msgpack_object_kv *kv;

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;
}
}
Expand All @@ -421,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;
Expand Down Expand Up @@ -456,28 +473,31 @@ 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;
msgpack_object_kv *kv;

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);
}
}
}

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);
Expand All @@ -491,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)",
Expand All @@ -518,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);

Expand All @@ -534,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);
Expand Down Expand Up @@ -576,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(
Expand All @@ -594,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);

Expand Down Expand Up @@ -675,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) {
Expand Down Expand Up @@ -757,6 +778,11 @@ static struct flb_config_map config_map[] = {
0, FLB_FALSE, 0,
"Remove prefix from affected keys if it matches this string"
},
{
FLB_CONFIG_MAP_BOOL, "suppress_warnings", "false",
0, FLB_TRUE, offsetof(struct filter_nest_ctx, suppress_warnings),
"Suppress warnings about non-matching keys"
},
{0}
};

Expand Down
1 change: 1 addition & 0 deletions plugins/filter_nest/nest.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct filter_nest_ctx
int key_len;
char *prefix;
int prefix_len;
bool suppress_warnings;
// nest
struct mk_list wildcards;
int wildcards_cnt;
Expand Down
Loading