Introducing ErrorTracker [Exception Tracking Module for Python]

Sonu Kumar
4 min readJan 8, 2020

Error tracking is essential for any application when many people are relying on the app. If your application is not working in production for whatever reason, it can be a nightmare experience for users depending on the functionality.

There are many exceptions tracking plugins and modules available. Some of them are open source, whereas others are paid, like Honey Badger, New Relic, ELK, etc.

What about having a custom exception handler that can track errors in different situations with additional features like:

  • Send emails or notify developers (interested entities).
  • Create or update tickets in Bugzilla or Jira.
  • Push exceptions to the ELK stack or any other system for analytics and store detailed exceptions in DB.
  • While capturing exceptions, provide local and global variables details (failure context).

Error Tracker

Error tracker is a python library. It provides all the required interfaces and features mentioned above. One interesting thing about this library is that it can mask sensitive variables (i.e. sensitive information should not be exposed, like password, credit card number, etc. in local/global variables). It’s well documented in Error Tracker doc and available via PyPI

A typical setup of Error Tracker can be visualised as:

Error Tracker can be used with any type of Python application. We’ll start with Flask.

The tools we need:

  1. Any IDE 2. Python2 or Python3

Installation

pip install error-tracker

Flask Application

We’ll use a settings file for app configuration you might call it config.py or some other format like YAML, we’ll use the python file for simplicity.

settings.py

There’re seven configurations in this file:

APP_ERROR_SEND_NOTIFICATION - This configures whether a notification has to be sent or not. If it's set, then it will try to send a notification using the notification interface.

APP_ERROR_RECIPIENT_EMAIL - This refers to the email recipients entities. It is required to build email content. It can be a list of emails or a single email address.

APP_ERROR_EMAIL_SENDER - Email sender, this is used when an exception occurs.

APP_ERROR_SUBJECT_PREFIX - Any email subject prefix. By default, an email subject would be [IndexError] [GET] http://127.0.0.1:5000/go but it can be customised.

APP_ERROR_MASK_WITH - What should be used to mask sensitive information? BY default it uses * to hide the variable data.

APP_ERROR_MASKED_KEY_HAS - What variables should be masked? It can mask all local and global variables. Masking is done based on the variable name. If a data type is a dictionary, or its subclass, then their keys are examined.

APP_ERROR_URL_PREFIX - All unique exceptions are stored in the database and can be browsed at this endpoint.

Create another python file

For email-sender, we’ll implement NotificationMixin

For issue tracking, we’ll implement TicketingMixin

Let’s create a Flask app to serve requests. We’ll use SQLite for simplicity.

The errors can be tracked by creating an instance of AppErrorTracker and initializing this with proper details. This application can store all the exceptions in the database.

For error tracking, we are going to use the decorator track_exception provided by AppErrorTracker class. We'll attach this decorator whenever HTTP 500 occurs.

Now, it’s ready to be used. We can fire some requests and see how it works. For testing, we’ve added one endpoint /go that will throw 500 errors with different types of exceptions.

Now, run the app in debug=False mode and browse, as it will fail. Now, once we browse http://127.0.0.1:5000/dev/error/, then it displays a table like this.

Any of the specific exceptions can be browsed by clicking the corresponding link, in the Exception detail section, all frame details that have been captured can be seen.

Exceptions available for analysis

The complete Flask application can be found at https://github.com/sonus21/error-tracker/tree/master/examples/flask-sample.

Implementation With Django

Using Exception Tracker with Django is also very simple.

Install the application using pip install error-tracker.

Once we've installed it, we need to configure some parts of the applications using the settings.py file.

For the Django application, we need to add this app to the INSTALLED_APPS list and add Middleware to catch exceptions. The middleware should be added at the end of the list so that it will be called first whenever an exception occurs.

Error Tracker provides a few default pages for listing exception, deleting exception, and browsing a specific exception. These endpoints can be plugged in with Django’s other URLs.

These are all the code changes we need to make for the Django app. The complete code is available at https://github.com/sonus21/error-tracker/tree/master/examples/DjangoSample.

Conclusion

Apps using Error Tracker can be further customised, like what models to use. Currently, it records only one instance of failure, while all failures can be tracked and dumped to some database. Read more about them at https://error-tracker.readthedocs.io/en/latest/index.html#.

The complete code is available on GitHub.

If you found this post helpful, please share and give a thumbs up.

--

--