Skip to content

Commit

Permalink
log_to_metrics: allow custom namespace and subsystem (#9101)
Browse files Browse the repository at this point in the history
* log_to_metrics: allow custom namespace and subsystem

---------

Signed-off-by: Florian Bezannier <[email protected]>
  • Loading branch information
flobz authored Jul 24, 2024
1 parent 1a571a9 commit ac4a8aa
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
32 changes: 29 additions & 3 deletions plugins/filter_log_to_metrics/log_to_metrics.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 ||
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions plugins/filter_log_to_metrics/log_to_metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
21 changes: 21 additions & 0 deletions tests/runtime/filter_log_to_metrics.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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);

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

}
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ac4a8aa

Please sign in to comment.