简体   繁体   English

如何抓取Airflow的错误日志并发邮件?

[英]How to capture the error logs from Airflow and send it in mail?

We are trying to report the failures that occur during Airflow job execution and capture the exceptions from logs and send it in email.我们正在尝试报告 Airflow 作业执行期间发生的故障,并从日志中捕获异常并在 email 中发送。

Currently we display following things in the failure email written in pyt.目前我们在用 pyt 编写的失败 email 中显示以下内容。

failure_msg = """
            :red_circle: Task Failed.
            *Dag*: {dag}  
            *Task*: {task}
            *Execution Time*: {exec_date}
            *Log Url*: {log_url} 
            """.format(
        dag=context.get('task_instance').dag_id,
        task=context.get('task_instance').task_id,
        ti=context.get('task_instance'),
        exec_date=context.get('execution_date'),
        log_url=context.get('task_instance').log_url

I was looking to capture the exception message from Airflow. The above message displays high level info like dag id, task id, url etc.我一直在寻找来自 Airflow 的异常消息。上面的消息显示高级信息,如 dag id、任务 id、url 等。

Referrer below Airflow documentation but so far did not get any way to capture exact exception message. Referrer 低于 Airflow 文档,但到目前为止还没有任何方法来捕获确切的异常消息。

Currently I am manually throwing error in one of the DAG as目前我在 DAG 之一中手动抛出错误

def scan_table():
    try:
        raise ValueError('File not parsed completely/correctly')
        logging.info(message)
    except Exception as error:
        raise ValueError('File not parsed completely/correctly inside exception block')
        print("Error while fetching data from backend", error)

Tried using this exception=context.get('task_instance').log.exception but it showed as尝试使用此exception=context.get('task_instance').log.exception但它显示为

<bound method Logger.exception of <Logger airflow.task (INFO)>> 

In the DAG log output in Airflow UI, the exception is thrown as:在Airflow UI的DAG日志output中,抛出的异常为:

[2023-01-04, 09:05:07 UTC] {taskinstance.py:1909} ERROR - Task failed with exception
Traceback (most recent call last):
  File "/opt/bitnami/airflow/dags/git_airflow-dags/scan_table.py", line 37, in scan_table
    raise ValueError('File not parsed completely/correctly')
ValueError: File not parsed completely/correctly

I want to capture this part of log and print in the failure_msg in the Python snippet.我想捕获这部分日志并在 Python 片段中的 failure_msg 中打印。

I was able to find answer to this.我能够找到答案。

We can get access to exception thrown by Airflow using below snipped.我们可以使用下面的片段访问 Airflow 抛出的异常。

context.get('exception') context.get('异常')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM