]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
Added functions distribution_marshal_text, distribution_count_marshal_text and distri...
authorelene-margalit <elene.margalit@gmail.com>
Thu, 27 Aug 2020 16:28:53 +0000 (18:28 +0200)
committerelene-margalit <elene.margalit@gmail.com>
Thu, 27 Aug 2020 16:28:53 +0000 (18:28 +0200)
src/daemon/metric.c
src/daemon/metric.h

index d18f51289a73c44f250029687ec5cf222920cb5f..7e3539eda3e8f7e43e72a0470f5539860eef8add 100644 (file)
@@ -26,6 +26,7 @@
  **/
 
 #include "collectd.h"
+#include "distribution.h"
 #include "metric.h"
 #include "plugin.h"
 
 /* Metric names must match the regex `[a-zA-Z_:][a-zA-Z0-9_:]*` */
 #define VALID_NAME_CHARS VALID_LABEL_CHARS ":"
 
+int distribution_count_marshal_text(strbuf_t *buf, distribution_t *dist) {
+
+  return strbuf_printf(buf, "%" PRIu64, distribution_total_counter(dist));
+}
+
+int distribution_sum_marshal_text(strbuf_t *buf, distribution_t *dist) {
+
+  return strbuf_printf(buf, GAUGE_FORMAT, distribution_total_sum(dist));
+}
+
+int distribution_marshal_text(strbuf_t *buf, distribution_t *dist) {
+
+  buckets_array_t buckets = get_buckets(dist);
+  strbuf_printf(buf, "buckets:\n");
+  for (size_t i = 0; i < buckets.num_buckets; i++) {
+    int status =
+        strbuf_printf(buf, "\"%.2f\":\"%lu\",\n", buckets.buckets[i].maximum,
+                      buckets.buckets[i].bucket_counter);
+    if (status != 0) {
+      return status;
+    }
+  }
+  strbuf_printf(buf, "%" PRIu64, distribution_total_counter(dist));
+  strbuf_printf(buf, GAUGE_FORMAT, distribution_total_sum(dist));
+  return 0;
+}
+
 int value_marshal_text(strbuf_t *buf, value_t v, metric_type_t type) {
   switch (type) {
   case METRIC_TYPE_GAUGE:
@@ -48,6 +76,8 @@ int value_marshal_text(strbuf_t *buf, value_t v, metric_type_t type) {
     return strbuf_printf(buf, GAUGE_FORMAT, v.gauge);
   case METRIC_TYPE_COUNTER:
     return strbuf_printf(buf, "%" PRIu64, v.counter);
+  case METRIC_TYPE_DISTRIBUTION:
+    return distribution_marshal_text(buf, v.distribution);
   default:
     ERROR("Unknown metric value type: %d", (int)type);
     return EINVAL;
index e0366a215b7b8c7944bc01b57fdca83282a76b1b..3167cd9b014bc4f96febb2ab1c1be92735b7ae65 100644 (file)
@@ -59,6 +59,18 @@ typedef union value_u value_t;
 /* value_marshal_text prints a text representation of v to buf. */
 int value_marshal_text(strbuf_t *buf, value_t v, metric_type_t type);
 
+/*distribution_marshal_text prints a text representation of a distribution
+ * metric to buf.*/
+int distribution_marshal_text(strbuf_t *buf, distribution_t *dist);
+
+/*distribution_marshal_text prints the total count of gauges registered in a
+ * distribution metric to buf.*/
+int distribution_count_marshal_text(strbuf_t *buf, distribution_t *dist);
+
+/*distribution_marshal_text prints the sum of all gauges registered in a
+ * distribution metric to buf.*/
+int distribution_sum_marshal_text(strbuf_t *buf, distribution_t *dist);
+
 /*
  * Labels
  */