Skip to content

Commit 68cd15e

Browse files
committed
logs_to_metric: Support optional value_field for counters
If a value_field is specified for a counter the counter is incremented by the given value. This allows to count different things then number of record. E.g. counting the number of bytes sent/received from an access log Signed-off-by: Fabian Ruff <[email protected]>
1 parent 9de4d3a commit 68cd15e

File tree

1 file changed

+53
-9
lines changed

1 file changed

+53
-9
lines changed

plugins/filter_log_to_metrics/log_to_metrics.c

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -639,13 +639,14 @@ static int cb_log_to_metrics_init(struct flb_filter_instance *f_ins,
639639
snprintf(metric_description, sizeof(metric_description) - 1, "%s",
640640
ctx->metric_description);
641641

642-
/* Value field only needed for modes gauge and histogram */
643-
if (ctx->mode > 0) {
644-
if (ctx->value_field == NULL || strlen(ctx->value_field) == 0) {
645-
flb_plg_error(f_ins, "value_field is not set");
646-
log_to_metrics_destroy(ctx);
647-
return -1;
642+
if (ctx->value_field == NULL || strlen(ctx->value_field) == 0) {
643+
/* require value field for modes gauge and histogram */
644+
if (ctx->mode > 0) {
645+
flb_plg_error(f_ins, "value_field is not set");
646+
log_to_metrics_destroy(ctx);
647+
return -1;
648648
}
649+
} else {
649650
snprintf(value_field, sizeof(value_field) - 1, "%s",
650651
ctx->value_field);
651652
}
@@ -825,6 +826,7 @@ static int cb_log_to_metrics_filter(const void *data, size_t bytes,
825826
char **label_values = NULL;
826827
int label_count = 0;
827828
int i;
829+
double counter_value = 0;
828830
double gauge_value = 0;
829831
double histogram_value = 0;
830832
char kubernetes_label_values
@@ -901,8 +903,50 @@ static int cb_log_to_metrics_filter(const void *data, size_t bytes,
901903
/* Calculating and setting metric depending on the mode */
902904
switch (ctx->mode) {
903905
case FLB_LOG_TO_METRICS_COUNTER:
904-
ret = cmt_counter_inc(ctx->c, ts, label_count,
905-
label_values);
906+
907+
// If value_field is not set, increment counter by 1
908+
if (ctx->value_field == NULL || strlen(ctx->value_field) == 0) {
909+
ret = cmt_counter_inc(ctx->c, ts, label_count,
910+
label_values);
911+
break;
912+
}
913+
// If value_field is set, increment counter by value
914+
ra = flb_ra_create(ctx->value_field, FLB_TRUE);
915+
if (!ra) {
916+
flb_plg_error(ctx->ins, "invalid record accessor key, aborting");
917+
break;
918+
}
919+
920+
rval = flb_ra_get_value_object(ra, map);
921+
922+
if (!rval) {
923+
flb_warn("given value field is empty or not existent");
924+
break;
925+
}
926+
if (rval->type == FLB_RA_STRING) {
927+
sscanf(rval->val.string, "%lf", &counter_value);
928+
}
929+
else if (rval->type == FLB_RA_FLOAT) {
930+
counter_value = rval->val.f64;
931+
}
932+
else if (rval->type == FLB_RA_INT) {
933+
counter_value = (double)rval->val.i64;
934+
}
935+
else {
936+
flb_plg_error(f_ins,
937+
"cannot convert given value to metric");
938+
break;
939+
}
940+
ret = cmt_counter_add(ctx->c, ts, counter_value,
941+
label_count, label_values);
942+
if (rval) {
943+
flb_ra_key_value_destroy(rval);
944+
rval = NULL;
945+
}
946+
if (ra) {
947+
flb_ra_destroy(ra);
948+
ra = NULL;
949+
}
906950
break;
907951

908952
case FLB_LOG_TO_METRICS_GAUGE:
@@ -1066,7 +1110,7 @@ static struct flb_config_map config_map[] = {
10661110
{
10671111
FLB_CONFIG_MAP_STR, "value_field", NULL,
10681112
0, FLB_TRUE, offsetof(struct log_to_metrics_ctx, value_field),
1069-
"Numeric field to use for gauge or histogram"
1113+
"Numeric field to use for gauge, histogram or counter"
10701114
},
10711115
{
10721116
FLB_CONFIG_MAP_STR, "metric_name", "a",

0 commit comments

Comments
 (0)