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
pipinstall--upgrade'sentry-sdk[django]'
If you have a Django project and want to use theDjangoIntegration
option, you will only need this Django extension. And addsentry-sdk
torequirements.txt
.
2. Create a project on Sentry website
Log in to your account or sign up for a new free account from theSentry 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 thesentry-sdk
into your project.
First, import thesentry-sdk
at the top of your file and add thesentry-sdk
to the end of yoursettings.py
file:
# blog_project/settings.pyimportsentry_sdksentry_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.pyfromdjango.urlsimportpathapp_name="core"deftrigger_error(request):division_by_zero=1/0urlpatterns=[path("sentry-debug/",trigger_error),]
4. Check the functionality
Start your development server withpython 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 thesentry-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 theDjangoIntegration
option, you need to importDjangoIntegration
at the top of thesettings.py
file and update the part from 'integrations' onwards.
# blog_project/settings.pyfromsentry_sdk.integrations.djangoimportDjangoIntegrationsentry_sdk.init(dsn="your-sentry-dsn",integrations=[DjangoIntegration(),],traces_sample_rate=1.0,# Enables performance monitoringsend_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 documentationDjango section
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse