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
Webserver UI β Real-Time Visual Monitoringβ
The Airflow Web UI is the first monitoring tool most teams use.
Key UI Views for Monitoringβ
| View | Use Case |
|---|---|
| DAGs View | Overall health |
| Grid View | Task state history |
| Graph View | Dependencies |
| Gantt View | Performance bottlenecks |
| SLA Misses | Slow tasks |
What the UI Tells You Instantlyβ
- Which DAG is failing
- Which task is retrying
- Where time is being spent
- Historical stability
π The UI reads directly from the metadata database.
Metrics β Production-Grade Monitoringβ
For serious environments, metrics are non-negotiable.
Airflow emits metrics via StatsD.
Common Airflow Metricsβ
| Metric | Meaning |
|---|---|
dagrun.duration | DAG runtime |
task.duration | Task runtime |
task.failures | Failure count |
scheduler.heartbeat | Scheduler health |
Example Metric Flowβ
Airflow β StatsD β Prometheus β Grafana
Inputβ
| Task runtime | 95 seconds |
Output (Metric)β
airflow.task.duration{dag="sales_reporting",task="generate_report"} 95
π This enables:
- SLA trend analysis
- Capacity planning
- Early performance warnings
Putting It All Together β A Monitoring Strategyβ
| Scenario | Monitoring Layer |
|---|---|
| Task failed | Callback + Email |
| DAG slow | SLA + Metrics |
| Scheduler down | Metrics |
| Manual investigation | Web UI |
No single tool is enough. Monitoring is a system.
Common Monitoring Mistakesβ
β Relying only on email
β Ignoring SLA misses
β No metrics in production
β Alert fatigue
β No ownership of alerts
Input & Output β Realistic Monitoring Scenarioβ
Inputβ
- DAG runs daily
- 20 tasks
- SLA = 30 minutes
- Email on failure
- Metrics enabled
Outputβ
| Event | Result |
|---|---|
| Task fails | Email + alert |
| Task slow | SLA miss logged |
| DAG slower over time | Grafana trend |
| Scheduler unhealthy | Ops alert |
Summary π§ β
- Monitoring is not optional in Airflow
- SLAs detect slow success
- Alerts react to failures
- Email notifies humans
- UI helps investigate
- Metrics ensure long-term stability
Key Takeawaysβ
- β Use SLAs sparingly but meaningfully
- β Always configure failure callbacks
- β Email is a baseline, not a strategy
- β Metrics are required for production
- β Monitoring prevents silent failures
Whatβs Next? πβ
β‘οΈ Task Retries & Error Handling in Airflow
(How retries work, backoff strategies, and failure design patterns)