X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fstatsd.c;h=a0c6e4f3e935117fd9f4c428a64072e7f007bdff;hb=21ab7512825cf8177d5eee5101344b45d0854610;hp=d12cb0d5b83d5677f0ba8125c7d29250e7db3353;hpb=c346dc6b18ee4215ccab911c1732c5c39a961baf;p=collectd.git diff --git a/src/statsd.c b/src/statsd.c index d12cb0d5..a0c6e4f3 100644 --- a/src/statsd.c +++ b/src/statsd.c @@ -29,13 +29,9 @@ #include "common.h" #include "configfile.h" #include "utils_avltree.h" -#include "utils_complain.h" #include "utils_latency.h" -#include - #include -#include #include #include @@ -65,6 +61,7 @@ struct statsd_metric_s { metric_type_t type; double value; + derive_t counter; latency_counter_t *latency; c_avl_tree_t *set; unsigned long updates_num; @@ -89,6 +86,7 @@ static _Bool conf_delete_sets = 0; static double *conf_timer_percentile = NULL; static size_t conf_timer_percentile_num = 0; +static _Bool conf_counter_sum = 0; static _Bool conf_timer_lower = 0; static _Bool conf_timer_upper = 0; static _Bool conf_timer_sum = 0; @@ -126,14 +124,13 @@ static statsd_metric_t *statsd_metric_lookup_unsafe (char const *name, /* {{{ */ return (NULL); } - metric = malloc (sizeof (*metric)); + metric = calloc (1, sizeof (*metric)); if (metric == NULL) { - ERROR ("statsd plugin: malloc failed."); + ERROR ("statsd plugin: calloc failed."); sfree (key_copy); return (NULL); } - memset (metric, 0, sizeof (*metric)); metric->type = type; metric->latency = NULL; @@ -262,6 +259,8 @@ static int statsd_handle_counter (char const *name, /* {{{ */ if (status != 0) return (status); + /* Changes to the counter are added to (statsd_metric_t*)->value. ->counter is + * only updated in statsd_metric_submit_unsafe(). */ return (statsd_metric_add (name, (double) (value.gauge / scale.gauge), STATSD_COUNTER)); } /* }}} int statsd_handle_counter */ @@ -356,7 +355,7 @@ static int statsd_handle_set (char const *name, /* {{{ */ /* Make sure metric->set exists. */ if (metric->set == NULL) - metric->set = c_avl_create ((void *) strcmp); + metric->set = c_avl_create ((int (*) (const void *, const void *)) strcmp); if (metric->set == NULL) { @@ -686,6 +685,8 @@ static int statsd_config (oconfig_item_t *ci) /* {{{ */ cf_util_get_boolean (child, &conf_delete_gauges); else if (strcasecmp ("DeleteSets", child->key) == 0) cf_util_get_boolean (child, &conf_delete_sets); + else if (strcasecmp ("CounterSum", child->key) == 0) + cf_util_get_boolean (child, &conf_counter_sum); else if (strcasecmp ("TimerLower", child->key) == 0) cf_util_get_boolean (child, &conf_timer_lower); else if (strcasecmp ("TimerUpper", child->key) == 0) @@ -708,7 +709,7 @@ static int statsd_init (void) /* {{{ */ { pthread_mutex_lock (&metrics_lock); if (metrics_tree == NULL) - metrics_tree = c_avl_create ((void *) strcmp); + metrics_tree = c_avl_create ((int (*) (const void *, const void *)) strcmp); if (!network_thread_running) { @@ -756,8 +757,7 @@ static int statsd_metric_clear_set_unsafe (statsd_metric_t *metric) /* {{{ */ } /* }}} int statsd_metric_clear_set_unsafe */ /* Must hold metrics_lock when calling this function. */ -static int statsd_metric_submit_unsafe (char const *name, /* {{{ */ - statsd_metric_t const *metric) +static int statsd_metric_submit_unsafe (char const *name, statsd_metric_t *metric) /* {{{ */ { value_t values[1]; value_list_t vl = VALUE_LIST_INIT; @@ -853,17 +853,28 @@ static int statsd_metric_submit_unsafe (char const *name, /* {{{ */ values[0].gauge = (gauge_t) c_avl_size (metric->set); } else { /* STATSD_COUNTER */ - /* - * Expand a single value to two metrics: - * - * - The absolute counter, as a gauge - * - A derived rate for this counter - */ - values[0].derive = (derive_t) metric->value; - plugin_dispatch_values(&vl); + gauge_t delta = nearbyint (metric->value); + + /* Etsy's statsd writes counters as two metrics: a rate and the change since + * the last write. Since collectd does not reset its DERIVE metrics to zero, + * this makes little sense, but we're dispatching a "count" metric here + * anyway - if requested by the user - for compatibility reasons. */ + if (conf_counter_sum) + { + sstrncpy (vl.type, "count", sizeof (vl.type)); + values[0].gauge = delta; + plugin_dispatch_values (&vl); + + /* restore vl.type */ + sstrncpy (vl.type, "derive", sizeof (vl.type)); + } - sstrncpy(vl.type, "gauge", sizeof (vl.type)); - values[0].gauge = (gauge_t) metric->value; + /* Rather than resetting value to zero, subtract delta so we correctly keep + * track of residuals. */ + metric->value -= delta; + metric->counter += (derive_t) delta; + + values[0].derive = metric->counter; } return (plugin_dispatch_values (&vl)); @@ -941,8 +952,6 @@ static int statsd_shutdown (void) /* {{{ */ void *key; void *value; - pthread_mutex_lock (&metrics_lock); - if (network_thread_running) { network_thread_shutdown = 1; @@ -951,6 +960,8 @@ static int statsd_shutdown (void) /* {{{ */ } network_thread_running = 0; + pthread_mutex_lock (&metrics_lock); + while (c_avl_pick (metrics_tree, &key, &value) == 0) { sfree (key);