📖 Scenario: You are managing data pipelines using Apache Airflow. You want to track when tasks miss their Service Level Agreement (SLA) deadlines and send notifications to the team.This helps ensure timely data processing and quick response to delays.
🎯 Goal: Build an Airflow DAG that defines tasks with SLAs and configures an SLA miss callback function to send notifications.You will create the DAG, set SLA times, define the notification function, and verify the SLA miss handling.
📋 What You'll Learn
Create an Airflow DAG with at least two tasks
Set SLA times for the tasks using sla parameter
Define a function called sla_miss_callback that accepts dag, task_list, blocking_task_list, slas, session parameters
Configure the DAG to use sla_miss_callback for SLA miss notifications
Print a message inside sla_miss_callback showing which tasks missed SLA
Trigger the DAG run and observe SLA miss notification output
💡 Why This Matters
🌍 Real World
In real data engineering teams, monitoring SLAs ensures pipelines run on time. SLA miss notifications help quickly fix delays to keep data fresh.
💼 Career
Understanding SLA misses and notifications is important for DevOps and data engineering roles managing workflow reliability and alerting.
Progress0 / 4 steps
1
Create the Airflow DAG and tasks
Create an Airflow DAG called sla_notification_dag with start_date set to datetime(2024, 1, 1) and schedule_interval set to timedelta(days=1). Inside the DAG, create two PythonOperator tasks named task_a and task_b that run simple Python functions printing 'Task A executed' and 'Task B executed' respectively.
Apache Airflow
Hint
Use DAG to create the DAG and PythonOperator for tasks. Define simple functions for python_callable.
2
Add SLA times to the tasks
Add an SLA of 10 minutes to task_a and 15 minutes to task_b by setting the sla parameter with timedelta(minutes=10) and timedelta(minutes=15) respectively in their PythonOperator definitions.
Apache Airflow
Hint
Add the sla argument inside the PythonOperator constructor for each task.
3
Define the SLA miss callback function
Define a function called sla_miss_callback that accepts parameters dag, task_list, blocking_task_list, slas, and session. Inside the function, print a message: "SLA missed for tasks: {task_list}" using an f-string.
Apache Airflow
Hint
Define the function with the exact name and parameters. Use an f-string to print the task list.
4
Configure the DAG to use the SLA miss callback and print output
Add the sla_miss_callback function to the sla_notification_dag by setting the sla_miss_callback parameter in the DAG constructor. Then, write a print statement outside the DAG that prints "SLA miss notification setup complete".
Apache Airflow
Hint
Pass sla_miss_callback=sla_miss_callback when creating the DAG. Use print() to show the confirmation message.
Practice
(1/5)
1. What does setting an sla parameter on an Airflow task do?
easy
A. It defines a time limit; if the task runs longer, it triggers an SLA miss.
B. It sets the maximum retries for the task.
C. It schedules the task to run at a specific time.
D. It disables the task from running automatically.
Solution
Step 1: Understand the purpose of sla in Airflow tasks
The sla parameter sets a time limit for task execution.
Step 2: Identify what happens when the limit is exceeded
If the task runs longer than the sla time, Airflow marks it as an SLA miss.
Final Answer:
It defines a time limit; if the task runs longer, it triggers an SLA miss. -> Option A
Quick Check:
sla sets time limit = C [OK]
Hint: Remember SLA means max allowed time for task [OK]
Common Mistakes:
Confusing SLA with retry count
Thinking SLA schedules task start time
Assuming SLA disables tasks
2. Which of the following is the correct way to define an SLA of 30 minutes on a task in Airflow?
easy
A. sla='30 minutes'
B. sla=30
C. sla=timedelta(minutes=30)
D. sla='00:30:00'
Solution
Step 1: Recall the correct data type for sla
The sla parameter expects a datetime.timedelta object.
Step 2: Identify the correct syntax for 30 minutes timedelta
timedelta(minutes=30) correctly creates a 30-minute duration.
Final Answer:
sla=timedelta(minutes=30) -> Option C
Quick Check:
sla uses timedelta object = B [OK]
Hint: Use timedelta for time durations in Airflow [OK]
Common Mistakes:
Using string instead of timedelta
Passing integer without timedelta
Using incorrect string format
3. Given this DAG snippet, what will happen if task1 runs longer than 10 minutes?
from datetime import timedelta
from airflow import DAG
from airflow.operators.dummy import DummyOperator
from airflow.utils.dates import days_ago
def sla_miss_callback(dag, task_list, blocking_task_list, slas, session=None):
print('SLA missed!')
dag = DAG('sla_example', start_date=days_ago(1))
task1 = DummyOperator(
task_id='task1',
dag=dag,
sla=timedelta(minutes=10)
)
dag.sla_miss_callback = sla_miss_callback
medium
A. The sla_miss_callback function will print 'SLA missed!' when the SLA is missed.
B. Nothing happens because DummyOperator ignores SLA.
C. The task will automatically retry until it finishes within 10 minutes.
D. Airflow will fail the DAG immediately.
Solution
Step 1: Understand SLA miss callback usage
The DAG-level sla_miss_callback is called when any task misses its SLA.
Step 2: Identify what the callback does
The callback prints 'SLA missed!' when triggered by a task exceeding its SLA.
Final Answer:
The sla_miss_callback function will print 'SLA missed!' when the SLA is missed. -> Option A
Quick Check:
SLA miss triggers callback print = A [OK]
Hint: SLA miss triggers callback, not retries or failure [OK]
Common Mistakes:
Thinking SLA causes retries automatically
Assuming DummyOperator ignores SLA
Believing DAG fails immediately on SLA miss
4. You wrote this code to notify on SLA misses but no notification happens: