Rootstrap Blog

Real-time monitoring using Django and Slack Webhooks

There are many developer options available for error tracking but some can be quite expensive, or have too many configurations and require a lot of maintenance.

In this blog post, I’ll introduce you to a simple approach for small to medium teams to keep track of any exceptions raised in Django applications.

In this tutorial, you’ll learn how to use Slack Incoming Webhooks, and the Django logging system to send alerts to a given slack channel(s). Examples include the status of an ETL pipeline, getting instant user feedback, and integrating some scheduler to get periodic reports.

To start, we need to create a Slack app that is going to deliver our app’s messages. To do so, open this link. Once you’re logged in, select “From scratch“, write a proper name for your app, and then click on “Create App”.

On the left menu go to “Basic Information > Building Apps for Slack > Add features and functionality > Incoming Webhooks“. Turn the switch on to activate this feature, as seen below.

In the “Webhook URLs for Your Workspace” section, click on “Add New Webhook to Workspace” and select a channel, and then click Allow” to finish.

Now you’ll get one webhook URL attached to the selected channel that should look like this:

Configuring Django to use the Webhook URL

We are going to leverage Django’s logging configuration dictionary. As you might know, Python logging configuration has the following parts:

  • Loggers
  • Handlers
  • Filters
  • Formatters

We are going to configure at least one logger and one handler.

Handlers: How our messages will reach Slack

In order to actually send messages to slack, we’ll need to install some library to help us with it. I use requests as it is super simple to use:

After you installed requests, proceed to create a new python file and add the following two classes to it:

  • SlackInterface
  • SlackHandler

The SlackInterface class will do the hard work in the sense of interacting with the Slack API, and the SlackHandler will act as an interface between Slack and the Logging system and will decide how the messages will reach slack.

Slack Interface

As you can see, we defined a _send method which contains the requests call, using the POST method, the webhook URL, and any valid JSON string as the payload.

Also, we defined a message_success and message_error, and you can find more details on what you can use within the payload at this link: Make it fancy with advanced formatting.

Slack Handler

According to Python’s documentation, the minimum requirement to implement a logging handler class is to have an emit method, with record as a parameter. You can find more details for the record object here: LogRecord objects.

Once you have this method coded, you can perform any action inside, for example, calling message_error and message_error or any other custom method you want.

Now you have a handler class that can be put on the handlers dict, just make sure you use a valid import path on the “class” field.

Loggers: Who can use Slack notifications?

The last part of this tutorial focuses on defining a logger. There are multiple ways to achieve this.

The easiest one is to add one of your app’s name to the loggers dict. This allows you to control which app in your project can use these alerts, or simply add the handler into the “Django” logger to process any log issued by the whole project that is not caught by any other handler.

Using the Logger

You can use the logger by instantiating it on any file of my_app:

And here’s the output of the snippet above:

Summary

In this post, we created a Slack App, interacted with it on SlackInterface and SlackHandler, and integrated them into Django’s logging system via the Logging Configuration dictionary.

This example is a full version of the configuration dict that we built during this tutorial. To see the variables we did not talk about here, take a look at this link: Configuring Logging.

As always, thank you for reading our blog, and stay tuned for more useful content.

About 

0 Comments

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.