From 9e8e337368013fc63fdfd7a4b05d1a529e71b707 Mon Sep 17 00:00:00 2001 From: BlackHoleFox Date: Mon, 22 Apr 2019 16:01:15 -0500 Subject: [PATCH] Added Prometheus hook-in --- .env | 1 + GearBot/Cogs/PromMonitoring.py | 77 ++++++++++++++++++++++++++++++++++ GearBot/Util/Translator.py | 1 - 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 .env create mode 100644 GearBot/Cogs/PromMonitoring.py diff --git a/.env b/.env new file mode 100644 index 00000000..0e170794 --- /dev/null +++ b/.env @@ -0,0 +1 @@ +PYTHONPATH=GearBot/ \ No newline at end of file diff --git a/GearBot/Cogs/PromMonitoring.py b/GearBot/Cogs/PromMonitoring.py new file mode 100644 index 00000000..d805bb74 --- /dev/null +++ b/GearBot/Cogs/PromMonitoring.py @@ -0,0 +1,77 @@ +import prometheus_client as prom +import asyncio + +import discord +from discord.ext import commands + +from Cogs.BaseCog import BaseCog + +class PromMonitor(BaseCog): + def __init__(self, bot): + super().__init__(bot) + self.running = True + self.command_counter = prom.Counter("commands_ran", "How many times commands were ran and who ran them", [ + "command_name", + "author_name", + "author_id", + "guild_id" + ]) + + self.message_counter = prom.Counter("messages_sent", "What messages have been sent and by who", [ + "author_name", + "author_id", + "guild_id", + "length" + ]) + + self.user_message_raw_count = prom.Counter("user_message_raw_count", "Raw count of how many messages we have seen from users") + self.bot_message_raw_count = prom.Counter("bot_message_raw_count", "Raw count of how many messages we have seen from bots") + + self.bot.loop.create_task(self.raw_stats_updater()) + + self.metric_server = prom.start_http_server(8082) + + def cog_unload(self): + self.running = False + + + @commands.Cog.listener() + async def on_command_completion(self, ctx): + self.command_counter.labels( + command_name = ctx.invoked_with, + author_name = ctx.author, + author_id = ctx.author.id, + guild_id = ctx.guild.id + ).inc() + + @commands.Cog.listener() + async def on_message(self, message: discord.Message): + self.message_counter.labels( + author_name = message.author, + author_id = message.author.id, + guild_id = message.guild.id, + length = len(message.content) + ).inc() + + + async def raw_stats_updater(self): + while self.running: + old_count = int(self.bot_message_raw_count.collect()[0].samples[0].value) + new_count = self.bot.bot_messages + if new_count != old_count: + inc_value = (new_count - old_count) # Keep the dashboards stats up to date with the internal count + self.bot_message_raw_count.inc(inc_value) + + old_count = int(self.user_message_raw_count.collect()[0].samples[0].value) + new_count = self.bot.user_messages + if new_count != old_count: + inc_value = (new_count - old_count) + self.user_message_raw_count.inc(inc_value) + + if not self.running: return + + await asyncio.sleep(10) + + +def setup(bot): + bot.add_cog(PromMonitor(bot)) \ No newline at end of file diff --git a/GearBot/Util/Translator.py b/GearBot/Util/Translator.py index 4cb867f3..bb7b908d 100644 --- a/GearBot/Util/Translator.py +++ b/GearBot/Util/Translator.py @@ -65,7 +65,6 @@ def translate(key, location, **kwargs): except (KeyError, ValueError, ParseError, VisitationError) as ex: BOT.loop.create_task(tranlator_log('NO', f'Corrupt English source string detected!\n**Translation key:** {key}\n```\n{LANGS["en_US"][key]}```')) GearbotLogging.error(ex) - GearbotLogging.info(f"Translated {key} to {lang_key}") return translated