Airflow 报警邮件定制化

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

1. 配置参数,如下所示

default_args = {
    'owner': 'airflow',
    'depends_on_past': False,
    'start_date': days_ago(1),
    'email': ['xxx@qq.com'],
    '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': ['xxx@qq.com'], -- 发送的目的地邮箱
'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"""<table width='100%' border='1' cellpadding='2' style='border-collapse: collapse'>
        <h1 style='color: red;'>Airflow {task_id}任务报警</h1>
        <tr><td width='150px'>DAG名称</td><td>{dag_id}</td></tr>
        <tr><td width='150px'>任务名称</td><td>{task_id}</td></tr>
        <tr><td width='150px'>运行周期</td><td>{schedule_interval}</td></tr>
        <tr><td width='150px'>运行 ID</td><td>{run_id}</td></tr>
        <tr><td width='150px'>任务类型</td><td>{operator}</td></tr>
        <tr><td width='150px' style='color: red;'>任务状态</td><td style='color: red;'>{state}</td></tr>
        <tr><td width='150px'>重试次数</td><td>{max_tries}</td></tr>
        <tr><td width='150px'>持续时长</td><td>{duration}s</td></tr>
        <tr><td width='150px'>运行主机</td><td>{hostname}</td></tr>
        <tr><td width='150px'>计划执行时间</td><td>{execution_date}</td></tr>
        <tr><td width='150px'>实际开始时间</td><td>{start_date}</td></tr>
        <tr><td width='150px'>实际结束时间</td><td>{end_date}</td></tr>
        <tr><td width='150px'>上次执行时间</td><td>{prev_execution_date}</td></tr>
        <tr><td width='150px'>下次执行时间</td><td>{next_execution_date}</td></tr>
        <tr><td width='150px'>参数</td><td>{params}</td></tr>
        <tr><td width='150px'>变量</td><td>{var}</td></tr>
        <tr><td width='150px'>是否测试模式</td><td>{test_mode}</td></tr>
        <tr><td width='150px' style='color: red;'>错误信息</td><td style='color: red;'>{exception}</td></tr></table>
    """

    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)

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

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值