From 8b7605fa38e9129eead2c99e45315a2129b6e4a2 Mon Sep 17 00:00:00 2001 From: TSKVenkat Date: Fri, 28 Aug 2026 23:25:18 +0530 Subject: [PATCH] refactor(prometheus): combine custom labels from standard metadata without a wrapper dict The three emitters that hold standard logging metadata rather than a whole payload were calling the payload-shaped helper with a {"metadata": ...} literal. That tripped the type-discipline gate's LIT002 ceiling (mutable-collection construction), which is already at its codebase limit, so the three new literals failed lint. Split the combination logic into a metadata-shaped helper and leave the payload-shaped one delegating to it, so those callers pass what they already hold and no wrapper dict is built. The delegation also drops the payload helper's own `or {}` and second guard, so this removes two more literals than it adds: LIT002 in this file goes 111 -> 109 against the merge base, and the gate reports every LIT rule within its ceiling. No behaviour change: a non-dict payload and a non-dict metadata both still yield {}. --- litellm/integrations/prometheus.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/litellm/integrations/prometheus.py b/litellm/integrations/prometheus.py index 6f350b84e53..9b878d524f4 100644 --- a/litellm/integrations/prometheus.py +++ b/litellm/integrations/prometheus.py @@ -2422,9 +2422,7 @@ class PrometheusLogger(CustomLogger): api_provider=api_provider, stream=(str(request_data.get("stream")) if litellm.prometheus_emit_stream_label else None), custom_metadata_labels=get_custom_labels_from_metadata( - metadata=_get_combined_custom_metadata_from_standard_logging_payload( - {"metadata": _standard_metadata} - ) + metadata=_get_combined_custom_metadata_from_standard_logging_metadata(_standard_metadata) ), ) _label_ctx: Final = PrometheusLabelFactoryContext(enum_values) @@ -3205,7 +3203,7 @@ class PrometheusLogger(CustomLogger): exception_class=self._get_exception_class_name(original_exception), tags=_tags, custom_metadata_labels=get_custom_labels_from_metadata( - metadata=_get_combined_custom_metadata_from_standard_logging_payload({"metadata": standard_metadata}) + metadata=_get_combined_custom_metadata_from_standard_logging_metadata(standard_metadata) ), ) PrometheusLogger._inc_labeled_counter( @@ -3249,7 +3247,7 @@ class PrometheusLogger(CustomLogger): exception_class=self._get_exception_class_name(original_exception), tags=_tags, custom_metadata_labels=get_custom_labels_from_metadata( - metadata=_get_combined_custom_metadata_from_standard_logging_payload({"metadata": standard_metadata}) + metadata=_get_combined_custom_metadata_from_standard_logging_metadata(standard_metadata) ), ) @@ -4275,10 +4273,19 @@ def _get_combined_custom_metadata_from_standard_logging_payload( user_api_key_project_alias, user_api_key_team_alias) so they are accessible via custom_prometheus_metadata_labels configuration. """ - if not isinstance(standard_logging_payload, dict): - return {} + return _get_combined_custom_metadata_from_standard_logging_metadata( + standard_logging_payload.get("metadata") if isinstance(standard_logging_payload, dict) else None + ) - standard_logging_metadata: Final = standard_logging_payload.get("metadata") or {} + +def _get_combined_custom_metadata_from_standard_logging_metadata( + standard_logging_metadata: dict | None, +) -> dict[str, object]: + """ + Same combination, for emitters holding the standard logging metadata itself + rather than a whole payload: the fallback emitters and the proxy failure hook + build it with StandardLoggingPayloadSetup.get_standard_logging_metadata. + """ if not isinstance(standard_logging_metadata, dict): return {}