Skip to content

Comments

AIP-72: Add logging for exception in Task Runner#45502

Merged
kaxil merged 1 commit intoapache:mainfrom
astronomer:add-log-exception
Jan 9, 2025
Merged

AIP-72: Add logging for exception in Task Runner#45502
kaxil merged 1 commit intoapache:mainfrom
astronomer:add-log-exception

Conversation

@kaxil
Copy link
Member

@kaxil kaxil commented Jan 9, 2025

Currently, when a task fails there are no log which makes it very difficult to debug.

Before:

image
{"file":"/files/dags/get_context.py","timestamp":"2025-01-09T07:17:10.066994","logger":"task","event":"DAG file parsed","level":"debug"}
{"json":"{\"rendered_fields\":{\"templates_dict\":null,\"op_args\":\"()\",\"op_kwargs\":{}},\"type\":\"SetRenderedFields\"}\n","timestamp":"2025-01-09T07:17:10.067242","logger":"task","event":"Sending request","level":"debug"}
{"json":"{\"state\":\"failed\",\"end_date\":\"2025-01-09T07:17:10.071585Z\",\"type\":\"TaskState\"}\n","timestamp":"2025-01-09T07:17:10.071638","logger":"task","event":"Sending request","level":"debug"}

After:

image
{"file":"/files/dags/get_context.py","timestamp":"2025-01-09T07:17:10.066994","logger":"task","event":"DAG file parsed","level":"debug"}
{"json":"{\"rendered_fields\":{\"templates_dict\":null,\"op_args\":\"()\",\"op_kwargs\":{}},\"type\":\"SetRenderedFields\"}\n","timestamp":"2025-01-09T07:17:10.067242","logger":"task","event":"Sending request","level":"debug"}
{"timestamp":"2025-01-09T07:17:10.071412","logger":"task","error_detail":[{"exc_type":"AirflowException","exc_value":"Current context was requested but no context was found! Are you running within an airflow task?","syntax_error":null,"is_cause":false,"frames":[{"filename":"/opt/airflow/task_sdk/src/airflow/sdk/execution_time/task_runner.py","lineno":457,"name":"run"},{"filename":"/opt/airflow/airflow/models/baseoperator.py","lineno":376,"name":"wrapper"},{"filename":"/opt/airflow/airflow/decorators/base.py","lineno":261,"name":"execute"},{"filename":"/opt/airflow/airflow/models/baseoperator.py","lineno":376,"name":"wrapper"},{"filename":"/opt/airflow/providers/src/airflow/providers/standard/operators/python.py","lineno":196,"name":"execute"},{"filename":"/opt/airflow/providers/src/airflow/providers/standard/operators/python.py","lineno":222,"name":"execute_callable"},{"filename":"/opt/airflow/airflow/utils/operator_helpers.py","lineno":269,"name":"run"},{"filename":"/files/dags/get_context.py","lineno":16,"name":"template_test"},{"filename":"/opt/airflow/providers/src/airflow/providers/standard/operators/python.py","lineno":1153,"name":"get_current_context"}]}],"event":"Task failed with exception","level":"error"}
{"json":"{\"state\":\"failed\",\"end_date\":\"2025-01-09T07:17:10.071585Z\",\"type\":\"TaskState\"}\n","timestamp":"2025-01-09T07:17:10.071638","logger":"task","event":"Sending request","level":"debug"}

^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in a newsfragment file, named {pr_number}.significant.rst or {issue_number}.significant.rst, in newsfragments.

Currently, when a task fails there are no log which makes it very difficult to debug.

Before:

```
{"file":"/files/dags/get_context.py","timestamp":"2025-01-09T07:17:10.066994","logger":"task","event":"DAG file parsed","level":"debug"}
{"json":"{\"rendered_fields\":{\"templates_dict\":null,\"op_args\":\"()\",\"op_kwargs\":{}},\"type\":\"SetRenderedFields\"}\n","timestamp":"2025-01-09T07:17:10.067242","logger":"task","event":"Sending request","level":"debug"}
{"json":"{\"state\":\"failed\",\"end_date\":\"2025-01-09T07:17:10.071585Z\",\"type\":\"TaskState\"}\n","timestamp":"2025-01-09T07:17:10.071638","logger":"task","event":"Sending request","level":"debug"}
```

After:

```
{"file":"/files/dags/get_context.py","timestamp":"2025-01-09T07:17:10.066994","logger":"task","event":"DAG file parsed","level":"debug"}
{"json":"{\"rendered_fields\":{\"templates_dict\":null,\"op_args\":\"()\",\"op_kwargs\":{}},\"type\":\"SetRenderedFields\"}\n","timestamp":"2025-01-09T07:17:10.067242","logger":"task","event":"Sending request","level":"debug"}
{"timestamp":"2025-01-09T07:17:10.071412","logger":"task","error_detail":[{"exc_type":"AirflowException","exc_value":"Current context was requested but no context was found! Are you running within an airflow task?","syntax_error":null,"is_cause":false,"frames":[{"filename":"/opt/airflow/task_sdk/src/airflow/sdk/execution_time/task_runner.py","lineno":457,"name":"run"},{"filename":"/opt/airflow/airflow/models/baseoperator.py","lineno":376,"name":"wrapper"},{"filename":"/opt/airflow/airflow/decorators/base.py","lineno":261,"name":"execute"},{"filename":"/opt/airflow/airflow/models/baseoperator.py","lineno":376,"name":"wrapper"},{"filename":"/opt/airflow/providers/src/airflow/providers/standard/operators/python.py","lineno":196,"name":"execute"},{"filename":"/opt/airflow/providers/src/airflow/providers/standard/operators/python.py","lineno":222,"name":"execute_callable"},{"filename":"/opt/airflow/airflow/utils/operator_helpers.py","lineno":269,"name":"run"},{"filename":"/files/dags/get_context.py","lineno":16,"name":"template_test"},{"filename":"/opt/airflow/providers/src/airflow/providers/standard/operators/python.py","lineno":1153,"name":"get_current_context"}]}],"event":"Task failed with exception","level":"error"}
{"json":"{\"state\":\"failed\",\"end_date\":\"2025-01-09T07:17:10.071585Z\",\"type\":\"TaskState\"}\n","timestamp":"2025-01-09T07:17:10.071638","logger":"task","event":"Sending request","level":"debug"}
```
Copy link
Contributor

@amoghrajesh amoghrajesh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, looks good!

@kaxil kaxil merged commit 9de80ab into apache:main Jan 9, 2025
42 checks passed
@kaxil kaxil deleted the add-log-exception branch January 9, 2025 07:58
karenbraganz pushed a commit to karenbraganz/airflow that referenced this pull request Jan 13, 2025
HariGS-DB pushed a commit to HariGS-DB/airflow that referenced this pull request Jan 16, 2025
got686-yandex pushed a commit to got686-yandex/airflow that referenced this pull request Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

3 participants