Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

DoriDoro
DoriDoro

Posted on

     

Integrate Sentry into your Django project

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]'
Enter fullscreen modeExit fullscreen mode

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,},)
Enter fullscreen modeExit fullscreen mode

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),]
Enter fullscreen modeExit fullscreen mode

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)
Enter fullscreen modeExit fullscreen mode

The Django integration has more features. But for a simple Django project these settings are sufficient.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

More fromDoriDoro

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp