ashb commented on a change in pull request #12925: URL: https://github.com/apache/airflow/pull/12925#discussion_r648694429
########## File path: airflow/providers/grafana/log/loki_task_handler.py ########## @@ -0,0 +1,242 @@ +# +# 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. + +"""Loki logging handler for tasks""" +import time +from typing import Dict, Optional, Tuple, Union + +import logging_loki +import requests +from cached_property import cached_property + +from airflow.hooks.base_hook import BaseHook +from airflow.models import TaskInstance +from airflow.utils.log.file_task_handler import FileTaskHandler +from airflow.utils.log.logging_mixin import LoggingMixin + +DEFAULT_LOGGER_NAME = "airflow" + + +class LokiTaskHandler(FileTaskHandler, LoggingMixin): + """ + LokiTaskHandler that directly makes Loki logging API calls while reading and writing logs. + This is a Python standard ``logging`` handler using that can be used to route Python standard + logging messages directly to the Loki Logging API. It can also be used to save logs for + executing tasks. To do this, you should set as a handler with the name "tasks". In this case, + it will also be used to read the log for display in Web UI. + :param base_log_folder: Base log folder to place logs (incase Loki is down). + :type base_log_folder: str + :param filename_template: template filename string (incase Loki is down) + :type filename_template: str + :param loki_conn_id: Connection ID that will be used for authorization to the Loki Platform. + :type loki_conn_id: str + :param name: the name of the custom log in Loki Logging. Defaults to 'airflow'. + :type name: str + :param labels: (Optional) Mapping of labels for the entry. + :type labels: dict + """ + + # pylint: disable=too-many-arguments + def __init__( + self, + base_log_folder: str, + filename_template: str, + loki_conn_id: str, + name: str = DEFAULT_LOGGER_NAME, + labels: Optional[Dict[str, str]] = None, + ): + super().__init__(base_log_folder, filename_template) + self.loki_conn_id = loki_conn_id + self.name: str = name + self.timestamp_pattern = "%Y-%m-%dT%H:%M:%S" + self.labels = labels + self._session: Optional[requests.Session] = None + + @cached_property + def get_conn(self): Review comment: Since this is a cached property, it should be named like this ```suggestion def conn(self): ``` -- 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. For queries about this service, please contact Infrastructure at: [email protected]
