Error tracking with Sentry, Python, and Django


Not long ago, I showed you how to add tracing to a JavaScript application with Sentry. In this post, we’ll stroll through using Sentry on the back end to track mistakes in a Python-Django application stack.

Set up and set up Python and Django

The initial step is to develop a Django application. We’re going to utilize Python 3, so begin by setting up Python 3 on your system. We’ll likewise require pip3. On Debian, I install them both by entering the command, sudo apt-get install python3 python3-pip. When you have Python and pip installed you need to get responses from the -V switch, as shown in Listing 1.

Listing 1. Make certain python3 and pip3 are available

$python3 -V Python 3.11.0 $ pip3 -V pip 22.3.1 from/ usr/lib/python3/ dist-packages/pip (python 3.11)

Next, set up the Django structure and validate it is working, as shown in Listing 2.

Listing 2. Install Django

$ pip3 install Django … $ django-admin– variation 4.1.3

Now we can start a brand-new application utilizing Django. Get in the command, django-admin startproject djangosentry. This develops a directory to hold our starter job. Now, CD into the job,/ djangosentry. Django handles the application database, so we just need to fill it with the initial information required for running a Django type. Get in, python3 migrate, where is the primary Django script. After that finishes, we’ll add a user by going into: python3 createsuperuser. We will need this user to log in. (Note that you will be prompted for a username and password– simply utilize something you will remember.)

We need to inform Django to enable the existing host. You can find out your IP address by pointing your internet browser to Go to djangosentry/setting. py and add the IP address where the instance lies to the ALLOWED_HOSTS range; something like: ALLOWED_HOSTS = [′] Now, you can begin the server by typing: python3 runserver

If you go to, you will see the Django welcome screen shown in Figure 1.

The Django welcome screen. IDG Figure 1. The Django welcome screen. Note that there is likewise an adminconsole to add users and groups at!.?.!.Set up Sentry Now that we have a running Django application, let’s set up Sentry to track mistakes. The initial step is to create a Sentry account, which is free if you pick the Designer alternative. After you have an account and log in, you’ll see the Sentry dashboard. The left-hand menu provides the primary navigation for Sentry. Click on Projects and create a project with the Create Job button in the upper-right corner. Config screen IDG Figure 2. Develop a new task

. Develop and configure a Django task A project provides us a central container for our tracing info. After you hit Produce Project, you can pick “Django” as the type and accept the default name of django-python.

After the task is produced, you will exist with the choice to configure it. Copy the code bit displayed in Figure 3 and add it to your job.

Copy the code snippet to configure Django. IDG Figure 3. Configure Django. Now that we have an application and a project, we can include the Sentry library. Return to the djangosentry job on your command line and type: pip set up– upgrade sentry-sdk. This command adds the Sentry library to your project.Now, we’ll import and configure the SDK into our djangosentry/settings. py file, which is the primary configuration apply for Django. This is simply a few lines of code, however we need the information source name (DSN) for the task. The concept here is that the DSN will develop the association between the Django job and the job in Sentry, so you can track project activity using the dashboard.

Now go to the djangosentry/djangosentry/settings. py file and include the code you copied to the bottom of the file. The outcome needs to appear like what you see in Noting 3.

Noting 3. Add Sentry SDK and project DSN to

import sentry_sdk from sentry_sdk. integrations.django import DjangoIntegration sentry_sdk. init( dsn=”“, integrations= [DjangoIntegration(),], traces_sample_rate=1.0, send_default_pii=True)

Believe it or not, the Django task is ready to capture error data. In truth, the traces_sample_rate setting informs Sentry to likewise record efficiency metrics– this worth can be anywhere from 0 to 1.0. Note that setting your sample rate to 100% (that is, 1.0) can rapidly tire your quota. Make certain to change your sample rate after collecting the initial performance data.Error reporting

with Sentry Let’s create a fast error to see if Sentry is working. Return into your file and get rid of the IP address from the permitted hosts. Then, reboot the application and reload the website. You’ll get an error page like the one in Figure 4. IDG Figure 4. An error in Django. If you inspect the Django console output you’ll likewise discover the mistake: [20/Nov/2022 11:17:40]” GET

/ HTTP/1.1 “400 69108.

The Django server is returning an HTTP approval error 400. Now, let’s look at the same mistake report from Sentry. By default, the e-mail address you used to establish your Sentry account is signed up for alerts

. For the HTTP approval error, you will receive an alert similar to what you see in Figure 5. IDG Figure 5. An email alert from Sentry. Next, take a look at the Sentry dashboard. If you open the python-django task you’ll see the error showed there, as shown in Figure 6. IDG Figure 6. The Django mistake in your Sentry control panel. You can click on the mistake in the control panel to get information. You will see pretty much whatever the system might understand about what happened, fromThe first error. the

user agent and timestamp to whether it was a handled exception(in this case, it was ). Configuring the environment Notice that the error in Figure 6 is tagged as environment: production. Doing not have further configuration, Sentry assumed the mistake originated from production. To manually set the environment, go to the djangosentry/settings. py

file. In the sentry_sdk.

init() function call, include an argument defining an environment, as displayed in Listing 4. Listing 4. Including an environment sentry_sdk. init (dsn =”https://[email protected]/4504193108672512″, integrations=[ DjangoIntegration (),], traces_sample_rate=1.0, send_default_pii=True

, environment=”dev “#

Leave a Reply

Your email address will not be published. Required fields are marked *