Skip to content

Commit ac4a8aa

Browse files
authored
log_to_metrics: allow custom namespace and subsystem (#9101)
* log_to_metrics: allow custom namespace and subsystem --------- Signed-off-by: Florian Bezannier <[email protected]>
1 parent 1a571a9 commit ac4a8aa

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

plugins/filter_log_to_metrics/log_to_metrics.c

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,8 @@ static int cb_log_to_metrics_init(struct flb_filter_instance *f_ins,
453453
flb_sds_t tmp;
454454
char metric_description[MAX_METRIC_LENGTH];
455455
char metric_name[MAX_METRIC_LENGTH];
456+
char metric_namespace[MAX_METRIC_LENGTH];
457+
char metric_subsystem[MAX_METRIC_LENGTH];
456458
char value_field[MAX_METRIC_LENGTH];
457459
struct flb_input_instance *input_ins;
458460
int label_count;
@@ -556,6 +558,17 @@ static int cb_log_to_metrics_init(struct flb_filter_instance *f_ins,
556558
return -1;
557559
}
558560
snprintf(metric_name, sizeof(metric_name) - 1, "%s", ctx->metric_name);
561+
snprintf(metric_namespace, sizeof(metric_namespace) - 1, "%s", ctx->metric_namespace);
562+
563+
/* Check property subsystem name */
564+
if (ctx->metric_subsystem == NULL || strlen(ctx->metric_subsystem) == 0) {
565+
snprintf(metric_subsystem, sizeof(metric_subsystem) - 1, "%s",
566+
tmp);
567+
}
568+
else {
569+
snprintf(metric_subsystem, sizeof(metric_subsystem) - 1, "%s",
570+
ctx->metric_subsystem);
571+
}
559572

560573
/* Check property metric description */
561574
if (ctx->metric_description == NULL ||
@@ -602,17 +615,17 @@ static int cb_log_to_metrics_init(struct flb_filter_instance *f_ins,
602615
/* Depending on mode create different types of cmetrics metrics */
603616
switch (ctx->mode) {
604617
case FLB_LOG_TO_METRICS_COUNTER:
605-
ctx->c = cmt_counter_create(ctx->cmt, "log_metric", "counter",
618+
ctx->c = cmt_counter_create(ctx->cmt, metric_namespace, metric_subsystem,
606619
metric_name, metric_description,
607620
label_count, ctx->label_keys);
608621
break;
609622
case FLB_LOG_TO_METRICS_GAUGE:
610-
ctx->g = cmt_gauge_create(ctx->cmt, "log_metric", "gauge",
623+
ctx->g = cmt_gauge_create(ctx->cmt, metric_namespace, metric_subsystem,
611624
metric_name, metric_description,
612625
label_count, ctx->label_keys);
613626
break;
614627
case FLB_LOG_TO_METRICS_HISTOGRAM:
615-
ctx->h = cmt_histogram_create(ctx->cmt, "log_metric", "histogram",
628+
ctx->h = cmt_histogram_create(ctx->cmt, metric_namespace, metric_subsystem,
616629
metric_name, metric_description,
617630
ctx->histogram_buckets,
618631
label_count, ctx->label_keys);
@@ -956,6 +969,19 @@ static struct flb_config_map config_map[] = {
956969
offsetof(struct log_to_metrics_ctx, metric_name),
957970
"Name of metric"
958971
},
972+
{
973+
FLB_CONFIG_MAP_STR, "metric_namespace",
974+
DEFAULT_LOG_TO_METRICS_NAMESPACE,
975+
FLB_FALSE, FLB_TRUE,
976+
offsetof(struct log_to_metrics_ctx, metric_namespace),
977+
"Namespace of the metric"
978+
},
979+
{
980+
FLB_CONFIG_MAP_STR, "metric_subsystem",NULL,
981+
FLB_FALSE, FLB_TRUE,
982+
offsetof(struct log_to_metrics_ctx, metric_subsystem),
983+
"Subsystem of the metric"
984+
},
959985
{
960986
FLB_CONFIG_MAP_STR, "metric_description", NULL,
961987
FLB_FALSE, FLB_TRUE,

plugins/filter_log_to_metrics/log_to_metrics.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#define MAX_LABEL_COUNT 32
5050

5151
#define FLB_MEM_BUF_LIMIT_DEFAULT "10M"
52+
#define DEFAULT_LOG_TO_METRICS_NAMESPACE "log_metric"
5253

5354

5455
struct log_to_metrics_ctx
@@ -57,6 +58,8 @@ struct log_to_metrics_ctx
5758
struct flb_filter_instance *ins;
5859
int mode;
5960
flb_sds_t metric_name;
61+
flb_sds_t metric_namespace;
62+
flb_sds_t metric_subsystem;
6063
flb_sds_t metric_description;
6164
struct cmt *cmt;
6265
struct flb_input_instance *input_ins;

tests/runtime/filter_log_to_metrics.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ void flb_test_log_to_metrics_counter_k8s(void)
199199
const char *expected = "\"value\":5.0,\"labels\":[\"k8s-dummy\","
200200
"\"testpod\",\"mycontainer\",\"abc123\","
201201
"\"def456\",\"red\",\"right\"]";
202+
const char *expected2 = "{\"ns\":\"log_metric\",\"ss\":\"counter\","
203+
"\"name\":\"test\",\"desc\":\"Counts messages\"}";
202204

203205
ctx = flb_create();
204206
flb_service_set(ctx, "Flush", "0.200000000", "Grace", "1", "Log_Level",
@@ -219,6 +221,7 @@ void flb_test_log_to_metrics_counter_k8s(void)
219221
"metric_mode", "counter",
220222
"metric_name", "test",
221223
"metric_description", "Counts messages",
224+
"metric_subsystem", "",
222225
"kubernetes_mode", "on",
223226
"label_field", "color",
224227
"label_field", "direction",
@@ -242,6 +245,10 @@ void flb_test_log_to_metrics_counter_k8s(void)
242245
if (!TEST_CHECK(result != NULL)) {
243246
TEST_MSG("expected substring:\n%s\ngot:\n%s\n", expected, finalString);
244247
}
248+
result = strstr(finalString, expected2);
249+
if (!TEST_CHECK(result != NULL)) {
250+
TEST_MSG("expected substring:\n%s\ngot:\n%s\n", expected, finalString);
251+
}
245252

246253
filter_test_destroy(ctx);
247254

@@ -260,6 +267,8 @@ void flb_test_log_to_metrics_counter(void)
260267
char *input = JSON_MSG1;
261268
char finalString[32768] = "";
262269
const char *expected = "\"value\":5.0,\"labels\":[\"red\",\"right\"]";
270+
const char *expected2 = "{\"ns\":\"myns\",\"ss\":\"subsystem\","
271+
"\"name\":\"test\",\"desc\":\"Counts messages\"}";
263272

264273
ctx = flb_create();
265274
flb_service_set(ctx, "Flush", "0.200000000", "Grace", "1", "Log_Level",
@@ -280,6 +289,8 @@ void flb_test_log_to_metrics_counter(void)
280289
"metric_mode", "counter",
281290
"metric_name", "test",
282291
"metric_description", "Counts messages",
292+
"metric_subsystem", "subsystem",
293+
"metric_namespace", "myns",
283294
"kubernetes_mode", "off",
284295
"label_field", "color",
285296
"label_field", "direction",
@@ -302,6 +313,10 @@ void flb_test_log_to_metrics_counter(void)
302313
if (!TEST_CHECK(result != NULL)) {
303314
TEST_MSG("expected substring:\n%s\ngot:\n%s\n", expected, finalString);
304315
}
316+
result = strstr(finalString, expected2);
317+
if (!TEST_CHECK(result != NULL)) {
318+
TEST_MSG("expected substring:\n%s\ngot:\n%s\n", expected, finalString);
319+
}
305320
filter_test_destroy(ctx);
306321

307322
}
@@ -346,6 +361,7 @@ void flb_test_log_to_metrics_counter_k8s_two_tuples(void)
346361
"metric_mode", "counter",
347362
"metric_name", "test",
348363
"metric_description", "Counts two different messages",
364+
"metric_subsystem", "",
349365
"kubernetes_mode", "on",
350366
"label_field", "color",
351367
"label_field", "direction",
@@ -414,6 +430,7 @@ void flb_test_log_to_metrics_gauge(void)
414430
"metric_mode", "gauge",
415431
"metric_name", "test",
416432
"metric_description", "Reports gauge from messages",
433+
"metric_subsystem", "",
417434
"kubernetes_mode", "off",
418435
"value_field", "duration",
419436
"label_field", "color",
@@ -478,6 +495,7 @@ void flb_test_log_to_metrics_histogram(void)
478495
"metric_mode", "histogram",
479496
"metric_name", "test",
480497
"metric_description", "Histogram of duration",
498+
"metric_subsystem", "",
481499
"kubernetes_mode", "off",
482500
"value_field", "duration",
483501
"label_field", "color",
@@ -542,6 +560,7 @@ void flb_test_log_to_metrics_reg(void)
542560
"metric_mode", "counter",
543561
"metric_name", "test",
544562
"metric_description", "Counts messages with regex",
563+
"metric_subsystem", "",
545564
"kubernetes_mode", "off",
546565
"label_field", "color",
547566
"label_field", "direction",
@@ -607,6 +626,7 @@ void flb_test_log_to_metrics_empty_label_keys_regex(void)
607626
"metric_mode", "counter",
608627
"metric_name", "test",
609628
"metric_description", "Counts messages with regex",
629+
"metric_subsystem", "",
610630
"kubernetes_mode", "off",
611631
"regex", "message .*el.*",
612632
NULL);
@@ -668,6 +688,7 @@ void flb_test_log_to_metrics_label(void)
668688
"metric_mode", "counter",
669689
"metric_name", "test",
670690
"metric_description", "Counts messages",
691+
"metric_subsystem", "",
671692
"kubernetes_mode", "off",
672693
"add_label", "pod_name $kubernetes['pod_name']",
673694
NULL);

0 commit comments

Comments
 (0)