From ac4a8aa7a3bdf1ad48b70ceaf1626cae97f99641 Mon Sep 17 00:00:00 2001 From: Florian BEZANNIER <48728684+flobz@users.noreply.github.com> Date: Thu, 25 Jul 2024 01:05:54 +0200 Subject: [PATCH] log_to_metrics: allow custom namespace and subsystem (#9101) * log_to_metrics: allow custom namespace and subsystem --------- Signed-off-by: Florian Bezannier --- .../filter_log_to_metrics/log_to_metrics.c | 32 +++++++++++++++++-- .../filter_log_to_metrics/log_to_metrics.h | 3 ++ tests/runtime/filter_log_to_metrics.c | 21 ++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/plugins/filter_log_to_metrics/log_to_metrics.c b/plugins/filter_log_to_metrics/log_to_metrics.c index e629e0a6e51..433c64e9060 100644 --- a/plugins/filter_log_to_metrics/log_to_metrics.c +++ b/plugins/filter_log_to_metrics/log_to_metrics.c @@ -453,6 +453,8 @@ static int cb_log_to_metrics_init(struct flb_filter_instance *f_ins, flb_sds_t tmp; char metric_description[MAX_METRIC_LENGTH]; char metric_name[MAX_METRIC_LENGTH]; + char metric_namespace[MAX_METRIC_LENGTH]; + char metric_subsystem[MAX_METRIC_LENGTH]; char value_field[MAX_METRIC_LENGTH]; struct flb_input_instance *input_ins; int label_count; @@ -556,6 +558,17 @@ static int cb_log_to_metrics_init(struct flb_filter_instance *f_ins, return -1; } snprintf(metric_name, sizeof(metric_name) - 1, "%s", ctx->metric_name); + snprintf(metric_namespace, sizeof(metric_namespace) - 1, "%s", ctx->metric_namespace); + + /* Check property subsystem name */ + if (ctx->metric_subsystem == NULL || strlen(ctx->metric_subsystem) == 0) { + snprintf(metric_subsystem, sizeof(metric_subsystem) - 1, "%s", + tmp); + } + else { + snprintf(metric_subsystem, sizeof(metric_subsystem) - 1, "%s", + ctx->metric_subsystem); + } /* Check property metric description */ if (ctx->metric_description == NULL || @@ -602,17 +615,17 @@ static int cb_log_to_metrics_init(struct flb_filter_instance *f_ins, /* Depending on mode create different types of cmetrics metrics */ switch (ctx->mode) { case FLB_LOG_TO_METRICS_COUNTER: - ctx->c = cmt_counter_create(ctx->cmt, "log_metric", "counter", + ctx->c = cmt_counter_create(ctx->cmt, metric_namespace, metric_subsystem, metric_name, metric_description, label_count, ctx->label_keys); break; case FLB_LOG_TO_METRICS_GAUGE: - ctx->g = cmt_gauge_create(ctx->cmt, "log_metric", "gauge", + ctx->g = cmt_gauge_create(ctx->cmt, metric_namespace, metric_subsystem, metric_name, metric_description, label_count, ctx->label_keys); break; case FLB_LOG_TO_METRICS_HISTOGRAM: - ctx->h = cmt_histogram_create(ctx->cmt, "log_metric", "histogram", + ctx->h = cmt_histogram_create(ctx->cmt, metric_namespace, metric_subsystem, metric_name, metric_description, ctx->histogram_buckets, label_count, ctx->label_keys); @@ -956,6 +969,19 @@ static struct flb_config_map config_map[] = { offsetof(struct log_to_metrics_ctx, metric_name), "Name of metric" }, + { + FLB_CONFIG_MAP_STR, "metric_namespace", + DEFAULT_LOG_TO_METRICS_NAMESPACE, + FLB_FALSE, FLB_TRUE, + offsetof(struct log_to_metrics_ctx, metric_namespace), + "Namespace of the metric" + }, + { + FLB_CONFIG_MAP_STR, "metric_subsystem",NULL, + FLB_FALSE, FLB_TRUE, + offsetof(struct log_to_metrics_ctx, metric_subsystem), + "Subsystem of the metric" + }, { FLB_CONFIG_MAP_STR, "metric_description", NULL, FLB_FALSE, FLB_TRUE, diff --git a/plugins/filter_log_to_metrics/log_to_metrics.h b/plugins/filter_log_to_metrics/log_to_metrics.h index c5f8706440c..4f1414d0b11 100644 --- a/plugins/filter_log_to_metrics/log_to_metrics.h +++ b/plugins/filter_log_to_metrics/log_to_metrics.h @@ -49,6 +49,7 @@ #define MAX_LABEL_COUNT 32 #define FLB_MEM_BUF_LIMIT_DEFAULT "10M" +#define DEFAULT_LOG_TO_METRICS_NAMESPACE "log_metric" struct log_to_metrics_ctx @@ -57,6 +58,8 @@ struct log_to_metrics_ctx struct flb_filter_instance *ins; int mode; flb_sds_t metric_name; + flb_sds_t metric_namespace; + flb_sds_t metric_subsystem; flb_sds_t metric_description; struct cmt *cmt; struct flb_input_instance *input_ins; diff --git a/tests/runtime/filter_log_to_metrics.c b/tests/runtime/filter_log_to_metrics.c index 642f4cd89b4..a68007b0885 100644 --- a/tests/runtime/filter_log_to_metrics.c +++ b/tests/runtime/filter_log_to_metrics.c @@ -199,6 +199,8 @@ void flb_test_log_to_metrics_counter_k8s(void) const char *expected = "\"value\":5.0,\"labels\":[\"k8s-dummy\"," "\"testpod\",\"mycontainer\",\"abc123\"," "\"def456\",\"red\",\"right\"]"; + const char *expected2 = "{\"ns\":\"log_metric\",\"ss\":\"counter\"," + "\"name\":\"test\",\"desc\":\"Counts messages\"}"; ctx = flb_create(); flb_service_set(ctx, "Flush", "0.200000000", "Grace", "1", "Log_Level", @@ -219,6 +221,7 @@ void flb_test_log_to_metrics_counter_k8s(void) "metric_mode", "counter", "metric_name", "test", "metric_description", "Counts messages", + "metric_subsystem", "", "kubernetes_mode", "on", "label_field", "color", "label_field", "direction", @@ -242,6 +245,10 @@ void flb_test_log_to_metrics_counter_k8s(void) if (!TEST_CHECK(result != NULL)) { TEST_MSG("expected substring:\n%s\ngot:\n%s\n", expected, finalString); } + result = strstr(finalString, expected2); + if (!TEST_CHECK(result != NULL)) { + TEST_MSG("expected substring:\n%s\ngot:\n%s\n", expected, finalString); + } filter_test_destroy(ctx); @@ -260,6 +267,8 @@ void flb_test_log_to_metrics_counter(void) char *input = JSON_MSG1; char finalString[32768] = ""; const char *expected = "\"value\":5.0,\"labels\":[\"red\",\"right\"]"; + const char *expected2 = "{\"ns\":\"myns\",\"ss\":\"subsystem\"," + "\"name\":\"test\",\"desc\":\"Counts messages\"}"; ctx = flb_create(); flb_service_set(ctx, "Flush", "0.200000000", "Grace", "1", "Log_Level", @@ -280,6 +289,8 @@ void flb_test_log_to_metrics_counter(void) "metric_mode", "counter", "metric_name", "test", "metric_description", "Counts messages", + "metric_subsystem", "subsystem", + "metric_namespace", "myns", "kubernetes_mode", "off", "label_field", "color", "label_field", "direction", @@ -302,6 +313,10 @@ void flb_test_log_to_metrics_counter(void) if (!TEST_CHECK(result != NULL)) { TEST_MSG("expected substring:\n%s\ngot:\n%s\n", expected, finalString); } + result = strstr(finalString, expected2); + if (!TEST_CHECK(result != NULL)) { + TEST_MSG("expected substring:\n%s\ngot:\n%s\n", expected, finalString); + } filter_test_destroy(ctx); } @@ -346,6 +361,7 @@ void flb_test_log_to_metrics_counter_k8s_two_tuples(void) "metric_mode", "counter", "metric_name", "test", "metric_description", "Counts two different messages", + "metric_subsystem", "", "kubernetes_mode", "on", "label_field", "color", "label_field", "direction", @@ -414,6 +430,7 @@ void flb_test_log_to_metrics_gauge(void) "metric_mode", "gauge", "metric_name", "test", "metric_description", "Reports gauge from messages", + "metric_subsystem", "", "kubernetes_mode", "off", "value_field", "duration", "label_field", "color", @@ -478,6 +495,7 @@ void flb_test_log_to_metrics_histogram(void) "metric_mode", "histogram", "metric_name", "test", "metric_description", "Histogram of duration", + "metric_subsystem", "", "kubernetes_mode", "off", "value_field", "duration", "label_field", "color", @@ -542,6 +560,7 @@ void flb_test_log_to_metrics_reg(void) "metric_mode", "counter", "metric_name", "test", "metric_description", "Counts messages with regex", + "metric_subsystem", "", "kubernetes_mode", "off", "label_field", "color", "label_field", "direction", @@ -607,6 +626,7 @@ void flb_test_log_to_metrics_empty_label_keys_regex(void) "metric_mode", "counter", "metric_name", "test", "metric_description", "Counts messages with regex", + "metric_subsystem", "", "kubernetes_mode", "off", "regex", "message .*el.*", NULL); @@ -668,6 +688,7 @@ void flb_test_log_to_metrics_label(void) "metric_mode", "counter", "metric_name", "test", "metric_description", "Counts messages", + "metric_subsystem", "", "kubernetes_mode", "off", "add_label", "pod_name $kubernetes['pod_name']", NULL);