How-to Guide for Pagerduty notifications

Introduction

The Pagerduty notifier (airflow.providers.pagerduty.notifications.pagerduty.PagerdutyNotifier) allows users to send messages to Pagerduty using the various on_*_callbacks at both the DAG level and Task level.

You can also use a notifier with sla_miss_callback.

Note

When notifiers are used with sla_miss_callback the context will contain only values passed to the callback, refer sla_miss_callback.

Example Code:

from datetime import datetime
from airflow import DAG
from airflow.operators.bash import BashOperator
from airflow.providers.pagerduty.notifications.pagerduty import send_pagerduty_notification

with DAG(
    "pagerduty_notifier",
    start_date=datetime(2023, 1, 1),
    on_failure_callback=[
        send_pagerduty_notification(
            summary="The dag {{ dag.dag_id }} failed",
            severity="critical",
            source="airflow dag_id: {{dag.dag_id}}",
            dedup_key="{{dag.dag_id}}-{{ti.task_id}}",
            group="{{dag.dag_id}}",
            component="airflow",
            class_type="Prod Data Pipeline",
        )
    ],
):
    BashOperator(
        task_id="mytask",
        bash_command="fail",
        on_failure_callback=[
            send_pagerduty_notification(
                summary="The task {{ ti.task_id }} failed",
                severity="critical",
                source="airflow dag_id: {{dag.dag_id}}",
                dedup_key="{{dag.dag_id}}-{{ti.task_id}}",
                group="{{dag.dag_id}}",
                component="airflow",
                class_type="Prod Data Pipeline",
            )
        ],
    )

Was this entry helpful?