Airflow 报警邮件定制化

使用过airflow的应该都知道,通过在DAG文件中配置default_args可以设置邮件,但是收到过邮件的人都会发现邮件内容非常的少,有些必要信息并不会显示出来,这样就不能满足我们的生产需求,因此本文主要讲解如何定制化自己的邮件内容。

1. 配置参数,如下所示

default_args = {
    'owner': 'airflow',
    'depends_on_past': False,
    'start_date': days_ago(1),
    'email': ['[email protected]'],
    'phone': ['xxxxxxxxxx'],
    'email_on_failure': False,
    'email_on_retry': False,
    'retries': 0,
    'retry_delay': timedelta(minutes=1),
    # 'pool': 'backfill',
    # 'priority_weight': 10,
    # 'end_date': datetime(2016, 1, 1),
    # 'execution_timeout': timedelta(seconds=300),
    'on_failure_callback': failure_callback,
    # 'on_success_callback': some_other_function,
    # 'on_retry_callback': another_function
}

其中最重要的是要关注一下几个参数

'email': ['[email protected]'], -- 发送的目的地邮箱
'phone': ['xxxxxxxxxx'], -- 发送短信
'email_on_failure': False, -- 失败不发送默认邮件
'email_on_retry': False, -- 重试不发送默认邮件
'on_failure_callback': failure_callback, -- 重要:失败执行回调函数,回调函数中发送邮件和短信

2. 编写failure_callback函数功能

def failure_callback(context: dict):
    dag_id = context['dag'].dag_id
    email = context['dag'].default_args['email']
    phone = context['dag'].default_args['phone']
    schedule_interval = context['dag'].schedule_interval
    task_id = context['task_instance'].task_id
    run_id = context['run_id']
    operator = context['task_instance'].operator
    state = context['task_instance'].state
    duration = '%.1f' % context['task_instance'].duration
    max_tries = context['task_instance'].max_tries
    hostname = context['task_instance'].hostname
    start_date = context['task_instance'].start_date.strftime('%Y-%m-%d %H:%M:%S')
    end_date = context['task_instance'].end_date.strftime('%Y-%m-%d %H:%M:%S')
    params = context['params']
    var = context['var']
    test_mode = context['test_mode']
    exception = context['exception']
    execution_date = context['execution_date'].strftime('%Y-%m-%d %H:%M:%S')
    prev_execution_date = context['prev_execution_date'].strftime('%Y-%m-%d %H:%M:%S')
    next_execution_date = context['next_execution_date'].strftime('%Y-%m-%d %H:%M:%S')

    msg = f"""

Airflow {task_id}任务报警

DAG名称{dag_id}
任务名称{task_id}
运行周期{schedule_interval}
运行 ID{run_id}
任务类型{operator}
任务状态{state}
重试次数{max_tries}
持续时长{duration}s
运行主机{hostname}
计划执行时间{execution_date}
实际开始时间{start_date}
实际结束时间{end_date}
上次执行时间{prev_execution_date}
下次执行时间{next_execution_date}
参数{params}
变量{var}
是否测试模式{test_mode}
错误信息{exception}
""" send_email = ','.join(email) subject_msg = f'Airflow {task_id}任务报警' send_email_fun(msg, send_email, subject_msg) if phone is not None and len(phone) > 0: send_sms = f'Airflow DAG是【{dag_id}】的【{task_id}】任务异常' send_phone = ','.join(phone) send_sms_fun(send_sms, send_phone)

这样我们就定制化了自己的邮件内容

 

 

你可能感兴趣的:(airflow,airflow,email,sms)