首页 > 解决方案 > 气流将自定义数据附加到电子邮件警报

问题描述

我想在失败时将运行手册 URL 附加到气流电子邮件警报中。默认电子邮件发送其他有用的信息,例如气流日志链接,我不想放弃它。我怎样才能做到这一点。

我探索了电子邮件运营商,但似乎它创建了一封新电子邮件,现在我收到 2 封电子邮件,其中一封用于失败的 dag,另一封来自电子邮件运营商。

有办法解决吗?

failure_email = EmailOperator (
            task_id='failure-email',
            name='failure-email',

            to=['oncall@gmail.com'],
            subject="T3 Failed",
            html_content='<a>https://path/to/runbook</a>',
            namespace= ,
            dag=dag

        )

标签: airflow-schedulerairflow

解决方案


您可以通过在 conf 中设置变量来自定义气流电子邮件内容,该html_content_template变量的值应该是 jinja2 文件的路径。

例如:

气流.cfg -

[email]
html_content_template = /path/to/template

/路径/到/模板 -

Try {{try_number}} out of {{max_tries + 1}}<br>
Exception:<br>{{exception_html}}<br>
Log: <a href="{{ti.log_url}}">Link</a><br>
Host: {{ti.hostname}}<br>
Log file: {{ti.log_filepath}}<br>
Mark success: <a href="{{ti.mark_success_url}}">Link</a><br>
Runbook: <a>https://path/to/runbook</a>

(气流电子邮件的默认内容取自https://github.com/apache/airflow/blob/6e269570cd6dd2d434a3c63ca0db9f1a59dea847/airflow/models/taskinstance.py#L1263)。


推荐阅读