res_chan_stats.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. * \brief Statsd channel stats. Exmaple of how to subscribe to Stasis events.
  20. *
  21. * This module subscribes to the channel caching topic and issues statsd stats
  22. * based on the received messages.
  23. *
  24. * \author David M. Lee, II <dlee@digium.com>
  25. * \since 12
  26. */
  27. /*** MODULEINFO
  28. <depend>res_statsd</depend>
  29. <defaultenabled>no</defaultenabled>
  30. <support_level>extended</support_level>
  31. ***/
  32. #include "asterisk.h"
  33. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  34. #include "asterisk/module.h"
  35. #include "asterisk/stasis_channels.h"
  36. #include "asterisk/stasis_message_router.h"
  37. #include "asterisk/statsd.h"
  38. #include "asterisk/time.h"
  39. /*! Regular Stasis subscription */
  40. static struct stasis_subscription *sub;
  41. /*! Stasis message router */
  42. static struct stasis_message_router *router;
  43. /*!
  44. * \brief Subscription callback for all channel messages.
  45. * \param data Data pointer given when creating the subscription.
  46. * \param sub This subscription.
  47. * \param topic The topic the message was posted to. This is not necessarily the
  48. * topic you subscribed to, since messages may be forwarded between
  49. * topics.
  50. * \param message The message itself.
  51. */
  52. static void statsmaker(void *data, struct stasis_subscription *sub,
  53. struct stasis_message *message)
  54. {
  55. RAII_VAR(struct ast_str *, metric, NULL, ast_free);
  56. if (stasis_subscription_final_message(sub, message)) {
  57. /* Normally, data points to an object that must be cleaned up.
  58. * The final message is an unsubscribe notification that's
  59. * guaranteed to be the last message this subscription receives.
  60. * This would be a safe place to kick off any needed cleanup.
  61. */
  62. return;
  63. }
  64. /* For no good reason, count message types */
  65. metric = ast_str_create(80);
  66. if (metric) {
  67. ast_str_set(&metric, 0, "stasis.message.%s",
  68. stasis_message_type_name(stasis_message_type(message)));
  69. ast_statsd_log(ast_str_buffer(metric), AST_STATSD_METER, 1);
  70. }
  71. }
  72. /*!
  73. * \brief Router callback for \ref stasis_cache_update messages.
  74. * \param data Data pointer given when added to router.
  75. * \param sub This subscription.
  76. * \param topic The topic the message was posted to. This is not necessarily the
  77. * topic you subscribed to, since messages may be forwarded between
  78. * topics.
  79. * \param message The message itself.
  80. */
  81. static void updates(void *data, struct stasis_subscription *sub,
  82. struct stasis_message *message)
  83. {
  84. /* Since this came from a message router, we know the type of the
  85. * message. We can cast the data without checking its type.
  86. */
  87. struct stasis_cache_update *update = stasis_message_data(message);
  88. /* We're only interested in channel snapshots, so check the type
  89. * of the underlying message.
  90. */
  91. if (ast_channel_snapshot_type() != update->type) {
  92. return;
  93. }
  94. /* There are three types of cache updates.
  95. * !old && new -> Initial cache entry
  96. * old && new -> Updated cache entry
  97. * old && !new -> Cache entry removed.
  98. */
  99. if (!update->old_snapshot && update->new_snapshot) {
  100. /* Initial cache entry; count a channel creation */
  101. ast_statsd_log_string("channels.count", AST_STATSD_GAUGE, "+1", 1.0);
  102. } else if (update->old_snapshot && !update->new_snapshot) {
  103. /* Cache entry removed. Compute the age of the channel and post
  104. * that, as well as decrementing the channel count.
  105. */
  106. struct ast_channel_snapshot *last;
  107. int64_t age;
  108. last = stasis_message_data(update->old_snapshot);
  109. age = ast_tvdiff_ms(*stasis_message_timestamp(message),
  110. last->creationtime);
  111. ast_statsd_log("channels.calltime", AST_STATSD_TIMER, age);
  112. /* And decrement the channel count */
  113. ast_statsd_log_string("channels.count", AST_STATSD_GAUGE, "-1", 1.0);
  114. }
  115. }
  116. /*!
  117. * \brief Router callback for any message that doesn't otherwise have a route.
  118. * \param data Data pointer given when added to router.
  119. * \param sub This subscription.
  120. * \param topic The topic the message was posted to. This is not necessarily the
  121. * topic you subscribed to, since messages may be forwarded between
  122. * topics.
  123. * \param message The message itself.
  124. */
  125. static void default_route(void *data, struct stasis_subscription *sub,
  126. struct stasis_message *message)
  127. {
  128. if (stasis_subscription_final_message(sub, message)) {
  129. /* Much like with the regular subscription, you may need to
  130. * perform some cleanup when done with a message router. You
  131. * can look for the final message in the default route.
  132. */
  133. return;
  134. }
  135. }
  136. static int unload_module(void)
  137. {
  138. stasis_unsubscribe_and_join(sub);
  139. sub = NULL;
  140. stasis_message_router_unsubscribe_and_join(router);
  141. router = NULL;
  142. return 0;
  143. }
  144. static int load_module(void)
  145. {
  146. /* You can create a message router to route messages by type */
  147. router = stasis_message_router_create(
  148. ast_channel_topic_all_cached());
  149. if (!router) {
  150. return AST_MODULE_LOAD_DECLINE;
  151. }
  152. stasis_message_router_add(router, stasis_cache_update_type(),
  153. updates, NULL);
  154. stasis_message_router_set_default(router, default_route, NULL);
  155. /* Or a subscription to receive all of the messages from a topic */
  156. sub = stasis_subscribe(ast_channel_topic_all(), statsmaker, NULL);
  157. if (!sub) {
  158. unload_module();
  159. return AST_MODULE_LOAD_DECLINE;
  160. }
  161. return AST_MODULE_LOAD_SUCCESS;
  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. );