]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
daemon: Add `metric_family_compare` and `label_set_compare`.
authorFlorian Forster <octo@collectd.org>
Thu, 14 Dec 2023 11:54:42 +0000 (12:54 +0100)
committerFlorian Forster <octo@collectd.org>
Mon, 18 Dec 2023 22:28:18 +0000 (23:28 +0100)
src/daemon/metric.c
src/daemon/metric.h

index 9bc0c6745216a9d0e909c4b6208759692a570604..276f3e50b550a3a58e1bc7d187fc800cc5b309ca 100644 (file)
@@ -59,7 +59,7 @@ int value_marshal_text(strbuf_t *buf, value_t v, metric_type_t type) {
   }
 }
 
-static int label_pair_compare(void const *a, void const *b) {
+static int label_name_compare(void const *a, void const *b) {
   return strcmp(((label_pair_t const *)a)->name,
                 ((label_pair_t const *)b)->name);
 }
@@ -79,7 +79,7 @@ static label_pair_t *label_set_read(label_set_t const *labels,
   };
 
   label_pair_t *ret = bsearch(&label, labels->ptr, labels->num,
-                              sizeof(*labels->ptr), label_pair_compare);
+                              sizeof(*labels->ptr), label_name_compare);
   if (ret == NULL) {
     errno = ENOENT;
     return NULL;
@@ -132,7 +132,7 @@ int label_set_add(label_set_t *labels, char const *name, char const *value) {
   labels->ptr[labels->num] = pair;
   labels->num++;
 
-  qsort(labels->ptr, labels->num, sizeof(*labels->ptr), label_pair_compare);
+  qsort(labels->ptr, labels->num, sizeof(*labels->ptr), label_name_compare);
   return 0;
 }
 
@@ -688,3 +688,36 @@ metric_t *metric_parse_identity(char const *buf) {
 
   return fam->metric.ptr;
 }
+
+static int label_pair_compare(label_pair_t a, label_pair_t b) {
+  int cmp = strcmp(a.name, b.name);
+  if (cmp != 0) {
+    return cmp;
+  }
+
+  return strcmp(a.value, b.value);
+}
+
+int label_set_compare(label_set_t a, label_set_t b) {
+  if (a.num != b.num) {
+    return a.num < b.num ? -1 : 1;
+  }
+
+  for (size_t i = 0; i < a.num; i++) {
+    int cmp = label_pair_compare(a.ptr[i], b.ptr[i]);
+    if (cmp != 0) {
+      return cmp;
+    }
+  }
+
+  return 0;
+}
+
+int metric_family_compare(metric_family_t const *a, metric_family_t const *b) {
+  int cmp = strcmp(a->name, b->name);
+  if (cmp != 0) {
+    return cmp;
+  }
+
+  return label_set_compare(a->resource, b->resource);
+}
index 16289d34575909c8084ce0273cf6d9f93f82af8a..dc422a94c70beb78e0a3dd8d95fda1b948674811 100644 (file)
@@ -83,6 +83,15 @@ int label_set_add(label_set_t *labels, char const *name, char const *value);
  * initializes the label set to zero. */
 void label_set_reset(label_set_t *labels);
 
+/* label_set_compare compares two label sets. It returns an integer indicating
+ * the result of the comparison, as follows:
+ *
+ *   - 0, if the a and b are equal;
+ *   - a negative value if a is less than b;
+ *   - a positive value if a is greater than b.
+ */
+int label_set_compare(label_set_t a, label_set_t b);
+
 /*
  * Metric
  */
@@ -190,4 +199,14 @@ void metric_family_free(metric_family_t *fam);
  * metric_family_free(). */
 metric_family_t *metric_family_clone(metric_family_t const *fam);
 
+/* metric_family_compare compares two metric families, taking into account the
+ * metric family name and any resource attributes. It returns an integer
+ * indicating the result of the comparison, as follows:
+ *
+ *   - 0, if the a and b are equal;
+ *   - a negative value if a is less than b;
+ *   - a positive value if a is greater than b.
+ */
+int metric_family_compare(metric_family_t const *a, metric_family_t const *b);
+
 #endif