]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
src/daemon/utils_cache.c: Let `uc_get_rate()` return early when called with a gauge...
authorFlorian Forster <octo@collectd.org>
Wed, 31 Jan 2024 07:32:20 +0000 (08:32 +0100)
committerFlorian Forster <octo@collectd.org>
Wed, 31 Jan 2024 09:56:18 +0000 (10:56 +0100)
src/daemon/utils_cache.c
src/daemon/utils_cache.h

index 78e55b509c7c05c34f43c951d533ba2f391ad38b..057531e1501a9768036ee3f7f27a5a9c58554441 100644 (file)
@@ -465,6 +465,14 @@ int uc_get_rate_by_name(const char *name, gauge_t *ret_values) {
 } /* gauge_t *uc_get_rate_by_name */
 
 int uc_get_rate(metric_t const *m, gauge_t *ret) {
+  if (m == NULL || m->family == NULL || ret == NULL) {
+    return EINVAL;
+  }
+  if (m->family->type == METRIC_TYPE_GAUGE) {
+    *ret = m->value.gauge;
+    return 0;
+  }
+
   strbuf_t buf = STRBUF_CREATE;
   int status = metric_identity(&buf, m);
   if (status != 0) {
index 98225d5d56d1b62dcc2cb613f1bd009ebd83616b..5756e641347eded1b2475545c23a31ef9e43ffcd 100644 (file)
@@ -59,7 +59,9 @@ int uc_get_rate_by_name(const char *name, gauge_t *ret_value);
  * occurred by comparing the time returned by `uc_first_metric()` with the
  * metric time: in an overflow/reset situation, the values are equal.
  *
- * For non-cumulative types (gauge), the last value is returned in `ret_value`.
+ * For non-cumulative types (gauge), the function takes a short cut and returns
+ * `m->value.gauge` in `ret_value`. Since this is a fast operation, plugin
+ * authors are discouraged from writing special cases for gauge metrics.
  *
  * Returns zero on success, ENOENT if the metric is not in the cache, and
  * EAGAIN if the metric has state STATE_MISSING.