You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "TiDeane (via GitHub)" <gi...@apache.org> on 2024/03/26 14:58:22 UTC

[PR] Fix #35622: TaskHandlerWithCustomFormatter now adds prefix only once [airflow]

TiDeane opened a new pull request, #38502:
URL: https://github.com/apache/airflow/pull/38502

   When using the [TaskHandlerWithCustomFormatter](https://github.com/apache/airflow/blob/main/airflow/utils/log/task_handler_with_custom_formatter.py) to add a prefix to logs, it was previously adding the prefix multiple times. This happened because its `set_context` method was being called multiple times from [logging_mixin.py](https://github.com/apache/airflow/blob/main/airflow/utils/log/logging_mixin.py#L256)'s `set_context`, and worsened because even when the handler's formatter was a [TimezoneAware](https://github.com/apache/airflow/blob/main/airflow/utils/log/timezone_aware.py) formatter (to include UTC offset), it was still adding an additional prefix. Because of this, I felt that any solution outside of the TaskHandlerWithCustomFormatter itself would either require a restructuring of the handlers' structure or slow down execution for all other handlers. And so, the solution I settled on was to add to [TaskHandlerWithCustomFormatter's initial 'if' statement](https://github.co
 m/apache/airflow/blob/main/airflow/utils/log/task_handler_with_custom_formatter.py#L48) a simple "`or self.prefix_jinja_template is not None`", so that it returns if the prefix has already been set. This is similar to what is done by the ElasticSearch handler [es_task_handler.py](https://github.com/apache/airflow/blob/main/airflow/providers/elasticsearch/log/es_task_handler.py#L443).
   
   Note: also fixed the documentation's example for the handler, as the previous one was incorrect and didn't work.
   
   closes: #35622
   
   <!--
    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at
   
      http://www.apache.org/licenses/LICENSE-2.0
   
    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.
    -->
   
   <!-- Please keep an empty line above the dashes. -->
   ---
   **^ Add meaningful description above**
   Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)** for more information.
   In case of fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals)) is needed.
   In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
   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](https://github.com/apache/airflow/tree/main/newsfragments).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix TaskHandlerWithCustomFormatter now adds prefix only once [airflow]

Posted by "TiDeane (via GitHub)" <gi...@apache.org>.
TiDeane commented on PR #38502:
URL: https://github.com/apache/airflow/pull/38502#issuecomment-2085408806

   Hello, I just rebased my fork, could I get this bug fix merged? @Lee-W 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix TaskHandlerWithCustomFormatter now adds prefix only once [airflow]

Posted by "boring-cyborg[bot] (via GitHub)" <gi...@apache.org>.
boring-cyborg[bot] commented on PR #38502:
URL: https://github.com/apache/airflow/pull/38502#issuecomment-2092568364

   Awesome work, congrats on your first merged pull request! You are invited to check our [Issue Tracker](https://github.com/apache/airflow/issues) for additional contributions.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix TaskHandlerWithCustomFormatter now adds prefix only once [airflow]

Posted by "Lee-W (via GitHub)" <gi...@apache.org>.
Lee-W merged PR #38502:
URL: https://github.com/apache/airflow/pull/38502


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix #35622: TaskHandlerWithCustomFormatter now adds prefix only once [airflow]

Posted by "boring-cyborg[bot] (via GitHub)" <gi...@apache.org>.
boring-cyborg[bot] commented on PR #38502:
URL: https://github.com/apache/airflow/pull/38502#issuecomment-2020666092

   Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contributors' Guide (https://github.com/apache/airflow/blob/main/contributing-docs/README.rst)
   Here are some useful points:
   - Pay attention to the quality of your code (ruff, mypy and type annotations). Our [pre-commits]( https://github.com/apache/airflow/blob/main/contributing-docs/08_static_code_checks.rst#prerequisites-for-pre-commit-hooks) will help you with that.
   - In case of a new feature add useful documentation (in docstrings or in `docs/` directory). Adding a new operator? Check this short [guide](https://github.com/apache/airflow/blob/main/docs/apache-airflow/howto/custom-operator.rst) Consider adding an example DAG that shows how users should use it.
   - Consider using [Breeze environment](https://github.com/apache/airflow/blob/main/dev/breeze/doc/README.rst) for testing locally, it's a heavy docker but it ships with a working Airflow and a lot of integrations.
   - Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
   - Please follow [ASF Code of Conduct](https://www.apache.org/foundation/policies/conduct) for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
   - Be sure to read the [Airflow Coding style]( https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#coding-style-and-best-practices).
   - Always keep your Pull Requests rebased, otherwise your build might fail due to changes not related to your commits.
   Apache Airflow is a community-driven project and together we are making it better 🚀.
   In case of doubts contact the developers at:
   Mailing List: dev@airflow.apache.org
   Slack: https://s.apache.org/airflow-slack
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix TaskHandlerWithCustomFormatter now adds prefix only once [airflow]

Posted by "Lee-W (via GitHub)" <gi...@apache.org>.
Lee-W commented on PR #38502:
URL: https://github.com/apache/airflow/pull/38502#issuecomment-2085548139

   Just approved the CI. If everything works fine, I'm planning on merging this early tomorrow. Please let me know if anyone wants to take a deep look


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix TaskHandlerWithCustomFormatter now adds prefix only once [airflow]

Posted by "TiDeane (via GitHub)" <gi...@apache.org>.
TiDeane commented on code in PR #38502:
URL: https://github.com/apache/airflow/pull/38502#discussion_r1541517912


##########
airflow/utils/log/task_handler_with_custom_formatter.py:
##########
@@ -45,7 +45,7 @@ def set_context(self, ti) -> None:
         :param ti:
         :return:
         """
-        if ti.raw or self.formatter is None:
+        if ti.raw or self.formatter is None or self.prefix_jinja_template is not None:

Review Comment:
   Thank you for the quick response! I updated the tests on `test_task_handler_with_custom_formatter.py`.
   
   I changed [`test_custom_formatter_custom_format_not_affected_by_config`](https://github.com/apache/airflow/blob/main/tests/utils/test_task_handler_with_custom_formatter.py#L87) to make it call `logging_mixin`'s `set_context` three times instead of one. As expected, even after adding more calls of `set_context` (or doing `assert expected_format == handler.formatter._fmt` in-between each call, which isn't included in the final version) the final result is correct, that is, only a single prefix is added.
   
   Along with this, I also made it so that both this test and [`test_custom_formatter_default_format`](https://github.com/apache/airflow/blob/main/tests/utils/test_task_handler_with_custom_formatter.py#L81) reset the handler's formatter at the end of their execution. This had to be changed, because previously the tests were passing by pure luck:
   - After `test_custom_formatter_default_format`, an empty prefix  (":") would be added to `handler.formatter._fmt`;
   - When `test_custom_formatter_custom_format_not_affected_by_config` was called, `expected_value` became `f"{prefix}:{handler.formatter._fmt}"`, which had an extra ":" because of the previously added prefix;
   - Finally, the `set_context` call would add an extra prefix on top of the previous one (the original bug), which would result in the given prefix with two ":", which was equal to the expected value and resulted in the test passing.
   
   Now it should work properly, and it also verifies that repeated calls of `set_context` don't add repeated prefixes.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix TaskHandlerWithCustomFormatter now adds prefix only once [airflow]

Posted by "Lee-W (via GitHub)" <gi...@apache.org>.
Lee-W commented on code in PR #38502:
URL: https://github.com/apache/airflow/pull/38502#discussion_r1544998812


##########
airflow/utils/log/task_handler_with_custom_formatter.py:
##########
@@ -45,7 +45,7 @@ def set_context(self, ti) -> None:
         :param ti:
         :return:
         """
-        if ti.raw or self.formatter is None:
+        if ti.raw or self.formatter is None or self.prefix_jinja_template is not None:

Review Comment:
   I just take another look and it looks good to me now. Thanks!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix TaskHandlerWithCustomFormatter now adds prefix only once [airflow]

Posted by "Lee-W (via GitHub)" <gi...@apache.org>.
Lee-W commented on code in PR #38502:
URL: https://github.com/apache/airflow/pull/38502#discussion_r1540379167


##########
airflow/utils/log/task_handler_with_custom_formatter.py:
##########
@@ -45,7 +45,7 @@ def set_context(self, ti) -> None:
         :param ti:
         :return:
         """
-        if ti.raw or self.formatter is None:
+        if ti.raw or self.formatter is None or self.prefix_jinja_template is not None:

Review Comment:
   Not sure whether we should add a comment on why `self.prefix_jinja_template is not None:` is added. It might not be that straightforward when looking at this condition alone. Also, could we also add a test case for it?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix TaskHandlerWithCustomFormatter now adds prefix only once [airflow]

Posted by "TiDeane (via GitHub)" <gi...@apache.org>.
TiDeane commented on code in PR #38502:
URL: https://github.com/apache/airflow/pull/38502#discussion_r1541517912


##########
airflow/utils/log/task_handler_with_custom_formatter.py:
##########
@@ -45,7 +45,7 @@ def set_context(self, ti) -> None:
         :param ti:
         :return:
         """
-        if ti.raw or self.formatter is None:
+        if ti.raw or self.formatter is None or self.prefix_jinja_template is not None:

Review Comment:
   Thank you for the quick response! I updated the tests on `test_task_handler_with_custom_formatter.py`, and added a comment on `TaskHandlerWithCustomFormatter`'s initial if statement.
   
   I changed [`test_custom_formatter_custom_format_not_affected_by_config`](https://github.com/apache/airflow/blob/main/tests/utils/test_task_handler_with_custom_formatter.py#L87) to make it call `logging_mixin`'s `set_context` three times instead of one. As expected, even after adding more calls of `set_context` (or doing `assert expected_format == handler.formatter._fmt` in-between each call, which isn't included in the final version) the final result is correct, that is, only a single prefix is added.
   
   Along with this, I also made it so that both this test and [`test_custom_formatter_default_format`](https://github.com/apache/airflow/blob/main/tests/utils/test_task_handler_with_custom_formatter.py#L81) reset the handler's formatter at the end of their execution. This had to be changed, because previously the tests were passing by pure luck:
   - After `test_custom_formatter_default_format`, an empty prefix  (":") would be added to `handler.formatter._fmt`;
   - When `test_custom_formatter_custom_format_not_affected_by_config` was called, `expected_value` became `f"{prefix}:{handler.formatter._fmt}"`, which had an extra ":" because of the previously added prefix;
   - Finally, the `set_context` call would add an extra prefix on top of the previous one (the original bug), which would result in the given prefix with two ":", which was equal to the expected value and resulted in the test passing.
   
   Now it should work properly, and it also verifies that repeated calls of `set_context` don't add repeated prefixes.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org