# 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 contextlib
import json
import logging
import os
import sys
import time
from collections import defaultdict
from collections.abc import Callable
from datetime import datetime
from operator import attrgetter
from pathlib import Path
from typing import TYPE_CHECKING, Any, Literal, cast
from urllib.parse import urlparse

import attrs
import pendulum
from opensearchpy import OpenSearch, helpers
from opensearchpy.exceptions import NotFoundError
from sqlalchemy import select

import airflow.logging_config as alc
from airflow.models import DagRun
from airflow.providers.common.compat.module_loading import import_string
from airflow.providers.common.compat.sdk import AirflowException, conf
from airflow.providers.opensearch.log.os_json_formatter import OpensearchJSONFormatter
from airflow.providers.opensearch.log.os_response import Hit, OpensearchResponse
from airflow.providers.opensearch.version_compat import AIRFLOW_V_3_0_PLUS, AIRFLOW_V_3_2_PLUS
from airflow.utils.log.file_task_handler import FileTaskHandler
from airflow.utils.log.logging_mixin import ExternalLoggingMixin, LoggingMixin
from airflow.utils.session import create_session

if TYPE_CHECKING:
    from airflow.models.taskinstance import TaskInstance, TaskInstanceKey
    from airflow.sdk.types import RuntimeTaskInstanceProtocol as RuntimeTI
    from airflow.utils.log.file_task_handler import LogMessages, LogMetadata, LogSourceInfo

if AIRFLOW_V_3_0_PLUS:
    from airflow.utils.log.file_task_handler import StructuredLogMessage

    OsLogMsgType = list[StructuredLogMessage] | str
else:
    OsLogMsgType = list[tuple[str, str]]  # type: ignore[assignment,misc]

if AIRFLOW_V_3_2_PLUS:
    from airflow.sdk import timezone
else:
    from airflow.utils import timezone  # type: ignore[attr-defined,no-redef]

USE_PER_RUN_LOG_ID = hasattr(DagRun, "get_log_template")
LOG_LINE_DEFAULTS = {"exc_text": "", "stack_info": ""}
TASK_LOG_FIELDS = ["timestamp", "event", "level", "chan", "logger", "error_detail", "message", "levelname"]


def _format_error_detail(error_detail: Any) -> str | None:
    """Render the structured ``error_detail`` written by the Airflow 3 supervisor as a traceback string."""
    if not error_detail:
        return None
    if not isinstance(error_detail, list):
        return str(error_detail)

    lines: list[str] = ["Traceback (most recent call last):"]
    for exc_info in error_detail:
        if not isinstance(exc_info, dict):
            lines.append(str(exc_info))
            continue
        if exc_info.get("is_cause"):
            lines.append("\nThe above exception was the direct cause of the following exception:\n")
            lines.append("Traceback (most recent call last):")
        for frame in exc_info.get("frames", []):
            lines.append(
                f'  File "{frame.get("filename", "<unknown>")}", line {frame.get("lineno", "?")}, in {frame.get("name", "<unknown>")}'
            )
        exc_type = exc_info.get("exc_type", "")
        exc_value = exc_info.get("exc_value", "")
        if exc_type:
            lines.append(f"{exc_type}: {exc_value}" if exc_value else exc_type)
    return "\n".join(lines)


def _build_log_fields(hit_dict: dict[str, Any]) -> dict[str, Any]:
    """Filter an OpenSearch hit to ``TASK_LOG_FIELDS`` and ensure compatibility with StructuredLogMessage."""
    fields = {k: v for k, v in hit_dict.items() if k.lower() in TASK_LOG_FIELDS or k == "@timestamp"}

    # Map @timestamp to timestamp
    if "@timestamp" in fields and "timestamp" not in fields:
        fields["timestamp"] = fields.pop("@timestamp")

    # Map levelname to level
    if "levelname" in fields and "level" not in fields:
        fields["level"] = fields.pop("levelname")

    # Airflow 3 StructuredLogMessage requires 'event'
    if "event" not in fields:
        fields["event"] = fields.pop("message", "")

    # Clean up error_detail if it's empty
    if "error_detail" in fields and not fields["error_detail"]:
        fields.pop("error_detail")
    return fields


def getattr_nested(obj, item, default):
    """
    Get item from obj but return default if not found.

    E.g. calling ``getattr_nested(a, 'b.c', "NA")`` will return
    ``a.b.c`` if such a value exists, and "NA" otherwise.

    :meta private:
    """
    try:
        return attrgetter(item)(obj)
    except AttributeError:
        return default


def _ensure_ti(ti: TaskInstanceKey | TaskInstance, session) -> TaskInstance:
    """
    Given TI | TIKey, return a TI object.

    Will raise exception if no TI is found in the database.
    """
    from airflow.models.taskinstance import TaskInstance, TaskInstanceKey

    if not isinstance(ti, TaskInstanceKey):
        return ti
    val = session.scalar(
        select(TaskInstance).where(
            TaskInstance.task_id == ti.task_id,
            TaskInstance.dag_id == ti.dag_id,
            TaskInstance.run_id == ti.run_id,
            TaskInstance.map_index == ti.map_index,
        )
    )
    if isinstance(val, TaskInstance):
        val.try_number = ti.try_number
        return val
    raise AirflowException(f"Could not find TaskInstance for {ti}")


def get_os_kwargs_from_config() -> dict[str, Any]:
    open_search_config = conf.getsection("opensearch_configs")
    kwargs_dict = {key: value for key, value in open_search_config.items()} if open_search_config else {}
    return kwargs_dict


def _format_url(host: str) -> str:
    """
    Format the given host string to ensure it starts with 'http' and check if it represents a valid URL.

    :params host: The host string to format and check.
    """
    parsed_url = urlparse(host)

    if parsed_url.scheme not in ("http", "https"):
        host = "http://" + host
        parsed_url = urlparse(host)

    if not parsed_url.netloc:
        raise ValueError(f"'{host}' is not a valid URL.")

    return host


def _create_opensearch_client(
    host: str,
    port: int | None,
    username: str,
    password: str,
    os_kwargs: dict[str, Any],
) -> OpenSearch:
    parsed_url = urlparse(_format_url(host))
    resolved_port = port if port is not None else (parsed_url.port or 9200)
    return OpenSearch(
        hosts=[{"host": parsed_url.hostname, "port": resolved_port, "scheme": parsed_url.scheme}],
        http_auth=(username, password),
        **os_kwargs,
    )


def _strip_userinfo(url: str) -> str:
    """
    Return ``url`` with any ``user:password@`` userinfo removed.

    The OpenSearch ``[opensearch] host`` config commonly embeds
    credentials (``https://user:password@opensearch.example.com:9200``).
    This value is reused as a display label for log-source grouping, so
    the credentials would otherwise end up in task logs. Anything that
    is not a valid URL is returned unchanged.
    """
    try:
        parsed = urlparse(url)
    except (TypeError, ValueError):
        return url
    if not parsed.hostname or (not parsed.username and not parsed.password):
        return url
    netloc = parsed.hostname
    if parsed.port is not None:
        netloc = f"{netloc}:{parsed.port}"
    return parsed._replace(netloc=netloc).geturl()


def _render_log_id(
    log_id_template: str, ti: TaskInstance | TaskInstanceKey | RuntimeTI, try_number: int
) -> str:
    return log_id_template.format(
        dag_id=ti.dag_id,
        task_id=ti.task_id,
        run_id=getattr(ti, "run_id", ""),
        try_number=try_number,
        map_index=getattr(ti, "map_index", ""),
    )


def _resolve_nested(hit: dict[Any, Any], parent_class=None) -> type[Hit]:
    """
    Resolve nested hits from OpenSearch by iteratively navigating the `_nested` field.

    The result is used to fetch the appropriate document class to handle the hit.
    """
    doc_class = Hit
    nested_field = None

    nested_path: list[str] = []
    nesting = hit["_nested"]
    while nesting and "field" in nesting:
        nested_path.append(nesting["field"])
        nesting = nesting.get("_nested")
    nested_path_str = ".".join(nested_path)

    if hasattr(parent_class, "_index"):
        nested_field = parent_class._index.resolve_field(nested_path_str)

    if nested_field is not None:
        return nested_field._doc_class

    return doc_class


class OpensearchTaskHandler(FileTaskHandler, ExternalLoggingMixin, LoggingMixin):
    """
    OpensearchTaskHandler is a Python log handler that reads logs from OpenSearch.

    Airflow flushes task logs to local files. Additional software setup can then ship those
    logs to OpenSearch. On Airflow 3, this task handler also registers a matching
    ``OpensearchRemoteLogIO`` so the new remote logging path can read from OpenSearch too.
    Airflow can also be configured to write task logs to OpenSearch directly. To enable this
    feature, set ``json_format`` and ``write_to_opensearch`` to ``True``.

    To efficiently query and sort OpenSearch results, this handler assumes each
    log message has a field `log_id` consists of ti primary keys:
    `log_id = {dag_id}-{task_id}-{logical_date}-{try_number}`
    Log messages with specific log_id are sorted based on `offset`,
    which is a unique integer indicates log message's order.
    Timestamps here are unreliable because multiple log messages
    might have the same timestamp.

    :param base_log_folder: Base folder to store logs locally.
    :param end_of_log_mark: A marker string to signify the end of logs.
    :param write_stdout: Whether to also write logs to stdout.
    :param json_format: Whether to format logs as JSON.
    :param json_fields: Comma-separated list of fields to include in the JSON log output.
    :param host: OpenSearch host name.
    :param port: OpenSearch port.
    :param username: Username for OpenSearch authentication.
    :param password: Password for OpenSearch authentication.
    :param write_to_opensearch: Whether to write logs directly to OpenSearch.
    :param target_index: Name of the index to write to when direct OpenSearch writes are enabled.
    :param host_field: The field name for the host in the logs (default is "host").
    :param offset_field: The field name for the log offset (default is "offset").
    :param index_patterns: Index pattern or template for storing logs.
    :param index_patterns_callable: Callable that dynamically generates index patterns based on context.
    :param os_kwargs: Additional OpenSearch client options. This can be set to "default_os_kwargs" to
                      load the default configuration from Airflow's settings.
    """

    PAGE = 0
    MAX_LINE_PER_PAGE = 1000
    LOG_NAME = "Opensearch"

    trigger_should_wrap = True

    def __init__(
        self,
        base_log_folder: str,
        end_of_log_mark: str,
        write_stdout: bool,
        json_format: bool,
        json_fields: str,
        host: str,
        port: int | None,
        username: str,
        password: str,
        write_to_opensearch: bool = False,
        target_index: str = "airflow-logs",
        host_field: str = "host",
        offset_field: str = "offset",
        index_patterns: str = conf.get("opensearch", "index_patterns", fallback="_all"),
        index_patterns_callable: str = conf.get("opensearch", "index_patterns_callable", fallback=""),
        log_id_template: str = conf.get("opensearch", "log_id_template", fallback="")
        or "{dag_id}-{task_id}-{run_id}-{map_index}-{try_number}",
        os_kwargs: dict | None | Literal["default_os_kwargs"] = "default_os_kwargs",
        max_bytes: int = 0,
        backup_count: int = 0,
        delay: bool = False,
        **kwargs,
    ) -> None:
        os_kwargs = os_kwargs or {}
        if os_kwargs == "default_os_kwargs":
            os_kwargs = get_os_kwargs_from_config()
        # support log file size handling of FileTaskHandler
        super().__init__(
            base_log_folder=base_log_folder, max_bytes=max_bytes, backup_count=backup_count, delay=delay
        )
        self.closed = False
        self.mark_end_on_close = True
        self.end_of_log_mark = end_of_log_mark.strip()
        self.write_stdout = write_stdout
        self.write_to_opensearch = write_to_opensearch
        self.json_format = json_format
        self.json_fields = [label.strip() for label in json_fields.split(",")]
        self.host = self.format_url(host)
        self.host_field = host_field
        self.offset_field = offset_field
        self.target_index = target_index
        self.index_patterns = index_patterns
        self.index_patterns_callable = index_patterns_callable
        self.context_set = False
        self.client = _create_opensearch_client(
            self.host,
            port,
            username,
            password,
            cast("dict[str, Any]", os_kwargs),
        )
        self.delete_local_copy = kwargs.get(
            "delete_local_copy", conf.getboolean("logging", "delete_local_logs")
        )
        self.log_id_template = log_id_template
        self.formatter: logging.Formatter
        self.handler: logging.FileHandler | logging.StreamHandler | None = None
        self._doc_type_map: dict[Any, Any] = {}
        self._doc_type: list[Any] = []
        self.io = OpensearchRemoteLogIO(
            host=self.host,
            port=port,
            username=username,
            password=password,
            write_to_opensearch=self.write_to_opensearch,
            target_index=self.target_index,
            write_stdout=self.write_stdout,
            offset_field=self.offset_field,
            host_field=self.host_field,
            base_log_folder=base_log_folder,
            delete_local_copy=self.delete_local_copy,
            json_format=self.json_format,
            log_id_template=self.log_id_template,
        )
        if AIRFLOW_V_3_0_PLUS:
            if AIRFLOW_V_3_2_PLUS:
                from airflow.logging_config import _ActiveLoggingConfig, get_remote_task_log

                if get_remote_task_log() is None:
                    _ActiveLoggingConfig.set(self.io, None)
            elif alc.REMOTE_TASK_LOG is None:  # type: ignore[attr-defined]
                alc.REMOTE_TASK_LOG = self.io  # type: ignore[attr-defined]

    def set_context(self, ti: TaskInstance, *, identifier: str | None = None) -> None:
        """
        Provide task_instance context to airflow task handler.

        :param ti: task instance object
        :param identifier: if set, identifies the Airflow component which is relaying logs from
            exceptional scenarios related to the task instance
        """
        is_trigger_log_context = getattr(ti, "is_trigger_log_context", None)
        is_ti_raw = getattr(ti, "raw", None)
        self.mark_end_on_close = not is_ti_raw and not is_trigger_log_context
        date_key = "logical_date" if AIRFLOW_V_3_0_PLUS else "execution_date"
        if self.json_format:
            self.formatter = OpensearchJSONFormatter(
                fmt=self.formatter._fmt,
                json_fields=[*self.json_fields, self.offset_field],
                extras={
                    "dag_id": str(ti.dag_id),
                    "task_id": str(ti.task_id),
                    date_key: (
                        self._clean_date(ti.logical_date)
                        if AIRFLOW_V_3_0_PLUS
                        else self._clean_date(ti.execution_date)
                    ),
                    "try_number": str(ti.try_number),
                    "log_id": self._render_log_id(ti, ti.try_number),
                },
            )

        if self.write_stdout:
            if self.context_set:
                # We don't want to re-set up the handler if this logger has
                # already been initialized
                return

            self.handler = logging.StreamHandler(stream=sys.__stdout__)
            self.handler.setLevel(self.level)
            self.handler.setFormatter(self.formatter)
        else:
            super().set_context(ti, identifier=identifier)
        self.context_set = True

    def emit(self, record):
        if self.handler:
            setattr(record, self.offset_field, int(time.time() * (10**9)))
            self.handler.emit(record)

    def close(self) -> None:
        # When application exit, system shuts down all handlers by
        # calling close method. Here we check if logger is already
        # closed to prevent uploading the log to remote storage multiple
        # times when `logging.shutdown` is called.
        if self.closed:
            return

        if not self.mark_end_on_close:
            # when we're closing due to task deferral, don't mark end of log
            self.closed = True
            return

        # Case which context of the handler was not set.
        if self.handler is None:
            self.closed = True
            return

        # Reopen the file stream, because FileHandler.close() would be called
        # first in logging.shutdown() and the stream in it would be set to None.
        if self.handler.stream is None or self.handler.stream.closed:
            self.handler.stream = self.handler._open()  # type: ignore[union-attr]

        # Mark the end of file using end of log mark,
        # so we know where to stop while auto-tailing.
        self.emit(logging.makeLogRecord({"msg": self.end_of_log_mark}))

        if self.write_stdout:
            self.handler.close()
            sys.stdout = sys.__stdout__

        super().close()

        self.closed = True

    def _read_grouped_logs(self):
        return True

    @staticmethod
    def _clean_date(value: datetime | None) -> str:
        """
        Clean up a date value so that it is safe to query in elasticsearch by removing reserved characters.

        https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_reserved_characters
        """
        if value is None:
            return ""
        return value.strftime("%Y_%m_%dT%H_%M_%S_%f")

    def _render_log_id(self, ti: TaskInstance | TaskInstanceKey, try_number: int) -> str:
        from airflow.models.taskinstance import TaskInstanceKey

        log_id_template = self.log_id_template
        with create_session() as session:
            if isinstance(ti, TaskInstanceKey):
                ti = _ensure_ti(ti, session)
            dag_run = ti.get_dagrun(session=session)
            if USE_PER_RUN_LOG_ID:
                log_id_template = dag_run.get_log_template(session=session).elasticsearch_id

        if self.json_format:
            data_interval_start = self._clean_date(dag_run.data_interval_start)
            data_interval_end = self._clean_date(dag_run.data_interval_end)
            logical_date = self._clean_date(dag_run.logical_date)
        else:
            data_interval_start = (
                dag_run.data_interval_start.isoformat() if dag_run.data_interval_start else ""
            )
            data_interval_end = dag_run.data_interval_end.isoformat() if dag_run.data_interval_end else ""
            logical_date = dag_run.logical_date.isoformat() if dag_run.logical_date else ""
        return log_id_template.format(
            dag_id=ti.dag_id,
            task_id=ti.task_id,
            run_id=getattr(ti, "run_id", ""),
            data_interval_start=data_interval_start,
            data_interval_end=data_interval_end,
            logical_date=logical_date,
            execution_date=logical_date,
            try_number=try_number,
            map_index=getattr(ti, "map_index", ""),
        )

    def _read(
        self, ti: TaskInstance, try_number: int, metadata: LogMetadata | None = None
    ) -> tuple[OsLogMsgType, LogMetadata]:
        """
        Endpoint for streaming log.

        :param ti: task instance object
        :param try_number: try_number of the task instance
        :param metadata: log metadata,
                         can be used for steaming log reading and auto-tailing.
        :return: a list of tuple with host and log documents, metadata.
        """
        if not metadata:
            # LogMetadata(TypedDict) is used as type annotation for log_reader; added ignore to suppress mypy error
            metadata = {"offset": 0}  # type: ignore[assignment]
        metadata = cast("LogMetadata", metadata)

        if "offset" not in metadata:
            metadata["offset"] = 0

        offset = metadata["offset"]
        log_id = self._render_log_id(ti, try_number)
        response = self.io._os_read(log_id, offset, ti)
        if response is not None and response.hits:
            logs_by_host = self.io._group_logs_by_host(response)
            next_offset = attrgetter(self.offset_field)(response[-1])
        else:
            logs_by_host = None
            next_offset = offset

        # Ensure a string here. Large offset numbers will get JSON.parsed incorrectly
        # on the client. Sending as a string prevents this issue.
        # https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER
        metadata["offset"] = str(next_offset)

        # end_of_log_mark may contain characters like '\n' which is needed to
        # have the log uploaded but will not be stored in elasticsearch.
        metadata["end_of_log"] = False
        if logs_by_host:
            if any(self._get_log_message(x[-1]) == self.end_of_log_mark for x in logs_by_host.values()):
                metadata["end_of_log"] = True

        cur_ts = pendulum.now()
        if "last_log_timestamp" in metadata:
            last_log_ts = timezone.parse(metadata["last_log_timestamp"])

            # if we are not getting any logs at all after more than N seconds of trying,
            # assume logs do not exist
            if int(next_offset) == 0 and cur_ts.diff(last_log_ts).in_seconds() > 5:
                metadata["end_of_log"] = True
                missing_log_message = (
                    f"*** Log {log_id} not found in Opensearch. "
                    "If your task started recently, please wait a moment and reload this page. "
                    "Otherwise, the logs for this task instance may have been removed."
                )
                if AIRFLOW_V_3_0_PLUS:
                    from airflow.utils.log.file_task_handler import StructuredLogMessage

                    # return list of StructuredLogMessage for Airflow 3.0+
                    return [StructuredLogMessage(event=missing_log_message)], metadata

                return [("", missing_log_message)], metadata  # type: ignore[list-item]
            if (
                # Assume end of log after not receiving new log for N min,
                cur_ts.diff(last_log_ts).in_minutes() >= 5
                # if max_offset specified, respect it
                or ("max_offset" in metadata and int(offset) >= int(metadata["max_offset"]))
            ):
                metadata["end_of_log"] = True

        if int(offset) != int(next_offset) or "last_log_timestamp" not in metadata:
            metadata["last_log_timestamp"] = str(cur_ts)

        # If we hit the end of the log, remove the actual end_of_log message
        # to prevent it from showing in the UI.
        if logs_by_host:
            if AIRFLOW_V_3_0_PLUS:
                from airflow.utils.log.file_task_handler import StructuredLogMessage

                header = [
                    StructuredLogMessage(
                        event="::group::Log message source details",
                        sources=[host for host in logs_by_host.keys()],
                    ),  # type: ignore[call-arg]
                    StructuredLogMessage(event="::endgroup::"),
                ]

                # Flatten all hits, filter to only desired fields, and construct StructuredLogMessage objects
                message = header + [
                    StructuredLogMessage(**_build_log_fields(hit.to_dict()))
                    for hits in logs_by_host.values()
                    for hit in hits
                ]
            else:
                message = [(host, self.concat_logs(hits)) for host, hits in logs_by_host.items()]  # type: ignore[misc]
        else:
            message = []
            metadata["end_of_log"] = True
        return message, metadata

    def _os_read(self, log_id: str, offset: int | str, ti: TaskInstance) -> OpensearchResponse | None:
        """
        Return the logs matching log_id in Elasticsearch and next offset or ''.

        :param log_id: the log_id of the log to read.
        :param offset: the offset start to read log from.
        :param ti: the task instance object

        :meta private:
        """
        query: dict[Any, Any] = {
            "query": {
                "bool": {
                    "filter": [{"range": {self.offset_field: {"gt": int(offset)}}}],
                    "must": [{"match_phrase": {"log_id": log_id}}],
                }
            }
        }
        index_patterns = self._get_index_patterns(ti)
        try:
            max_log_line = self.client.count(index=index_patterns, body=query)["count"]
        except NotFoundError as e:
            self.log.exception("The target index pattern %s does not exist", index_patterns)
            raise e

        if max_log_line != 0:
            try:
                res = self.client.search(
                    index=index_patterns,
                    body=query,
                    sort=[self.offset_field],
                    size=self.MAX_LINE_PER_PAGE,
                    from_=self.MAX_LINE_PER_PAGE * self.PAGE,
                )
                return OpensearchResponse(self, res)
            except Exception as err:
                self.log.exception("Could not read log with log_id: %s. Exception: %s", log_id, err)

        return None

    def _get_index_patterns(self, ti: TaskInstance | None) -> str:
        """
        Get index patterns by calling index_patterns_callable, if provided, or the configured index_patterns.

        :param ti: A TaskInstance object or None.
        """
        if self.index_patterns_callable:
            self.log.debug("Using index_patterns_callable: %s", self.index_patterns_callable)
            index_pattern_callable_obj = import_string(self.index_patterns_callable)
            return index_pattern_callable_obj(ti)
        self.log.debug("Using index_patterns: %s", self.index_patterns)
        return self.index_patterns

    def _get_result(self, hit: dict[Any, Any], parent_class=None) -> Hit:
        """
        Process a hit (i.e., a result) from an Elasticsearch response and transform it into a class instance.

        The transformation depends on the contents of the hit. If the document in hit contains a nested field,
        the '_resolve_nested' method is used to determine the appropriate class (based on the nested path).
        If the hit has a document type that is present in the '_doc_type_map', the corresponding class is
        used. If not, the method iterates over the '_doc_type' classes and uses the first one whose '_matches'
        method returns True for the hit.

        If the hit contains any 'inner_hits', these are also processed into 'ElasticSearchResponse' instances
        using the determined class.

        Finally, the transformed hit is returned. If the determined class has a 'from_es' method, this is
        used to transform the hit

        An example of the hit argument:

        {'_id': 'jdeZT4kBjAZqZnexVUxk',
         '_index': '.ds-filebeat-8.8.2-2023.07.09-000001',
         '_score': 2.482621,
         '_source': {'@timestamp': '2023-07-13T14:13:15.140Z',
                     'asctime': '2023-07-09T07:47:43.907+0000',
                     'container': {'id': 'airflow'},
                     'dag_id': 'example_bash_operator',
                     'ecs': {'version': '8.0.0'},
                     'logical_date': '2023_07_09T07_47_32_000000',
                     'filename': 'taskinstance.py',
                     'input': {'type': 'log'},
                     'levelname': 'INFO',
                     'lineno': 1144,
                     'log': {'file': {'path': "/opt/airflow/Documents/GitHub/airflow/logs/
                     dag_id=example_bash_operator'/run_id=owen_run_run/
                     task_id=run_after_loop/attempt=1.log"},
                             'offset': 0},
                     'log.offset': 1688888863907337472,
                     'log_id': 'example_bash_operator-run_after_loop-owen_run_run--1-1',
                     'message': 'Dependencies all met for dep_context=non-requeueable '
                                'deps ti=<TaskInstance: '
                                'example_bash_operator.run_after_loop owen_run_run '
                                '[queued]>',
                     'task_id': 'run_after_loop',
                     'try_number': '1'},
         '_type': '_doc'}
        """
        doc_class = Hit
        dt = hit.get("_type")

        if "_nested" in hit:
            doc_class = _resolve_nested(hit, parent_class)

        elif dt in self._doc_type_map:
            doc_class = self._doc_type_map[dt]

        else:
            for doc_type in self._doc_type:
                if hasattr(doc_type, "_matches") and doc_type._matches(hit):
                    doc_class = doc_type
                    break

        for t in hit.get("inner_hits", ()):
            hit["inner_hits"][t] = OpensearchResponse(self, hit["inner_hits"][t], doc_class=doc_class)

        # callback should get the Hit class if "from_es" is not defined
        callback: type[Hit] | Callable[..., Any] = getattr(doc_class, "from_es", doc_class)
        return callback(hit)

    def _group_logs_by_host(self, response: OpensearchResponse) -> dict[str, list[Hit]]:
        grouped_logs = defaultdict(list)
        host_fallback = _strip_userinfo(self.host)
        for hit in response:
            key = getattr_nested(hit, self.host_field, None) or host_fallback
            grouped_logs[key].append(hit)
        return grouped_logs

    def _format_msg(self, hit: Hit):
        """Format ES Record to match settings.LOG_FORMAT when used with json_format."""
        # Using formatter._style.format makes it future proof i.e.
        # if we change the formatter style from '%' to '{' or '$', this will still work
        if self.json_format:
            with contextlib.suppress(Exception):
                return self.formatter._style.format(
                    logging.makeLogRecord({**LOG_LINE_DEFAULTS, **hit.to_dict()})
                )

        # Just a safe-guard to preserve backwards-compatibility
        return self._get_log_message(hit)

    def _get_log_message(self, hit: Hit) -> str:
        if hasattr(hit, "event"):
            return hit.event
        if hasattr(hit, "message"):
            return hit.message
        return ""

    def concat_logs(self, hits: list[Hit]) -> str:
        log_range = (len(hits) - 1) if self._get_log_message(hits[-1]) == self.end_of_log_mark else len(hits)
        return "\n".join(self._format_msg(hits[i]) for i in range(log_range))

    @property
    def supports_external_link(self) -> bool:
        """
        Whether we can support external links.

        TODO: It should support frontend just like ElasticSearchTaskhandler.
        """
        return False

    def get_external_log_url(self, task_instance, try_number) -> str:
        """
        Create an address for an external log collecting service.

        TODO: It should support frontend just like ElasticSearchTaskhandler.
        """
        return ""

    @property
    def log_name(self) -> str:
        """The log name."""
        return self.LOG_NAME

    @staticmethod
    def format_url(host: str) -> str:
        return _format_url(host)


@attrs.define(kw_only=True)
class OpensearchRemoteLogIO(LoggingMixin):  # noqa: D101
    json_format: bool = False
    write_stdout: bool = False
    write_to_opensearch: bool = False
    delete_local_copy: bool = False
    host: str = "localhost"
    port: int | None = 9200
    username: str = ""
    password: str = ""
    host_field: str = "host"
    target_index: str = "airflow-logs"
    offset_field: str = "offset"
    base_log_folder: Path = attrs.field(converter=Path)
    log_id_template: str = (
        conf.get("opensearch", "log_id_template", fallback="")
        or "{dag_id}-{task_id}-{run_id}-{map_index}-{try_number}"
    )

    processors = ()

    def __attrs_post_init__(self):
        self.host = _format_url(self.host)
        self.port = self.port if self.port is not None else (urlparse(self.host).port or 9200)
        self.client = _create_opensearch_client(
            self.host,
            self.port,
            self.username,
            self.password,
            get_os_kwargs_from_config(),
        )
        self.index_patterns_callable = conf.get("opensearch", "index_patterns_callable", fallback="")
        self.PAGE = 0
        self.MAX_LINE_PER_PAGE = 1000
        self.index_patterns = conf.get("opensearch", "index_patterns", fallback="_all")
        self._doc_type_map: dict[Any, Any] = {}
        self._doc_type: list[Any] = []

    def upload(self, path: os.PathLike | str, ti: RuntimeTI):
        """Emit structured task logs to stdout and/or write them directly to OpenSearch."""
        path = Path(path)
        local_loc = path if path.is_absolute() else self.base_log_folder.joinpath(path)
        if not local_loc.is_file():
            return

        log_id = _render_log_id(self.log_id_template, ti, ti.try_number)  # type: ignore[arg-type]
        if self.write_stdout or self.write_to_opensearch:
            log_lines = self._parse_raw_log(local_loc.read_text(), log_id)

            if self.write_stdout:
                for line in log_lines:
                    sys.stdout.write(json.dumps(line) + "\n")
                    sys.stdout.flush()

            if self.write_to_opensearch:
                success = self._write_to_opensearch(log_lines)
                if success and self.delete_local_copy:
                    local_loc.unlink(missing_ok=True)
                    base_dir = self.base_log_folder
                    parent = local_loc.parent
                    while parent != base_dir and parent.is_dir():
                        if any(parent.iterdir()):
                            break
                        with contextlib.suppress(OSError):
                            parent.rmdir()
                        parent = parent.parent

    def _parse_raw_log(self, log: str, log_id: str) -> list[dict[str, Any]]:
        parsed_logs = []
        offset = 1
        for line in log.split("\n"):
            if not line.strip():
                continue
            try:
                log_dict = json.loads(line)
            except json.JSONDecodeError:
                self.log.warning("Skipping non-JSON log line: %r", line)
                log_dict = {"event": line}
            log_dict.update({"log_id": log_id, self.offset_field: offset})
            offset += 1
            parsed_logs.append(log_dict)
        return parsed_logs

    def _write_to_opensearch(self, log_lines: list[dict[str, Any]]) -> bool:
        """
        Write logs to OpenSearch; return `True` on success and `False` on failure.

        :param log_lines: The parsed log lines to write to OpenSearch.
        """
        bulk_actions = [{"_index": self.target_index, "_source": log} for log in log_lines]
        try:
            _ = helpers.bulk(self.client, bulk_actions)
            return True
        except helpers.BulkIndexError as bie:
            self.log.exception("Bulk upload failed for %d log(s)", len(bie.errors))
            for error in bie.errors:
                self.log.exception(error)
            return False
        except Exception as e:
            self.log.exception("Unable to insert logs into OpenSearch. Reason: %s", str(e))
            return False

    def read(self, _relative_path: str, ti: RuntimeTI) -> tuple[LogSourceInfo, LogMessages]:
        log_id = _render_log_id(self.log_id_template, ti, ti.try_number)  # type: ignore[arg-type]
        self.log.info("Reading log %s from Opensearch", log_id)
        response = self._os_read(log_id, 0, ti)
        if response is not None and response.hits:
            logs_by_host = self._group_logs_by_host(response)
        else:
            logs_by_host = None

        if logs_by_host is None:
            missing_log_message = (
                f"*** Log {log_id} not found in Opensearch. "
                "If your task started recently, please wait a moment and reload this page. "
                "Otherwise, the logs for this task instance may have been removed."
            )
            return [], [missing_log_message]

        header = list(logs_by_host.keys())
        message = []
        for hits in logs_by_host.values():
            for hit in hits:
                message.append(json.dumps(_build_log_fields(hit.to_dict())))
        return header, message

    def _os_read(self, log_id: str, offset: int | str, ti: RuntimeTI) -> OpensearchResponse | None:
        """Return the logs matching ``log_id`` in OpenSearch."""
        query: dict[Any, Any] = {
            "query": {
                "bool": {
                    "filter": [{"range": {self.offset_field: {"gt": int(offset)}}}],
                    "must": [{"match_phrase": {"log_id": log_id}}],
                }
            }
        }
        index_patterns = self._get_index_patterns(ti)
        try:
            max_log_line = self.client.count(index=index_patterns, body=query)["count"]
        except NotFoundError as e:
            self.log.exception("The target index pattern %s does not exist", index_patterns)
            raise e

        if max_log_line != 0:
            try:
                res = self.client.search(
                    index=index_patterns,
                    body=query,
                    sort=[self.offset_field],
                    size=self.MAX_LINE_PER_PAGE,
                    from_=self.MAX_LINE_PER_PAGE * self.PAGE,
                )
                return OpensearchResponse(self, res)
            except Exception as err:
                self.log.exception("Could not read log with log_id: %s. Exception: %s", log_id, err)

        return None

    def _get_index_patterns(self, ti: RuntimeTI | None) -> str:
        if self.index_patterns_callable:
            self.log.debug("Using index_patterns_callable: %s", self.index_patterns_callable)
            index_pattern_callable_obj = import_string(self.index_patterns_callable)
            return index_pattern_callable_obj(ti)
        self.log.debug("Using index_patterns: %s", self.index_patterns)
        return self.index_patterns

    def _group_logs_by_host(self, response: OpensearchResponse) -> dict[str, list[Hit]]:
        grouped_logs = defaultdict(list)
        host_fallback = _strip_userinfo(self.host)
        for hit in response:
            key = getattr_nested(hit, self.host_field, None) or host_fallback
            grouped_logs[key].append(hit)
        return grouped_logs

    def _get_result(self, hit: dict[Any, Any], parent_class=None) -> Hit:
        doc_class = Hit
        dt = hit.get("_type")

        if "_nested" in hit:
            doc_class = _resolve_nested(hit, parent_class)
        elif dt in self._doc_type_map:
            doc_class = self._doc_type_map[dt]
        else:
            for doc_type in self._doc_type:
                if hasattr(doc_type, "_matches") and doc_type._matches(hit):
                    doc_class = doc_type
                    break

        for t in hit.get("inner_hits", ()):
            hit["inner_hits"][t] = OpensearchResponse(self, hit["inner_hits"][t], doc_class=doc_class)

        callback: type[Hit] | Callable[..., Any] = getattr(doc_class, "from_es", doc_class)
        return callback(hit)
