]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
Add uc_get_percentile and uc_get_percentile_by_name functions
authorBarbara Kaczorowska <bkjg@google.com>
Fri, 14 Aug 2020 11:26:03 +0000 (11:26 +0000)
committerBarbara Kaczorowska <bkjg@google.com>
Wed, 19 Aug 2020 07:42:07 +0000 (07:42 +0000)
src/daemon/utils_cache.c
src/daemon/utils_cache.h

index 596de26b6f0d2e27a348887045908ea4aa67f3f6..3e56127c804697dd3967a7dea563683ac113f1a7 100644 (file)
@@ -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;
index 17e1ebffc98e2402b3949d6d1414005639e7e0d3..0e7e506c42a0a87d3811abd447cf8457d63e8f2f 100644 (file)
@@ -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);