]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
Adjusted distribution_marshal_text function to Prometheus text format of Histograms...
authorelene-margalit <elene.margalit@gmail.com>
Mon, 31 Aug 2020 10:07:22 +0000 (12:07 +0200)
committerelene-margalit <elene.margalit@gmail.com>
Mon, 31 Aug 2020 10:07:22 +0000 (12:07 +0200)
src/daemon/metric.c
src/daemon/metric_test.c

index e604655fb512d34900e226c85138c42021662187..a5da95b79f560fe49e08a498b4b43f1730a61e02 100644 (file)
@@ -52,36 +52,38 @@ int distribution_sum_marshal_text(strbuf_t *buf, distribution_t *dist) {
 }
 
 int distribution_marshal_text(strbuf_t *buf, distribution_t *dist) {
-  buckets_array_t buckets = get_buckets(dist);
-  int status_buckets_heading = strbuf_printf(buf, "\"buckets:\" {\n");
-  if (status_buckets_heading != 0) {
-    return status_buckets_heading;
+  if (dist == NULL) {
+    return EINVAL;
   }
+
+  buckets_array_t buckets = get_buckets(dist);
+
   for (size_t i = 0; i < buckets.num_buckets; i++) {
     if (i < buckets.num_buckets - 1) {
-      int status_buckets =
-          strbuf_printf(buf, "\"%.2f\":\"%lu\",\n", buckets.buckets[i].maximum,
-                        buckets.buckets[i].bucket_counter);
+      int status_buckets = strbuf_printf(buf, "bucket{l=\"%.2f\"} %lu\n",
+                                         buckets.buckets[i].maximum,
+                                         buckets.buckets[i].bucket_counter);
       if (status_buckets != 0) {
         return status_buckets;
       }
     } else {
-      int status_buckets =
-          strbuf_printf(buf, "\"%.2f\":\"%lu\"\n", buckets.buckets[i].maximum,
-                        buckets.buckets[i].bucket_counter);
+      int status_buckets = strbuf_printf(buf, "bucket{l=\"+%.2f\"} %lu\n",
+                                         buckets.buckets[i].maximum,
+                                         buckets.buckets[i].bucket_counter);
       if (status_buckets != 0) {
         return status_buckets;
       }
     }
   }
-  int status_count = strbuf_printf(buf, "},\n\"count\":\"%lu\",\n",
-                                   distribution_total_counter(dist));
+
+  int status_count =
+      strbuf_printf(buf, "sum %.2f\n", distribution_total_sum(dist));
   if (status_count != 0) {
     return status_count;
   }
 
   int status_sum =
-      strbuf_printf(buf, "\"sum\":\"%.2f\n", distribution_total_sum(dist));
+      strbuf_printf(buf, "count %lu\n", distribution_total_counter(dist));
   if (status_sum != 0) {
     return status_sum;
   }
index 953584f831c5076b6bb591ef1543849eb9e15453..f9afc20ecb1241fc609dc19f4779ad2769473991 100644 (file)
@@ -309,39 +309,36 @@ DEF_TEST(distribution_marshal_text) {
   } cases[] = {
       {
           .value.distribution = distribution_new_linear(2, 20),
-          .want = "\"buckets:\" {\n"
-                  "\"20.00\":\"0\",\n"
-                  "\"inf\":\"0\"\n"
-                  "},\n\"count\":\"0\",\n"
-                  "\"sum\":\"0.00\n",
+          .want = "bucket{l=\"20.00\"} 0\n"
+                  "bucket{l=\"+inf\"} 0\n"
+                  "sum 0.00\n"
+                  "count 0\n"
       },
       {
           .value.distribution = distribution_new_exponential(10, 2, 3),
-          .want = "\"buckets:\" {\n"
-                  "\"3.00\":\"0\",\n"
-                  "\"6.00\":\"0\",\n"
-                  "\"12.00\":\"0\",\n"
-                  "\"24.00\":\"0\",\n"
-                  "\"48.00\":\"0\",\n"
-                  "\"96.00\":\"0\",\n"
-                  "\"192.00\":\"0\",\n"
-                  "\"384.00\":\"0\",\n"
-                  "\"768.00\":\"0\",\n"
-                  "\"inf\":\"0\"\n"
-                  "},\n\"count\":\"0\",\n"
-                  "\"sum\":\"0.00\n",
+          .want = "bucket{l=\"3.00\"} 0\n" 
+                  "bucket{l=\"6.00\"} 0\n" 
+                  "bucket{l=\"12.00\"} 0\n" 
+                  "bucket{l=\"24.00\"} 0\n" 
+                  "bucket{l=\"48.00\"} 0\n" 
+                  "bucket{l=\"96.00\"} 0\n" 
+                  "bucket{l=\"192.00\"} 0\n" 
+                  "bucket{l=\"384.00\"} 0\n" 
+                  "bucket{l=\"768.00\"} 0\n" 
+                  "bucket{l=\"+inf\"} 0\n" 
+                  "sum 0.00\n"
+                  "count 0\n"
       },
       {
           .value.distribution =
               distribution_new_custom(4, (double[]){3, 10, 50, 100}),
-          .want = "\"buckets:\" {\n"
-                  "\"3.00\":\"0\",\n"
-                  "\"10.00\":\"0\",\n"
-                  "\"50.00\":\"0\",\n"
-                  "\"100.00\":\"0\",\n"
-                  "\"inf\":\"0\"\n"
-                  "},\n\"count\":\"0\",\n"
-                  "\"sum\":\"0.00\n",
+          .want = "bucket{l=\"3.00\"} 0\n" 
+                  "bucket{l=\"10.00\"} 0\n" 
+                  "bucket{l=\"50.00\"} 0\n" 
+                  "bucket{l=\"100.00\"} 0\n" 
+                  "bucket{l=\"+inf\"} 0\n" 
+                  "sum 0.00\n"
+                  "count 0\n"
       },
   };