From e25fc6475771947d26d8590c5413923719fce8ae Mon Sep 17 00:00:00 2001 From: Ueli Graf Date: Thu, 26 Sep 2024 16:05:03 +0200 Subject: [PATCH 1/3] out_influxdb: allow stripping of tag prefix Removes a configured prefix from measurement names Signed-off-by: Ueli Graf --- plugins/out_influxdb/influxdb.c | 27 ++++++++++++++++++++++++++- plugins/out_influxdb/influxdb.h | 4 ++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/plugins/out_influxdb/influxdb.c b/plugins/out_influxdb/influxdb.c index 2c191354b31..414cd807300 100644 --- a/plugins/out_influxdb/influxdb.c +++ b/plugins/out_influxdb/influxdb.c @@ -74,6 +74,8 @@ static int influxdb_format(struct flb_config *config, char *str = NULL; size_t str_size; char tmp[128]; + int prefix_match = 0; + int prefix_offset = 0; msgpack_object map; struct flb_time tm; struct influxdb_bulk *bulk = NULL; @@ -124,8 +126,16 @@ static int influxdb_format(struct flb_config *config, ctx->seq++; } + prefix_match = strncmp(tag, ctx->prefix, ctx->prefix_len); + if (prefix_match == 0) { + if (tag_len > ctx->prefix_len) { + prefix_offset = ctx->prefix_len; + } + } + ret = influxdb_bulk_append_header(bulk_head, - tag, tag_len, + tag + prefix_offset, + tag_len - prefix_offset, seq, ctx->seq_name, ctx->seq_len); if (ret == -1) { @@ -369,6 +379,15 @@ static int cb_influxdb_init(struct flb_output_instance *ins, struct flb_config * } ctx->seq_len = strlen(ctx->seq_name); + /* prefix */ + tmp = flb_output_get_property("strip_prefix", ins); + if (!tmp) { + ctx->prefix = flb_strdup(""); + } else { + ctx->prefix = flb_strdup(tmp); + } + ctx->prefix_len = strlen(ctx->prefix); + if (ctx->custom_uri) { /* custom URI endpoint (e.g: Grafana */ if (ctx->custom_uri[0] != '/') { @@ -697,6 +716,12 @@ static struct flb_config_map config_map[] = { "Use influxdb line protocol's integer type suffix." }, + { + FLB_CONFIG_MAP_STR, "strip_prefix", NULL, + 0, FLB_FALSE, 0, + "Prefix to be removed from the record tag when writing influx measurements." + }, + /* EOF */ {0} }; diff --git a/plugins/out_influxdb/influxdb.h b/plugins/out_influxdb/influxdb.h index c21145611c8..c6cc54e57d3 100644 --- a/plugins/out_influxdb/influxdb.h +++ b/plugins/out_influxdb/influxdb.h @@ -56,6 +56,10 @@ struct flb_influxdb { char *seq_name; int seq_len; + /* prefix */ + char *prefix; + int prefix_len; + /* auto_tags: on/off */ int auto_tags; From 79bc4852209dbb2d3307c0d8acf1997aba7a7fc7 Mon Sep 17 00:00:00 2001 From: Ueli Graf Date: Mon, 30 Sep 2024 08:39:51 +0000 Subject: [PATCH 2/3] out_influxdb properly free configuration string value on exit Signed-off-by: Ueli Graf --- plugins/out_influxdb/influxdb.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/plugins/out_influxdb/influxdb.c b/plugins/out_influxdb/influxdb.c index 414cd807300..8f8a1a34400 100644 --- a/plugins/out_influxdb/influxdb.c +++ b/plugins/out_influxdb/influxdb.c @@ -614,6 +614,10 @@ static int cb_influxdb_exit(void *data, struct flb_config *config) flb_free(ctx->seq_name); } + if (ctx->prefix) { + flb_free(ctx->prefix); + } + flb_upstream_destroy(ctx->u); flb_free(ctx); From 2d115fa7eb4dbfdfe8947731eb57c89a2598d8d8 Mon Sep 17 00:00:00 2001 From: Ueli Graf Date: Fri, 24 Oct 2025 18:21:34 +0200 Subject: [PATCH 3/3] Add clarifying remarks, make sure prefix match length is always reset Signed-off-by: Ueli Graf --- plugins/out_influxdb/influxdb.c | 7 ++++++- plugins/out_influxdb/influxdb.h | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/plugins/out_influxdb/influxdb.c b/plugins/out_influxdb/influxdb.c index 8f8a1a34400..7136eaa0e9b 100644 --- a/plugins/out_influxdb/influxdb.c +++ b/plugins/out_influxdb/influxdb.c @@ -126,6 +126,10 @@ static int influxdb_format(struct flb_config *config, ctx->seq++; } + /* Find the overlap betwen the tag and a given prefix (to be removed): + If the prefix matches the tag for exactly the length of the prefix and + the tag is longer than the prefix, we have a valid match. */ + prefix_offset = 0; prefix_match = strncmp(tag, ctx->prefix, ctx->prefix_len); if (prefix_match == 0) { if (tag_len > ctx->prefix_len) { @@ -133,6 +137,7 @@ static int influxdb_format(struct flb_config *config, } } + /* Read the tag offset by the length of the prefix to remove it. */ ret = influxdb_bulk_append_header(bulk_head, tag + prefix_offset, tag_len - prefix_offset, @@ -379,7 +384,7 @@ static int cb_influxdb_init(struct flb_output_instance *ins, struct flb_config * } ctx->seq_len = strlen(ctx->seq_name); - /* prefix */ + /* prefix to be removed from the tag */ tmp = flb_output_get_property("strip_prefix", ins); if (!tmp) { ctx->prefix = flb_strdup(""); diff --git a/plugins/out_influxdb/influxdb.h b/plugins/out_influxdb/influxdb.h index c6cc54e57d3..ed9f2b6e58e 100644 --- a/plugins/out_influxdb/influxdb.h +++ b/plugins/out_influxdb/influxdb.h @@ -56,7 +56,7 @@ struct flb_influxdb { char *seq_name; int seq_len; - /* prefix */ + /* prefix to be removed from the tag */ char *prefix; int prefix_len;