From: Florian Forster Date: Thu, 14 Dec 2023 11:54:42 +0000 (+0100) Subject: daemon: Add `metric_family_compare` and `label_set_compare`. X-Git-Tag: 6.0.0-rc0~34^2~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e4a294d367989dcda2273e9cb209f4959efd88cb;p=thirdparty%2Fcollectd.git daemon: Add `metric_family_compare` and `label_set_compare`. --- diff --git a/src/daemon/metric.c b/src/daemon/metric.c index 9bc0c6745..276f3e50b 100644 --- a/src/daemon/metric.c +++ b/src/daemon/metric.c @@ -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); +} diff --git a/src/daemon/metric.h b/src/daemon/metric.h index 16289d345..dc422a94c 100644 --- a/src/daemon/metric.h +++ b/src/daemon/metric.h @@ -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