From: Florian Forster Date: Tue, 20 Feb 2024 17:19:05 +0000 (+0100) Subject: write_prometheus plugin: Fix updating of existing metrics (+test). X-Git-Tag: collectd-6.0.0.rc3~2^2~4 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=be46fa83410869803554602992dc7251237702d6;p=thirdparty%2Fcollectd.git write_prometheus plugin: Fix updating of existing metrics (+test). --- diff --git a/src/write_prometheus.c b/src/write_prometheus.c index 9cf2f4d83..f9a2c3bc9 100644 --- a/src/write_prometheus.c +++ b/src/write_prometheus.c @@ -917,7 +917,7 @@ static int prom_write(metric_family_t const *fam, for (size_t i = 0; i < fam->metric.num; i++) { prometheus_metric_t pm = to_prometheus_metric(fam->metric.ptr[i]); - metric_t *mmatch = bsearch(&pm, pfam->metrics, pfam->metrics_num, + prometheus_metric_t *mmatch = bsearch(&pm, pfam->metrics, pfam->metrics_num, sizeof(*pfam->metrics), prom_metric_cmp); if (mmatch == NULL) { prom_metric_family_metric_append(pfam, pm); diff --git a/src/write_prometheus_test.c b/src/write_prometheus_test.c index 53237c840..ba91a6136 100644 --- a/src/write_prometheus_test.c +++ b/src/write_prometheus_test.c @@ -463,6 +463,71 @@ DEF_TEST(end_to_end) { "# collectd/write_prometheus " PACKAGE_VERSION " at example.com\n", // clang-format on }, + { + .name = "multiple data points of one metric", + .fams = + (metric_family_t[]){ + { + .name = "unit.test", + .type = METRIC_TYPE_COUNTER, + .resource = + { + .ptr = + (label_pair_t[]){ + {"host.name", "example.org"}, + {"service.instance.id", "instance1"}, + {"service.name", "name1"}, + }, + .num = 3, + }, + .metric = + { + .ptr = + &(metric_t){ + .time = TIME_T_TO_CDTIME_T(100), + .value.counter = 42, + }, + .num = 1, + }, + }, + { + .name = "unit.test", + .type = METRIC_TYPE_COUNTER, + .resource = + { + .ptr = + (label_pair_t[]){ + {"host.name", "example.org"}, + {"service.instance.id", "instance1"}, + {"service.name", "name1"}, + }, + .num = 3, + }, + .metric = + { + .ptr = + &(metric_t){ + .time = TIME_T_TO_CDTIME_T(110), + .value.counter = 62, + }, + .num = 1, + }, + }, + }, + .fams_num = 2, + // clang-format off + .want = + "# HELP target_info Target metadata\n" + "# TYPE target_info gauge\n" + "target_info{job=\"name1\",instance=\"instance1\",host_name=\"example.org\"} 1\n" + "\n" + "# HELP unit_test_total\n" + "# TYPE unit_test_total counter\n" + "unit_test_total{job=\"name1\",instance=\"instance1\"} 62 110000\n" + "\n" + "# collectd/write_prometheus " PACKAGE_VERSION " at example.com\n", + // clang-format on + }, { .name = "multiple resources", .fams =