<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Raouf Aghrout on Medium]]></title>
        <description><![CDATA[Stories by Raouf Aghrout on Medium]]></description>
        <link>https://medium.com/@raouf.aghrout?source=rss-19eec5e298eb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*9TVl-hRAHiMDnCUbqAIOhw.jpeg</url>
            <title>Stories by Raouf Aghrout on Medium</title>
            <link>https://medium.com/@raouf.aghrout?source=rss-19eec5e298eb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 15:21:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@raouf.aghrout/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Quick & Easy Alerting for Apache Airflow]]></title>
            <link>https://medium.com/@raouf.aghrout/quick-easy-alerting-for-apache-airflow-53c3f1ba2ca?source=rss-19eec5e298eb------2</link>
            <guid isPermaLink="false">https://medium.com/p/53c3f1ba2ca</guid>
            <category><![CDATA[open-source]]></category>
            <category><![CDATA[airflow]]></category>
            <category><![CDATA[data-engineering]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[opsgenie]]></category>
            <dc:creator><![CDATA[Raouf Aghrout]]></dc:creator>
            <pubDate>Tue, 25 Jun 2019 10:41:14 GMT</pubDate>
            <atom:updated>2026-03-16T09:41:05.200Z</atom:updated>
            <content:encoded><![CDATA[<h4>Find out quickly when and why your pipelines are 🔥 and not 👌</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*j0h2m6AGSYSHY_c0EfaaXA.png" /></figure><p>By the end of this article you’ll know how the Data Engineering team at <a href="https://unruly.co/about-us/were-hiring/?city=london&amp;team=product">Unruly</a> setup alerting for our Apache Airflow instance in a really simple but powerful way. I’ll start by giving a lightweight intro to some Airflow concepts, but if you’re already familiar with Airflow you can skip this. Together we’ll go through a worked example of a failing task in an Airflow DAG that triggers a concise, super informative and actionable alert in our teams alerting system. We’re using Opsgenie, but don’t worry if you’re using something else, the same can be applied to any alerting system that supports creating alerts via an API. Finally, as a bonus, I’ll show how these alerts look when they’re automatically propagated to our notification channels like Slack.</p><h4>Apache Airflow — The Whistle Stop Tour</h4><p><em>Skip this section if you’ve used Airflow before!</em></p><p>Apache Airflow is an open source technology for orchestrating workflows and is typically used for data processing pipelines. It’s a great tool for batch ETL (extract, transform and load) style jobs, moving data from A to B, changing the format or schema of your data into something more convenient for some downstream work etc. These workflows are setup as Directed Acyclic Graphs, or <strong>DAGs</strong> for short. DAGs are typically composed of independently executable <strong>Tasks </strong>that are sequenced together to form some logical pipeline of work you want to conduct.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/490/1*_whkfbqHtz1AnhpbBTJiQw.png" /><figcaption>An example of a DAG. Source: <a href="https://airflow.apache.org/concepts.html">Airflow documentation</a>.</figcaption></figure><p>As you can see from the above graph, the ability to branch and merge these tasks provides a lot of flexibility to create most workflows you could imagine. When building a DAG, tasks are instantiated as <strong>Operators</strong>, an operator is just a class that takes some configuration. Airflow operators are ultimately responsible for doing the work, whether that’s triggering an action in another system, transferring data between systems or sensing when something should be triggered and making it happen. Finally there’s <strong>Hooks</strong>, these are operator building blocks that can be used to interface with external systems like a database or an API. Hooks utilise <strong>Connections </strong>that are configured with the credentials needed to access these external systems.</p><p>So now you should have a basic idea of what’s meant by a <strong>DAG</strong>, a <strong>Task</strong>, an <strong>Operator</strong>, a <strong>Hook</strong> and a <strong>Connection</strong> which is enough to grasp the rest of this article. There are of course much better and more in-depth introductions to Airflow and I’d definitely recommend exploring those to find out more if you’re interested. The majority of this summary was lifted directly from the <a href="http://Airflow documentation">Airflow documentation</a>, which is pretty good!</p><h4>A Task Doomed to Fail</h4><p>As our starting point let’s define a DAG with a single task that runs a simple <a href="https://airflow.apache.org/_api/airflow/operators/bash_operator/index.html#airflow.operators.bash_operator.BashOperator">BashOperator</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/60a7e48a64676529eeed4373f315bb80/href">https://medium.com/media/60a7e48a64676529eeed4373f315bb80/href</a></iframe><p>We’ve made sure this operator always fails by simply calling <em>exit 1</em> so we can test what we want to do off the back of the failure.</p><h4>The Power of BaseOperator</h4><p>All operators in Airflow inherit from the <a href="https://airflow.apache.org/_api/airflow/models/index.html#airflow.models.BaseOperator">BaseOperator</a> class, which means for any operator we’re using, including our BashOperator, we’ll have access to the parameters available in this class. The BaseOperator parameter we’ll be taking advantage of specifically is:</p><p><strong><em>on_failure_callback</em></strong><em> (callable) — a function to be called when a task instance of this task fails. a context dictionary is passed as a single parameter to this function. Context contains references to related objects to the task instance and is documented under the macros section of the API.</em></p><p>This can be configured to call a custom function in the event of our operator failing, which is exactly what we’re going to do next with the BashOperator we defined earlier.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1d47c7961ad7d92f82957f390436f96c/href">https://medium.com/media/1d47c7961ad7d92f82957f390436f96c/href</a></iframe><p>The <em>do_something</em> function that’s triggered when our task fails currently isn’t defined, but we’ll get there. The context dictionary that’s mentioned in the documentation is also key for us, this will allows us to get metadata about the instance of the task that has just failed. Later on we’ll use this context dictionary to customise our alerts, making them informative and user friendly.</p><h4>Setting up our Connection</h4><p>Airflow comes with a bunch of built in hooks, some of these are <a href="https://github.com/apache/airflow/tree/master/airflow/hooks">published by the Airflow team</a> and others are <a href="https://github.com/apache/airflow/tree/master/airflow/contrib/hooks">community contributed</a>. We’ll be using the community contributed <a href="https://github.com/apache/airflow/blob/master/airflow/contrib/hooks/opsgenie_alert_hook.py">OpsgenieAlertHook</a> in our example, but you can find pretty much anything you need here. If you’re using a different alerting platform with an API you can achieve pretty much anything with <a href="https://github.com/apache/airflow/blob/master/airflow/hooks/http_hook.py">HttpHook</a>, which is also the parent class of OpsgenieAlertHook.</p><p>For this hook to work we need to configure a connection to Opsgenie, looking at the code for this hook it’s already setup to use the existing <em>opsgenie_default</em> connection that comes with Airflow. For simplicity I can edit this existing connection in the Airflow UI, this will save <a href="https://airflow.readthedocs.io/en/stable/howto/secure-connections.html">and encrypt</a> it in our Airflow database. However, if you’re not keen on having this state lying around because you’re using the default in-memory Airflow database or haven’t encrypted your secrets, then you can also configure connections using <a href="https://airflow.readthedocs.io/en/latest/howto/connection/index.html#creating-a-connection-with-environment-variables">environment variables</a> or at run-time in your <a href="https://stackoverflow.com/questions/53740885/create-and-use-connections-in-airflow-operator-at-runtime">Airflow code</a>. This is great if you want to grab the credentials from your secrets manager and the way I’d recommend doing this in a production environment. For now we’re just going to drop our Opsgenie API key into the password field and save this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lkgPo5ClESr50RGsIeS2kA.png" /><figcaption>Configuring the Opsgenie connection in the Airflow UI.</figcaption></figure><p><strong>Note:</strong> When you revisit this connection in the Airflow UI you won’t see anything in the password field, it isn’t even visible but obfuscated, however it should be persisted!</p><h4>Leveraging the Hook</h4><p>Let’s make our <em>do_something </em>function actually… do something, this will be our hook into our alerting platform. Let’s rename <em>do_something</em> to <em>opsgenie_hook</em> and import this functionality from a different Python file. This will allows us to build something generic and then easily re-use this code in all of our DAG definition files.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f0466195ad5c7e3b8b83020b2fe2030d/href">https://medium.com/media/f0466195ad5c7e3b8b83020b2fe2030d/href</a></iframe><p>So <em>hook.execute() </em>will, as configured in OpsgenieAlertHook by default, send a POST request to the default Opsgenie alerts API endpoint at <em>https://api.opsgenie.com/v2/alerts</em>. It’s able to authenticate against the Opsgenie API utilising the <em>opsgenie_default</em> connection that we just configured. We’re supplying the name of this connection as the argument to OpsgenieAlertHook constructor. As you’ve probably noticed, the <em>execute </em>function currently takes an empty object, but not for much longer, this will be our JSON payload.</p><h4>Configuring &amp; Customising our Alert</h4><p>Remember we talked about the context dictionary earlier? You may have noticed that our <em>opsgenie_hook </em>is taking <em>context</em> as an argument, the contents of which we can use to build and customise our JSON payload.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ce4776435e33fcd689f9685ca60bf983/href">https://medium.com/media/ce4776435e33fcd689f9685ca60bf983/href</a></iframe><p>There’s a little bit going on here so let us unpack it piece by piece. Firstly we’re extracting some useful stuff from the context dictionary:</p><ul><li><strong><em>dag</em></strong> is the name of our DAG</li><li><strong><em>task</em></strong> is the name of our Task</li><li><strong><em>ts</em></strong> is the timestamp of when our Task was scheduled to run</li><li><strong><em>log_url</em></strong> is the location of the logs for the instance of this Task on our Airflow instance</li></ul><p>These seemed the most useful but you can see all that’s available in the context dictionary in the <a href="https://github.com/apache/airflow/blob/master/airflow/models/taskinstance.py">TaskInstance</a> class. We’re using <strong><em>dag</em></strong>, <strong><em>task</em></strong> and a slightly nicer formatted <strong><em>ts </em></strong>to<em> </em>construct a short informative message from which you can immediately discern what’s wrong. An example would be:</p><p><em>Airflow DAG eternal_failure, failed to run task_that_always_fails, scheduled at 2019–06–21 16:00.</em></p><p>We can also embed the <strong><em>log_url</em></strong> as metadata within the JSON payload itself, this makes our alert actionable, anybody responding to this alert can immediately head to the first port of call when troubleshooting an issue. We’re replacing localhost with the actual DNS of our airflow instance to avoid a manual step here too. The rest of this payload is pretty Opsgenie specific, but of course we’re routing the alert to the appropriate team, setting the appropriate priority for the alert, adding useful tags and so on. These are all things you’ll obviously want to tweak depending on your own implementation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BKlpI0kH77bD9b1mXpEr3Q.png" /><figcaption>Our actionable alert arriving in the Opsgenie platform.</figcaption></figure><p>Opsgenie, like any alerting platform, also takes care of notifying us through various channels like Email, SMS and Slack. Having a concise, informative and actionable alert is critical when you’re on call. It’s also great to be able to interact with these alerts via Slack directly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2lyU7egASAFQGIXLRTxEsg.png" /><figcaption>Interacting with our actionable alert in Slack.</figcaption></figure><h4>Conclusion</h4><p>We can all agree it’s important to know immediately when and why things aren’t working in production, especially in an era where we’re responsible for building, running and owning our complete stack end to end. The tasks you run as part of your Airflow DAGs are no exception to this, especially as it’s often likely you’re holding up some downstream workloads that can be business critical. There’s also no reason why setting up this kind of alerting should be a hassle either. Airflow delivers that ease with some really simple functionality that’s built in and trivial to leverage.</p><p>All of the Gists embedded here are available publicly and a concise final version of this example is available <a href="https://github.com/raoufaghrout/airflow-opsgenie-alerts">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=53c3f1ba2ca" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>