Introduction
When your project is ready to go live, it is a good time to integrate Sentry services to monitor your failures. It enables automatic reporting of defects and exceptions, as well as traceability.
1. Install Sentry in your project
pip install --upgrade 'sentry-sdk[django]'
If you have a Django project and want to use the DjangoIntegration
option, you will only need this Django extension. And add sentry-sdk
to requirements.txt
.
2. Create a project on Sentry website
Log in to your account or sign up for a new free account from the Sentry home page. You will be taken to the main account dashboard after logging in or completing the Sentry sign-up process.
Click on 'Projects' in the left sidebar to go to the Projects page and create a new Sentry Project just for this application. Click on the 'Create Project' button at the top right of the page.
You choose 'Django' as platform. The next step is to give your new project a name and then press the 'Create Project' button. We can now integrate our Python code into our new project.
3. Set up your Django project
In your newly created Sentry project, you will find the documentation you need to continue to integrate the sentry-sdk
into your project.
First, import the sentry-sdk
at the top of your file and add the sentry-sdk
to the end of your settings.py
file:
# blog_project/settings.py
import sentry_sdk
sentry_sdk.init(
dsn="https://yourkeygoeshere.ingest.sentry.io/project-number",
traces_sample_rate=1.0,
_experiments={
"continuous_profiling_auto_start": True,
},
)
The next step is to add a URL to your project to trigger your first error, which will be reported in your Sentry dashboard. Personally, in order to be able to check the functionality of the Sentry integration later in production, I do not remove this URL and the ability to trigger an error in your project.
# core/urls.py
from django.urls import path
app_name = "core"
def trigger_error(request):
division_by_zero = 1 / 0
urlpatterns = [
path("sentry-debug/", trigger_error),
]
4. Check the functionality
Start your development server with python manage.py runserver
. Navigate to your URL pattern: http://127.0.0.1:8000/sentry-debug/
.
This URL will cause a zero revision error in your project. Wait a minute or two and refresh the Sentry dashboard to see if the sentry-sdk
integration was successful.
5. Additional informations
DjangoIntegration
makes it much easier to debug and monitor a Django application with Sentry. It's highly recommended to include it when using Sentry in a Django project.
In order to integrate the DjangoIntegration
option, you need to import DjangoIntegration
at the top of the settings.py
file and update the part from 'integrations' onwards.
# blog_project/settings.py
from sentry_sdk.integrations.django import DjangoIntegration
sentry_sdk.init(
dsn="your-sentry-dsn",
integrations=[
DjangoIntegration(),
],
traces_sample_rate=1.0, # Enables performance monitoring
send_default_pii=True, # Sends Personally Identifiable Information (PII), like user details
)
The Django integration has more features. But for a simple Django project these settings are sufficient.
- Sentry documentation Django section
Top comments (0)