Skip to main content

An extension for the discord.py library that enables Prometheus metrics

Project description

discord-ext-prometheus

PyPi Version PyPI Python Version License MIT

This is a library that makes it easy to add prometheus metrics to your Python Discord bot.

Installation

pip install discord-ext-prometheus

Exposed Metrics

Name Documentation Labels
discord_connected Determines if the bot is connected to Discord shard
discord_latency Latency to Discord shard
discord_event_on_interaction Amount of interactions called by users shard, interaction, command
discord_event_on_command Amount of commands called by users shard, command
discord_stat_total_guilds Amount of guild this bot is a member of None
discord_stat_total_channels Amount of channels this bot is has access to None
discord_stat_total_users Amount of users this bot can see None
discord_stat_total_commands Amount of commands None
logging Log entries logger, level

Notes:

  • on_interaction are application interactions such as slash commands or modals
  • on_command are traditional message commands (usualy using the command prefix)

Grafana Dashboard

Dashboard Preview

Available to import from Grafana dashboards

How to use

Once the cog is added to your bot, the Prometheus metric endpoint can be accessed at localhost:8000/metrics.

Sample code with the Prometheus Cog

import asyncio
from discord import Intents
from discord.ext import commands
from discord.ext.prometheus import PrometheusCog

async def main():
	bot = commands.Bot(
		command_prefix='!',
		intents=Intents.all(),
	)

	await bot.add_cog(PrometheusCog(bot))

	await bot.start('YOUR TOKEN')

asyncio.run(main())

Sample code with logging metrics

import asyncio
import logging
from discord import Intents
from discord.ext import commands
from discord.ext.prometheus import PrometheusCog, PrometheusLoggingHandler

logging.getLogger().addHandler(PrometheusLoggingHandler())

async def main():
	bot = commands.Bot(
		command_prefix='!',
		intents=Intents.all(),
	)

	await bot.add_cog(PrometheusCog(bot))

	@bot.listen()
	async def on_ready():
		logging.info(f'Logged in as {bot.user.name}#{bot.user.discriminator}')

	logging.info('Starting the bot')
	await bot.start('YOUR TOKEN')

asyncio.run(main())

Change the Prometheus port

The default port is 8000 but can be changed while creating the cog.

await bot.add_cog(PrometheusCog(bot, port=7000))

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

discord_ext_prometheus-0.1.3.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

discord_ext_prometheus-0.1.3-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file discord_ext_prometheus-0.1.3.tar.gz.

File metadata

File hashes

Hashes for discord_ext_prometheus-0.1.3.tar.gz
Algorithm Hash digest
SHA256 fd8fe3d041b1a50782a43cdb34650f1ca1173f858e719622c9ffb8aa0f16c494
MD5 d734f6fda4df3dee924132938032ceaa
BLAKE2b-256 2a46ecf6a4fffe6e6d8d78f6b67d5daf1eb48f317bc6235ddf5f4196edef0466

See more details on using hashes here.

File details

Details for the file discord_ext_prometheus-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for discord_ext_prometheus-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 990617830d784682d7ebb0fe97dd22bc3547b694776a728fa097f05b6a277af6
MD5 cd171e160c6f6d4983d6f7eeb3cbcb77
BLAKE2b-256 9e893ef3f284cde144835c38cb5b3187d63e7ad0fb44d27a83f3dc4bc536e051

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page