@@ -639,13 +639,14 @@ static int cb_log_to_metrics_init(struct flb_filter_instance *f_ins,
639
639
snprintf (metric_description , sizeof (metric_description ) - 1 , "%s" ,
640
640
ctx -> metric_description );
641
641
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 ;
648
648
}
649
+ } else {
649
650
snprintf (value_field , sizeof (value_field ) - 1 , "%s" ,
650
651
ctx -> value_field );
651
652
}
@@ -825,6 +826,7 @@ static int cb_log_to_metrics_filter(const void *data, size_t bytes,
825
826
char * * label_values = NULL ;
826
827
int label_count = 0 ;
827
828
int i ;
829
+ double counter_value = 0 ;
828
830
double gauge_value = 0 ;
829
831
double histogram_value = 0 ;
830
832
char kubernetes_label_values
@@ -901,8 +903,50 @@ static int cb_log_to_metrics_filter(const void *data, size_t bytes,
901
903
/* Calculating and setting metric depending on the mode */
902
904
switch (ctx -> mode ) {
903
905
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
+ }
906
950
break ;
907
951
908
952
case FLB_LOG_TO_METRICS_GAUGE :
@@ -1066,7 +1110,7 @@ static struct flb_config_map config_map[] = {
1066
1110
{
1067
1111
FLB_CONFIG_MAP_STR , "value_field" , NULL ,
1068
1112
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 "
1070
1114
},
1071
1115
{
1072
1116
FLB_CONFIG_MAP_STR , "metric_name" , "a" ,
0 commit comments