Monitoring, alerts en callbacks

Introductie tot Apache Airflow in Python

Mike Metzger

Data Engineer

Dag-levenscyclus

 

  • Elke Dag-run en taak doorloopt statussen
    • Queued
    • Running
    • Eindstatus: Success, Failed, Skipped
  • Airflow volgt statussen en transities

Diagram van de levenscyclus van een Dag-run: van queued naar running naar eindstatus

Introductie tot Apache Airflow in Python

Callbacks

  • Functies op Dag-run of taak die Airflow automatisch aanroept
  • Callbacks voor specifieke statustransities
    • on_failure_callback - Dag-run/taak faalt
    • on_success_callback- Dag-run/taak slaagt
  • Taakspecifieke callbacks
    • on_retry_callback
    • on_skipped_callback
    • on_execute_callback

Illustratie van Airflow-callbacks die worden getriggerd bij statustransities

Introductie tot Apache Airflow in Python

Callback-context

  • Airflow geeft automatisch een context-dictionary door
  • context bevat info over de Dag-run/taak
    • context["dag"].dag_id - Naam van de Dag
    • context["task_instance"].task_id - Naam van de taak
    • context["logical_date"] - Datum van de Dag-run
  • Callback-functie moet context accepteren
def alert_on_failure(context):

dag_id = context["dag"].dag_id task_id = context["task_instance"].task_id print(f"Task {task_id} in DAG {dag_id} has failed.")
Introductie tot Apache Airflow in Python

Callback-voorbeeld

def alert_on_failure(context):
    dag_id = context["dag"].dag_id
    task_id = context["task_instance"].task_id
    print(f"Task {task_id} in Dag {dag_id} has failed.")

@dag(on_failure_callback=alert_on_failure) def sales_etl_dag(): @task() def data_import_task(): raise ValueError("Simulated failure")
# Task data_import_task in Dag sales_etl_dag has failed.
Introductie tot Apache Airflow in Python

Notifiers

  • Functies die je aan callbacks kunt koppelen
  • Stuur alerts naar externe systemen
  • Beschikbare notifiers:
    • SmtpNotifier - Verstuurt e-mailalerts
    • SlackNotifier - Post berichten in een Slack-kanaal
    • App-specifieke notifiers - PagerDuty, OpsGenie

Illustratie van Airflow-notifiers die alerts naar externe systemen sturen

Introductie tot Apache Airflow in Python

SmtpNotifier

  • Verstuurt bij een callback een e-mail
  • In de library airflow.providers.smtp.notifications.smtp
  • Vereist from_email en to
  • Kan subject, html_content, etc. bevatten
    @dag(dag_id=`sales_etl_dag`,
       on_failure_callback=SmtpNotifier(
           to='[email protected]',
           from_email='[email protected]',
           subject='Dag sales_etl_dag has failed'
       )
    )
    
Introductie tot Apache Airflow in Python

Audit Log

  • Gebruik de Audit Log om Airflow te monitoren
  • Bevat de volgorde van alle events op de Airflow-instance

Airflow-auditlogpagina met systeemevents per type

Introductie tot Apache Airflow in Python

Laten we oefenen!

Introductie tot Apache Airflow in Python

Preparing Video For Download...