要在Apache Airflow的故障邮件中包含日志,您可以使用以下代码示例来更改默认的电子邮件模板:
首先,创建一个名为email_on_failure_with_logs.py
的Python文件,并将以下代码复制到文件中:
from airflow.utils.email import send_email
from airflow import configuration as conf
from airflow.exceptions import AirflowException
def email_on_failure_with_logs(context):
"""Send custom email on task failure with logs."""
try:
subject = "[Airflow] Task Failed"
html_content = """
Task Failed
DAG: {0}
Task: {1}
Execution Time: {2}
Log:
{3}
""".format(
context['task_instance'].dag_id,
context['task_instance'].task_id,
context['execution_date'],
context['task_instance'].log_url,
)
send_email(
to=conf.get('email', 'EMAIL_DEFAULT_TO'),
subject=subject,
html_content=html_content
)
except AirflowException as e:
raise AirflowException("Failed to send email: " + str(e))
然后,在您的DAG文件中,导入email_on_failure_with_logs
方法,并在您的任务定义中添加on_failure_callback
参数,如下所示:
from email_on_failure_with_logs import email_on_failure_with_logs
default_args = {
'on_failure_callback': email_on_failure_with_logs,
# other default arguments
}
with DAG('your_dag_id', default_args=default_args, schedule_interval=None) as dag:
# your tasks
这样,当任务失败时,将会发送一封包含日志的电子邮件。请确保在airflow.cfg
配置文件中配置了正确的电子邮件设置。
上一篇:Apache Airflow的Gitlab CI/CD
下一篇:Apache Airflow的问题 - “DAG中已经存在一个task_id为create_tag_template_field_result的任务。”