X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fstatsd.c;h=1819b3ea21437e28ee0d505b941e84243ffbdd76;hb=95dcf60c822e4ab92c0dd1a7ff6cba73bc55bcd1;hp=0885e234e8863195bb5561074c31a82d619855f6;hpb=dd8429c16bc57f949abb2537e003b76ad88b6f90;p=collectd.git diff --git a/src/statsd.c b/src/statsd.c index 0885e234..1819b3ea 100644 --- a/src/statsd.c +++ b/src/statsd.c @@ -32,10 +32,7 @@ #include "utils_complain.h" #include "utils_latency.h" -#include - #include -#include #include #include @@ -65,6 +62,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 +87,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 +125,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; @@ -195,6 +193,35 @@ static int statsd_metric_add (char const *name, double delta, /* {{{ */ return (0); } /* }}} int statsd_metric_add */ +static void statsd_metric_free (statsd_metric_t *metric) /* {{{ */ +{ + if (metric == NULL) + return; + + if (metric->latency != NULL) + { + latency_counter_destroy (metric->latency); + metric->latency = NULL; + } + + if (metric->set != NULL) + { + void *key; + void *value; + + while (c_avl_pick (metric->set, &key, &value) == 0) + { + sfree (key); + assert (value == NULL); + } + + c_avl_destroy (metric->set); + metric->set = NULL; + } + + sfree (metric); +} /* }}} void statsd_metric_free */ + static int statsd_parse_value (char const *str, value_t *ret_value) /* {{{ */ { char *endptr = NULL; @@ -233,6 +260,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 */ @@ -533,6 +562,7 @@ static int statsd_network_init (struct pollfd **ret_fds, /* {{{ */ if (tmp == NULL) { ERROR ("statsd plugin: realloc failed."); + close (fd); continue; } fds = tmp; @@ -656,6 +686,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) @@ -726,8 +758,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; @@ -753,39 +784,42 @@ static int statsd_metric_submit_unsafe (char const *name, /* {{{ */ else if (metric->type == STATSD_TIMER) { size_t i; + _Bool have_events = (metric->updates_num > 0); - if (metric->updates_num == 0) - return (0); - + /* Make sure all timer metrics share the *same* timestamp. */ vl.time = cdtime (); ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s-average", name); - values[0].gauge = CDTIME_T_TO_DOUBLE ( - latency_counter_get_average (metric->latency)); + values[0].gauge = have_events + ? CDTIME_T_TO_DOUBLE (latency_counter_get_average (metric->latency)) + : NAN; plugin_dispatch_values (&vl); if (conf_timer_lower) { ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s-lower", name); - values[0].gauge = CDTIME_T_TO_DOUBLE ( - latency_counter_get_min (metric->latency)); + values[0].gauge = have_events + ? CDTIME_T_TO_DOUBLE (latency_counter_get_min (metric->latency)) + : NAN; plugin_dispatch_values (&vl); } if (conf_timer_upper) { ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s-upper", name); - values[0].gauge = CDTIME_T_TO_DOUBLE ( - latency_counter_get_max (metric->latency)); + values[0].gauge = have_events + ? CDTIME_T_TO_DOUBLE (latency_counter_get_max (metric->latency)) + : NAN; plugin_dispatch_values (&vl); } if (conf_timer_sum) { ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s-sum", name); - values[0].gauge = CDTIME_T_TO_DOUBLE ( - latency_counter_get_sum (metric->latency)); + values[0].gauge = have_events + ? CDTIME_T_TO_DOUBLE (latency_counter_get_sum (metric->latency)) + : NAN; plugin_dispatch_values (&vl); } @@ -793,9 +827,9 @@ static int statsd_metric_submit_unsafe (char const *name, /* {{{ */ { ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s-percentile-%.0f", name, conf_timer_percentile[i]); - values[0].gauge = CDTIME_T_TO_DOUBLE ( - latency_counter_get_percentile ( - metric->latency, conf_timer_percentile[i])); + values[0].gauge = have_events + ? CDTIME_T_TO_DOUBLE (latency_counter_get_percentile (metric->latency, conf_timer_percentile[i])) + : NAN; plugin_dispatch_values (&vl); } @@ -819,8 +853,30 @@ static int statsd_metric_submit_unsafe (char const *name, /* {{{ */ else values[0].gauge = (gauge_t) c_avl_size (metric->set); } - else - values[0].derive = (derive_t) metric->value; + else { /* STATSD_COUNTER */ + 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)); + } + + /* 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)); } /* }}} int statsd_metric_submit_unsafe */ @@ -882,7 +938,7 @@ static int statsd_read (void) /* {{{ */ } sfree (name); - sfree (metric); + statsd_metric_free (metric); } pthread_mutex_unlock (&metrics_lock); @@ -897,8 +953,6 @@ static int statsd_shutdown (void) /* {{{ */ void *key; void *value; - pthread_mutex_lock (&metrics_lock); - if (network_thread_running) { network_thread_shutdown = 1; @@ -907,10 +961,12 @@ 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); - sfree (value); + statsd_metric_free (value); } c_avl_destroy (metrics_tree); metrics_tree = NULL;