res_chan_stats.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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("channels.count", AST_STATSD_COUNTER, 1);
  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("channels.count", AST_STATSD_COUNTER, -1);
  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 load_module(void)
  137. {
  138. /* You can create a message router to route messages by type */
  139. router = stasis_message_router_create(
  140. ast_channel_topic_all_cached());
  141. if (!router) {
  142. return AST_MODULE_LOAD_FAILURE;
  143. }
  144. stasis_message_router_add(router, stasis_cache_update_type(),
  145. updates, NULL);
  146. stasis_message_router_set_default(router, default_route, NULL);
  147. /* Or a subscription to receive all of the messages from a topic */
  148. sub = stasis_subscribe(ast_channel_topic_all(), statsmaker, NULL);
  149. if (!sub) {
  150. return AST_MODULE_LOAD_FAILURE;
  151. }
  152. return AST_MODULE_LOAD_SUCCESS;
  153. }
  154. static int unload_module(void)
  155. {
  156. stasis_unsubscribe_and_join(sub);
  157. sub = NULL;
  158. stasis_message_router_unsubscribe_and_join(router);
  159. router = NULL;
  160. return 0;
  161. }
  162. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Example of how to use Stasis",
  163. .support_level = AST_MODULE_SUPPORT_EXTENDED,
  164. .load = load_module,
  165. .unload = unload_module,
  166. .nonoptreq = "res_statsd"
  167. );