Example of how to use the Stasis message bus
authorDavid M. Lee <dlee@digium.com>
Fri, 26 Apr 2013 20:05:15 +0000 (20:05 +0000)
committerDavid M. Lee <dlee@digium.com>
Fri, 26 Apr 2013 20:05:15 +0000 (20:05 +0000)
In order to get people familiar with the Stasis message bus, it would
be useful to have something of a tutorial. Since I'm not clever enough
to think of some cool integration we could do with Twitter, I settled
for something that might actually be useful.

This patch adds a res_statsd.so module, which implements a basic
statsd[1] client. Statsd is a very simple statistics gathering server,
which can publish its results to a backend graphing engine, like
Graphite[2]. There are several different Statsd server
implementations[3], so you can pick what works best for your
environment.

The actual example of how to use the Stasis message bus is in
res_chan_stats.so. This module demonstrates how to use subscriptions
and the message router by monitoring messages and posting channels
stats to the statsd server.

A wiki page walking through res_chan_stats.so is forthcoming.

 [1]: https://github.com/etsy/statsd/
 [2]: http://graphite.readthedocs.org/en/latest/
 [3]: http://joemiller.me/2011/09/21/list-of-statsd-server-implementations/

Review: https://reviewboard.asterisk.org/r/2460/

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@386624 65c4cc65-6c06-0410-ace0-fbb531ad65f3

configs/statsd.conf.sample [new file with mode: 0644]
include/asterisk/statsd.h [new file with mode: 0644]
include/asterisk/utils.h
res/res_chan_stats.c [new file with mode: 0644]
res/res_statsd.c [new file with mode: 0644]
res/res_statsd.exports.in [new file with mode: 0644]

diff --git a/configs/statsd.conf.sample b/configs/statsd.conf.sample
new file mode 100644 (file)
index 0000000..8060973
--- /dev/null
@@ -0,0 +1,8 @@
+[general]
+;enabled = yes                 ; When set to yes, statsd support is enabled
+;server = 127.0.0.1            ; server[:port] of statsd server to use.
+                               ; If not specified, the port is 8125
+;prefix =                      ; Prefix to prepend to all metrics
+;add_newline = no              ; Append a newline to every event. This is
+                               ; useful if you want to run a fake statsd
+                               ; server using netcat (nc -lu 8125)
diff --git a/include/asterisk/statsd.h b/include/asterisk/statsd.h
new file mode 100644 (file)
index 0000000..8e5e2f9
--- /dev/null
@@ -0,0 +1,85 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * David M. Lee, II <dlee@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+#ifndef _ASTERISK_STATSD_H
+#define _ASTERISK_STATSD_H
+
+/*!
+ * \brief Support for publishing to a statsd server.
+ *
+ * \author David M. Lee, II <dlee@digium.com>
+ * \since 12
+ */
+
+#include "asterisk/optional_api.h"
+
+/*! An instantaneous measurement of a value. */
+#define AST_STATSD_GUAGE "g"
+/*! A change in a value. */
+#define AST_STATSD_COUNTER "c"
+/*! Measure of milliseconds. */
+#define AST_STATSD_TIMER "ms"
+/*! Distribution of values over time. */
+#define AST_STATSD_HISTOGRAM "h"
+/*! Events over time. Sorta like increment-only counters. */
+#define AST_STATSD_METER "m"
+
+/*!
+ * \brief Send a stat to the configured statsd server.
+ *
+ * The is the most flexible function for sending a message to the statsd server,
+ * but also the least easy to use. See ast_statsd_log() or
+ * ast_statsd_log_sample() for a slightly more convenient interface.
+ *
+ * \param metric_name String (UTF-8) name of the metric.
+ * \param type_str Type of metric to send.
+ * \param value Value to send.
+ * \param sample_rate Percentage of samples to send.
+ * \since 12
+ */
+AST_OPTIONAL_API(void, ast_statsd_log_full, (const char *metric_name,
+       const char *metric_type, intmax_t value, double sample_rate), {});
+
+/*!
+ * \brief Send a stat to the configured statsd server.
+ * \param metric_name String (UTF-8) name of the metric.
+ * \param metric_type Type of metric to send.
+ * \param value Value to send.
+ * \since 12
+ */
+AST_OPTIONAL_API(void, ast_statsd_log, (const char *metric_name,
+       const char *metric_type, intmax_t value), {});
+
+/*!
+ * \brief Send a random sampling of a stat to the configured statsd server.
+ *
+ * The type of sampled metrics is always \ref AST_STATSD_COUNTER. The given
+ * \a sample_rate should be a percentage between 0.0 and 1.0. If it's <= 0.0,
+ * then no samples will be sent. If it's >= 1.0, then all samples will be sent.
+ *
+ * \param metric_name String (UTF-8) name of the metric.
+ * \param value Value to send.
+ * \param sample_rate Percentage of samples to send.
+ * \since 12
+ */
+AST_OPTIONAL_API(void, ast_statsd_log_sample, (const char *metric_name,
+               intmax_t value, double sample_rate), {});
+
+
+#endif /* _ASTERISK_STATSD_H */
+
index f93f563..9452dd5 100644 (file)
@@ -458,6 +458,11 @@ char *ast_process_quotes_and_slashes(char *start, char find, char replace_with);
 
 long int ast_random(void);
 
+/*!
+ * \brief Returns a random number between 0.0 and 1.0, inclusive.
+ * \since 12
+ */
+#define ast_random_double() (((double)ast_random()) / RAND_MAX)
 
 /*!
  * \brief free() wrapper
diff --git a/res/res_chan_stats.c b/res/res_chan_stats.c
new file mode 100644 (file)
index 0000000..f5f2267
--- /dev/null
@@ -0,0 +1,188 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * David M. Lee, II <dlee@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \brief Statsd channel stats. Exmaple of how to subscribe to Stasis events.
+ *
+ * This module subscribes to the channel caching topic and issues statsd stats
+ * based on the received messages.
+ *
+ * \author David M. Lee, II <dlee@digium.com>
+ * \since 12
+ */
+
+/*** MODULEINFO
+       <depend>res_statsd</depend>
+       <defaultenabled>no</defaultenabled>
+       <support_level>extended</support_level>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/module.h"
+#include "asterisk/stasis_channels.h"
+#include "asterisk/stasis_message_router.h"
+#include "asterisk/statsd.h"
+#include "asterisk/time.h"
+
+/*! Regular Stasis subscription */
+static struct stasis_subscription *sub;
+/*! Stasis message router */
+static struct stasis_message_router *router;
+
+/*!
+ * \brief Subscription callback for all channel messages.
+ * \param data Data pointer given when creating the subscription.
+ * \param sub This subscription.
+ * \param topic The topic the message was posted to. This is not necessarily the
+ *              topic you subscribed to, since messages may be forwarded between
+ *              topics.
+ * \param message The message itself.
+ */
+static void statsmaker(void *data, struct stasis_subscription *sub,
+       struct stasis_topic *topic, struct stasis_message *message)
+{
+       RAII_VAR(struct ast_str *, metric, NULL, ast_free);
+
+       if (stasis_subscription_final_message(sub, message)) {
+               /* Normally, data points to an object that must be cleaned up.
+                * The final message is an unsubscribe notification that's
+                * guaranteed to be the last message this subscription receives.
+                * This would be a safe place to kick off any needed cleanup.
+                */
+               return;
+       }
+
+       /* For no good reason, count message types */
+       metric = ast_str_create(80);
+       if (metric) {
+               ast_str_set(&metric, 0, "stasis.message.%s",
+                       stasis_message_type_name(stasis_message_type(message)));
+               ast_statsd_log(ast_str_buffer(metric), AST_STATSD_METER, 1);
+       }
+}
+
+/*!
+ * \brief Router callback for \ref stasis_cache_update messages.
+ * \param data Data pointer given when added to router.
+ * \param sub This subscription.
+ * \param topic The topic the message was posted to. This is not necessarily the
+ *              topic you subscribed to, since messages may be forwarded between
+ *              topics.
+ * \param message The message itself.
+ */
+static void updates(void *data, struct stasis_subscription *sub,
+       struct stasis_topic *topic, struct stasis_message *message)
+{
+       /* Since this came from a message router, we know the type of the
+        * message. We can cast the data without checking its type.
+        */
+       struct stasis_cache_update *update = stasis_message_data(message);
+
+       /* We're only interested in channel snapshots, so check the type
+        * of the underlying message.
+        */
+       if (ast_channel_snapshot_type() != update->type) {
+               return;
+       }
+
+       /* There are three types of cache updates.
+        * !old && new -> Initial cache entry
+        * old && new -> Updated cache entry
+        * old && !new -> Cache entry removed.
+        */
+
+       if (!update->old_snapshot && update->new_snapshot) {
+               /* Initial cache entry; count a channel creation */
+               ast_statsd_log("channels.count", AST_STATSD_COUNTER, 1);
+       } else if (update->old_snapshot && !update->new_snapshot) {
+               /* Cache entry removed. Compute the age of the channel and post
+                * that, as well as decrementing the channel count.
+                */
+               struct ast_channel_snapshot *last;
+               int64_t age;
+
+               last = stasis_message_data(update->old_snapshot);
+               age = ast_tvdiff_ms(*stasis_message_timestamp(message),
+                       last->creationtime);
+               ast_statsd_log("channels.calltime", AST_STATSD_TIMER, age);
+
+               /* And decrement the channel count */
+               ast_statsd_log("channels.count", AST_STATSD_COUNTER, -1);
+       }
+}
+
+/*!
+ * \brief Router callback for any message that doesn't otherwise have a route.
+ * \param data Data pointer given when added to router.
+ * \param sub This subscription.
+ * \param topic The topic the message was posted to. This is not necessarily the
+ *              topic you subscribed to, since messages may be forwarded between
+ *              topics.
+ * \param message The message itself.
+ */
+static void default_route(void *data, struct stasis_subscription *sub,
+       struct stasis_topic *topic, struct stasis_message *message)
+{
+       if (stasis_subscription_final_message(sub, message)) {
+               /* Much like with the regular subscription, you may need to
+                * perform some cleanup when done with a message router. You
+                * can look for the final message in the default route.
+                */
+               return;
+       }
+}
+
+static int load_module(void)
+{
+       /* You can create a message router to route messages by type */
+       router = stasis_message_router_create(
+               stasis_caching_get_topic(ast_channel_topic_all_cached()));
+       if (!router) {
+               return AST_MODULE_LOAD_FAILURE;
+       }
+       stasis_message_router_add(router, stasis_cache_update_type(),
+               updates, NULL);
+       stasis_message_router_set_default(router, default_route, NULL);
+
+       /* Or a subscription to receive all of the messages from a topic */
+       sub = stasis_subscribe(ast_channel_topic_all(), statsmaker, NULL);
+       if (!sub) {
+               return AST_MODULE_LOAD_FAILURE;
+       }
+       return AST_MODULE_LOAD_SUCCESS;
+}
+
+static int unload_module(void)
+{
+       stasis_unsubscribe(sub);
+       sub = NULL;
+       stasis_message_router_unsubscribe(router);
+       router = NULL;
+       return 0;
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY,
+       AST_MODFLAG_DEFAULT,
+       "Example of how to use Stasis",
+       .load = load_module,
+       .unload = unload_module,
+       .nonoptreq = "res_statsd"
+       );
diff --git a/res/res_statsd.c b/res/res_statsd.c
new file mode 100644 (file)
index 0000000..12963bf
--- /dev/null
@@ -0,0 +1,326 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * David M. Lee, II <dlee@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \brief Support for publishing to a statsd server.
+ *
+ * \author David M. Lee, II <dlee@digium.com>
+ * \since 12
+ */
+
+/*** MODULEINFO
+       <support_level>extended</support_level>
+ ***/
+
+/*** DOCUMENTATION
+       <configInfo name="res_statsd" language="en_US">
+               <synopsis>Statsd client.</synopsis>
+               <configFile name="statsd.conf">
+                       <configObject name="global">
+                               <synopsis>Global configuration settings</synopsis>
+                               <configOption name="enabled">
+                                       <synopsis>Enable/disable the stasis-http module</synopsis>
+                               </configOption>
+                               <configOption name="server">
+                                       <synopsis>Address of the statsd server</synopsis>
+                               </configOption>
+                               <configOption name="prefix">
+                                       <synopsis>Prefix to prepend to every metric</synopsis>
+                               </configOption>
+                               <configOption name="add_newline">
+                                       <synopsis>Append a newline to every event. This is useful if you want to fake out a server using netcat (nc -lu 8125)</synopsis>
+                               </configOption>
+                       </configObject>
+               </configFile>
+       </configInfo>
+***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/config_options.h"
+#include "asterisk/module.h"
+#include "asterisk/netsock2.h"
+
+#define AST_API_MODULE
+#include "asterisk/statsd.h"
+
+#define DEFAULT_STATSD_PORT 8125
+
+#define MAX_PREFIX 40
+
+/*! Socket for sending statd messages */
+static int socket_fd = -1;
+
+/*! \brief Global configuration options for statsd client. */
+struct conf_global_options {
+       /*! Enabled by default, disabled if false. */
+       int enabled;
+       /*! Disabled by default, appends newlines to all messages when enabled. */
+       int add_newline;
+       /*! Statsd server address[:port]. */
+       struct ast_sockaddr statsd_server;
+       /*! Prefix to put on every stat. */
+       char prefix[MAX_PREFIX + 1];
+};
+
+/*! \brief All configuration options for statsd client. */
+struct conf {
+       /*! The general section configuration options. */
+       struct conf_global_options *global;
+};
+
+/*! \brief Locking container for safe configuration access. */
+static AO2_GLOBAL_OBJ_STATIC(confs);
+
+static void conf_server(const struct conf *cfg, struct ast_sockaddr *addr)
+{
+       *addr = cfg->global->statsd_server;
+       if (ast_sockaddr_port(addr) == 0) {
+               ast_sockaddr_set_port(addr, DEFAULT_STATSD_PORT);
+       }
+}
+
+void AST_OPTIONAL_API_NAME(ast_statsd_log_full)(const char *metric_name,
+       const char *metric_type, intmax_t value, double sample_rate)
+{
+       RAII_VAR(struct conf *, cfg, NULL, ao2_cleanup);
+       RAII_VAR(struct ast_str *, msg, NULL, ast_free);
+       size_t len;
+       struct ast_sockaddr statsd_server;
+
+       if (socket_fd == -1) {
+               return;
+       }
+
+       cfg = ao2_global_obj_ref(confs);
+       conf_server(cfg, &statsd_server);
+
+       /* Rates <= 0.0 never get logged.
+        * Rates >= 1.0 always get logged.
+        * All others leave it to chance.
+        */
+       if (sample_rate <= 0.0 ||
+               (sample_rate < 1.0 && sample_rate < ast_random_double())) {
+               return;
+       }
+
+       cfg = ao2_global_obj_ref(confs);
+
+       msg = ast_str_create(40);
+       if (!msg) {
+               return;
+       }
+
+       if (!ast_strlen_zero(cfg->global->prefix)) {
+               ast_str_append(&msg, 0, "%s.", cfg->global->prefix);
+       }
+
+       ast_str_append(&msg, 0, "%s:%jd|%s", metric_name, value, metric_type);
+
+       if (sample_rate < 1.0) {
+               ast_str_append(&msg, 0, "|@%.2f", sample_rate);
+       }
+
+       if (cfg->global->add_newline) {
+               ast_str_append(&msg, 0, "\n");
+       }
+
+       len = ast_str_strlen(msg);
+
+       ast_debug(6, "send: %s\n", ast_str_buffer(msg));
+       ast_sendto(socket_fd, ast_str_buffer(msg), len, 0, &statsd_server);
+}
+
+void AST_OPTIONAL_API_NAME(ast_statsd_log)(const char *metric_name,
+       const char *metric_type, intmax_t value)
+{
+       ast_statsd_log_full(metric_name, metric_type, value, 1.0);
+}
+
+void AST_OPTIONAL_API_NAME(ast_statsd_log_sample)(const char *metric_name,
+       intmax_t value, double sample_rate)
+{
+       ast_statsd_log_full(metric_name, AST_STATSD_COUNTER, value,
+               sample_rate);
+}
+
+/*! \brief Mapping of the stasis http conf struct's globals to the
+ *         general context in the config file. */
+static struct aco_type global_option = {
+       .type = ACO_GLOBAL,
+       .name = "global",
+       .item_offset = offsetof(struct conf, global),
+       .category = "^general$",
+       .category_match = ACO_WHITELIST
+};
+
+static struct aco_type *global_options[] = ACO_TYPES(&global_option);
+
+/*! \brief Disposes of the stasis http conf object */
+static void conf_destructor(void *obj)
+{
+    struct conf *cfg = obj;
+    ao2_cleanup(cfg->global);
+}
+
+/*! \brief Creates the statis http conf object. */
+static void *conf_alloc(void)
+{
+    struct conf *cfg;
+
+    if (!(cfg = ao2_alloc(sizeof(*cfg), conf_destructor))) {
+        return NULL;
+    }
+
+    if (!(cfg->global = ao2_alloc(sizeof(*cfg->global), NULL))) {
+        ao2_ref(cfg, -1);
+        return NULL;
+    }
+    return cfg;
+}
+
+/*! \brief The conf file that's processed for the module. */
+static struct aco_file conf_file = {
+       /*! The config file name. */
+       .filename = "statsd.conf",
+       /*! The mapping object types to be processed. */
+       .types = ACO_TYPES(&global_option),
+};
+
+CONFIG_INFO_STANDARD(cfg_info, confs, conf_alloc,
+                    .files = ACO_FILES(&conf_file));
+
+/*! \brief Helper function to check if module is enabled. */
+static char is_enabled(void)
+{
+       RAII_VAR(struct conf *, cfg, ao2_global_obj_ref(confs), ao2_cleanup);
+       return cfg->global->enabled;
+}
+
+static int statsd_init(void)
+{
+       RAII_VAR(struct conf *, cfg, ao2_global_obj_ref(confs), ao2_cleanup);
+       char *server;
+       struct ast_sockaddr statsd_server;
+
+       ast_assert(is_enabled());
+
+       ast_debug(3, "Configuring statsd client.\n");
+
+       if (socket_fd == -1) {
+               ast_debug(3, "Creating statsd socket.\n");
+               socket_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+               if (socket_fd == -1) {
+                       perror("Error creating statsd socket");
+                       return -1;
+               }
+       }
+
+       conf_server(cfg, &statsd_server);
+       server = ast_sockaddr_stringify_fmt(&statsd_server,
+               AST_SOCKADDR_STR_DEFAULT);
+       ast_debug(3, "  statsd server = %s.\n", server);
+       ast_debug(3, "  add newline = %s\n", AST_YESNO(cfg->global->add_newline));
+       ast_debug(3, "  prefix = %s\n", cfg->global->prefix);
+
+       return 0;
+}
+
+static void statsd_shutdown(void)
+{
+       ast_debug(3, "Shutting down statsd client.\n");
+       if (socket_fd != -1) {
+               close(socket_fd);
+               socket_fd = -1;
+       }
+}
+
+static int load_module(void)
+{
+       if (aco_info_init(&cfg_info)) {
+               aco_info_destroy(&cfg_info);
+               return AST_MODULE_LOAD_DECLINE;
+       }
+
+       aco_option_register(&cfg_info, "enabled", ACO_EXACT, global_options,
+               "no", OPT_BOOL_T, 1,
+               FLDSET(struct conf_global_options, enabled));
+
+       aco_option_register(&cfg_info, "add_newline", ACO_EXACT, global_options,
+               "no", OPT_BOOL_T, 1,
+               FLDSET(struct conf_global_options, add_newline));
+
+       aco_option_register(&cfg_info, "server", ACO_EXACT, global_options,
+               "127.0.0.1", OPT_SOCKADDR_T, 0,
+               FLDSET(struct conf_global_options, statsd_server));
+
+       aco_option_register(&cfg_info, "prefix", ACO_EXACT, global_options,
+               "", OPT_CHAR_ARRAY_T, 0,
+               CHARFLDSET(struct conf_global_options, prefix));
+
+       if (aco_process_config(&cfg_info, 0)) {
+               aco_info_destroy(&cfg_info);
+               return AST_MODULE_LOAD_DECLINE;
+       }
+
+       if (!is_enabled()) {
+               return AST_MODULE_LOAD_SUCCESS;
+       }
+
+       if (statsd_init() != 0) {
+               return AST_MODULE_LOAD_FAILURE;
+       }
+
+       return AST_MODULE_LOAD_SUCCESS;
+}
+
+static int unload_module(void)
+{
+       statsd_shutdown();
+       aco_info_destroy(&cfg_info);
+       ao2_global_obj_release(confs);
+       return 0;
+}
+
+static int reload_module(void)
+{
+       if (aco_process_config(&cfg_info, 1)) {
+               return AST_MODULE_LOAD_DECLINE;
+       }
+
+       if (is_enabled()) {
+               return statsd_init();
+       } else {
+               statsd_shutdown();
+               return AST_MODULE_LOAD_SUCCESS;
+       }
+}
+
+/* The priority of this module is set to be as low as possible, since it could
+ * be used by any other sort of module.
+ */
+AST_MODULE_INFO(ASTERISK_GPL_KEY,
+       AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER,
+       "Statsd client support",
+       .load = load_module,
+       .unload = unload_module,
+       .reload = reload_module,
+       .load_pri = 0,
+       );
diff --git a/res/res_statsd.exports.in b/res/res_statsd.exports.in
new file mode 100644 (file)
index 0000000..6f02b25
--- /dev/null
@@ -0,0 +1,8 @@
+{
+       global:
+               LINKER_SYMBOL_PREFIX*ast_statsd_log;
+               LINKER_SYMBOL_PREFIX*ast_statsd_log_full;
+               LINKER_SYMBOL_PREFIX*ast_statsd_log_sample;
+       local:
+               *;
+};