From b21feec71f37201c43fb1bf89e1570b2844b7907 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Wed, 31 Jan 2024 08:32:20 +0100 Subject: [PATCH] src/daemon/utils_cache.c: Let `uc_get_rate()` return early when called with a gauge metric. --- src/daemon/utils_cache.c | 8 ++++++++ src/daemon/utils_cache.h | 4 +++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/daemon/utils_cache.c b/src/daemon/utils_cache.c index 78e55b509..057531e15 100644 --- a/src/daemon/utils_cache.c +++ b/src/daemon/utils_cache.c @@ -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) { diff --git a/src/daemon/utils_cache.h b/src/daemon/utils_cache.h index 98225d5d5..5756e6413 100644 --- a/src/daemon/utils_cache.h +++ b/src/daemon/utils_cache.h @@ -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. -- 2.47.3