0
0
Apache Airflowdevops~10 mins

Integration with PagerDuty and Slack in Apache Airflow - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Integration with PagerDuty and Slack
Airflow Task Fails
Trigger Alert Handler
Send Alert to PD
Alert Delivered
Log Alert
When an Airflow task fails, an alert handler checks where to send the alert. It sends notifications to PagerDuty and Slack, then logs the alert delivery.
Execution Sample
Apache Airflow
def alert_handler(context):
    if 'pagerduty' in context['alert_channels']:
        send_pagerduty_alert(context)
    if 'slack' in context['alert_channels']:
        send_slack_alert(context)
    log_alert(context)
This function sends alerts to PagerDuty and Slack based on the alert channels in the context, then logs the alert.
Process Table
StepActionConditionResultOutput
1Task fails in AirflowN/ATrigger alert_handlerAlert handler called
2Check if 'pagerduty' in alert_channelsTrueCall send_pagerduty_alertPagerDuty alert sent
3Check if 'slack' in alert_channelsTrueCall send_slack_alertSlack alert sent
4Log alert deliveryN/ALog successAlert logged
5End of alert_handlerN/AReturn from functionAlert process complete
💡 All alert channels processed and alert logged, function ends
Status Tracker
VariableStartAfter Step 2After Step 3Final
context['alert_channels']['pagerduty', 'slack']['pagerduty', 'slack']['pagerduty', 'slack']['pagerduty', 'slack']
alert sent to PagerDutyFalseTrueTrueTrue
alert sent to SlackFalseFalseTrueTrue
alert loggedFalseFalseFalseTrue
Key Moments - 3 Insights
Why does the alert_handler check for each alert channel separately instead of using else-if?
Because both PagerDuty and Slack alerts can be sent independently if both are in alert_channels, as shown in steps 2 and 3 where both conditions are checked and both alerts sent.
What happens if 'pagerduty' is not in alert_channels?
Step 2 condition becomes False, so send_pagerduty_alert is skipped, but Slack alert can still be sent if present, as shown by the condition checks in the execution_table.
Why is logging done after sending alerts?
Logging confirms alert delivery status after all alert channels are processed, as seen in step 4 where logging happens after both alerts are sent.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the Slack alert sent?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Check the 'Action' and 'Output' columns in the execution_table for Slack alert sending
According to the variable tracker, what is the value of 'alert sent to PagerDuty' after step 2?
AFalse
BNone
CTrue
DUndefined
💡 Hint
Look at the 'alert sent to PagerDuty' row under 'After Step 2' in variable_tracker
If 'slack' was removed from alert_channels, how would the execution table change?
AStep 2 would skip sending PagerDuty alert
BStep 3 would skip sending Slack alert
CStep 4 would not log alert delivery
DStep 5 would not be reached
💡 Hint
Refer to the condition checks in steps 2 and 3 in execution_table
Concept Snapshot
Integration with PagerDuty and Slack in Airflow:
- On task failure, alert_handler runs
- Checks alert_channels for 'pagerduty' and 'slack'
- Sends alerts to each channel independently
- Logs alert delivery after sending
- Ensures notifications reach both systems if configured
Full Transcript
When an Airflow task fails, it triggers an alert handler function. This function checks which alert channels are configured in the context, such as PagerDuty and Slack. If PagerDuty is included, it sends an alert there. Similarly, if Slack is included, it sends an alert to Slack. After sending alerts to all configured channels, it logs the alert delivery. This process ensures that notifications are sent to the right places and recorded for tracking.