Monitoring DAGs
Professional guide to monitoring Apache Airflow DAGs using SLAs, alerts, email notifications, the webserver UI, and metrics. Includes examples, inputs/outputs, and production best practices.
Monitoring DAGs in Apache Airflow
SLAs, Alerts, Email, Webserver & Metrics ππ¨β
The Story: When Pipelines Go Silentβ
Imagine youβve built a perfect Airflow DAG.
It runs daily, processes business-critical data, and everyone trusts it.
One morning, dashboards are empty.
No errors. No emails. No alerts.
The DAG did run β but it finished 4 hours late.
Thatβs when you realize:
Scheduling is not enough. Monitoring is mandatory.
In production Airflow, monitoring DAGs is as important as writing them. This article explains how Airflow monitors DAGs, how to configure SLAs, alerts, email notifications, UI monitoring, and metrics, and how to do it professionally at scale.
What Does βMonitoring a DAGβ Really Mean?β
Monitoring in Airflow answers five critical questions:
- Did the DAG run?
- Did it finish on time?
- Did any task fail or retry?
- Did performance degrade?
- Did anyone get notified?
Airflow provides multiple monitoring layers, not just one.
Monitoring Layers in Airflowβ
| Layer | Purpose |
|---|---|
| SLA | Detects slow tasks |
| Alerts | Reacts to failures |
| Notifies humans | |
| Webserver UI | Visual monitoring |
| Metrics | Long-term observability |
Each layer covers a different failure mode.
SLA Monitoring β Detecting βSlow Successββ
What Is an SLA in Airflow?β
An SLA (Service Level Agreement) defines how long a task is allowed to run.
π Important:
An SLA does not fail the task.
It triggers an SLA miss event.
SLA Example in a DAGβ
from airflow import DAG
from airflow.operators.bash import BashOperator
from datetime import datetime, timedelta
with DAG(
dag_id="sales_reporting",
start_date=datetime(2024, 1, 1),
schedule_interval="@daily",
catchup=False,
) as dag:
generate_report = BashOperator(
task_id="generate_report",
bash_command="sleep 120",
sla=timedelta(minutes=1),
)
Inputβ
| Parameter | Value |
|---|---|
| Task runtime | 120 seconds |
| SLA | 60 seconds |
Output (SLA Miss)β
- Task succeeds β
- SLA is missed β
- SLA callback is triggered
- Visible in Airflow UI β Browse β SLA Misses
SLA Best Practicesβ
β
Use SLAs only for business-critical tasks
β Do not add SLAs to every task
β
Combine SLAs with alert callbacks
Alerts & Callbacks β Reacting to Failuresβ
Airflow allows callbacks to react to events.
Common Callbacksβ
| Callback | Trigger |
|---|---|
on_failure_callback | Task fails |
on_success_callback | Task succeeds |
sla_miss_callback | SLA missed |
Failure Alert Exampleβ
def notify_failure(context):
dag_id = context["dag"].dag_id
task_id = context["task_instance"].task_id
print(f"ALERT: {dag_id}.{task_id} failed")
task = BashOperator(
task_id="load_data",
bash_command="exit 1",
on_failure_callback=notify_failure,
)
Inputβ
| Event | Value |
|---|---|
| Task exit code | 1 |
Outputβ
ALERT: sales_reporting.load_data failed
π In production, this callback usually sends:
- Slack message
- PagerDuty alert
Email Alerts β The Simplest Notification Layerβ
Email is built-in and widely used.
Email Configuration Exampleβ
default_args = {
"email": ["data-team@example.com"],
"email_on_failure": True,
"email_on_retry": False,
}
with DAG(
dag_id="email_alert_dag",
default_args=default_args,
schedule_interval="@daily",
):
...
Inputβ
| Condition | Value |
|---|---|
| Task state | FAILED |
Outputβ
π§ Email sent with:
- DAG ID
- Task ID
- Execution date
- Log link
Email Best Practicesβ
β Avoid email floods
β
Email only on final failure
β
Combine email with chat alerts