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

Add functionality to the modify filter to move fields to the start or end #6103

Merged
merged 1 commit into from
Oct 21, 2022
Merged
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
64 changes: 64 additions & 0 deletions plugins/filter_modify/modify.c
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,12 @@ static int setup(struct filter_modify_ctx *ctx,
rule->ruletype = REMOVE_REGEX;
rule->key_is_regex = true;
}
else if (strcasecmp(kv->key, "move_to_start") == 0) {
rule->ruletype = MOVE_TO_START;
}
else if (strcasecmp(kv->key, "move_to_end") == 0) {
rule->ruletype = MOVE_TO_END;
}
else {
flb_plg_error(ctx->ins, "Invalid operation %s : %s in "
"configuration", kv->key, kv->val);
Expand Down Expand Up @@ -1196,6 +1202,50 @@ static inline int apply_rule_REMOVE_REGEX(msgpack_packer * packer,
}
}

static inline int apply_rule_MOVE_TO_END(struct filter_modify_ctx *ctx,
msgpack_packer *packer,
msgpack_object *map,
struct modify_rule *rule)
{

int match_keys =
map_count_keys_matching_wildcard(map, rule->key, rule->key_len);

if (match_keys == 0) {
return FLB_FILTER_NOTOUCH;
}
else {
msgpack_pack_map(packer, map->via.map.size);
map_pack_each_fn(packer, map, rule,
kv_key_does_not_match_wildcard_rule_key);
map_pack_each_fn(packer, map, rule,
kv_key_matches_wildcard_rule_key);
return FLB_FILTER_MODIFIED;
}
}

static inline int apply_rule_MOVE_TO_START(struct filter_modify_ctx *ctx,
msgpack_packer *packer,
msgpack_object *map,
struct modify_rule *rule)
{

int match_keys =
map_count_keys_matching_wildcard(map, rule->key, rule->key_len);

if (match_keys == 0) {
return FLB_FILTER_NOTOUCH;
}
else {
msgpack_pack_map(packer, map->via.map.size);
map_pack_each_fn(packer, map, rule,
kv_key_matches_wildcard_rule_key);
map_pack_each_fn(packer, map, rule,
kv_key_does_not_match_wildcard_rule_key);
return FLB_FILTER_MODIFIED;
}
}

static inline int apply_modifying_rule(struct filter_modify_ctx *ctx,
msgpack_packer *packer,
msgpack_object *map,
Expand All @@ -1220,6 +1270,10 @@ static inline int apply_modifying_rule(struct filter_modify_ctx *ctx,
return apply_rule_COPY(ctx, packer, map, rule);
case HARD_COPY:
return apply_rule_HARD_COPY(ctx, packer, map, rule);
case MOVE_TO_START:
return apply_rule_MOVE_TO_START(ctx, packer, map, rule);
case MOVE_TO_END:
return apply_rule_MOVE_TO_END(ctx, packer, map, rule);
default:
flb_plg_warn(ctx->ins, "Unknown ruletype for rule with key %s, ignoring",
rule->key);
Expand Down Expand Up @@ -1446,6 +1500,16 @@ static struct flb_config_map config_map[] = {
FLB_CONFIG_MAP_MULT, FLB_FALSE, 0,
"Remove all key/value pairs with key matching regexp KEY"
},
{
FLB_CONFIG_MAP_STR, "Move_To_Start", NULL,
FLB_CONFIG_MAP_MULT, FLB_FALSE, 0,
"Move key/value pairs with keys matching KEY to the start of the message"
},
{
FLB_CONFIG_MAP_STR, "Move_To_End", NULL,
FLB_CONFIG_MAP_MULT, FLB_FALSE, 0,
"Move key/value pairs with keys matching KEY to the end of the message"
},
{
FLB_CONFIG_MAP_STR, "Rename", NULL,
FLB_CONFIG_MAP_MULT, FLB_FALSE, 0,
Expand Down
4 changes: 3 additions & 1 deletion plugins/filter_modify/modify.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ enum FLB_FILTER_MODIFY_RULETYPE {
REMOVE_WILDCARD,
REMOVE_REGEX,
COPY,
HARD_COPY
HARD_COPY,
MOVE_TO_START,
MOVE_TO_END
};

enum FLB_FILTER_MODIFY_CONDITIONTYPE {
Expand Down