Skip to content

Commit

Permalink
Rewrite Prometheus metrics (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
No767 authored Jun 13, 2024
1 parent 323e1d3 commit 98d6700
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 167 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
default_language_version:
python: python3.11
python: python3
files: '.py'
exclude: ".env,.yml,.gitignore,.git,.md,.txt"
default_stages: [push, commit]
Expand Down
12 changes: 6 additions & 6 deletions bot/catherinecore.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from libs.utils.config import CatherineConfig
from libs.utils.reloader import Reloader
from libs.utils.tree import CatherineCommandTree
from prometheus_async.aio.web import start_http_server


class Catherine(commands.Bot):
Expand All @@ -37,7 +36,7 @@ def __init__(
**kwargs,
)
self.logger: logging.Logger = logging.getLogger("catherine")
self.metrics = prometheus.create_gauges(self)
self.metrics = prometheus.MetricCollector(self)
self.session = session
self.pool = pool
self.version = str(VERSION)
Expand Down Expand Up @@ -66,19 +65,20 @@ async def setup_hook(self) -> None:
prom_host = self._prometheus.get("host", "127.0.0.1")
prom_port = self._prometheus.get("port", 6789)

await start_http_server(addr=prom_host, port=prom_port)
await self.metrics.start(host=prom_host, port=prom_port)
self.logger.info("Prometheus Server started on %s:%s", prom_host, prom_port)

self.metrics.fill_gauges()
self.metrics.fill()

if self._dev_mode:
self._reloader.start()

async def on_ready(self):
if not hasattr(self, "uptime"):
self.uptime = discord.utils.utcnow()
elif self._prometheus_enabled and not hasattr(self, "guild_metrics_created"):
self.guild_metrics_created = self.metrics.create_guild_gauges()

if self._prometheus_enabled and not hasattr(self, "guild_metrics_created"):
self.guild_metrics_created = self.metrics.guilds.fill()

curr_user = None if self.user is None else self.user.name
self.logger.info(f"{curr_user} is fully ready!")
Expand Down
4 changes: 2 additions & 2 deletions bot/cogs/blacklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async def add(self, ctx: commands.Context, id: discord.Object):
"""
await self.pool.execute(query, given_id, True)
get_blacklist.cache_invalidate(given_id, self.pool)
self.bot.metrics.blacklisted_users.inc()
self.bot.metrics.blacklist.users.inc()
await ctx.send(f"Done. Added ID {given_id} to the blacklist")

@blacklist.command(name="remove")
Expand All @@ -57,7 +57,7 @@ async def remove(self, ctx: commands.Context, id: discord.Object):
"""
await self.pool.execute(query, given_id)
get_blacklist.cache_invalidate(given_id, self.pool)
self.bot.metrics.blacklisted_users.dec()
self.bot.metrics.blacklist.users.dec()
await ctx.send(f"Done. Removed ID {given_id} from the blacklist")


Expand Down
Loading

0 comments on commit 98d6700

Please sign in to comment.