Skip to main content

OpenTelemetry instrumentation for discord.py bots

Project description

OpenTelemetry Instrumentation for Discord.py

This library provides OpenTelemetry instrumentation for bots built with discord.py, making it easy to collect traces, metrics, and logs to analyze the performance and reliability of your Discord bots.

Features

  • Automatic Instrumentation: Automatically instruments your Discord.py bot to trace key events and operations with minimal setup.
  • Manual Instrumentation: Provides decorators for fine-grained control over tracing specific commands and event handlers.
  • Comprehensive Coverage: Instruments event listeners, message operations, commands, HTTP requests, and voice operations.
  • Detailed Context: Captures rich contextual information such as guild IDs, channel names, message content lengths, and more.
  • Flexible Configuration: Allows you to enable or disable instrumentation for specific components based on your needs.
  • Performance Monitoring: Track execution times, identify bottlenecks, and monitor the health of your Discord bot.
  • Error Tracking: Automatically captures exceptions and errors in your bot's operations.
  • Seamless Integration: Works with any OpenTelemetry exporter (Console, OTLP, Jaeger, etc.).

Features Overview

The OpenTelemetry Instrumentation for Discord.py enhances the observability of your Discord bots, providing detailed insights into their operations and interactions. Here are the key components that are instrumented:

Event Listeners

  • on_message: Traces message processing and the bot's responsiveness to user messages.
  • on_ready: Monitors bot startup and initialization.
  • Other Events: Automatically traces all Discord.py events.

Message Operations

  • send_message: Traces message sending operations, including execution times and potential bottlenecks.
  • edit_message: Captures information about message edit operations.
  • delete_message: Monitors message deletion operations.

Commands

  • Command Invocation: Traces command execution, including parsing and handling.
  • Command Errors: Captures and traces command errors and exceptions.

HTTP Operations

  • API Requests: Traces Discord API requests made by the bot.
  • Rate Limiting: Monitors rate limit encounters and handling.

Voice Operations

  • Voice Connections: Traces voice channel connections and disconnections.
  • Audio Playback: Monitors audio streaming and playback operations.

Installation

Install this package with pip:

pip install opentelemetry-instrumentation-discordpy

Usage

Automatic Instrumentation

To automatically instrument your Discord bot, simply initialize the instrumentation at the start of your bot's code:

from opentelemetry_instrumentation_discordpy import DiscordPyInstrumentor
DiscordPyInstrumentor().instrument()

Manual Instrumentation

For more fine-grained control, use the provided decorators to instrument specific commands or event handlers:

from opentelemetry_instrumentation_discordpy.decorators import trace_command, trace_event
from opentelemetry import trace
from opentelemetry.trace import SpanKind

# Get a tracer for custom spans
tracer = trace.get_tracer("my-discord-bot")

@bot.event
@trace_event(attributes={"custom.attribute": "value"})
async def on_message(message):
    # Your code here...
    await bot.process_commands(message)

@bot.command()
@trace_command(attributes={"command.type": "utility"})
async def ping(ctx):
    # Create a custom span for measuring latency
    with tracer.start_as_current_span(
        "ping_latency",
        kind=SpanKind.INTERNAL,
        attributes={"discord.channel.id": str(ctx.channel.id)}
    ) as span:
        # Measure latency
        message = await ctx.send("Pinging...")
        latency = bot.latency * 1000
        
        # Record latency in span
        span.set_attribute("ping.latency_ms", latency)
        
        await message.edit(content=f"Pong! Latency: {latency:.2f}ms")

Examples

For complete examples of setting up a bot with OpenTelemetry instrumentation:

Configuration

Configuring the Instrumentor

You can configure which components of discord.py to instrument:

# Instrument only specific components
DiscordPyInstrumentor().instrument(
    event_listeners=True,    # Instrument event listeners (on_message, on_ready, etc.)
    message_operations=True, # Instrument message operations (send, edit, delete)
    commands=True,           # Instrument command handling
    http=True,               # Instrument HTTP requests
    voice=True               # Instrument voice operations
)

Setting Up Exporters

The library works with any OpenTelemetry exporter:

# Console exporter (for development)
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
tracer_provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))

# OTLP exporter (for production)
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace.export import BatchSpanProcessor
otlp_exporter = OTLPSpanExporter(endpoint="your-otlp-endpoint:4317")
tracer_provider.add_span_processor(BatchSpanProcessor(otlp_exporter))

For more detailed configuration options and advanced usage, refer to the documentation.

Contributing

Contributions are welcome! See CONTRIBUTING.md for how to get started.

License

This library is licensed under the BSD License.

Support

If you encounter any issues or have questions, please file an issue on GitHub.

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

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file opentelemetry_instrumentation_discordpy-0.1.13.tar.gz.

File metadata

File hashes

Hashes for opentelemetry_instrumentation_discordpy-0.1.13.tar.gz
Algorithm Hash digest
SHA256 c64a9ed932144ec24d9d9772ae9b0ffef0b962fbf47ddf4bec00bb21ebcd9be2
MD5 ad328695674159889a89c1ef8ba34e29
BLAKE2b-256 12f97ee77860b1a67b34afb2dec15b855240c7694d8d1550360b1e1bad16ffaa

See more details on using hashes here.

File details

Details for the file opentelemetry_instrumentation_discordpy-0.1.13-py3-none-any.whl.

File metadata

File hashes

Hashes for opentelemetry_instrumentation_discordpy-0.1.13-py3-none-any.whl
Algorithm Hash digest
SHA256 27174f75caa090cdf7b82ff6d08574c19d4634cd07485584b16047c60e817760
MD5 bcc258f5738986c9d1806963ca82ea47
BLAKE2b-256 07b69586d6e60027cde4b4027f3e75c792644848f614edf157f935f0a5644db2

See more details on using hashes here.

Supported by

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