vincbeck commented on code in PR #32646: URL: https://github.com/apache/airflow/pull/32646#discussion_r1273627776
########## airflow/utils/log/task_log_shipper.py: ########## @@ -0,0 +1,91 @@ +# +# 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. +from __future__ import annotations + +import logging +from copy import copy +from typing import TYPE_CHECKING + +from airflow.config_templates.airflow_local_settings import TASK_LOG_SHIPPER_ENABLED + +if TYPE_CHECKING: + from airflow.models.taskinstance import TaskInstance + + +logger = logging.getLogger(__name__) + + +class TaskLogShipper: + """ + TaskLogShipper enables shipping, typically in exceptional circumstances, messages to the task instance + logs from e.g. the executor or scheduler. + + :meta private: + """ + + def __init__(self, component_name: str): + """ + Initialize the task log shipper with the component name. + + :param component_name: the name of the component that will be used to identify the log messages + """ + self.component_name = component_name + self.task_handler = self._get_task_handler() + self.task_handler_can_ship_logs = self._can_ship_logs() + + def _can_ship_logs(self) -> bool: + if not TASK_LOG_SHIPPER_ENABLED: + return False + if self.task_handler is None or not self.task_handler.supports_task_log_ship: + logger.warning("Task handler does not support task log shipping") + return False + return True + + @staticmethod + def _get_task_handler(): + """Returns the task handler that supports task log shipping.""" + handlers = [ + handler + for handler in logging.getLogger("airflow.task").handlers + if getattr(handler, "supports_task_log_ship", False) + ] + return handlers[0] if handlers else None + + def ship_task_message(self, ti: TaskInstance, message: str, level: int): Review Comment: Also, in order to have an API similar to the loggers, you could do something like this: ```python def ship_task_message(self, ti: TaskInstance, message: str, level: int, *args): message = msg % args ``` That way, you could just call `self._task_log_shipper.error(ti, msg, ti, state, ti.state, info)` ########## airflow/utils/log/task_log_shipper.py: ########## @@ -0,0 +1,91 @@ +# +# 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. +from __future__ import annotations + +import logging +from copy import copy +from typing import TYPE_CHECKING + +from airflow.config_templates.airflow_local_settings import TASK_LOG_SHIPPER_ENABLED + +if TYPE_CHECKING: + from airflow.models.taskinstance import TaskInstance + + +logger = logging.getLogger(__name__) + + +class TaskLogShipper: + """ + TaskLogShipper enables shipping, typically in exceptional circumstances, messages to the task instance + logs from e.g. the executor or scheduler. + + :meta private: + """ + + def __init__(self, component_name: str): + """ + Initialize the task log shipper with the component name. + + :param component_name: the name of the component that will be used to identify the log messages + """ + self.component_name = component_name + self.task_handler = self._get_task_handler() + self.task_handler_can_ship_logs = self._can_ship_logs() + + def _can_ship_logs(self) -> bool: + if not TASK_LOG_SHIPPER_ENABLED: + return False + if self.task_handler is None or not self.task_handler.supports_task_log_ship: + logger.warning("Task handler does not support task log shipping") + return False + return True + + @staticmethod + def _get_task_handler(): + """Returns the task handler that supports task log shipping.""" + handlers = [ + handler + for handler in logging.getLogger("airflow.task").handlers + if getattr(handler, "supports_task_log_ship", False) + ] + return handlers[0] if handlers else None + + def ship_task_message(self, ti: TaskInstance, message: str, level: int): Review Comment: Maybe, you can expose similar methods as the logger: `info`, `warning`, `error` which use `ship_task_message` underneath. That would make the usage of it much easier -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
