Skip to main content

Django

DebuggAI's Django SDK integrates with your Django application to provide powerful error tracking and monitoring capabilities. This guide will help you get started with the Django SDK and configure it for your project.

Installation

Install the required DebuggAI SDK packages:

pip install debugg-ai-sdk

You may also need to install additional dependencies for integrations like Celery and Redis:

pip install celery redis

Prerequisites

Before using the DebuggAI Django SDK, make sure you have:

  • Python 3.6 or later
  • Django 2.x or later
  • An API key from DebuggAI (sign up at app.debugg.ai)
  • The required environment variables set for configuration (DEBUGG_AI_DSN, DEBUGG_AI_HOST, etc.)

Configuration

To set up DebuggAI in your Django application, follow the steps below.

Step 1: Import DebuggAI SDK

Add the necessary imports to your Django settings file (e.g., settings.py):

import debugg_ai_sdk
import debugg_ai_sdk.integrations.celery
import debugg_ai_sdk.integrations.django
import debugg_ai_sdk.integrations.logging
import debugg_ai_sdk.integrations.redis

Step 2: Set DebuggAI Configuration

Set the necessary configuration values in your settings.py (or other appropriate configuration file). Make sure to replace placeholders like company_key and project_key with your actual values.

DEBUGG_AI_HOST = env('DEBUGG_AI_HOST', default='default')
DEBUGG_AI_INGEST_URL = env('DEBUGG_AI_INGEST_URL', default=f'https://debuggai-backend.ngrok.app/api/v1/ingest/{company_key}/{project_key}/')

DEBUGG_AI_DSN = DEBUGG_AI_INGEST_URL
DEBUGG_AI_LOG_LEVEL = env.int("DEBUGG_AI_LOG_LEVEL", logging.ERROR)

Step 3: Initialize DebuggAI Logging Integration

Configure the logging integration to capture logs for debugging and error tracking.

debugg_ai_logging = debugg_ai_sdk.integrations.logging.LoggingIntegration(
level=DEBUGG_AI_LOG_LEVEL, # Capture info and above as breadcrumbs
event_level=logging.ERROR, # Send errors as events
)

Step 4: Integrate DebuggAI with Django, Celery, and Redis

Set up DebuggAI integrations with Django, Celery, and Redis for complete monitoring. The debugg_ai_integrations list includes the logging integration, as well as support for Django, Celery, and Redis.

debugg_ai_integrations = [
debugg_ai_logging,
debugg_ai_sdk.integrations.django.DjangoIntegration(),
debugg_ai_sdk.integrations.celery.CeleryIntegration(
monitor_beat_tasks=True
),
debugg_ai_sdk.integrations.redis.RedisIntegration(),
]

Step 5: Initialize DebuggAI SDK

Finally, initialize the DebuggAI SDK with your DSN and integrations in your Django project.

debugg_ai_sdk.init(
dsn=DEBUGG_AI_DSN,
integrations=debugg_ai_integrations,
environment=env("DEBUGG_AI_ENVIRONMENT", default="local"),
host_name=DEBUGG_AI_HOST,
traces_sample_rate=env.float("DEBUGG_AI_TRACES_SAMPLE_RATE", default=0.0),
)

Best Practices

  1. Configure Early: Initialize the DebuggAI SDK as early as possible in your Django application to capture all events.
  2. Enable Logging: Use the LoggingIntegration to log important events and errors.
  3. Monitor Celery: Integrate Celery for background task monitoring, especially for scheduled tasks like Celery beat tasks.
  4. Monitor Redis: Integrate Redis if you're using it for caching, message brokering, or other operations.
  5. Redact Sensitive Data: Configure redaction of sensitive data in production environments, especially in error reports.

Troubleshooting

If you encounter issues:

  1. Verify DSN: Ensure that your DEBUGG_AI_DSN is correct and points to the right endpoint.
  2. Network Connectivity: Make sure your server can communicate with the DebuggAI ingestion endpoint.
  3. Check Logs: Review Django logs to diagnose issues with SDK initialization or integration.
  4. Configuration: Ensure that the environment variables are properly set, and check for any missing or incorrect values.

Next Steps

  • Learn more about Error Tracing to see how DebuggAI helps you debug issues in your Django app.
  • Explore Analytics to gain insights into your app's performance and user interactions.
  • Check out Click to Fix to automatically fix common issues directly from your DebuggAI dashboard.