]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
write_prometheus plugin: Fix updating of existing metrics (+test).
authorFlorian Forster <octo@collectd.org>
Tue, 20 Feb 2024 17:19:05 +0000 (18:19 +0100)
committerFlorian Forster <octo@collectd.org>
Tue, 20 Feb 2024 17:19:05 +0000 (18:19 +0100)
src/write_prometheus.c
src/write_prometheus_test.c

index 9cf2f4d833d07a919600fe2a0100770db962f073..f9a2c3bc908f56c3b1a11a23de5e043443428eb1 100644 (file)
@@ -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);
index 53237c840acd23c59eaa91dc12f4f847b4f3ccc8..ba91a61365775a2df408bbc4d130922f7c16908e 100644 (file)
@@ -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 =