Tracking errors on Django with WSGI and Sentry.io

taiphuong
3 min readDec 25, 2020

As a developer worked whole of time in backend, my headache is not bugs coming, this is where and when bugs come. It’s easy find the bugs when you’re developing, but what happen when they come in production. It’s an obsessed pain.

I’m a junior python developer, working whole time which Django, Rest Framework and something related... Today I will give you a fine-tuning — (to make very small changes to something in order to make it work as well as possible) (I hope so, :))

Requisitions:

  • Django and Python environment.
  • WSGI
  • Sentry and its python-sdk
  • And have a obsessed pain :)

1/ Create an account in Sentry.

Navigate to: https://sentry.io/ and create account, it’s will be faster when you choose sign in with Google, Github and Azure. Because of loving something middle, I chose Github.

Next one, you need an Organization

Create a new organization
Continue to create new Django settings

Choose Django or Python Platform, and see like below

2/ Setting Sentry in your project

Follow the instruction in get started to get your DSN which I check by red color. But simply, just copy this code to your settings.py file in your project.

If you miss dns, just go to Settings -> Projects -> Choose a proper project -> Clients Key(DSN) in Sidebar

After creating a new django project, go to the urls.py, let make a example like:

and stimulate by raising ZeroDivisionError.

We have a result like that.

Result after raising unhandled errors

3/ For production deploy by WSGI

With some reasons the settings above are not working, seem like your version library python is not compatible. We moved to see how sentry connects in Python or some servers that supports:

For Django deploy production by WSGI, my setting will be like that, also recommend bring your dsn to environment.


import os
import sys
from django.core.wsgi import get_wsgi_application
import sentry_sdk
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
sentry_sdk.init(dsn="your dsn")sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))os.environ.setdefault("DJANGO_SETTINGS_MODULE", 'project.settings')application = SentryWsgiMiddleware(get_wsgi_application())

4/ Additions:

If you really need when bugs come, let try connect sentry some communition apps like Slack. Go to Settings -> Integrations

The result will be like that:

In the end of sharing, happy to give some things that helpful. Thanks

--

--