res_chan_stats.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. #include "asterisk/module.h"
  34. #include "asterisk/stasis_channels.h"
  35. #include "asterisk/stasis_message_router.h"
  36. #include "asterisk/statsd.h"
  37. #include "asterisk/time.h"
  38. /*! Regular Stasis subscription */
  39. static struct stasis_subscription *sub;
  40. /*! Stasis message router */
  41. static struct stasis_message_router *router;
  42. /*!
  43. * \brief Subscription callback for all channel messages.
  44. * \param data Data pointer given when creating the subscription.
  45. * \param sub This subscription.
  46. * \param message The message itself.
  47. */
  48. static void statsmaker(void *data, struct stasis_subscription *sub,
  49. struct stasis_message *message)
  50. {
  51. RAII_VAR(struct ast_str *, metric, NULL, ast_free);
  52. if (stasis_subscription_final_message(sub, message)) {
  53. /* Normally, data points to an object that must be cleaned up.
  54. * The final message is an unsubscribe notification that's
  55. * guaranteed to be the last message this subscription receives.
  56. * This would be a safe place to kick off any needed cleanup.
  57. */
  58. return;
  59. }
  60. /* For no good reason, count message types */
  61. metric = ast_str_create(80);
  62. if (metric) {
  63. ast_str_set(&metric, 0, "stasis.message.%s",
  64. stasis_message_type_name(stasis_message_type(message)));
  65. ast_statsd_log(ast_str_buffer(metric), AST_STATSD_METER, 1);
  66. }
  67. }
  68. /*!
  69. * \brief Router callback for \ref ast_channel_snapshot_update messages.
  70. * \param data Data pointer given when added to router.
  71. * \param sub This subscription.
  72. * \param message The message itself.
  73. */
  74. static void updates(void *data, struct stasis_subscription *sub,
  75. struct stasis_message *message)
  76. {
  77. /* Since this came from a message router, we know the type of the
  78. * message. We can cast the data without checking its type.
  79. */
  80. struct ast_channel_snapshot_update *update = stasis_message_data(message);
  81. /* There are three types of channel snapshot updates.
  82. * !old && new -> Initial channel creation
  83. * old && new -> Updated channel snapshot
  84. * old && dead -> Final channel snapshot
  85. */
  86. if (!update->old_snapshot && update->new_snapshot) {
  87. /* Initial channel snapshot; count a channel creation */
  88. ast_statsd_log_string("channels.count", AST_STATSD_GAUGE, "+1", 1.0);
  89. } else if (update->old_snapshot && ast_test_flag(&update->new_snapshot->flags, AST_FLAG_DEAD)) {
  90. /* Channel is gone. Compute the age of the channel and post
  91. * that, as well as decrementing the channel count.
  92. */
  93. int64_t age;
  94. age = ast_tvdiff_ms(*stasis_message_timestamp(message),
  95. update->new_snapshot->base->creationtime);
  96. ast_statsd_log("channels.calltime", AST_STATSD_TIMER, age);
  97. /* And decrement the channel count */
  98. ast_statsd_log_string("channels.count", AST_STATSD_GAUGE, "-1", 1.0);
  99. }
  100. }
  101. /*!
  102. * \brief Router callback for any message that doesn't otherwise have a route.
  103. * \param data Data pointer given when added to router.
  104. * \param sub This subscription.
  105. * \param message The message itself.
  106. */
  107. static void default_route(void *data, struct stasis_subscription *sub,
  108. struct stasis_message *message)
  109. {
  110. if (stasis_subscription_final_message(sub, message)) {
  111. /* Much like with the regular subscription, you may need to
  112. * perform some cleanup when done with a message router. You
  113. * can look for the final message in the default route.
  114. */
  115. return;
  116. }
  117. }
  118. static int unload_module(void)
  119. {
  120. stasis_unsubscribe_and_join(sub);
  121. sub = NULL;
  122. stasis_message_router_unsubscribe_and_join(router);
  123. router = NULL;
  124. return 0;
  125. }
  126. static int load_module(void)
  127. {
  128. /* You can create a message router to route messages by type */
  129. router = stasis_message_router_create(
  130. ast_channel_topic_all());
  131. if (!router) {
  132. return AST_MODULE_LOAD_DECLINE;
  133. }
  134. stasis_message_router_add(router, ast_channel_snapshot_type(),
  135. updates, NULL);
  136. stasis_message_router_set_default(router, default_route, NULL);
  137. /* Or a subscription to receive all of the messages from a topic */
  138. sub = stasis_subscribe(ast_channel_topic_all(), statsmaker, NULL);
  139. if (!sub) {
  140. unload_module();
  141. return AST_MODULE_LOAD_DECLINE;
  142. }
  143. return AST_MODULE_LOAD_SUCCESS;
  144. }
  145. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Example of how to use Stasis",
  146. .support_level = AST_MODULE_SUPPORT_EXTENDED,
  147. .load = load_module,
  148. .unload = unload_module,
  149. .requires = "res_statsd"
  150. );