Skip to content

Commit 91597b8

Browse files
committed
input_metrics: discard cmetrics with no series
Signed-off-by: Eduardo Silva <[email protected]>
1 parent 70494d7 commit 91597b8

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

include/fluent-bit/flb_input_metric.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ int flb_input_metrics_append(struct flb_input_instance *ins,
2828
const char *tag, size_t tag_len,
2929
struct cmt *cmt);
3030

31+
int flb_input_metrics_is_empty(struct cmt *cmt);
32+
3133
int flb_input_metrics_append_skip_processor_stages(
3234
struct flb_input_instance *ins,
3335
size_t processor_starting_stage,

src/flb_input_metric.c

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@
2222
#include <fluent-bit/flb_input_chunk.h>
2323
#include <fluent-bit/flb_input_metric.h>
2424
#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+
}
2535

2636
static int input_metrics_append(struct flb_input_instance *ins,
2737
size_t processor_starting_stage,
@@ -33,6 +43,7 @@ static int input_metrics_append(struct flb_input_instance *ins,
3343
size_t mt_size;
3444
int processor_is_active;
3545
struct cmt *out_context = NULL;
46+
struct cmt *encode_context;
3647

3748
processor_is_active = flb_processor_is_active(ins->processor);
3849
if (processor_is_active) {
@@ -60,29 +71,31 @@ static int input_metrics_append(struct flb_input_instance *ins,
6071
}
6172
}
6273

74+
if (out_context) {
75+
encode_context = out_context;
76+
}
77+
else {
78+
encode_context = cmt;
79+
}
6380

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) {
6984
cmt_destroy(out_context);
7085
}
86+
return 0;
87+
}
7188

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);
7491

75-
return -1;
76-
}
92+
if (out_context && out_context != cmt) {
93+
cmt_destroy(out_context);
7794
}
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;
8495

85-
}
96+
if (ret != 0) {
97+
flb_plg_error(ins, "could not encode metrics");
98+
return -1;
8699
}
87100

88101
/* Append packed metrics */

0 commit comments

Comments
 (0)