]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
Extended function format_metric to call the new function format_metric_distribution...
authorelene-margalit <elene.margalit@gmail.com>
Thu, 27 Aug 2020 16:18:15 +0000 (18:18 +0200)
committerelene-margalit <elene.margalit@gmail.com>
Thu, 27 Aug 2020 16:18:15 +0000 (18:18 +0200)
src/utils/format_json/format_json.c

index e759d24b3343c0080664d72a4f4019c4a0f05a75..99bf1811a4a1f090a65bd9f6d709d22821e70763 100644 (file)
@@ -1,3 +1,4 @@
+
 /**
  * collectd - src/utils_format_json.c
  * Copyright (C) 2009-2015  Florian octo Forster
@@ -113,6 +114,41 @@ static int format_time(yajl_gen g, cdtime_t t) /* {{{ */
   return 0;
 } /* }}} int format_time */
 
+static int format_metric_distribution(strbuf_t buf, yajl_gen g,
+                                      metric_t const *m) {
+
+  CHECK(json_add_string(g, "buckets"));
+  CHECK(yajl_gen_map_open(g)); /*Begin Buckets*/
+
+  buckets_array_t buckets = get_buckets(m->value.distribution);
+  for (size_t i = 0; i < buckets.num_buckets; i++) {
+
+    double max = buckets.buckets[i].maximum;
+    char max_char[sizeof(max)];
+    snprintf(max_char, 15, "%.2f", max);
+
+    uint64_t bucket_counter = buckets.buckets[i].bucket_counter;
+    char counter_char[sizeof(bucket_counter)];
+    snprintf(counter_char, 15, "%lu", bucket_counter);
+    CHECK(json_add_string(g, max_char));
+    CHECK(json_add_string(g, counter_char));
+  }
+  CHECK(yajl_gen_map_close(g)); /*End Buckets*/
+
+  distribution_count_marshal_text(&buf, m->value.distribution);
+
+  CHECK(json_add_string(g, "count"));
+  CHECK(json_add_string(g, buf.ptr));
+  strbuf_reset(&buf);
+
+  distribution_sum_marshal_text(&buf, m->value.distribution);
+
+  CHECK(json_add_string(g, "sum"));
+  CHECK(json_add_string(g, buf.ptr));
+  STRBUF_DESTROY(buf);
+  return 0;
+}
+
 /* TODO(octo): format_metric should export the interval, too. */
 /* TODO(octo): Decide whether format_metric should export meta data. */
 static int format_metric(yajl_gen g, metric_t const *m) {
@@ -140,6 +176,14 @@ static int format_metric(yajl_gen g, metric_t const *m) {
   }
 
   strbuf_t buf = STRBUF_CREATE;
+
+  if (m->family != NULL && m->family->type == METRIC_TYPE_DISTRIBUTION) {
+    format_metric_distribution(buf, g, m);
+    STRBUF_DESTROY(buf);
+    CHECK(yajl_gen_map_close(g)); /* END metric */
+    return 0;
+  }
+
   int status = value_marshal_text(&buf, m->value, m->family->type);
   if (status != 0) {
     STRBUF_DESTROY(buf);
@@ -188,6 +232,9 @@ static int json_metric_family(yajl_gen g, metric_family_t const *fam) {
   case METRIC_TYPE_UNTYPED:
     type = "UNTYPED";
     break;
+  case METRIC_TYPE_DISTRIBUTION:
+    type = "DISTRIBUTION";
+    break;
   default:
     ERROR("format_json_metric: Unknown value type: %d", fam->type);
     return EINVAL;
@@ -429,4 +476,4 @@ int format_json_notification(char *buffer, size_t buffer_size, /* {{{ */
   yajl_gen_clear(g);
   yajl_gen_free(g);
   return 0;
-} /* }}} format_json_notification */
+} /* }}} format_json_notification */
\ No newline at end of file