stasis: Store subscriber uniqueids with topic statistics.
authorJoshua C. Colp <jcolp@digium.com>
Wed, 20 Feb 2019 18:22:31 +0000 (14:22 -0400)
committerJoshua C. Colp <jcolp@digium.com>
Wed, 20 Feb 2019 18:24:11 +0000 (12:24 -0600)
This change provides an easier mechanism to determine which
subscribers are subscribed to a topic. Using this you can
inspect the specific subscribers for further details.

Change-Id: I8deea21703cd5c5357b85593b46c3eaf24e18c0c

main/stasis.c

index f05f5ff..4bfe892 100644 (file)
@@ -347,8 +347,8 @@ struct stasis_topic_statistics {
        int messages_not_dispatched;
        /*! \brief The number of messages that were dispatched to at least 1 subscriber */
        int messages_dispatched;
-       /*! \brief The number of subscribers to this topic */
-       int subscriber_count;
+       /*! \brief The ids of the subscribers to this topic */
+       struct ao2_container *subscribers;
        /*! \brief Name of the topic */
        char name[0];
 };
@@ -405,15 +405,28 @@ static void topic_dtor(void *obj)
 }
 
 #ifdef AST_DEVMODE
+static void topic_statistics_destroy(void *obj)
+{
+       struct stasis_topic_statistics *statistics = obj;
+
+       ao2_cleanup(statistics->subscribers);
+}
+
 static struct stasis_topic_statistics *stasis_topic_statistics_create(const char *name)
 {
        struct stasis_topic_statistics *statistics;
 
-       statistics = ao2_alloc(sizeof(*statistics) + strlen(name) + 1, NULL);
+       statistics = ao2_alloc(sizeof(*statistics) + strlen(name) + 1, topic_statistics_destroy);
        if (!statistics) {
                return NULL;
        }
 
+       statistics->subscribers = ast_str_container_alloc(1);
+       if (!statistics->subscribers) {
+               ao2_ref(statistics, -1);
+               return NULL;
+       }
+
        strcpy(statistics->name, name); /* SAFE */
        ao2_link(topic_statistics, statistics);
 
@@ -998,7 +1011,7 @@ static int topic_add_subscription(struct stasis_topic *topic, struct stasis_subs
        }
 
 #ifdef AST_DEVMODE
-       topic->statistics->subscriber_count += 1;
+       ast_str_container_add(topic->statistics->subscribers, stasis_subscription_uniqueid(sub));
 #endif
 
        ao2_unlock(topic);
@@ -1021,7 +1034,7 @@ static int topic_remove_subscription(struct stasis_topic *topic, struct stasis_s
 
 #ifdef AST_DEVMODE
        if (!res) {
-               topic->statistics->subscriber_count -= 1;
+               ast_str_container_remove(topic->statistics->subscribers, stasis_subscription_uniqueid(sub));
        }
 #endif
 
@@ -2292,6 +2305,8 @@ static char *topic_statistics_complete_name(const char *word, int state)
 static char *statistics_show_topic(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
        struct stasis_topic_statistics *statistics;
+       struct ao2_iterator i;
+       char *uniqueid;
 
        switch (cmd) {
        case CLI_INIT:
@@ -2323,7 +2338,15 @@ static char *statistics_show_topic(struct ast_cli_entry *e, int cmd, struct ast_
        ast_cli(a->fd, "Number of messages that went to at least one subscriber: %d\n", statistics->messages_dispatched);
        ast_cli(a->fd, "Lowest amount of time (in milliseconds) spent dispatching message: %ld\n", statistics->lowest_time_dispatched);
        ast_cli(a->fd, "Highest amount of time (in milliseconds) spent dispatching messages: %ld\n", statistics->highest_time_dispatched);
-       ast_cli(a->fd, "Number of subscribers: %d\n", statistics->subscriber_count);
+       ast_cli(a->fd, "Number of subscribers: %d\n", ao2_container_count(statistics->subscribers));
+
+       ast_cli(a->fd, "Subscribers:\n");
+       i = ao2_iterator_init(statistics->subscribers, 0);
+       while ((uniqueid = ao2_iterator_next(&i))) {
+               ast_cli(a->fd, "\t%s\n", uniqueid);
+               ao2_ref(uniqueid, -1);
+       }
+       ao2_iterator_destroy(&i);
 
        ao2_ref(statistics, -1);