]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
src/daemon/metric.[ch]: Add metric_family_metric_reset().
authorFlorian Forster <octo@google.com>
Sun, 28 Jun 2020 19:41:47 +0000 (21:41 +0200)
committerFlorian Forster <octo@google.com>
Tue, 21 Jul 2020 15:29:12 +0000 (17:29 +0200)
This and metric_family_metric_append() replace the metric_list_{add,reset}
functions.

src/daemon/metric.c
src/daemon/metric.h

index 1890fbf9c41dc38f783a1cc20a21805006498950..3efa5de600619dd030191040520671c1bf654acd 100644 (file)
@@ -284,7 +284,7 @@ char const *metric_label_get(metric_t const *m, char const *name) {
   return set->value;
 }
 
-int metric_list_add(metric_list_t *metrics, metric_t m) {
+static int metric_list_add(metric_list_t *metrics, metric_t m) {
   if (metrics == NULL) {
     return EINVAL;
   }
@@ -316,6 +316,20 @@ int metric_list_add(metric_list_t *metrics, metric_t m) {
   return 0;
 }
 
+static void metric_list_reset(metric_list_t *metrics) {
+  if (metrics == NULL) {
+    return;
+  }
+
+  for (size_t i = 0; i < metrics->num; i++) {
+    metric_reset(metrics->ptr + i);
+  }
+  free(metrics->ptr);
+
+  metrics->ptr = NULL;
+  metrics->num = 0;
+}
+
 static int metric_list_clone(metric_list_t *dest, metric_list_t src,
                              metric_family_t *fam) {
   if (src.num == 0) {
@@ -349,46 +363,21 @@ static int metric_list_clone(metric_list_t *dest, metric_list_t src,
   return 0;
 }
 
-void metric_list_reset(metric_list_t *metrics) {
-  if (metrics == NULL) {
-    return;
-  }
-
-  for (size_t i = 0; i < metrics->num; i++) {
-    label_set_reset(&metrics->ptr[i].label);
-    meta_data_destroy(metrics->ptr[i].meta);
+int metric_family_metric_append(metric_family_t *fam, metric_t m) {
+  if (fam == NULL) {
+    return EINVAL;
   }
-  free(metrics->ptr);
 
-  metrics->ptr = NULL;
-  metrics->num = 0;
+  m.family = fam;
+  return metric_list_add(&fam->metric, m);
 }
 
-int metric_family_metrics_append(metric_family_t *fam, value_t v,
-                                 label_t const *label, size_t label_num) {
-  if ((fam == NULL) || ((label_num != 0) && (label == NULL))) {
+int metric_family_metric_reset(metric_family_t *fam) {
+  if (fam == NULL) {
     return EINVAL;
   }
 
-  metric_t m = {
-      .family = fam,
-      .value = v,
-  };
-
-  for (size_t i = 0; i < label_num; i++) {
-    int status = label_set_create(&m.label, label[i].name, label[i].value);
-    if (status != 0) {
-      label_set_reset(&m.label);
-      return status;
-    }
-  }
-
-  int status = metric_list_add(&fam->metric, m);
-  if (status != 0) {
-    label_set_reset(&m.label);
-    return status;
-  }
-
+  metric_list_reset(&fam->metric);
   return 0;
 }
 
index 1a6f9b210a364977dc8174689f335f8fe4e1f94b..66d36380d63bc8ed001809d40eadae6db9178bf3 100644 (file)
@@ -133,14 +133,6 @@ typedef struct {
   size_t num;
 } metric_list_t;
 
-/* metric_list_add appends a metric to the metric list. The metric's labels and
- * meta data are copied. */
-int metric_list_add(metric_list_t *metrics, metric_t m);
-
-/* metric_list_reset frees all the metrics in the metric list. It does *not*
- * free the passed "metric_list_t*" itself. */
-void metric_list_reset(metric_list_t *metrics);
-
 /*
  * Metric Family
  */
@@ -153,10 +145,13 @@ struct metric_family_s {
   metric_list_t metric;
 };
 
-/* metric_family_metrics_append appends a new metric to the metric family. This
- * allocates memory which must be freed using metric_family_metrics_reset. */
-int metric_family_metrics_append(metric_family_t *fam, value_t v,
-                                 label_t const *label, size_t label_num);
+/* metric_family_metric_append appends a new metric to the metric family. This
+ * allocates memory which must be freed using metric_family_metric_reset. */
+int metric_family_metric_append(metric_family_t *fam, metric_t m);
+
+/* metric_family_metric_reset frees all metrics in the metric family and
+ * resets the count to zero. */
+int metric_family_metric_reset(metric_family_t *fam);
 
 /* metric_family_free frees a "metric_family_t" that was allocated with
  * metric_family_clone(). */