22
22
#include <fluent-bit/flb_input_chunk.h>
23
23
#include <fluent-bit/flb_input_metric.h>
24
24
#include <fluent-bit/flb_input_plugin.h>
25
+ #include <cfl/cfl.h>
26
+
27
+ int flb_input_metrics_is_empty (struct cmt * cmt )
28
+ {
29
+ return cfl_list_is_empty (& cmt -> counters ) &&
30
+ cfl_list_is_empty (& cmt -> gauges ) &&
31
+ cfl_list_is_empty (& cmt -> histograms ) &&
32
+ cfl_list_is_empty (& cmt -> summaries ) &&
33
+ cfl_list_is_empty (& cmt -> untypeds );
34
+ }
25
35
26
36
static int input_metrics_append (struct flb_input_instance * ins ,
27
37
size_t processor_starting_stage ,
@@ -33,6 +43,7 @@ static int input_metrics_append(struct flb_input_instance *ins,
33
43
size_t mt_size ;
34
44
int processor_is_active ;
35
45
struct cmt * out_context = NULL ;
46
+ struct cmt * encode_context ;
36
47
37
48
processor_is_active = flb_processor_is_active (ins -> processor );
38
49
if (processor_is_active ) {
@@ -60,29 +71,31 @@ static int input_metrics_append(struct flb_input_instance *ins,
60
71
}
61
72
}
62
73
74
+ if (out_context ) {
75
+ encode_context = out_context ;
76
+ }
77
+ else {
78
+ encode_context = cmt ;
79
+ }
63
80
64
- if (out_context != NULL ) {
65
- /* Convert metrics to msgpack */
66
- ret = cmt_encode_msgpack_create (out_context , & mt_buf , & mt_size );
67
-
68
- if (out_context != cmt ) {
81
+ /* Drop the context if it contains no metrics */
82
+ if (encode_context == NULL || flb_input_metrics_is_empty (encode_context )) {
83
+ if (out_context && out_context != cmt ) {
69
84
cmt_destroy (out_context );
70
85
}
86
+ return 0 ;
87
+ }
71
88
72
- if ( ret != 0 ) {
73
- flb_plg_error ( ins , "could not encode metrics" );
89
+ /* Convert metrics to msgpack */
90
+ ret = cmt_encode_msgpack_create ( encode_context , & mt_buf , & mt_size );
74
91
75
- return -1 ;
76
- }
92
+ if ( out_context && out_context != cmt ) {
93
+ cmt_destroy ( out_context );
77
94
}
78
- else {
79
- /* Convert metrics to msgpack */
80
- ret = cmt_encode_msgpack_create (cmt , & mt_buf , & mt_size );
81
- if (ret != 0 ) {
82
- flb_plg_error (ins , "could not encode metrics" );
83
- return -1 ;
84
95
85
- }
96
+ if (ret != 0 ) {
97
+ flb_plg_error (ins , "could not encode metrics" );
98
+ return -1 ;
86
99
}
87
100
88
101
/* Append packed metrics */
0 commit comments