From: Barbara Kaczorowska Date: Fri, 14 Aug 2020 11:26:03 +0000 (+0000) Subject: Add uc_get_percentile and uc_get_percentile_by_name functions X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a640ec1bf8870fdcb22ee52176ae955ee53e3aaa;p=thirdparty%2Fcollectd.git Add uc_get_percentile and uc_get_percentile_by_name functions --- diff --git a/src/daemon/utils_cache.c b/src/daemon/utils_cache.c index 596de26b6..3e56127c8 100644 --- a/src/daemon/utils_cache.c +++ b/src/daemon/utils_cache.c @@ -354,7 +354,7 @@ static int uc_update_metric(metric_t const *m) { } case METRIC_TYPE_DISTRIBUTION: { - distribution_t *diff = distribution_diff(ce->values_raw.distribution, m->value.distribution); + distribution_t *diff = distribution_sub(ce->values_raw.distribution, m->value.distribution); distribution_destroy(ce->values_distribution); distribution_destroy(ce->values_raw.distribution); ce->values_distribution = diff; @@ -437,6 +437,54 @@ int uc_set_callbacks_mask(const char *name, unsigned long mask) { return 0; } +int uc_get_percentile_by_name(const char *name, gauge_t *ret_values, double percent) { + cache_entry_t *ce = NULL; + int status = 0; + + pthread_mutex_lock(&cache_lock); + + if (c_avl_get(cache_tree, name, (void *)&ce) == 0) { + assert(ce != NULL); + + /* remove missing values from getval */ + if (ce->state == STATE_MISSING) { + DEBUG("utils_cache: uc_get_percentile_by_name: requested metric \"%s\" is in " + "state \"missing\".", + name); + status = -1; + } else { + *ret_values = distribution_percentile(ce->values_distribution, percent); + } + } else { + DEBUG("utils_cache: uc_get_percentile_by_name: No such value: %s", name); + status = -1; + } + + pthread_mutex_unlock(&cache_lock); + + return status; +} /* gauge_t *uc_get_percentile_by_name */ + +int uc_get_percentile(metric_t const *m, gauge_t *ret, double percent) { + if (m->family->type != METRIC_TYPE_DISTRIBUTION) { + ERROR("uc_get_percentile: Don't know how to handle data source type %i.", + m->family->type); + return -1; + } + + strbuf_t buf = STRBUF_CREATE; + int status = metric_identity(&buf, m); + if (status != 0) { + ERROR("uc_get_percentile: metric_identity failed with status %d.", status); + STRBUF_DESTROY(buf); + return status; + } + + status = uc_get_percentile_by_name(buf.ptr, ret, percent); + STRBUF_DESTROY(buf); + return status; +} + int uc_get_rate_by_name(const char *name, gauge_t *ret_values) { cache_entry_t *ce = NULL; int status = 0; diff --git a/src/daemon/utils_cache.h b/src/daemon/utils_cache.h index 17e1ebffc..0e7e506c4 100644 --- a/src/daemon/utils_cache.h +++ b/src/daemon/utils_cache.h @@ -47,7 +47,8 @@ int uc_get_value_by_name_vl(const char *name, value_t **ret_values, size_t *ret_values_num); value_t *uc_get_value_vl(const data_set_t *ds, const value_list_t *vl); -int uc_get_percentile(metric_t const *m, gauge_t *ret_value); +int uc_get_percentile_by_name(const char *name, gauge_t *ret_value, double percent); +int uc_get_percentile(metric_t const *m, gauge_t *ret_value, double percent); int uc_get_rate_by_name(const char *name, gauge_t *ret_value); int uc_get_rate(metric_t const *m, gauge_t *ret_value); int uc_get_value_by_name(const char *name, value_t *ret_value);