Skip to content

Commit ee27fae

Browse files
tchronoedsiper
authored andcommitted
decode: prometheus: Fix when consecutive metrics have differnet label count
Signed-off-by: Thiago Padilha <thiago@calyptia.com>
1 parent 86132ba commit ee27fae

3 files changed

Lines changed: 64 additions & 2 deletions

File tree

src/cmt_decode_prometheus.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <cmt_decode_prometheus_parser.h>
3535
#include <stdio.h>
3636
#include <string.h>
37+
#include <cmetrics/cmt_map.h>
3738

3839
static void reset_context(struct cmt_decode_prometheus_context *context,
3940
bool reset_summary)
@@ -591,7 +592,7 @@ static int add_metric_histogram(struct cmt_decode_prometheus_context *context)
591592
}
592593

593594
h = context->current.histogram;
594-
if (!h) {
595+
if (!h || label_i != h->map->label_count) {
595596
cmt_buckets = cmt_histogram_buckets_create_size(buckets, bucket_count);
596597
if (!cmt_buckets) {
597598
ret = report_error(context,
@@ -806,7 +807,7 @@ static int add_metric_summary(struct cmt_decode_prometheus_context *context)
806807
}
807808

808809
s = context->current.summary;
809-
if (!s) {
810+
if (!s || label_i != s->map->label_count) {
810811
s = cmt_summary_create(context->cmt,
811812
context->metric.ns,
812813
context->metric.subsystem,
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# HELP k8s_network_load Network load
2+
# TYPE k8s_network_load histogram
3+
k8s_network_load_bucket{le="0.05"} 0 0
4+
k8s_network_load_bucket{le="5.0"} 1 0
5+
k8s_network_load_bucket{le="10.0"} 2 0
6+
k8s_network_load_bucket{le="+Inf"} 3 0
7+
k8s_network_load_sum 1013 0
8+
k8s_network_load_count 3 0
9+
k8s_network_load_bucket{le="0.05",my_label="my_val"} 0 0
10+
k8s_network_load_bucket{le="5.0",my_label="my_val"} 1 0
11+
k8s_network_load_bucket{le="10.0",my_label="my_val"} 2 0
12+
k8s_network_load_bucket{le="+Inf",my_label="my_val"} 3 0
13+
k8s_network_load_sum{my_label="my_val"} 1013 0
14+
k8s_network_load_count{my_label="my_val"} 3 0

tests/prometheus_parser.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,6 +1414,52 @@ void test_pr_168()
14141414
cmt_decode_prometheus_destroy(cmt);
14151415
}
14161416

1417+
void test_histogram_different_label_count()
1418+
{
1419+
char errbuf[256];
1420+
int status;
1421+
struct cmt *cmt;
1422+
cfl_sds_t result = NULL;
1423+
struct cmt_decode_prometheus_parse_opts opts;
1424+
memset(&opts, 0, sizeof(opts));
1425+
opts.errbuf = errbuf;
1426+
opts.errbuf_size = sizeof(errbuf);
1427+
cfl_sds_t in_buf = read_file(CMT_TESTS_DATA_PATH "/histogram_different_label_count.txt");
1428+
const char expected[] =
1429+
"# HELP k8s_network_load Network load\n"
1430+
"# TYPE k8s_network_load histogram\n"
1431+
"k8s_network_load_bucket{le=\"0.05\"} 0 0\n"
1432+
"k8s_network_load_bucket{le=\"5.0\"} 1 0\n"
1433+
"k8s_network_load_bucket{le=\"10.0\"} 2 0\n"
1434+
"k8s_network_load_bucket{le=\"+Inf\"} 3 0\n"
1435+
"k8s_network_load_sum 1013 0\n"
1436+
"k8s_network_load_count 3 0\n"
1437+
"# HELP k8s_network_load Network load\n"
1438+
"# TYPE k8s_network_load histogram\n"
1439+
"k8s_network_load_bucket{le=\"0.05\",my_label=\"my_val\"} 0 0\n"
1440+
"k8s_network_load_bucket{le=\"5.0\",my_label=\"my_val\"} 1 0\n"
1441+
"k8s_network_load_bucket{le=\"10.0\",my_label=\"my_val\"} 2 0\n"
1442+
"k8s_network_load_bucket{le=\"+Inf\",my_label=\"my_val\"} 3 0\n"
1443+
"k8s_network_load_sum{my_label=\"my_val\"} 1013 0\n"
1444+
"k8s_network_load_count{my_label=\"my_val\"} 3 0\n"
1445+
;
1446+
1447+
cmt_initialize();
1448+
status = cmt_decode_prometheus_create(&cmt, in_buf, cfl_sds_len(in_buf), &opts);
1449+
TEST_CHECK(status == 0);
1450+
if (!status) {
1451+
result = cmt_encode_prometheus_create(cmt, CMT_TRUE);
1452+
status = strcmp(result, expected);
1453+
TEST_CHECK(status == 0);
1454+
if (status) {
1455+
fprintf(stderr, "EXPECTED:\n======\n%s\n======\nRESULT:\n======\n%s\n======\n", expected, result);
1456+
}
1457+
cfl_sds_destroy(result);
1458+
}
1459+
cfl_sds_destroy(in_buf);
1460+
cmt_decode_prometheus_destroy(cmt);
1461+
}
1462+
14171463
TEST_LIST = {
14181464
{"header_help", test_header_help},
14191465
{"header_type", test_header_type},
@@ -1444,5 +1490,6 @@ TEST_LIST = {
14441490
{"issue_fluent_bit_6021", test_issue_fluent_bit_6021},
14451491
{"override_timestamp", test_override_timestamp},
14461492
{"pr_168", test_pr_168},
1493+
{"histogram_different_label_count", test_histogram_different_label_count},
14471494
{ 0 }
14481495
};

0 commit comments

Comments
 (0)