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.
  • Custom Event Support: Register your own custom events for tracing, beyond the standard Discord.py events.
  • 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.30.tar.gz.

File metadata

File hashes

Hashes for opentelemetry_instrumentation_discordpy-0.1.30.tar.gz
Algorithm Hash digest
SHA256 3605630fd5eb55f22e4bdf7630090e4c6ef70b3b9ee78ecc848247d2b52e8b03
MD5 450f06a56209594faf472c24e6b2027b
BLAKE2b-256 29d65d0a574e627ebd89ce872662361faa46c7a08ea5ad75919bf2a868a05c9d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for opentelemetry_instrumentation_discordpy-0.1.30-py3-none-any.whl
Algorithm Hash digest
SHA256 c59cc727148d9a7b5b793cce1e2fc3d910c364690f2ac8a9ded5092387959de4
MD5 1059179b7901e18eef36484fb498a27e
BLAKE2b-256 12493872be8b9adc61a64b371c8da07b3c46958d4c37b3e47a434a59372a3086

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