Asterisk - The Open Source Telephony Project  21.4.1
res_chan_stats.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * David M. Lee, II <dlee@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18 
19 /*!
20  * \brief Statsd channel stats. Exmaple of how to subscribe to Stasis events.
21  *
22  * This module subscribes to the channel caching topic and issues statsd stats
23  * based on the received messages.
24  *
25  * \author David M. Lee, II <dlee@digium.com>
26  * \since 12
27  */
28 
29 /*** MODULEINFO
30  <depend>res_statsd</depend>
31  <defaultenabled>no</defaultenabled>
32  <support_level>extended</support_level>
33  ***/
34 
35 #include "asterisk.h"
36 
37 #include "asterisk/module.h"
38 #include "asterisk/stasis_channels.h"
39 #include "asterisk/stasis_message_router.h"
40 #include "asterisk/statsd.h"
41 #include "asterisk/time.h"
42 
43 /*! Regular Stasis subscription */
44 static struct stasis_subscription *sub;
45 /*! Stasis message router */
46 static struct stasis_message_router *router;
47 
48 /*!
49  * \brief Subscription callback for all channel messages.
50  * \param data Data pointer given when creating the subscription.
51  * \param sub This subscription.
52  * \param message The message itself.
53  */
54 static void statsmaker(void *data, struct stasis_subscription *sub,
55  struct stasis_message *message)
56 {
57  RAII_VAR(struct ast_str *, metric, NULL, ast_free);
58 
59  if (stasis_subscription_final_message(sub, message)) {
60  /* Normally, data points to an object that must be cleaned up.
61  * The final message is an unsubscribe notification that's
62  * guaranteed to be the last message this subscription receives.
63  * This would be a safe place to kick off any needed cleanup.
64  */
65  return;
66  }
67 
68  /* For no good reason, count message types */
69  metric = ast_str_create(80);
70  if (metric) {
71  ast_str_set(&metric, 0, "stasis.message.%s",
73  ast_statsd_log(ast_str_buffer(metric), AST_STATSD_METER, 1);
74  }
75 }
76 
77 /*!
78  * \brief Router callback for \ref ast_channel_snapshot_update messages.
79  * \param data Data pointer given when added to router.
80  * \param sub This subscription.
81  * \param message The message itself.
82  */
83 static void updates(void *data, struct stasis_subscription *sub,
84  struct stasis_message *message)
85 {
86  /* Since this came from a message router, we know the type of the
87  * message. We can cast the data without checking its type.
88  */
89  struct ast_channel_snapshot_update *update = stasis_message_data(message);
90 
91  /* There are three types of channel snapshot updates.
92  * !old && new -> Initial channel creation
93  * old && new -> Updated channel snapshot
94  * old && dead -> Final channel snapshot
95  */
96 
97  if (!update->old_snapshot && update->new_snapshot) {
98  /* Initial channel snapshot; count a channel creation */
99  ast_statsd_log_string("channels.count", AST_STATSD_GAUGE, "+1", 1.0);
100  } else if (update->old_snapshot && ast_test_flag(&update->new_snapshot->flags, AST_FLAG_DEAD)) {
101  /* Channel is gone. Compute the age of the channel and post
102  * that, as well as decrementing the channel count.
103  */
104  int64_t age;
105 
106  age = ast_tvdiff_ms(*stasis_message_timestamp(message),
107  update->new_snapshot->base->creationtime);
108  ast_statsd_log("channels.calltime", AST_STATSD_TIMER, age);
109 
110  /* And decrement the channel count */
111  ast_statsd_log_string("channels.count", AST_STATSD_GAUGE, "-1", 1.0);
112  }
113 }
114 
115 /*!
116  * \brief Router callback for any message that doesn't otherwise have a route.
117  * \param data Data pointer given when added to router.
118  * \param sub This subscription.
119  * \param message The message itself.
120  */
121 static void default_route(void *data, struct stasis_subscription *sub,
122  struct stasis_message *message)
123 {
124  if (stasis_subscription_final_message(sub, message)) {
125  /* Much like with the regular subscription, you may need to
126  * perform some cleanup when done with a message router. You
127  * can look for the final message in the default route.
128  */
129  return;
130  }
131 }
132 
133 static int unload_module(void)
134 {
136  sub = NULL;
138  router = NULL;
139  return 0;
140 }
141 
142 static int load_module(void)
143 {
144  /* You can create a message router to route messages by type */
145  router = stasis_message_router_create(
147  if (!router) {
149  }
151  updates, NULL);
152  stasis_message_router_set_default(router, default_route, NULL);
153 
154  /* Or a subscription to receive all of the messages from a topic */
155  sub = stasis_subscribe(ast_channel_topic_all(), statsmaker, NULL);
156  if (!sub) {
157  unload_module();
159  }
161 }
162 
163 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Example of how to use Stasis",
164  .support_level = AST_MODULE_SUPPORT_EXTENDED,
165  .load = load_module,
166  .unload = unload_module,
167  .requires = "res_statsd"
168 );
const char * stasis_message_type_name(const struct stasis_message_type *type)
Gets the name of a given message type.
struct ast_channel_snapshot_base * base
Asterisk main include file. File version handling, generic pbx functions.
int stasis_message_router_add(struct stasis_message_router *router, struct stasis_message_type *message_type, stasis_subscription_cb callback, void *data)
Add a route to a message router.
Time-related functions and macros.
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
struct stasis_message_type * stasis_message_type(const struct stasis_message *msg)
Get the message type for a stasis_message.
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:107
Structure representing a change of snapshot of channel state.
void stasis_message_router_unsubscribe_and_join(struct stasis_message_router *router)
Unsubscribe the router from the upstream topic, blocking until the final message has been processed...
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:1113
struct stasis_topic * ast_channel_topic_all(void)
A topic which publishes the events for all channels.
const struct timeval * stasis_message_timestamp(const struct stasis_message *msg)
Get the time when a message was created.
Support for dynamic strings.
Definition: strings.h:623
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.
int stasis_subscription_final_message(struct stasis_subscription *sub, struct stasis_message *msg)
Determine whether a message is the final message to be received on a subscription.
Definition: stasis.c:1174
struct stasis_subscription * stasis_unsubscribe_and_join(struct stasis_subscription *subscription)
Cancel a subscription, blocking until the last message is processed.
Definition: stasis.c:1134
struct ast_channel_snapshot * new_snapshot
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
struct stasis_message_type * ast_channel_snapshot_type(void)
Message type for ast_channel_snapshot_update.
struct ast_channel_snapshot * old_snapshot
struct ast_flags flags
int stasis_message_router_set_default(struct stasis_message_router *router, stasis_subscription_cb callback, void *data)
Sets the default route of a router.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Definition: strings.h:659