1 /**
2 * collectd - src/statsd.c
3 * Copyright (C) 2013 Florian octo Forster
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 */
27 #include "collectd.h"
29 #include "plugin.h"
30 #include "common.h"
31 #include "utils_avltree.h"
32 #include "utils_latency.h"
34 #include <sys/types.h>
35 #include <netdb.h>
36 #include <poll.h>
38 /* AIX doesn't have MSG_DONTWAIT */
39 #ifndef MSG_DONTWAIT
40 # define MSG_DONTWAIT MSG_NONBLOCK
41 #endif
43 #ifndef STATSD_DEFAULT_NODE
44 # define STATSD_DEFAULT_NODE NULL
45 #endif
47 #ifndef STATSD_DEFAULT_SERVICE
48 # define STATSD_DEFAULT_SERVICE "8125"
49 #endif
51 enum metric_type_e
52 {
53 STATSD_COUNTER,
54 STATSD_TIMER,
55 STATSD_GAUGE,
56 STATSD_SET
57 };
58 typedef enum metric_type_e metric_type_t;
60 struct statsd_metric_s
61 {
62 metric_type_t type;
63 double value;
64 derive_t counter;
65 latency_counter_t *latency;
66 c_avl_tree_t *set;
67 unsigned long updates_num;
68 };
69 typedef struct statsd_metric_s statsd_metric_t;
71 static c_avl_tree_t *metrics_tree = NULL;
72 static pthread_mutex_t metrics_lock = PTHREAD_MUTEX_INITIALIZER;
74 static pthread_t network_thread;
75 static _Bool network_thread_running = 0;
76 static _Bool network_thread_shutdown = 0;
78 static char *conf_node = NULL;
79 static char *conf_service = NULL;
81 static _Bool conf_delete_counters = 0;
82 static _Bool conf_delete_timers = 0;
83 static _Bool conf_delete_gauges = 0;
84 static _Bool conf_delete_sets = 0;
86 static double *conf_timer_percentile = NULL;
87 static size_t conf_timer_percentile_num = 0;
89 static _Bool conf_counter_sum = 0;
90 static _Bool conf_timer_lower = 0;
91 static _Bool conf_timer_upper = 0;
92 static _Bool conf_timer_sum = 0;
93 static _Bool conf_timer_count = 0;
95 /* Must hold metrics_lock when calling this function. */
96 static statsd_metric_t *statsd_metric_lookup_unsafe (char const *name, /* {{{ */
97 metric_type_t type)
98 {
99 char key[DATA_MAX_NAME_LEN + 2];
100 char *key_copy;
101 statsd_metric_t *metric;
102 int status;
104 switch (type)
105 {
106 case STATSD_COUNTER: key[0] = 'c'; break;
107 case STATSD_TIMER: key[0] = 't'; break;
108 case STATSD_GAUGE: key[0] = 'g'; break;
109 case STATSD_SET: key[0] = 's'; break;
110 default: return (NULL);
111 }
113 key[1] = ':';
114 sstrncpy (&key[2], name, sizeof (key) - 2);
116 status = c_avl_get (metrics_tree, key, (void *) &metric);
117 if (status == 0)
118 return (metric);
120 key_copy = strdup (key);
121 if (key_copy == NULL)
122 {
123 ERROR ("statsd plugin: strdup failed.");
124 return (NULL);
125 }
127 metric = calloc (1, sizeof (*metric));
128 if (metric == NULL)
129 {
130 ERROR ("statsd plugin: calloc failed.");
131 sfree (key_copy);
132 return (NULL);
133 }
135 metric->type = type;
136 metric->latency = NULL;
137 metric->set = NULL;
139 status = c_avl_insert (metrics_tree, key_copy, metric);
140 if (status != 0)
141 {
142 ERROR ("statsd plugin: c_avl_insert failed.");
143 sfree (key_copy);
144 sfree (metric);
145 return (NULL);
146 }
148 return (metric);
149 } /* }}} statsd_metric_lookup_unsafe */
151 static int statsd_metric_set (char const *name, double value, /* {{{ */
152 metric_type_t type)
153 {
154 statsd_metric_t *metric;
156 pthread_mutex_lock (&metrics_lock);
158 metric = statsd_metric_lookup_unsafe (name, type);
159 if (metric == NULL)
160 {
161 pthread_mutex_unlock (&metrics_lock);
162 return (-1);
163 }
165 metric->value = value;
166 metric->updates_num++;
168 pthread_mutex_unlock (&metrics_lock);
170 return (0);
171 } /* }}} int statsd_metric_set */
173 static int statsd_metric_add (char const *name, double delta, /* {{{ */
174 metric_type_t type)
175 {
176 statsd_metric_t *metric;
178 pthread_mutex_lock (&metrics_lock);
180 metric = statsd_metric_lookup_unsafe (name, type);
181 if (metric == NULL)
182 {
183 pthread_mutex_unlock (&metrics_lock);
184 return (-1);
185 }
187 metric->value += delta;
188 metric->updates_num++;
190 pthread_mutex_unlock (&metrics_lock);
192 return (0);
193 } /* }}} int statsd_metric_add */
195 static void statsd_metric_free (statsd_metric_t *metric) /* {{{ */
196 {
197 if (metric == NULL)
198 return;
200 if (metric->latency != NULL)
201 {
202 latency_counter_destroy (metric->latency);
203 metric->latency = NULL;
204 }
206 if (metric->set != NULL)
207 {
208 void *key;
209 void *value;
211 while (c_avl_pick (metric->set, &key, &value) == 0)
212 {
213 sfree (key);
214 assert (value == NULL);
215 }
217 c_avl_destroy (metric->set);
218 metric->set = NULL;
219 }
221 sfree (metric);
222 } /* }}} void statsd_metric_free */
224 static int statsd_parse_value (char const *str, value_t *ret_value) /* {{{ */
225 {
226 char *endptr = NULL;
228 ret_value->gauge = (gauge_t) strtod (str, &endptr);
229 if ((str == endptr) || ((endptr != NULL) && (*endptr != 0)))
230 return (-1);
232 return (0);
233 } /* }}} int statsd_parse_value */
235 static int statsd_handle_counter (char const *name, /* {{{ */
236 char const *value_str,
237 char const *extra)
238 {
239 value_t value;
240 value_t scale;
241 int status;
243 if ((extra != NULL) && (extra[0] != '@'))
244 return (-1);
246 scale.gauge = 1.0;
247 if (extra != NULL)
248 {
249 status = statsd_parse_value (extra + 1, &scale);
250 if (status != 0)
251 return (status);
253 if (!isfinite (scale.gauge) || (scale.gauge <= 0.0) || (scale.gauge > 1.0))
254 return (-1);
255 }
257 value.gauge = 1.0;
258 status = statsd_parse_value (value_str, &value);
259 if (status != 0)
260 return (status);
262 /* Changes to the counter are added to (statsd_metric_t*)->value. ->counter is
263 * only updated in statsd_metric_submit_unsafe(). */
264 return (statsd_metric_add (name, (double) (value.gauge / scale.gauge),
265 STATSD_COUNTER));
266 } /* }}} int statsd_handle_counter */
268 static int statsd_handle_gauge (char const *name, /* {{{ */
269 char const *value_str)
270 {
271 value_t value;
272 int status;
274 value.gauge = 0;
275 status = statsd_parse_value (value_str, &value);
276 if (status != 0)
277 return (status);
279 if ((value_str[0] == '+') || (value_str[0] == '-'))
280 return (statsd_metric_add (name, (double) value.gauge, STATSD_GAUGE));
281 else
282 return (statsd_metric_set (name, (double) value.gauge, STATSD_GAUGE));
283 } /* }}} int statsd_handle_gauge */
285 static int statsd_handle_timer (char const *name, /* {{{ */
286 char const *value_str,
287 char const *extra)
288 {
289 statsd_metric_t *metric;
290 value_t value_ms;
291 value_t scale;
292 cdtime_t value;
293 int status;
295 if ((extra != NULL) && (extra[0] != '@'))
296 return (-1);
298 scale.gauge = 1.0;
299 if (extra != NULL)
300 {
301 status = statsd_parse_value (extra + 1, &scale);
302 if (status != 0)
303 return (status);
305 if (!isfinite (scale.gauge) || (scale.gauge <= 0.0) || (scale.gauge > 1.0))
306 return (-1);
307 }
309 value_ms.derive = 0;
310 status = statsd_parse_value (value_str, &value_ms);
311 if (status != 0)
312 return (status);
314 value = MS_TO_CDTIME_T (value_ms.gauge / scale.gauge);
316 pthread_mutex_lock (&metrics_lock);
318 metric = statsd_metric_lookup_unsafe (name, STATSD_TIMER);
319 if (metric == NULL)
320 {
321 pthread_mutex_unlock (&metrics_lock);
322 return (-1);
323 }
325 if (metric->latency == NULL)
326 metric->latency = latency_counter_create ();
327 if (metric->latency == NULL)
328 {
329 pthread_mutex_unlock (&metrics_lock);
330 return (-1);
331 }
333 latency_counter_add (metric->latency, value);
334 metric->updates_num++;
336 pthread_mutex_unlock (&metrics_lock);
337 return (0);
338 } /* }}} int statsd_handle_timer */
340 static int statsd_handle_set (char const *name, /* {{{ */
341 char const *set_key_orig)
342 {
343 statsd_metric_t *metric = NULL;
344 char *set_key;
345 int status;
347 pthread_mutex_lock (&metrics_lock);
349 metric = statsd_metric_lookup_unsafe (name, STATSD_SET);
350 if (metric == NULL)
351 {
352 pthread_mutex_unlock (&metrics_lock);
353 return (-1);
354 }
356 /* Make sure metric->set exists. */
357 if (metric->set == NULL)
358 metric->set = c_avl_create ((int (*) (const void *, const void *)) strcmp);
360 if (metric->set == NULL)
361 {
362 pthread_mutex_unlock (&metrics_lock);
363 ERROR ("statsd plugin: c_avl_create failed.");
364 return (-1);
365 }
367 set_key = strdup (set_key_orig);
368 if (set_key == NULL)
369 {
370 pthread_mutex_unlock (&metrics_lock);
371 ERROR ("statsd plugin: strdup failed.");
372 return (-1);
373 }
375 status = c_avl_insert (metric->set, set_key, /* value = */ NULL);
376 if (status < 0)
377 {
378 pthread_mutex_unlock (&metrics_lock);
379 if (status < 0)
380 ERROR ("statsd plugin: c_avl_insert (\"%s\") failed with status %i.",
381 set_key, status);
382 sfree (set_key);
383 return (-1);
384 }
385 else if (status > 0) /* key already exists */
386 {
387 sfree (set_key);
388 }
390 metric->updates_num++;
392 pthread_mutex_unlock (&metrics_lock);
393 return (0);
394 } /* }}} int statsd_handle_set */
396 static int statsd_parse_line (char *buffer) /* {{{ */
397 {
398 char *name = buffer;
399 char *value;
400 char *type;
401 char *extra;
403 type = strchr (name, '|');
404 if (type == NULL)
405 return (-1);
406 *type = 0;
407 type++;
409 value = strrchr (name, ':');
410 if (value == NULL)
411 return (-1);
412 *value = 0;
413 value++;
415 extra = strchr (type, '|');
416 if (extra != NULL)
417 {
418 *extra = 0;
419 extra++;
420 }
422 if (strcmp ("c", type) == 0)
423 return (statsd_handle_counter (name, value, extra));
424 else if (strcmp ("ms", type) == 0)
425 return (statsd_handle_timer (name, value, extra));
427 /* extra is only valid for counters and timers */
428 if (extra != NULL)
429 return (-1);
431 if (strcmp ("g", type) == 0)
432 return (statsd_handle_gauge (name, value));
433 else if (strcmp ("s", type) == 0)
434 return (statsd_handle_set (name, value));
435 else
436 return (-1);
437 } /* }}} void statsd_parse_line */
439 static void statsd_parse_buffer (char *buffer) /* {{{ */
440 {
441 while (buffer != NULL)
442 {
443 char orig[64];
444 char *next;
445 int status;
447 next = strchr (buffer, '\n');
448 if (next != NULL)
449 {
450 *next = 0;
451 next++;
452 }
454 if (*buffer == 0)
455 {
456 buffer = next;
457 continue;
458 }
460 sstrncpy (orig, buffer, sizeof (orig));
462 status = statsd_parse_line (buffer);
463 if (status != 0)
464 ERROR ("statsd plugin: Unable to parse line: \"%s\"", orig);
466 buffer = next;
467 }
468 } /* }}} void statsd_parse_buffer */
470 static void statsd_network_read (int fd) /* {{{ */
471 {
472 char buffer[4096];
473 size_t buffer_size;
474 ssize_t status;
476 status = recv (fd, buffer, sizeof (buffer), /* flags = */ MSG_DONTWAIT);
477 if (status < 0)
478 {
479 char errbuf[1024];
481 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
482 return;
484 ERROR ("statsd plugin: recv(2) failed: %s",
485 sstrerror (errno, errbuf, sizeof (errbuf)));
486 return;
487 }
489 buffer_size = (size_t) status;
490 if (buffer_size >= sizeof (buffer))
491 buffer_size = sizeof (buffer) - 1;
492 buffer[buffer_size] = 0;
494 statsd_parse_buffer (buffer);
495 } /* }}} void statsd_network_read */
497 static int statsd_network_init (struct pollfd **ret_fds, /* {{{ */
498 size_t *ret_fds_num)
499 {
500 struct pollfd *fds = NULL;
501 size_t fds_num = 0;
503 struct addrinfo *ai_list;
504 int status;
506 char const *node = (conf_node != NULL) ? conf_node : STATSD_DEFAULT_NODE;
507 char const *service = (conf_service != NULL)
508 ? conf_service : STATSD_DEFAULT_SERVICE;
510 struct addrinfo ai_hints = {
511 .ai_family = AF_UNSPEC,
512 .ai_flags = AI_PASSIVE | AI_ADDRCONFIG,
513 .ai_socktype = SOCK_DGRAM
514 };
516 status = getaddrinfo (node, service, &ai_hints, &ai_list);
517 if (status != 0)
518 {
519 ERROR ("statsd plugin: getaddrinfo (\"%s\", \"%s\") failed: %s",
520 node, service, gai_strerror (status));
521 return (status);
522 }
524 for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
525 {
526 int fd;
527 struct pollfd *tmp;
529 char dbg_node[NI_MAXHOST];
530 char dbg_service[NI_MAXSERV];
532 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
533 if (fd < 0)
534 {
535 char errbuf[1024];
536 ERROR ("statsd plugin: socket(2) failed: %s",
537 sstrerror (errno, errbuf, sizeof (errbuf)));
538 continue;
539 }
541 getnameinfo (ai_ptr->ai_addr, ai_ptr->ai_addrlen,
542 dbg_node, sizeof (dbg_node), dbg_service, sizeof (dbg_service),
543 NI_DGRAM | NI_NUMERICHOST | NI_NUMERICSERV);
544 DEBUG ("statsd plugin: Trying to bind to [%s]:%s ...", dbg_node, dbg_service);
546 status = bind (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
547 if (status != 0)
548 {
549 char errbuf[1024];
550 ERROR ("statsd plugin: bind(2) failed: %s",
551 sstrerror (errno, errbuf, sizeof (errbuf)));
552 close (fd);
553 continue;
554 }
556 tmp = realloc (fds, sizeof (*fds) * (fds_num + 1));
557 if (tmp == NULL)
558 {
559 ERROR ("statsd plugin: realloc failed.");
560 close (fd);
561 continue;
562 }
563 fds = tmp;
564 tmp = fds + fds_num;
565 fds_num++;
567 memset (tmp, 0, sizeof (*tmp));
568 tmp->fd = fd;
569 tmp->events = POLLIN | POLLPRI;
570 }
572 freeaddrinfo (ai_list);
574 if (fds_num == 0)
575 {
576 ERROR ("statsd plugin: Unable to create listening socket for [%s]:%s.",
577 (node != NULL) ? node : "::", service);
578 return (ENOENT);
579 }
581 *ret_fds = fds;
582 *ret_fds_num = fds_num;
583 return (0);
584 } /* }}} int statsd_network_init */
586 static void *statsd_network_thread (void *args) /* {{{ */
587 {
588 struct pollfd *fds = NULL;
589 size_t fds_num = 0;
590 int status;
592 status = statsd_network_init (&fds, &fds_num);
593 if (status != 0)
594 {
595 ERROR ("statsd plugin: Unable to open listening sockets.");
596 pthread_exit ((void *) 0);
597 }
599 while (!network_thread_shutdown)
600 {
601 status = poll (fds, (nfds_t) fds_num, /* timeout = */ -1);
602 if (status < 0)
603 {
604 char errbuf[1024];
606 if ((errno == EINTR) || (errno == EAGAIN))
607 continue;
609 ERROR ("statsd plugin: poll(2) failed: %s",
610 sstrerror (errno, errbuf, sizeof (errbuf)));
611 break;
612 }
614 for (size_t i = 0; i < fds_num; i++)
615 {
616 if ((fds[i].revents & (POLLIN | POLLPRI)) == 0)
617 continue;
619 statsd_network_read (fds[i].fd);
620 fds[i].revents = 0;
621 }
622 } /* while (!network_thread_shutdown) */
624 /* Clean up */
625 for (size_t i = 0; i < fds_num; i++)
626 close (fds[i].fd);
627 sfree (fds);
629 return ((void *) 0);
630 } /* }}} void *statsd_network_thread */
632 static int statsd_config_timer_percentile (oconfig_item_t *ci) /* {{{ */
633 {
634 double percent = NAN;
635 double *tmp;
636 int status;
638 status = cf_util_get_double (ci, &percent);
639 if (status != 0)
640 return (status);
642 if ((percent <= 0.0) || (percent >= 100))
643 {
644 ERROR ("statsd plugin: The value for \"%s\" must be between 0 and 100, "
645 "exclusively.", ci->key);
646 return (ERANGE);
647 }
649 tmp = realloc (conf_timer_percentile,
650 sizeof (*conf_timer_percentile) * (conf_timer_percentile_num + 1));
651 if (tmp == NULL)
652 {
653 ERROR ("statsd plugin: realloc failed.");
654 return (ENOMEM);
655 }
656 conf_timer_percentile = tmp;
657 conf_timer_percentile[conf_timer_percentile_num] = percent;
658 conf_timer_percentile_num++;
660 return (0);
661 } /* }}} int statsd_config_timer_percentile */
663 static int statsd_config (oconfig_item_t *ci) /* {{{ */
664 {
665 for (int i = 0; i < ci->children_num; i++)
666 {
667 oconfig_item_t *child = ci->children + i;
669 if (strcasecmp ("Host", child->key) == 0)
670 cf_util_get_string (child, &conf_node);
671 else if (strcasecmp ("Port", child->key) == 0)
672 cf_util_get_service (child, &conf_service);
673 else if (strcasecmp ("DeleteCounters", child->key) == 0)
674 cf_util_get_boolean (child, &conf_delete_counters);
675 else if (strcasecmp ("DeleteTimers", child->key) == 0)
676 cf_util_get_boolean (child, &conf_delete_timers);
677 else if (strcasecmp ("DeleteGauges", child->key) == 0)
678 cf_util_get_boolean (child, &conf_delete_gauges);
679 else if (strcasecmp ("DeleteSets", child->key) == 0)
680 cf_util_get_boolean (child, &conf_delete_sets);
681 else if (strcasecmp ("CounterSum", child->key) == 0)
682 cf_util_get_boolean (child, &conf_counter_sum);
683 else if (strcasecmp ("TimerLower", child->key) == 0)
684 cf_util_get_boolean (child, &conf_timer_lower);
685 else if (strcasecmp ("TimerUpper", child->key) == 0)
686 cf_util_get_boolean (child, &conf_timer_upper);
687 else if (strcasecmp ("TimerSum", child->key) == 0)
688 cf_util_get_boolean (child, &conf_timer_sum);
689 else if (strcasecmp ("TimerCount", child->key) == 0)
690 cf_util_get_boolean (child, &conf_timer_count);
691 else if (strcasecmp ("TimerPercentile", child->key) == 0)
692 statsd_config_timer_percentile (child);
693 else
694 ERROR ("statsd plugin: The \"%s\" config option is not valid.",
695 child->key);
696 }
698 return (0);
699 } /* }}} int statsd_config */
701 static int statsd_init (void) /* {{{ */
702 {
703 pthread_mutex_lock (&metrics_lock);
704 if (metrics_tree == NULL)
705 metrics_tree = c_avl_create ((int (*) (const void *, const void *)) strcmp);
707 if (!network_thread_running)
708 {
709 int status;
711 status = pthread_create (&network_thread,
712 /* attr = */ NULL,
713 statsd_network_thread,
714 /* args = */ NULL);
715 if (status != 0)
716 {
717 char errbuf[1024];
718 pthread_mutex_unlock (&metrics_lock);
719 ERROR ("statsd plugin: pthread_create failed: %s",
720 sstrerror (errno, errbuf, sizeof (errbuf)));
721 return (status);
722 }
723 }
724 network_thread_running = 1;
726 pthread_mutex_unlock (&metrics_lock);
728 return (0);
729 } /* }}} int statsd_init */
731 /* Must hold metrics_lock when calling this function. */
732 static int statsd_metric_clear_set_unsafe (statsd_metric_t *metric) /* {{{ */
733 {
734 void *key;
735 void *value;
737 if ((metric == NULL) || (metric->type != STATSD_SET))
738 return (EINVAL);
740 if (metric->set == NULL)
741 return (0);
743 while (c_avl_pick (metric->set, &key, &value) == 0)
744 {
745 sfree (key);
746 sfree (value);
747 }
749 return (0);
750 } /* }}} int statsd_metric_clear_set_unsafe */
752 /* Must hold metrics_lock when calling this function. */
753 static int statsd_metric_submit_unsafe (char const *name, statsd_metric_t *metric) /* {{{ */
754 {
755 value_list_t vl = VALUE_LIST_INIT;
757 vl.values = &(value_t) { .gauge = NAN };
758 vl.values_len = 1;
759 sstrncpy (vl.plugin, "statsd", sizeof (vl.plugin));
761 if (metric->type == STATSD_GAUGE)
762 sstrncpy (vl.type, "gauge", sizeof (vl.type));
763 else if (metric->type == STATSD_TIMER)
764 sstrncpy (vl.type, "latency", sizeof (vl.type));
765 else if (metric->type == STATSD_SET)
766 sstrncpy (vl.type, "objects", sizeof (vl.type));
767 else /* if (metric->type == STATSD_COUNTER) */
768 sstrncpy (vl.type, "derive", sizeof (vl.type));
770 sstrncpy (vl.type_instance, name, sizeof (vl.type_instance));
772 if (metric->type == STATSD_GAUGE)
773 vl.values[0].gauge = (gauge_t) metric->value;
774 else if (metric->type == STATSD_TIMER)
775 {
776 _Bool have_events = (metric->updates_num > 0);
778 /* Make sure all timer metrics share the *same* timestamp. */
779 vl.time = cdtime ();
781 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
782 "%s-average", name);
783 vl.values[0].gauge = have_events
784 ? CDTIME_T_TO_DOUBLE (latency_counter_get_average (metric->latency))
785 : NAN;
786 plugin_dispatch_values (&vl);
788 if (conf_timer_lower) {
789 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
790 "%s-lower", name);
791 vl.values[0].gauge = have_events
792 ? CDTIME_T_TO_DOUBLE (latency_counter_get_min (metric->latency))
793 : NAN;
794 plugin_dispatch_values (&vl);
795 }
797 if (conf_timer_upper) {
798 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
799 "%s-upper", name);
800 vl.values[0].gauge = have_events
801 ? CDTIME_T_TO_DOUBLE (latency_counter_get_max (metric->latency))
802 : NAN;
803 plugin_dispatch_values (&vl);
804 }
806 if (conf_timer_sum) {
807 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
808 "%s-sum", name);
809 vl.values[0].gauge = have_events
810 ? CDTIME_T_TO_DOUBLE (latency_counter_get_sum (metric->latency))
811 : NAN;
812 plugin_dispatch_values (&vl);
813 }
815 for (size_t i = 0; i < conf_timer_percentile_num; i++)
816 {
817 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
818 "%s-percentile-%.0f", name, conf_timer_percentile[i]);
819 vl.values[0].gauge = have_events
820 ? CDTIME_T_TO_DOUBLE (latency_counter_get_percentile (metric->latency, conf_timer_percentile[i]))
821 : NAN;
822 plugin_dispatch_values (&vl);
823 }
825 /* Keep this at the end, since vl.type is set to "gauge" here. The
826 * vl.type's above are implicitly set to "latency". */
827 if (conf_timer_count) {
828 sstrncpy (vl.type, "gauge", sizeof (vl.type));
829 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
830 "%s-count", name);
831 vl.values[0].gauge = latency_counter_get_num (metric->latency);
832 plugin_dispatch_values (&vl);
833 }
835 latency_counter_reset (metric->latency);
836 return (0);
837 }
838 else if (metric->type == STATSD_SET)
839 {
840 if (metric->set == NULL)
841 vl.values[0].gauge = 0.0;
842 else
843 vl.values[0].gauge = (gauge_t) c_avl_size (metric->set);
844 }
845 else { /* STATSD_COUNTER */
846 gauge_t delta = nearbyint (metric->value);
848 /* Etsy's statsd writes counters as two metrics: a rate and the change since
849 * the last write. Since collectd does not reset its DERIVE metrics to zero,
850 * this makes little sense, but we're dispatching a "count" metric here
851 * anyway - if requested by the user - for compatibility reasons. */
852 if (conf_counter_sum)
853 {
854 sstrncpy (vl.type, "count", sizeof (vl.type));
855 vl.values[0].gauge = delta;
856 plugin_dispatch_values (&vl);
858 /* restore vl.type */
859 sstrncpy (vl.type, "derive", sizeof (vl.type));
860 }
862 /* Rather than resetting value to zero, subtract delta so we correctly keep
863 * track of residuals. */
864 metric->value -= delta;
865 metric->counter += (derive_t) delta;
867 vl.values[0].derive = metric->counter;
868 }
870 return (plugin_dispatch_values (&vl));
871 } /* }}} int statsd_metric_submit_unsafe */
873 static int statsd_read (void) /* {{{ */
874 {
875 c_avl_iterator_t *iter;
876 char *name;
877 statsd_metric_t *metric;
879 char **to_be_deleted = NULL;
880 size_t to_be_deleted_num = 0;
882 pthread_mutex_lock (&metrics_lock);
884 if (metrics_tree == NULL)
885 {
886 pthread_mutex_unlock (&metrics_lock);
887 return (0);
888 }
890 iter = c_avl_get_iterator (metrics_tree);
891 while (c_avl_iterator_next (iter, (void *) &name, (void *) &metric) == 0)
892 {
893 if ((metric->updates_num == 0)
894 && ((conf_delete_counters && (metric->type == STATSD_COUNTER))
895 || (conf_delete_timers && (metric->type == STATSD_TIMER))
896 || (conf_delete_gauges && (metric->type == STATSD_GAUGE))
897 || (conf_delete_sets && (metric->type == STATSD_SET))))
898 {
899 DEBUG ("statsd plugin: Deleting metric \"%s\".", name);
900 strarray_add (&to_be_deleted, &to_be_deleted_num, name);
901 continue;
902 }
904 /* Names have a prefix, e.g. "c:", which determines the (statsd) type.
905 * Remove this here. */
906 statsd_metric_submit_unsafe (name + 2, metric);
908 /* Reset the metric. */
909 metric->updates_num = 0;
910 if (metric->type == STATSD_SET)
911 statsd_metric_clear_set_unsafe (metric);
912 }
913 c_avl_iterator_destroy (iter);
915 for (size_t i = 0; i < to_be_deleted_num; i++)
916 {
917 int status;
919 status = c_avl_remove (metrics_tree, to_be_deleted[i],
920 (void *) &name, (void *) &metric);
921 if (status != 0)
922 {
923 ERROR ("stats plugin: c_avl_remove (\"%s\") failed with status %i.",
924 to_be_deleted[i], status);
925 continue;
926 }
928 sfree (name);
929 statsd_metric_free (metric);
930 }
932 pthread_mutex_unlock (&metrics_lock);
934 strarray_free (to_be_deleted, to_be_deleted_num);
936 return (0);
937 } /* }}} int statsd_read */
939 static int statsd_shutdown (void) /* {{{ */
940 {
941 void *key;
942 void *value;
944 if (network_thread_running)
945 {
946 network_thread_shutdown = 1;
947 pthread_kill (network_thread, SIGTERM);
948 pthread_join (network_thread, /* retval = */ NULL);
949 }
950 network_thread_running = 0;
952 pthread_mutex_lock (&metrics_lock);
954 while (c_avl_pick (metrics_tree, &key, &value) == 0)
955 {
956 sfree (key);
957 statsd_metric_free (value);
958 }
959 c_avl_destroy (metrics_tree);
960 metrics_tree = NULL;
962 sfree (conf_node);
963 sfree (conf_service);
965 pthread_mutex_unlock (&metrics_lock);
967 return (0);
968 } /* }}} int statsd_shutdown */
970 void module_register (void)
971 {
972 plugin_register_complex_config ("statsd", statsd_config);
973 plugin_register_init ("statsd", statsd_init);
974 plugin_register_read ("statsd", statsd_read);
975 plugin_register_shutdown ("statsd", statsd_shutdown);
976 }
978 /* vim: set sw=2 sts=2 et fdm=marker : */