Bird
Raised Fist0
Apache Airflowdevops~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of integrating Airflow with PagerDuty and Slack?
easy
A. To store Airflow logs in PagerDuty and Slack
B. To schedule Airflow DAGs automatically
C. To replace Airflow's scheduler with PagerDuty
D. To receive fast alerts and notifications about workflow issues

Solution

  1. Step 1: Understand the role of PagerDuty and Slack in Airflow

    PagerDuty and Slack are used to send alerts and notifications when Airflow workflows have issues or important events.
  2. Step 2: Identify the main benefit of integration

    The integration helps teams respond quickly by notifying them immediately about failures or important status updates.
  3. Final Answer:

    To receive fast alerts and notifications about workflow issues -> Option D
  4. Quick Check:

    Alerts and notifications = C [OK]
Hint: Think: Why notify teams fast? Alerts! [OK]
Common Mistakes:
  • Confusing scheduling with alerting
  • Thinking logs are stored in PagerDuty or Slack
  • Assuming PagerDuty replaces Airflow scheduler
2. Which Airflow operator is used to send alerts to PagerDuty within a DAG?
easy
A. PagerDutyIncidentOperator
B. SlackAPIPostOperator
C. EmailOperator
D. HttpSensor

Solution

  1. Step 1: Identify operators for PagerDuty and Slack

    SlackAPIPostOperator sends messages to Slack, PagerDutyIncidentOperator sends incidents to PagerDuty.
  2. Step 2: Match operator to PagerDuty alerts

    PagerDutyIncidentOperator is designed specifically for PagerDuty integration.
  3. Final Answer:

    PagerDutyIncidentOperator -> Option A
  4. Quick Check:

    PagerDuty alerts = PagerDutyIncidentOperator A [OK]
Hint: PagerDuty operator name includes 'PagerDuty' [OK]
Common Mistakes:
  • Choosing SlackAPIPostOperator for PagerDuty alerts
  • Confusing EmailOperator with PagerDuty integration
  • Selecting HttpSensor which is unrelated
3. Given this Airflow DAG snippet, what will be the output when the task notify_slack runs successfully?
notify_slack = SlackAPIPostOperator(
    task_id='notify_slack',
    token='xoxb-1234',
    channel='#alerts',
    text='DAG completed successfully!'
)
medium
A. No message is sent because the channel name is incorrect
B. An error occurs because the token is invalid
C. A message 'DAG completed successfully!' is posted to the #alerts Slack channel
D. The DAG fails to run because SlackAPIPostOperator is deprecated

Solution

  1. Step 1: Understand SlackAPIPostOperator parameters

    The operator sends a message to the specified Slack channel using the provided token and text.
  2. Step 2: Analyze the given snippet

    The token and channel are set, and the text is 'DAG completed successfully!'. If the token is valid, the message posts successfully.
  3. Final Answer:

    A message 'DAG completed successfully!' is posted to the #alerts Slack channel -> Option C
  4. Quick Check:

    Slack message sent = D [OK]
Hint: SlackAPIPostOperator posts text to channel if token valid [OK]
Common Mistakes:
  • Assuming token is invalid without evidence
  • Thinking SlackAPIPostOperator is deprecated
  • Confusing channel name format
4. You wrote this task to send a PagerDuty alert but it fails with an authentication error:
pagerduty_alert = PagerDutyIncidentOperator(
    task_id='pagerduty_alert',
    routing_key='invalid_key',
    title='Airflow failure',
    severity='critical'
)
What is the most likely cause of the failure?
medium
A. The routing_key is incorrect or invalid
B. The severity value must be 'low' instead of 'critical'
C. PagerDutyIncidentOperator does not support the title parameter
D. The task_id must be 'pagerduty_incident' exactly

Solution

  1. Step 1: Check the routing_key parameter

    The routing_key is a secret token that must be valid for PagerDuty API authentication.
  2. Step 2: Understand authentication errors

    An invalid routing_key causes authentication failure, blocking alert creation.
  3. Final Answer:

    The routing_key is incorrect or invalid -> Option A
  4. Quick Check:

    Authentication error = invalid routing_key A [OK]
Hint: Authentication errors usually mean bad keys or tokens [OK]
Common Mistakes:
  • Changing severity to fix authentication
  • Removing title parameter incorrectly
  • Changing task_id unnecessarily
5. You want to create an Airflow DAG that sends a Slack message only if a PagerDuty incident is triggered. Which approach correctly integrates both operators to achieve this?
hard
A. Use PagerDutyIncidentOperator first, then SlackAPIPostOperator with a trigger rule 'all_done' to send Slack message regardless of PagerDuty success
B. Use PagerDutyIncidentOperator first, then SlackAPIPostOperator with trigger rule 'all_success' to send Slack message only if PagerDuty alert succeeds
C. Use SlackAPIPostOperator first, then PagerDutyIncidentOperator with trigger rule 'one_failed' to send PagerDuty alert only if Slack fails
D. Use both operators in parallel without dependencies to send alerts simultaneously

Solution

  1. Step 1: Define the desired workflow

    Slack message should be sent only if PagerDuty incident is successfully triggered.
  2. Step 2: Set operator order and trigger rules

    Run PagerDutyIncidentOperator first. Set SlackAPIPostOperator to run after it with trigger_rule='all_success' to ensure Slack message only sends if PagerDuty alert succeeds.
  3. Final Answer:

    Use PagerDutyIncidentOperator first, then SlackAPIPostOperator with trigger rule 'all_success' to send Slack message only if PagerDuty alert succeeds -> Option B
  4. Quick Check:

    Slack after PagerDuty success = B [OK]
Hint: Slack depends on PagerDuty success with 'all_success' trigger [OK]
Common Mistakes:
  • Sending Slack message regardless of PagerDuty result
  • Reversing operator order
  • Running both operators in parallel without dependency
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard