Integrating Slack Alerts in Airflow

Kaxil Naik
Dec 11, 2018 · 6 min read

You just triggered your Airflow DAG that sends data to your clients and you being confident that the DAG will succeed (Why will it not — you wrote it.. There is no way it can fail), you go to have coffee with your colleagues in Company’s kitchen where the awesome Coffee Machine is waiting for you to serve the most delicious coffee ☕. You discuss how you can make the company better (Of course you don’t talk about how awesome the new Avengers trailer is !!). And then you finally decide to go back to your seat being a smirk to see the green status on your DAG. But wait…… what just happened, your DAG failed — of course, it was not your fault, the letters “DAG” decided to change their order to “DGA” when you were having coffee, hence your DAG failed. At the same time, your boss comes to you and asks you — “How’s your work going? Was the data sent to all clients?”. And you wished that there was some way you could have received an alert on your mobile when you were having coffee.

Well, I can go on and on with this stupid story but the fact is you need alerting when your DAG fails so that you can take actions at the earliest. Airflow has a built-in capability to send alerts on emails but well it gets lost in the pile of other 1000 unread emails. And it is just easier to get alerts where your entire team has an eye on — SLACK.

Image for post
Image for post

There are 2 ways in which you can integrate Slack with Airflow.

(1) Using Slack Legacy Tokens:

Legacy tokens are an old method of generating tokens for testing and development and Slack themselves don’t recommend to use this but it is the simplest method — hence you can still use it but bear in mind that it can get deprecated anytime.

Follow this steps:

  • Create a Slack Token from https://api.slack.com/custom-integrations/legacy-tokens. You will see the list of Slack Workspaces your email is associated with. Click on Create Token next to the workspace where you want to send alerts.
  • Use the SlackAPIPostOperator (Remember to install slack dependencies pip install apache-airflow[slack]) Operator in your DAG as below

You can try this example in iPython or using Jupyter notebook as follows:

Image for post
Image for post
An Example of running the code in iPython and the sample message received on Slack

However, this is just an example to send a message on slack and not alerts on task failures. Each task in Airflow contains a parameter called on_failure_callback (of callable type) to which you pass a function to be called when a task fails.

Example:

Now when you run the dag with the above task, it would send you an alert as shown in the image below:

Image for post
Image for post
Example of alerts on task failures. Note that the task ran 6 times so there are 6 alerts

This is useful but there are still 2 issues with the above code:

  • The slack token is stored in plain-text
  • The Slack alert message isn’t properly formatted

The first issue can be resolved by storing the Slack token in Airflow Connections in the password field as follows:

Image for post
Image for post

I also recommend running pip install apache-airflow[crypto] which encrypts connection passwords in metadata db.

Now let’s update our function to use token and channel name from connections and also improve alert format:

Sample alert with this function would be as follows:

Image for post
Image for post

As you can see it also gives you a Log URL so that you can directly go to the log associated with the failed task.

(2) Using Slack Web Hooks:

Slack recommends Web Hook to send data to it.

Note: There was a bug in SlackWebhookOperator in Airflow≤1.10.3 (Bug Jira Issue). This was fixed in 1.10.4 with this PR (fix commit).

Follow the steps below:

  • Create a Slack app if you don’t have already.
Image for post
Image for post
  • Enable Incoming Webhooks on the next page
Image for post
Image for post
Image for post
Image for post
  • Create an Incoming Webhook by clicking on Add New Webhook to Workspace on the same page
Image for post
Image for post

You will see something similar to below image:

Image for post
Image for post

So go ahead and pick a channel that the app will post to, and then click to Authorize your app. You’ll be sent back to your app settings, and you should now see a new entry under the Webhook URLs for Your Workspace section, with a Webhook URL that’ll look something like this:

  • Create an Airflow connection for Slack with HTTP connection and the part after https://hooks.slack.com/services should go under password:
    Host: https://hooks.slack.com/services
    Conn Type: HTTP
    Password: /T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX
Image for post
Image for post
  • Create a Python function

Note: Reminding you again if you didn’t read this above: there was a bug in SlackWebhookOperator in Airflow≤1.10.3 (Bug Jira Issue). This was fixed in 1.10.4 with this PR (fix commit).

Bonus Tip

You can add on_failure_callback to default_args when defining DAG as below so that you get alert if any task in the DAG fails:

You can follow the same steps to integrate Slack with Google Cloud Composer.

Let me know in the comments if you have any issues.

DataReply

Data Reply Engineering & Data Science

Kaxil Naik

Written by

Apache Airflow PMC and Committer | Google Cloud Certified Data Engineer & Spark Certified Developer

DataReply

DataReply

Data Reply Engineering & Data Science

Kaxil Naik

Written by

Apache Airflow PMC and Committer | Google Cloud Certified Data Engineer & Spark Certified Developer

DataReply

DataReply

Data Reply Engineering & Data Science

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store