From 76fb9d3c966516ca0a64a64a05a3687661b7c622 Mon Sep 17 00:00:00 2001 From: harish-berri Date: Thu, 30 Apr 2026 22:13:11 +0000 Subject: [PATCH] refactor: remove AuthMetrics and related combined_view query metrics This commit deletes the AuthMetrics class and its associated methods, which were responsible for tracking combined_view SQL query metrics. The PrometheusLogger integration has been updated to remove references to these metrics, streamlining the codebase. Additionally, minor whitespace adjustments were made in the cache coordinator for consistency. --- litellm/integrations/prometheus.py | 13 ---- litellm/proxy/auth/auth_checks.py | 2 - litellm/proxy/auth/auth_metrics.py | 60 ------------------- .../proxy/common_utils/cache_coordinator.py | 2 +- litellm/types/integrations/prometheus.py | 7 --- 5 files changed, 1 insertion(+), 83 deletions(-) delete mode 100644 litellm/proxy/auth/auth_metrics.py diff --git a/litellm/integrations/prometheus.py b/litellm/integrations/prometheus.py index 1a899cf0e83..d9e57ee7cee 100644 --- a/litellm/integrations/prometheus.py +++ b/litellm/integrations/prometheus.py @@ -266,19 +266,6 @@ class PrometheusLogger(CustomLogger): # LiteLLM Virtual API KEY metrics ######################################## - # Auth DB load diagnostic: count direct combined_view SQL queries. - # Each increment means a virtual-key cache miss that hit the DB. - # Useful for validating that enable_redis_auth_cache is working. - self.litellm_auth_combined_view_queries_total = self._counter_factory( - "litellm_auth_combined_view_queries_total", - "Number of times the combined_view SQL query was issued for virtual-key auth. " - "Each count is a cache miss that hit the database. Use to validate " - "enable_redis_auth_cache is reducing DB load.", - labelnames=self.get_labels_for_metric( - "litellm_auth_combined_view_queries_total" - ), - ) - # Remaining MODEL RPM limit for API Key self.litellm_remaining_api_key_requests_for_model = self._gauge_factory( "litellm_remaining_api_key_requests_for_model", diff --git a/litellm/proxy/auth/auth_checks.py b/litellm/proxy/auth/auth_checks.py index 9266a917c5f..113a8f538c0 100644 --- a/litellm/proxy/auth/auth_checks.py +++ b/litellm/proxy/auth/auth_checks.py @@ -59,7 +59,6 @@ from litellm.proxy._types import ( SpecialModelNames, UserAPIKeyAuth, ) -from litellm.proxy.auth.auth_metrics import AuthMetrics from litellm.proxy.auth.route_checks import RouteChecks from litellm.proxy.db.exception_handler import PrismaDBExceptionHandler from litellm.proxy.guardrails.tool_name_extraction import ( @@ -2262,7 +2261,6 @@ async def _fetch_key_object_from_db_with_reconnect( Fetch key object from DB and retry once if a DB connection error can be healed. """ try: - AuthMetrics.inc_combined_view_query(hashed_token) return await prisma_client.get_data( token=hashed_token, table_name="combined_view", diff --git a/litellm/proxy/auth/auth_metrics.py b/litellm/proxy/auth/auth_metrics.py deleted file mode 100644 index 7284ba42736..00000000000 --- a/litellm/proxy/auth/auth_metrics.py +++ /dev/null @@ -1,60 +0,0 @@ -""" -Prometheus metric helpers for the auth layer. - -All metrics are thin wrappers around the shared ``PrometheusLogger`` instance so -that every counter follows the same registration path (``_counter_factory``, -label-filter config) as the rest of LiteLLM's metrics. - -Usage:: - - from litellm.proxy.auth.auth_metrics import AuthMetrics - - AuthMetrics.inc_combined_view_query(hashed_token="sk-xxx") -""" - -from litellm._logging import verbose_proxy_logger -from litellm.types.integrations.prometheus import UserAPIKeyLabelValues - - -class AuthMetrics: - """Static helpers for incrementing auth-layer Prometheus counters.""" - - @staticmethod - def _get_prom(): - """Return the active PrometheusLogger, or None if Prometheus is not configured.""" - try: - from litellm.router_utils.cooldown_callbacks import ( - _get_prometheus_logger_from_callbacks, - ) - - return _get_prometheus_logger_from_callbacks() - except Exception: - return None - - @staticmethod - def inc_combined_view_query(hashed_token: str) -> None: - """ - Increment ``litellm_auth_combined_view_queries_total``. - - Called once per virtual-key DB lookup (combined_view query). Each - increment represents a cache miss that hit the database — use this to - validate that ``enable_redis_auth_cache`` is reducing DB load. - """ - try: - prom = AuthMetrics._get_prom() - if prom is not None: - # Counter labelnames include ``hashed_api_key`` plus any - # ``custom_prometheus_metadata_labels`` / ``custom_prometheus_tags`` - # (see ``PrometheusMetricLabels.get_labels``). Use the same - # ``_inc_labeled_counter`` + ``prometheus_label_factory`` path as - # other metrics so label cardinality always matches registration. - prom._inc_labeled_counter( - prom.litellm_auth_combined_view_queries_total, - "litellm_auth_combined_view_queries_total", - UserAPIKeyLabelValues(hashed_api_key=hashed_token), - ) - except Exception as e: - verbose_proxy_logger.debug( - "AuthMetrics.inc_combined_view_query: failed to increment counter: %s", - e, - ) diff --git a/litellm/proxy/common_utils/cache_coordinator.py b/litellm/proxy/common_utils/cache_coordinator.py index fc46ab6d78e..abb0402d3b9 100644 --- a/litellm/proxy/common_utils/cache_coordinator.py +++ b/litellm/proxy/common_utils/cache_coordinator.py @@ -153,7 +153,7 @@ class EventDrivenCacheCoordinator: elapsed_ms, value, ) - + await cache.async_set_cache(key=cache_key, value=value) if self._log_prefix: verbose_proxy_logger.debug("%s Result cached", self._log_prefix) diff --git a/litellm/types/integrations/prometheus.py b/litellm/types/integrations/prometheus.py index a4573535f72..43a287f29bc 100644 --- a/litellm/types/integrations/prometheus.py +++ b/litellm/types/integrations/prometheus.py @@ -228,8 +228,6 @@ DEFINED_PROMETHEUS_METRICS = Literal[ "litellm_guardrail_latency_seconds", "litellm_guardrail_errors_total", "litellm_guardrail_requests_total", - # Auth DB diagnostic metrics - "litellm_auth_combined_view_queries_total", # Cache metrics "litellm_cache_hits_metric", "litellm_cache_misses_metric", @@ -309,11 +307,6 @@ class PrometheusMetricLabels: litellm_guardrail_errors_total: List[str] = [] litellm_guardrail_requests_total: List[str] = [] - # Auth DB diagnostic - label by key so you can see which virtual key causes DB hits - litellm_auth_combined_view_queries_total = [ - UserAPIKeyLabelNames.API_KEY_HASH.value, - ] - litellm_proxy_total_requests_metric = [ UserAPIKeyLabelNames.END_USER.value, UserAPIKeyLabelNames.API_KEY_HASH.value,