2 * collectd - src/statsd.c
4 * Copyright (C) 2013 Florian octo Forster
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * Florian octo Forster <octo at collectd.org>
25 #include "configfile.h"
26 #include "utils_avltree.h"
27 #include "utils_complain.h"
28 #include "utils_latency.h"
32 #include <sys/types.h>
33 #include <sys/socket.h>
37 /* AIX doesn't have MSG_DONTWAIT */
39 # define MSG_DONTWAIT MSG_NONBLOCK
42 #ifndef STATSD_DEFAULT_NODE
43 # define STATSD_DEFAULT_NODE NULL
46 #ifndef STATSD_DEFAULT_SERVICE
47 # define STATSD_DEFAULT_SERVICE "8125"
57 typedef enum metric_type_e metric_type_t;
59 struct statsd_metric_s
63 latency_counter_t *latency;
65 unsigned long updates_num;
67 typedef struct statsd_metric_s statsd_metric_t;
69 static c_avl_tree_t *metrics_tree = NULL;
70 static pthread_mutex_t metrics_lock = PTHREAD_MUTEX_INITIALIZER;
72 static pthread_t network_thread;
73 static _Bool network_thread_running = 0;
74 static _Bool network_thread_shutdown = 0;
76 static char *conf_node = NULL;
77 static char *conf_service = NULL;
79 static _Bool conf_delete_counters = 0;
80 static _Bool conf_delete_timers = 0;
81 static _Bool conf_delete_gauges = 0;
82 static _Bool conf_delete_sets = 0;
84 static double *conf_timer_percentile = NULL;
85 static size_t conf_timer_percentile_num = 0;
87 static _Bool conf_timer_lower = 0;
88 static _Bool conf_timer_upper = 0;
89 static _Bool conf_timer_sum = 0;
90 static _Bool conf_timer_count = 0;
92 /* Must hold metrics_lock when calling this function. */
93 static statsd_metric_t *statsd_metric_lookup_unsafe (char const *name, /* {{{ */
96 char key[DATA_MAX_NAME_LEN + 2];
98 statsd_metric_t *metric;
103 case STATSD_COUNTER: key[0] = 'c'; break;
104 case STATSD_TIMER: key[0] = 't'; break;
105 case STATSD_GAUGE: key[0] = 'g'; break;
106 case STATSD_SET: key[0] = 's'; break;
107 default: return (NULL);
111 sstrncpy (&key[2], name, sizeof (key) - 2);
113 status = c_avl_get (metrics_tree, key, (void *) &metric);
117 key_copy = strdup (key);
118 if (key_copy == NULL)
120 ERROR ("statsd plugin: strdup failed.");
124 metric = malloc (sizeof (*metric));
127 ERROR ("statsd plugin: malloc failed.");
131 memset (metric, 0, sizeof (*metric));
134 metric->latency = NULL;
137 status = c_avl_insert (metrics_tree, key_copy, metric);
140 ERROR ("statsd plugin: c_avl_insert failed.");
147 } /* }}} statsd_metric_lookup_unsafe */
149 static int statsd_metric_set (char const *name, double value, /* {{{ */
152 statsd_metric_t *metric;
154 pthread_mutex_lock (&metrics_lock);
156 metric = statsd_metric_lookup_unsafe (name, type);
159 pthread_mutex_unlock (&metrics_lock);
163 metric->value = value;
164 metric->updates_num++;
166 pthread_mutex_unlock (&metrics_lock);
169 } /* }}} int statsd_metric_set */
171 static int statsd_metric_add (char const *name, double delta, /* {{{ */
174 statsd_metric_t *metric;
176 pthread_mutex_lock (&metrics_lock);
178 metric = statsd_metric_lookup_unsafe (name, type);
181 pthread_mutex_unlock (&metrics_lock);
185 metric->value += delta;
186 metric->updates_num++;
188 pthread_mutex_unlock (&metrics_lock);
191 } /* }}} int statsd_metric_add */
193 static void statsd_metric_free (statsd_metric_t *metric) /* {{{ */
198 if (metric->latency != NULL)
200 latency_counter_destroy (metric->latency);
201 metric->latency = NULL;
204 if (metric->set != NULL)
209 while (c_avl_pick (metric->set, &key, &value) == 0)
212 assert (value == NULL);
215 c_avl_destroy (metric->set);
220 } /* }}} void statsd_metric_free */
222 static int statsd_parse_value (char const *str, value_t *ret_value) /* {{{ */
226 ret_value->gauge = (gauge_t) strtod (str, &endptr);
227 if ((str == endptr) || ((endptr != NULL) && (*endptr != 0)))
231 } /* }}} int statsd_parse_value */
233 static int statsd_handle_counter (char const *name, /* {{{ */
234 char const *value_str,
241 if ((extra != NULL) && (extra[0] != '@'))
247 status = statsd_parse_value (extra + 1, &scale);
251 if (!isfinite (scale.gauge) || (scale.gauge <= 0.0) || (scale.gauge > 1.0))
256 status = statsd_parse_value (value_str, &value);
260 return (statsd_metric_add (name, (double) (value.gauge / scale.gauge),
262 } /* }}} int statsd_handle_counter */
264 static int statsd_handle_gauge (char const *name, /* {{{ */
265 char const *value_str)
271 status = statsd_parse_value (value_str, &value);
275 if ((value_str[0] == '+') || (value_str[0] == '-'))
276 return (statsd_metric_add (name, (double) value.gauge, STATSD_GAUGE));
278 return (statsd_metric_set (name, (double) value.gauge, STATSD_GAUGE));
279 } /* }}} int statsd_handle_gauge */
281 static int statsd_handle_timer (char const *name, /* {{{ */
282 char const *value_str,
285 statsd_metric_t *metric;
291 if ((extra != NULL) && (extra[0] != '@'))
297 status = statsd_parse_value (extra + 1, &scale);
301 if (!isfinite (scale.gauge) || (scale.gauge <= 0.0) || (scale.gauge > 1.0))
306 status = statsd_parse_value (value_str, &value_ms);
310 value = MS_TO_CDTIME_T (value_ms.gauge / scale.gauge);
312 pthread_mutex_lock (&metrics_lock);
314 metric = statsd_metric_lookup_unsafe (name, STATSD_TIMER);
317 pthread_mutex_unlock (&metrics_lock);
321 if (metric->latency == NULL)
322 metric->latency = latency_counter_create ();
323 if (metric->latency == NULL)
325 pthread_mutex_unlock (&metrics_lock);
329 latency_counter_add (metric->latency, value);
330 metric->updates_num++;
332 pthread_mutex_unlock (&metrics_lock);
334 } /* }}} int statsd_handle_timer */
336 static int statsd_handle_set (char const *name, /* {{{ */
337 char const *set_key_orig)
339 statsd_metric_t *metric = NULL;
343 pthread_mutex_lock (&metrics_lock);
345 metric = statsd_metric_lookup_unsafe (name, STATSD_SET);
348 pthread_mutex_unlock (&metrics_lock);
352 /* Make sure metric->set exists. */
353 if (metric->set == NULL)
354 metric->set = c_avl_create ((void *) strcmp);
356 if (metric->set == NULL)
358 pthread_mutex_unlock (&metrics_lock);
359 ERROR ("statsd plugin: c_avl_create failed.");
363 set_key = strdup (set_key_orig);
366 pthread_mutex_unlock (&metrics_lock);
367 ERROR ("statsd plugin: strdup failed.");
371 status = c_avl_insert (metric->set, set_key, /* value = */ NULL);
374 pthread_mutex_unlock (&metrics_lock);
376 ERROR ("statsd plugin: c_avl_insert (\"%s\") failed with status %i.",
381 else if (status > 0) /* key already exists */
386 metric->updates_num++;
388 pthread_mutex_unlock (&metrics_lock);
390 } /* }}} int statsd_handle_set */
392 static int statsd_parse_line (char *buffer) /* {{{ */
399 type = strchr (name, '|');
405 value = strrchr (name, ':');
411 extra = strchr (type, '|');
418 if (strcmp ("c", type) == 0)
419 return (statsd_handle_counter (name, value, extra));
420 else if (strcmp ("ms", type) == 0)
421 return (statsd_handle_timer (name, value, extra));
423 /* extra is only valid for counters and timers */
427 if (strcmp ("g", type) == 0)
428 return (statsd_handle_gauge (name, value));
429 else if (strcmp ("s", type) == 0)
430 return (statsd_handle_set (name, value));
433 } /* }}} void statsd_parse_line */
435 static void statsd_parse_buffer (char *buffer) /* {{{ */
437 while (buffer != NULL)
443 next = strchr (buffer, '\n');
456 sstrncpy (orig, buffer, sizeof (orig));
458 status = statsd_parse_line (buffer);
460 ERROR ("statsd plugin: Unable to parse line: \"%s\"", orig);
464 } /* }}} void statsd_parse_buffer */
466 static void statsd_network_read (int fd) /* {{{ */
472 status = recv (fd, buffer, sizeof (buffer), /* flags = */ MSG_DONTWAIT);
477 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
480 ERROR ("statsd plugin: recv(2) failed: %s",
481 sstrerror (errno, errbuf, sizeof (errbuf)));
485 buffer_size = (size_t) status;
486 if (buffer_size >= sizeof (buffer))
487 buffer_size = sizeof (buffer) - 1;
488 buffer[buffer_size] = 0;
490 statsd_parse_buffer (buffer);
491 } /* }}} void statsd_network_read */
493 static int statsd_network_init (struct pollfd **ret_fds, /* {{{ */
496 struct pollfd *fds = NULL;
499 struct addrinfo ai_hints;
500 struct addrinfo *ai_list = NULL;
501 struct addrinfo *ai_ptr;
504 char const *node = (conf_node != NULL) ? conf_node : STATSD_DEFAULT_NODE;
505 char const *service = (conf_service != NULL)
506 ? conf_service : STATSD_DEFAULT_SERVICE;
508 memset (&ai_hints, 0, sizeof (ai_hints));
509 ai_hints.ai_flags = AI_PASSIVE;
511 ai_hints.ai_flags |= AI_ADDRCONFIG;
513 ai_hints.ai_family = AF_UNSPEC;
514 ai_hints.ai_socktype = SOCK_DGRAM;
516 status = getaddrinfo (node, service, &ai_hints, &ai_list);
519 ERROR ("statsd plugin: getaddrinfo (\"%s\", \"%s\") failed: %s",
520 node, service, gai_strerror (status));
524 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
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);
536 ERROR ("statsd plugin: socket(2) failed: %s",
537 sstrerror (errno, errbuf, sizeof (errbuf)));
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);
550 ERROR ("statsd plugin: bind(2) failed: %s",
551 sstrerror (errno, errbuf, sizeof (errbuf)));
556 tmp = realloc (fds, sizeof (*fds) * (fds_num + 1));
559 ERROR ("statsd plugin: realloc failed.");
567 memset (tmp, 0, sizeof (*tmp));
569 tmp->events = POLLIN | POLLPRI;
572 freeaddrinfo (ai_list);
576 ERROR ("statsd plugin: Unable to create listening socket for [%s]:%s.",
577 (node != NULL) ? node : "::", service);
582 *ret_fds_num = fds_num;
584 } /* }}} int statsd_network_init */
586 static void *statsd_network_thread (void *args) /* {{{ */
588 struct pollfd *fds = NULL;
593 status = statsd_network_init (&fds, &fds_num);
596 ERROR ("statsd plugin: Unable to open listening sockets.");
597 pthread_exit ((void *) 0);
600 while (!network_thread_shutdown)
602 status = poll (fds, (nfds_t) fds_num, /* timeout = */ -1);
607 if ((errno == EINTR) || (errno == EAGAIN))
610 ERROR ("statsd plugin: poll(2) failed: %s",
611 sstrerror (errno, errbuf, sizeof (errbuf)));
615 for (i = 0; i < fds_num; i++)
617 if ((fds[i].revents & (POLLIN | POLLPRI)) == 0)
620 statsd_network_read (fds[i].fd);
623 } /* while (!network_thread_shutdown) */
626 for (i = 0; i < fds_num; i++)
631 } /* }}} void *statsd_network_thread */
633 static int statsd_config_timer_percentile (oconfig_item_t *ci) /* {{{ */
635 double percent = NAN;
639 status = cf_util_get_double (ci, &percent);
643 if ((percent <= 0.0) || (percent >= 100))
645 ERROR ("statsd plugin: The value for \"%s\" must be between 0 and 100, "
646 "exclusively.", ci->key);
650 tmp = realloc (conf_timer_percentile,
651 sizeof (*conf_timer_percentile) * (conf_timer_percentile_num + 1));
654 ERROR ("statsd plugin: realloc failed.");
657 conf_timer_percentile = tmp;
658 conf_timer_percentile[conf_timer_percentile_num] = percent;
659 conf_timer_percentile_num++;
662 } /* }}} int statsd_config_timer_percentile */
664 static int statsd_config (oconfig_item_t *ci) /* {{{ */
668 for (i = 0; i < ci->children_num; i++)
670 oconfig_item_t *child = ci->children + i;
672 if (strcasecmp ("Host", child->key) == 0)
673 cf_util_get_string (child, &conf_node);
674 else if (strcasecmp ("Port", child->key) == 0)
675 cf_util_get_service (child, &conf_service);
676 else if (strcasecmp ("DeleteCounters", child->key) == 0)
677 cf_util_get_boolean (child, &conf_delete_counters);
678 else if (strcasecmp ("DeleteTimers", child->key) == 0)
679 cf_util_get_boolean (child, &conf_delete_timers);
680 else if (strcasecmp ("DeleteGauges", child->key) == 0)
681 cf_util_get_boolean (child, &conf_delete_gauges);
682 else if (strcasecmp ("DeleteSets", child->key) == 0)
683 cf_util_get_boolean (child, &conf_delete_sets);
684 else if (strcasecmp ("TimerLower", child->key) == 0)
685 cf_util_get_boolean (child, &conf_timer_lower);
686 else if (strcasecmp ("TimerUpper", child->key) == 0)
687 cf_util_get_boolean (child, &conf_timer_upper);
688 else if (strcasecmp ("TimerSum", child->key) == 0)
689 cf_util_get_boolean (child, &conf_timer_sum);
690 else if (strcasecmp ("TimerCount", child->key) == 0)
691 cf_util_get_boolean (child, &conf_timer_count);
692 else if (strcasecmp ("TimerPercentile", child->key) == 0)
693 statsd_config_timer_percentile (child);
695 ERROR ("statsd plugin: The \"%s\" config option is not valid.",
700 } /* }}} int statsd_config */
702 static int statsd_init (void) /* {{{ */
704 pthread_mutex_lock (&metrics_lock);
705 if (metrics_tree == NULL)
706 metrics_tree = c_avl_create ((void *) strcmp);
708 if (!network_thread_running)
712 status = pthread_create (&network_thread,
714 statsd_network_thread,
719 pthread_mutex_unlock (&metrics_lock);
720 ERROR ("statsd plugin: pthread_create failed: %s",
721 sstrerror (errno, errbuf, sizeof (errbuf)));
725 network_thread_running = 1;
727 pthread_mutex_unlock (&metrics_lock);
730 } /* }}} int statsd_init */
732 /* Must hold metrics_lock when calling this function. */
733 static int statsd_metric_clear_set_unsafe (statsd_metric_t *metric) /* {{{ */
738 if ((metric == NULL) || (metric->type != STATSD_SET))
741 if (metric->set == NULL)
744 while (c_avl_pick (metric->set, &key, &value) == 0)
751 } /* }}} int statsd_metric_clear_set_unsafe */
753 /* Must hold metrics_lock when calling this function. */
754 static int statsd_metric_submit_unsafe (char const *name, /* {{{ */
755 statsd_metric_t const *metric)
758 value_list_t vl = VALUE_LIST_INIT;
762 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
763 sstrncpy (vl.plugin, "statsd", sizeof (vl.plugin));
765 if (metric->type == STATSD_GAUGE)
766 sstrncpy (vl.type, "gauge", sizeof (vl.type));
767 else if (metric->type == STATSD_TIMER)
768 sstrncpy (vl.type, "latency", sizeof (vl.type));
769 else if (metric->type == STATSD_SET)
770 sstrncpy (vl.type, "objects", sizeof (vl.type));
771 else /* if (metric->type == STATSD_COUNTER) */
772 sstrncpy (vl.type, "derive", sizeof (vl.type));
774 sstrncpy (vl.type_instance, name, sizeof (vl.type_instance));
776 if (metric->type == STATSD_GAUGE)
777 values[0].gauge = (gauge_t) metric->value;
778 else if (metric->type == STATSD_TIMER)
781 _Bool have_events = (metric->updates_num > 0);
783 /* Make sure all timer metrics share the *same* timestamp. */
786 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
788 values[0].gauge = have_events
789 ? CDTIME_T_TO_DOUBLE (latency_counter_get_average (metric->latency))
791 plugin_dispatch_values (&vl);
793 if (conf_timer_lower) {
794 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
796 values[0].gauge = have_events
797 ? CDTIME_T_TO_DOUBLE (latency_counter_get_min (metric->latency))
799 plugin_dispatch_values (&vl);
802 if (conf_timer_upper) {
803 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
805 values[0].gauge = have_events
806 ? CDTIME_T_TO_DOUBLE (latency_counter_get_max (metric->latency))
808 plugin_dispatch_values (&vl);
811 if (conf_timer_sum) {
812 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
814 values[0].gauge = have_events
815 ? CDTIME_T_TO_DOUBLE (latency_counter_get_sum (metric->latency))
817 plugin_dispatch_values (&vl);
820 for (i = 0; i < conf_timer_percentile_num; i++)
822 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
823 "%s-percentile-%.0f", name, conf_timer_percentile[i]);
824 values[0].gauge = have_events
825 ? CDTIME_T_TO_DOUBLE (latency_counter_get_percentile (metric->latency, conf_timer_percentile[i]))
827 plugin_dispatch_values (&vl);
830 /* Keep this at the end, since vl.type is set to "gauge" here. The
831 * vl.type's above are implicitly set to "latency". */
832 if (conf_timer_count) {
833 sstrncpy (vl.type, "gauge", sizeof (vl.type));
834 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
836 values[0].gauge = latency_counter_get_num (metric->latency);
837 plugin_dispatch_values (&vl);
840 latency_counter_reset (metric->latency);
843 else if (metric->type == STATSD_SET)
845 if (metric->set == NULL)
846 values[0].gauge = 0.0;
848 values[0].gauge = (gauge_t) c_avl_size (metric->set);
850 else { /* STATSD_COUNTER */
852 * Expand a single value to two metrics:
854 * - The absolute counter, as a gauge
855 * - A derived rate for this counter
857 values[0].derive = (derive_t) metric->value;
858 plugin_dispatch_values(&vl);
860 sstrncpy(vl.type, "gauge", sizeof (vl.type));
861 values[0].gauge = (gauge_t) metric->value;
864 return (plugin_dispatch_values (&vl));
865 } /* }}} int statsd_metric_submit_unsafe */
867 static int statsd_read (void) /* {{{ */
869 c_avl_iterator_t *iter;
871 statsd_metric_t *metric;
873 char **to_be_deleted = NULL;
874 size_t to_be_deleted_num = 0;
877 pthread_mutex_lock (&metrics_lock);
879 if (metrics_tree == NULL)
881 pthread_mutex_unlock (&metrics_lock);
885 iter = c_avl_get_iterator (metrics_tree);
886 while (c_avl_iterator_next (iter, (void *) &name, (void *) &metric) == 0)
888 if ((metric->updates_num == 0)
889 && ((conf_delete_counters && (metric->type == STATSD_COUNTER))
890 || (conf_delete_timers && (metric->type == STATSD_TIMER))
891 || (conf_delete_gauges && (metric->type == STATSD_GAUGE))
892 || (conf_delete_sets && (metric->type == STATSD_SET))))
894 DEBUG ("statsd plugin: Deleting metric \"%s\".", name);
895 strarray_add (&to_be_deleted, &to_be_deleted_num, name);
899 /* Names have a prefix, e.g. "c:", which determines the (statsd) type.
900 * Remove this here. */
901 statsd_metric_submit_unsafe (name + 2, metric);
903 /* Reset the metric. */
904 metric->updates_num = 0;
905 if (metric->type == STATSD_SET)
906 statsd_metric_clear_set_unsafe (metric);
908 c_avl_iterator_destroy (iter);
910 for (i = 0; i < to_be_deleted_num; i++)
914 status = c_avl_remove (metrics_tree, to_be_deleted[i],
915 (void *) &name, (void *) &metric);
918 ERROR ("stats plugin: c_avl_remove (\"%s\") failed with status %i.",
919 to_be_deleted[i], status);
924 statsd_metric_free (metric);
927 pthread_mutex_unlock (&metrics_lock);
929 strarray_free (to_be_deleted, to_be_deleted_num);
932 } /* }}} int statsd_read */
934 static int statsd_shutdown (void) /* {{{ */
939 pthread_mutex_lock (&metrics_lock);
941 if (network_thread_running)
943 network_thread_shutdown = 1;
944 pthread_kill (network_thread, SIGTERM);
945 pthread_join (network_thread, /* retval = */ NULL);
947 network_thread_running = 0;
949 while (c_avl_pick (metrics_tree, &key, &value) == 0)
952 statsd_metric_free (value);
954 c_avl_destroy (metrics_tree);
958 sfree (conf_service);
960 pthread_mutex_unlock (&metrics_lock);
963 } /* }}} int statsd_shutdown */
965 void module_register (void)
967 plugin_register_complex_config ("statsd", statsd_config);
968 plugin_register_init ("statsd", statsd_init);
969 plugin_register_read ("statsd", statsd_read);
970 plugin_register_shutdown ("statsd", statsd_shutdown);
973 /* vim: set sw=2 sts=2 et fdm=marker : */