Asterisk - The Open Source Telephony Project  18.5.0
Functions | Variables
res_chan_stats.c File Reference
#include "asterisk.h"
#include "asterisk/module.h"
#include "asterisk/stasis_channels.h"
#include "asterisk/stasis_message_router.h"
#include "asterisk/statsd.h"
#include "asterisk/time.h"
Include dependency graph for res_chan_stats.c:

Go to the source code of this file.

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static void default_route (void *data, struct stasis_subscription *sub, struct stasis_message *message)
 Router callback for any message that doesn't otherwise have a route. More...
 
static int load_module (void)
 
static void statsmaker (void *data, struct stasis_subscription *sub, struct stasis_message *message)
 Subscription callback for all channel messages. More...
 
static int unload_module (void)
 
static void updates (void *data, struct stasis_subscription *sub, struct stasis_message *message)
 Router callback for ast_channel_snapshot_update messages. More...
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Example of how to use Stasis" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_EXTENDED, .load = load_module, .unload = unload_module, .requires = "res_statsd" }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct stasis_message_routerrouter
 
static struct stasis_subscriptionsub
 Statsd channel stats. Exmaple of how to subscribe to Stasis events. More...
 

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 177 of file res_chan_stats.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 177 of file res_chan_stats.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 177 of file res_chan_stats.c.

◆ default_route()

static void default_route ( void *  data,
struct stasis_subscription sub,
struct stasis_message message 
)
static

Router callback for any message that doesn't otherwise have a route.

Parameters
dataData pointer given when added to router.
subThis subscription.
topicThe topic the message was posted to. This is not necessarily the topic you subscribed to, since messages may be forwarded between topics.
messageThe message itself.

Definition at line 130 of file res_chan_stats.c.

References stasis_subscription_final_message().

Referenced by load_module().

132 {
133  if (stasis_subscription_final_message(sub, message)) {
134  /* Much like with the regular subscription, you may need to
135  * perform some cleanup when done with a message router. You
136  * can look for the final message in the default route.
137  */
138  return;
139  }
140 }
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:1176

◆ load_module()

static int load_module ( void  )
static

Definition at line 151 of file res_chan_stats.c.

References ast_channel_snapshot_type(), ast_channel_topic_all(), AST_MODFLAG_DEFAULT, AST_MODULE_INFO(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, AST_MODULE_SUPPORT_EXTENDED, ASTERISK_GPL_KEY, default_route(), NULL, stasis_message_router_add(), stasis_message_router_create, stasis_message_router_set_default(), stasis_subscribe, statsmaker(), unload_module(), and updates().

152 {
153  /* You can create a message router to route messages by type */
156  if (!router) {
158  }
160  updates, NULL);
162 
163  /* Or a subscription to receive all of the messages from a topic */
165  if (!sub) {
166  unload_module();
168  }
170 }
static int unload_module(void)
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.
#define NULL
Definition: resample.c:96
struct stasis_topic * ast_channel_topic_all(void)
A topic which publishes the events for all channels.
static void default_route(void *data, struct stasis_subscription *sub, struct stasis_message *message)
Router callback for any message that doesn't otherwise have a route.
static struct stasis_subscription * sub
Statsd channel stats. Exmaple of how to subscribe to Stasis events.
static void updates(void *data, struct stasis_subscription *sub, struct stasis_message *message)
Router callback for ast_channel_snapshot_update messages.
static struct stasis_message_router * router
#define stasis_message_router_create(topic)
static void statsmaker(void *data, struct stasis_subscription *sub, struct stasis_message *message)
Subscription callback for all channel messages.
#define stasis_subscribe(topic, callback, data)
Definition: stasis.h:652
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
int stasis_message_router_set_default(struct stasis_message_router *router, stasis_subscription_cb callback, void *data)
Sets the default route of a router.
struct stasis_message_type * ast_channel_snapshot_type(void)
Message type for ast_channel_snapshot_update.

◆ statsmaker()

static void statsmaker ( void *  data,
struct stasis_subscription sub,
struct stasis_message message 
)
static

Subscription callback for all channel messages.

Parameters
dataData pointer given when creating the subscription.
subThis subscription.
topicThe topic the message was posted to. This is not necessarily the topic you subscribed to, since messages may be forwarded between topics.
messageThe message itself.

Definition at line 57 of file res_chan_stats.c.

References ast_free, ast_statsd_log(), AST_STATSD_METER, ast_str_buffer(), ast_str_create, ast_str_set(), NULL, RAII_VAR, stasis_message_type(), stasis_message_type_name(), and stasis_subscription_final_message().

Referenced by load_module().

59 {
60  RAII_VAR(struct ast_str *, metric, NULL, ast_free);
61 
62  if (stasis_subscription_final_message(sub, message)) {
63  /* Normally, data points to an object that must be cleaned up.
64  * The final message is an unsubscribe notification that's
65  * guaranteed to be the last message this subscription receives.
66  * This would be a safe place to kick off any needed cleanup.
67  */
68  return;
69  }
70 
71  /* For no good reason, count message types */
72  metric = ast_str_create(80);
73  if (metric) {
74  ast_str_set(&metric, 0, "stasis.message.%s",
77  }
78 }
const char * stasis_message_type_name(const struct stasis_message_type *type)
Gets the name of a given message type.
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
struct stasis_message_type * stasis_message_type(const struct stasis_message *msg)
Get the message type for a stasis_message.
#define AST_STATSD_METER
Definition: statsd.h:45
#define NULL
Definition: resample.c:96
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:1065
#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:911
void ast_statsd_log(const char *metric_name, const char *metric_type, intmax_t value)
Send a stat to the configured statsd server.
Definition: res_statsd.c:222
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
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:1176
#define ast_free(a)
Definition: astmm.h:182
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Definition: strings.h:620

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 142 of file res_chan_stats.c.

References NULL, stasis_message_router_unsubscribe_and_join(), and stasis_unsubscribe_and_join().

Referenced by load_module().

143 {
145  sub = NULL;
147  router = NULL;
148  return 0;
149 }
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...
#define NULL
Definition: resample.c:96
static struct stasis_subscription * sub
Statsd channel stats. Exmaple of how to subscribe to Stasis events.
static struct stasis_message_router * router
struct stasis_subscription * stasis_unsubscribe_and_join(struct stasis_subscription *subscription)
Cancel a subscription, blocking until the last message is processed.
Definition: stasis.c:1136

◆ updates()

static void updates ( void *  data,
struct stasis_subscription sub,
struct stasis_message message 
)
static

Router callback for ast_channel_snapshot_update messages.

Parameters
dataData pointer given when added to router.
subThis subscription.
topicThe topic the message was posted to. This is not necessarily the topic you subscribed to, since messages may be forwarded between topics.
messageThe message itself.

Definition at line 89 of file res_chan_stats.c.

References AST_FLAG_DEAD, AST_STATSD_GAUGE, ast_statsd_log(), ast_statsd_log_string(), AST_STATSD_TIMER, ast_test_flag, ast_tvdiff_ms(), ast_channel_snapshot::base, ast_channel_snapshot_base::creationtime, ast_channel_snapshot::flags, ast_channel_snapshot_update::new_snapshot, ast_channel_snapshot_update::old_snapshot, stasis_message_data(), stasis_message_timestamp(), and update().

Referenced by load_module().

91 {
92  /* Since this came from a message router, we know the type of the
93  * message. We can cast the data without checking its type.
94  */
96 
97  /* There are three types of channel snapshot updates.
98  * !old && new -> Initial channel creation
99  * old && new -> Updated channel snapshot
100  * old && dead -> Final channel snapshot
101  */
102 
103  if (!update->old_snapshot && update->new_snapshot) {
104  /* Initial channel snapshot; count a channel creation */
105  ast_statsd_log_string("channels.count", AST_STATSD_GAUGE, "+1", 1.0);
106  } else if (update->old_snapshot && ast_test_flag(&update->new_snapshot->flags, AST_FLAG_DEAD)) {
107  /* Channel is gone. Compute the age of the channel and post
108  * that, as well as decrementing the channel count.
109  */
110  int64_t age;
111 
112  age = ast_tvdiff_ms(*stasis_message_timestamp(message),
113  update->new_snapshot->base->creationtime);
114  ast_statsd_log("channels.calltime", AST_STATSD_TIMER, age);
115 
116  /* And decrement the channel count */
117  ast_statsd_log_string("channels.count", AST_STATSD_GAUGE, "-1", 1.0);
118  }
119 }
struct ast_channel_snapshot_base * base
#define ast_test_flag(p, flag)
Definition: utils.h:63
static void update(int code_size, int y, int wi, int fi, int dq, int sr, int dqsez, struct g726_state *state_ptr)
Definition: codec_g726.c:367
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:98
Structure representing a change of snapshot of channel state.
void ast_statsd_log_string(const char *metric_name, const char *metric_type, const char *value, double sample_rate)
Send a stat to the configured statsd server.
Definition: res_statsd.c:111
const struct timeval * stasis_message_timestamp(const struct stasis_message *msg)
Get the time when a message was created.
void ast_statsd_log(const char *metric_name, const char *metric_type, intmax_t value)
Send a stat to the configured statsd server.
Definition: res_statsd.c:222
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.
#define AST_STATSD_TIMER
Definition: statsd.h:41
struct ast_channel_snapshot * new_snapshot
struct ast_channel_snapshot * old_snapshot
#define AST_STATSD_GAUGE
Support for publishing to a statsd server.
Definition: statsd.h:32
struct ast_flags flags

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Example of how to use Stasis" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_EXTENDED, .load = load_module, .unload = unload_module, .requires = "res_statsd" }
static

Definition at line 177 of file res_chan_stats.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 177 of file res_chan_stats.c.

◆ router

struct stasis_message_router* router
static

Stasis message router

Definition at line 46 of file res_chan_stats.c.

◆ sub

struct stasis_subscription* sub
static

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@.nosp@m.digi.nosp@m.um.co.nosp@m.m
Since
12

Regular Stasis subscription

Definition at line 44 of file res_chan_stats.c.