<?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 Hannan Kravitz on Medium]]></title>
        <description><![CDATA[Stories by Hannan Kravitz on Medium]]></description>
        <link>https://medium.com/@hkravitz?source=rss-a933111a352d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*qv7tz-wKjx9pmxdQ</url>
            <title>Stories by Hannan Kravitz on Medium</title>
            <link>https://medium.com/@hkravitz?source=rss-a933111a352d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 04:05:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hkravitz/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[Monitoring Airflow DAG Metadata Changes: A Practical Approach]]></title>
            <link>https://medium.com/@hkravitz/monitoring-airflow-dag-metadata-changes-a-practical-approach-e3971ce3750c?source=rss-a933111a352d------2</link>
            <guid isPermaLink="false">https://medium.com/p/e3971ce3750c</guid>
            <category><![CDATA[airflow]]></category>
            <category><![CDATA[airflow-dag]]></category>
            <dc:creator><![CDATA[Hannan Kravitz]]></dc:creator>
            <pubDate>Tue, 10 Dec 2024 19:02:13 GMT</pubDate>
            <atom:updated>2024-12-10T19:02:13.561Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/692/1*zTRa7pkCwdmbwRlfqYLwgA.png" /></figure><h3>Introduction</h3><p>As the complexity of workflows in Airflow increases, managing and monitoring the metadata of DAGs becomes essential. Have you ever wondered if there’s a way to automatically:</p><ul><li>Detect new DAGs added to your environment?</li><li>Identify DAGs removed from the system?</li><li>Monitor changes in DAG schedules or their paused states?</li></ul><p>In this article, I’ll demonstrate how to create two interconnected Airflow DAGs to:</p><ol><li>Detect and track changes in Airflow DAG metadata.</li><li>Trigger alerts and update the stored metadata when changes are found.</li></ol><p>This solution uses:</p><ul><li>Airflow Variables to store and retrieve metadata snapshots.</li><li>Airflow Datasets to trigger downstream workflows efficiently.</li><li>Slack for real-time alerts about changes.</li></ul><h3>Solution Overview</h3><p>We’ll implement two DAGs:</p><ol><li><strong>Metadata Change Detection DAG</strong>: This DAG runs at regular intervals (e.g., hourly). It compares the current DAG metadata against the last stored state and sends Slack notifications if changes are detected.</li><li><strong>Metadata Storage DAG</strong>: Triggered only when changes are detected, this DAG updates the stored metadata in an Airflow Variable.</li></ol><h3>DAG 1: Metadata Change Detection DAG</h3><p>The <strong>Metadata Change Detection DAG</strong>:</p><ul><li>Compares the current metadata fetched from DagBag() with the stored metadata in the Airflow Variable dag_details_variable.</li><li>Sends a detailed Slack notification if changes are found.</li><li>Updates a Dataset to trigger the storage DAG.</li></ul><p>Here’s the complete code:</p><pre><br>from datetime import datetime, timedelta<br>import requests<br>from airflow.datasets import Dataset<br>from airflow.decorators import task, dag<br>from airflow.models import DagBag, Variable<br><br><br>SCHEDULE_INTERVAL = &#39;55 * * * *&#39;<br>SLACK_WEBHOOK_URL = &#39;https://hooks.slack.com/services/&lt;your_slack_token&gt;&#39;<br>dataset_trigger = Dataset(&quot;dags_metadata/set_variable/dag_details_variable&quot;)<br><br>def send_slack_message(changes, last_updated_timestamp):<br>    blocks = [<br>        {<br>            &quot;type&quot;: &quot;section&quot;,<br>            &quot;text&quot;: {<br>                &quot;type&quot;: &quot;mrkdwn&quot;,<br>                &quot;text&quot;: f&quot;*DAG Status Changes Detected* (Last updated: {last_updated_timestamp})&quot;<br>            }<br>        },<br>        {&quot;type&quot;: &quot;divider&quot;}<br>    ]<br>    for change in changes:<br>        if &quot;changed&quot; in change:<br>            blocks.append({<br>                &quot;type&quot;: &quot;section&quot;,<br>                &quot;text&quot;: {<br>                    &quot;type&quot;: &quot;mrkdwn&quot;,<br>                    &quot;text&quot;: f&quot;:warning: {change}&quot;<br>                }<br>            })<br>        elif &quot;is new&quot; in change:<br>            blocks.append({<br>                &quot;type&quot;: &quot;section&quot;,<br>                &quot;text&quot;: {<br>                    &quot;type&quot;: &quot;mrkdwn&quot;,<br>                    &quot;text&quot;: f&quot;:heavy_plus_sign: {change}&quot;<br>                }<br>            })<br>        elif &quot;was removed&quot; in change:<br>            blocks.append({<br>                &quot;type&quot;: &quot;section&quot;,<br>                &quot;text&quot;: {<br>                    &quot;type&quot;: &quot;mrkdwn&quot;,<br>                    &quot;text&quot;: f&quot;:wastebasket: {change}&quot;<br>                }<br>            })<br><br>    slack_data = {&quot;blocks&quot;: blocks}<br>    response = requests.post(SLACK_WEBHOOK_URL, json=slack_data)<br>    if response.status_code != 200:<br>        raise Exception(f&quot;Slack notification failed: {response.text}&quot;)<br>    else:<br>        print(f&quot;Message was sent successfully&quot;)<br><br>def compare_and_notify(**kwargs):<br>    stored_data = Variable.get(&quot;dag_details_variable&quot;, default_var=&#39;{}&#39;, deserialize_json=True)<br>    stored_dags = {dag[&#39;DAG ID&#39;]: dag for dag in stored_data.get(&quot;dags_info&quot;, [])}<br>    current_dags = {}<br>    dagbag = DagBag()<br><br>    differences = []<br>    for dag_id, dag in dagbag.dags.items():<br>        current_info = {<br>            &quot;DAG ID&quot;: dag_id,<br>            &quot;Schedule&quot;: str(dag.schedule_interval),<br>            &quot;Paused&quot;: dag.get_is_paused(),<br>            &quot;Number of Tasks&quot;: len(dag.tasks)<br>        }<br>        current_dags[dag_id] = current_info<br><br>        if dag_id in stored_dags:<br>            stored_info = stored_dags[dag_id]<br>            if current_info != stored_info:<br>                differences.append(f&quot;DAG &#39;{dag_id}&#39; metadata changed.&quot;)<br>        else:<br>            differences.append(f&quot;DAG &#39;{dag_id}&#39; is new.&quot;)<br><br>    for stored_dag_id in stored_dags:<br>        if stored_dag_id not in current_dags:<br>            differences.append(f&quot;DAG &#39;{stored_dag_id}&#39; was removed.&quot;)<br><br>    if differences:<br>        send_slack_message(differences, stored_data.get(&quot;update_time&quot;))<br>        return True<br>    return False<br><br>DEFAULT_DAG_ARGS = {<br>    &#39;start_date&#39;: datetime(2024, 12, 10),<br>    &#39;retries&#39;: 3,<br>    &#39;retry_delay&#39;: timedelta(minutes=2),<br>}<br><br>@dag(<br>    dag_id=&quot;metadata_change_detection&quot;,<br>    description=&#39;Airflow Metadata Change Detection&#39;,<br>    schedule=SCHEDULE_INTERVAL,<br>    default_args=DEFAULT_DAG_ARGS<br>)<br>def taskflow():<br>    @task.short_circuit<br>    def detect_changes():<br>        return compare_and_notify()<br><br>    @task(outlets=[dataset_trigger])<br>    def trigger_update():<br>        print(f&quot;Changes detected, triggering Dataset {dataset_trigger}&quot;)<br><br>    changes_detected = detect_changes()<br>    changes_detected &gt;&gt; trigger_update()<br><br>dag = taskflow()</pre><h3>DAG 2: Metadata Storage DAG</h3><p>The <strong>Metadata Storage DAG</strong> updates the metadata stored in dag_details_variable. It is triggered by the Dataset created in the previous DAG.</p><p>Here’s the code:</p><pre>from airflow.decorators import task, dag<br>from airflow.models import DagBag, Variable<br>from airflow.datasets import Dataset<br>from datetime import datetime<br><br>dataset_trigger = Dataset(&quot;dags_metadata/set_variable/dag_details_variable&quot;)<br><br>@dag(<br>    dag_id=&quot;metadata_storage&quot;,<br>    description=&#39;Update stored Airflow metadata&#39;,<br>    schedule=[dataset_trigger]<br>)<br>def taskflow():<br>    @task<br>    def update_metadata():<br>        dagbag = DagBag()<br>        all_dags = [{<br>            &quot;DAG ID&quot;: dag_id,<br>            &quot;Schedule&quot;: str(dag.schedule_interval),<br>            &quot;Paused&quot;: dag.get_is_paused(),<br>            &quot;Number of Tasks&quot;: len(dag.tasks)<br>        } for dag_id, dag in dagbag.dags.items()]<br>        Variable.set(&quot;dag_details_variable&quot;, {<br>            &quot;update_time&quot;: str(datetime.now()),<br>            &quot;dags_info&quot;: all_dags<br>        })<br><br>    update_metadata()<br><br>dag = taskflow()</pre><p>This is how the changes will be reflected in your slack channel:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/705/1*Daw_FLFAyjvC8PQVEFTTNQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/618/1*SBCZJb5YFJVmg9t8bnF0dw.png" /></figure><h3>Benefits</h3><ul><li><strong>Proactive Monitoring</strong>: Detect and respond to changes in DAGs quickly.</li><li><strong>Automated Alerts</strong>: Stay informed with detailed Slack notifications.</li><li><strong>Efficient Execution</strong>: Use Airflow Datasets to optimize DAG triggering.</li></ul><h3>Conclusion</h3><p>This two-DAG approach leverages Airflow’s powerful features to monitor and manage DAG metadata effectively. By combining Slack alerts, Variables, and Datasets, this solution provides operational transparency and enhances the robustness of your workflow management.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e3971ce3750c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>