0

Requirement: How to get the error message into the slack message Airflow version: 2.2.4

This below slack message only says it failed but doesn't say the error why it failed

def slack_alert(context):
    SLACK_CONN_ID = 'slack_conn_id'
    slack_webhook_token = BaseHook.get_connection(SLACK_CONN_ID).password

    slack_msg = """
            :red_circle: Task Failed. 
            *Task*: {task}  
            *Dag*: {dag} 
            *Execution Time*: {exec_date}  
            *Log Url*: {log_url} 
            """.format(
        task=context.get("task_instance").task_id,
        dag=context.get("task_instance").dag_id,
        ti=context.get("task_instance"),
        exec_date=context.get("logical_date"),
        log_url=context.get("task_instance").log_url.replace(
            "http://localhost:8080", "https://airflow.yourdomain.com"
        ),
    )

    slack_notification =SlackWebhookOperator(
        task_id="slack_notification",
        http_conn_id=SLACK_CONN_ID,
        webhook_token=slack_webhook_token,
        message=slack_msg,
        username='airflow')
    return slack_notification.execute(context=context)
Karthik
  • 441
  • 5
  • 17

1 Answers1

0

Figured out the answer , its context.get("errror") or context.get("reason")

Karthik
  • 441
  • 5
  • 17