"""Structlog processor for metric log entries.

Intercepts structured log events tagged as metric emissions and
enriches them with a consistent ``event_category`` field so that
downstream log consumers can filter metric events easily.

Based on Forgejo issue #579.
"""

from __future__ import annotations

from typing import Any


def metrics_log_processor(
    _logger: Any,
    method_name: str,
    event_dict: dict[str, Any],
) -> dict[str, Any]:
    """Structlog processor that tags metric log events.

    Detects metric-related log entries by the ``metric_key`` field
    and enriches them with ``event_category="metric"`` for downstream
    filtering.

    Args:
        _logger: The wrapped logger object.
        method_name: The name of the log method called.
        event_dict: The log event dictionary.

    Returns:
        The (possibly enriched) event dictionary.
    """
    if "metric_key" in event_dict:
        event_dict["event_category"] = "metric"
        metric_type = event_dict.get("metric_type", "unknown")
        event_dict.setdefault(
            "metric_summary",
            f"{event_dict.get('metric_key')}={event_dict.get('metric_value')} "
            f"[{metric_type}]",
        )
    return event_dict
