mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-23 22:07:29 +00:00
* fix(caching): collect the Redis circuit-breaker state gauge as one series per state across worker processes The gauge declares multiprocess_mode=livesum, so PROMETHEUS_MULTIPROC_DIR apps get one series per state summed over the live workers instead of one series per worker pid Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(caching): register Redis circuit-breaker metrics only when LiteLLM prometheus logging is on The breaker collectors join the default registry once the prometheus or prometheus_system callback is configured or a Prometheus logger starts, with earlier breaker events tallied and seeded in, and litellm.disable_redis_circuit_breaker_metrics turns them off for apps that export their own metrics Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * refactor(caching): drop the disable_redis_circuit_breaker_metrics opt-out Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * refactor(caching): keep the Redis breaker metrics gate to a per-state count and a publish hook Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(caching): pin the Redis breaker metrics gate against an injected registry Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(caching): publish the breaker collectors before reading them in the existing metrics test Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(caching): report Redis circuit-breaker metrics through the Prometheus services logger Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(integrations): build the Redis breaker collectors with typed prometheus_client constructors Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --------- Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
337 lines
14 KiB
Python
337 lines
14 KiB
Python
# used for monitoring litellm services health on `/metrics` endpoint on LiteLLM Proxy
|
|
#### What this does ####
|
|
# On success + failure, log events to Prometheus for litellm / adjacent services (litellm, redis, postgres, llm api providers)
|
|
|
|
|
|
from dataclasses import dataclass
|
|
from functools import lru_cache
|
|
from typing import TYPE_CHECKING, Final
|
|
|
|
import litellm
|
|
from litellm._logging import print_verbose, verbose_logger
|
|
from litellm.types.integrations.prometheus import LATENCY_BUCKETS
|
|
from litellm.types.services import (
|
|
DEFAULT_SERVICE_CONFIGS,
|
|
ServiceLoggerPayload,
|
|
ServiceMetrics,
|
|
ServiceTypes,
|
|
)
|
|
|
|
FAILED_REQUESTS_LABELS: Final = ["error_class", "function_name"]
|
|
|
|
if TYPE_CHECKING:
|
|
from prometheus_client import Counter as PrometheusCounter
|
|
from prometheus_client import Gauge
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class _RedisBreakerCollectors:
|
|
state: "Gauge"
|
|
transitions: "PrometheusCounter"
|
|
failures: "PrometheusCounter"
|
|
|
|
|
|
@lru_cache(maxsize=1)
|
|
def _redis_breaker_collectors() -> _RedisBreakerCollectors:
|
|
from prometheus_client import Counter as PrometheusCounter
|
|
from prometheus_client import Gauge
|
|
|
|
return _RedisBreakerCollectors(
|
|
state=Gauge(
|
|
"litellm_redis_circuit_breaker_state",
|
|
"Number of Redis circuit breakers currently in each state",
|
|
labelnames=("state",),
|
|
multiprocess_mode="livesum",
|
|
),
|
|
transitions=PrometheusCounter(
|
|
"litellm_redis_circuit_breaker_transitions",
|
|
"Redis circuit breaker state transitions",
|
|
labelnames=("state",),
|
|
),
|
|
failures=PrometheusCounter(
|
|
"litellm_redis_circuit_breaker_failures",
|
|
"Redis health failures counted by the circuit breaker",
|
|
labelnames=("failure_class",),
|
|
),
|
|
)
|
|
|
|
|
|
class PrometheusServicesLogger:
|
|
# Class variables or attributes
|
|
litellm_service_latency = None # Class-level attribute to store the Histogram
|
|
|
|
def __init__(
|
|
self,
|
|
mock_testing: bool = False,
|
|
**kwargs,
|
|
):
|
|
try:
|
|
try:
|
|
from prometheus_client import REGISTRY, Counter, Gauge, Histogram
|
|
from prometheus_client.gc_collector import Collector
|
|
except ImportError:
|
|
raise Exception("Missing prometheus_client. Run `pip install prometheus-client`")
|
|
|
|
_custom_buckets: Final = litellm.prometheus_latency_buckets
|
|
self.latency_buckets = tuple(_custom_buckets) if _custom_buckets is not None else LATENCY_BUCKETS
|
|
|
|
self.Histogram = Histogram
|
|
self.Counter = Counter
|
|
self.Gauge = Gauge
|
|
self.REGISTRY = REGISTRY
|
|
breaker_collectors: Final = _redis_breaker_collectors()
|
|
self.redis_breaker_state = breaker_collectors.state
|
|
self.redis_breaker_transitions = breaker_collectors.transitions
|
|
self.redis_breaker_failures = breaker_collectors.failures
|
|
self._refresh_redis_breaker_gauge()
|
|
|
|
verbose_logger.debug("in init prometheus services metrics")
|
|
|
|
self.payload_to_prometheus_map: dict[str, list[Histogram | Counter | Gauge | Collector]] = {}
|
|
|
|
for service in ServiceTypes:
|
|
service_metrics: list[Histogram | Counter | Gauge | Collector] = []
|
|
|
|
metrics_to_initialize = self._get_service_metrics_initialize(service)
|
|
|
|
# Initialize only the configured metrics for each service
|
|
if ServiceMetrics.HISTOGRAM in metrics_to_initialize:
|
|
histogram = self.create_histogram(service.value, type_of_request="latency")
|
|
if histogram:
|
|
service_metrics.append(histogram)
|
|
|
|
if ServiceMetrics.COUNTER in metrics_to_initialize:
|
|
counter_failed_request = self.create_counter(
|
|
service.value,
|
|
type_of_request="failed_requests",
|
|
additional_labels=FAILED_REQUESTS_LABELS,
|
|
)
|
|
if counter_failed_request:
|
|
service_metrics.append(counter_failed_request)
|
|
counter_total_requests = self.create_counter(service.value, type_of_request="total_requests")
|
|
if counter_total_requests:
|
|
service_metrics.append(counter_total_requests)
|
|
|
|
if ServiceMetrics.GAUGE in metrics_to_initialize:
|
|
gauge = self.create_gauge(service.value, type_of_request="size")
|
|
if gauge:
|
|
service_metrics.append(gauge)
|
|
|
|
if service_metrics:
|
|
self.payload_to_prometheus_map[service.value] = service_metrics
|
|
|
|
self.prometheus_to_amount_map: dict = {}
|
|
### MOCK TESTING ###
|
|
self.mock_testing = mock_testing
|
|
self.mock_testing_success_calls = 0
|
|
self.mock_testing_failure_calls = 0
|
|
|
|
except Exception as e:
|
|
print_verbose(f"Got exception on init prometheus client {e}")
|
|
raise e
|
|
|
|
def _get_service_metrics_initialize(self, service: ServiceTypes) -> list[ServiceMetrics]:
|
|
DEFAULT_METRICS: Final = [ServiceMetrics.COUNTER, ServiceMetrics.HISTOGRAM]
|
|
if service not in DEFAULT_SERVICE_CONFIGS:
|
|
return DEFAULT_METRICS
|
|
|
|
metrics: Final = DEFAULT_SERVICE_CONFIGS.get(service, {}).get("metrics", [])
|
|
if not metrics:
|
|
verbose_logger.debug("No metrics found for service %s", service)
|
|
return DEFAULT_METRICS
|
|
return metrics
|
|
|
|
def _refresh_redis_breaker_gauge(self) -> None:
|
|
from litellm.caching.redis_cache import RedisCircuitBreaker, redis_breaker_state_counts
|
|
|
|
counts: Final = redis_breaker_state_counts()
|
|
for state in (RedisCircuitBreaker.CLOSED, RedisCircuitBreaker.OPEN, RedisCircuitBreaker.HALF_OPEN):
|
|
self.redis_breaker_state.labels(state).set(counts.get(state, 0))
|
|
|
|
def redis_breaker_hook(self, transitions_to: str | None, failure_class: str | None) -> None:
|
|
self._refresh_redis_breaker_gauge()
|
|
if transitions_to is not None:
|
|
self.redis_breaker_transitions.labels(transitions_to).inc()
|
|
if failure_class is not None:
|
|
self.redis_breaker_failures.labels(failure_class).inc()
|
|
|
|
def is_metric_registered(self, metric_name) -> bool:
|
|
# Use _names_to_collectors (O(1)) instead of REGISTRY.collect() (O(n)) to avoid
|
|
# perf regression when a new Router is created per request (e.g. router_settings in DB).
|
|
names_to_collectors: Final = getattr(self.REGISTRY, "_names_to_collectors", None)
|
|
if names_to_collectors is not None:
|
|
return metric_name in names_to_collectors
|
|
for metric in self.REGISTRY.collect():
|
|
if metric_name == metric.name:
|
|
return True
|
|
return False
|
|
|
|
def _get_metric(self, metric_name):
|
|
"""
|
|
Helper function to get a metric from the registry by name.
|
|
"""
|
|
return self.REGISTRY._names_to_collectors.get( # pyright: ignore[reportPrivateUsage] # Registry lookup has no public API
|
|
metric_name
|
|
)
|
|
|
|
def create_histogram(self, service: str, type_of_request: str):
|
|
metric_name: Final = f"litellm_{service}_{type_of_request}"
|
|
is_registered: Final = self.is_metric_registered(metric_name)
|
|
if is_registered:
|
|
return self._get_metric(metric_name)
|
|
return self.Histogram(
|
|
metric_name,
|
|
f"Latency for {service} service",
|
|
labelnames=[service],
|
|
buckets=self.latency_buckets,
|
|
)
|
|
|
|
def create_gauge(self, service: str, type_of_request: str):
|
|
metric_name: Final = f"litellm_{service}_{type_of_request}"
|
|
is_registered: Final = self.is_metric_registered(metric_name)
|
|
if is_registered:
|
|
return self._get_metric(metric_name)
|
|
return self.Gauge(metric_name, f"Gauge for {service} service", labelnames=[service])
|
|
|
|
def create_counter(
|
|
self,
|
|
service: str,
|
|
type_of_request: str,
|
|
additional_labels: list[str] | None = None,
|
|
):
|
|
metric_name: Final = f"litellm_{service}_{type_of_request}"
|
|
is_registered: Final = self.is_metric_registered(metric_name)
|
|
if is_registered:
|
|
return self._get_metric(metric_name)
|
|
return self.Counter(
|
|
metric_name,
|
|
f"Total {type_of_request} for {service} service",
|
|
labelnames=[service] + (additional_labels or []),
|
|
)
|
|
|
|
def observe_histogram(
|
|
self,
|
|
histogram,
|
|
labels: str,
|
|
amount: float,
|
|
):
|
|
assert isinstance(histogram, self.Histogram)
|
|
|
|
histogram.labels(labels).observe(amount)
|
|
|
|
def update_gauge(
|
|
self,
|
|
gauge,
|
|
labels: str,
|
|
amount: float,
|
|
):
|
|
assert isinstance(gauge, self.Gauge)
|
|
gauge.labels(labels).set(amount)
|
|
|
|
def increment_counter(
|
|
self,
|
|
counter,
|
|
labels: str,
|
|
amount: float,
|
|
additional_labels: list[str] | None = [],
|
|
):
|
|
assert isinstance(counter, self.Counter)
|
|
|
|
if additional_labels:
|
|
counter.labels(labels, *additional_labels).inc(amount)
|
|
else:
|
|
counter.labels(labels).inc(amount)
|
|
|
|
def service_success_hook(self, payload: ServiceLoggerPayload):
|
|
if self.mock_testing:
|
|
self.mock_testing_success_calls += 1
|
|
|
|
if payload.service.value in self.payload_to_prometheus_map:
|
|
prom_objects: Final = self.payload_to_prometheus_map[payload.service.value]
|
|
for obj in prom_objects:
|
|
if isinstance(obj, self.Histogram):
|
|
self.observe_histogram(
|
|
histogram=obj,
|
|
labels=payload.service.value,
|
|
amount=payload.duration,
|
|
)
|
|
elif isinstance(obj, self.Counter) and "total_requests" in obj._name: # pyright: ignore[reportPrivateUsage] # Metric names have no public accessor
|
|
self.increment_counter(
|
|
counter=obj,
|
|
labels=payload.service.value,
|
|
amount=1, # LOG TOTAL REQUESTS TO PROMETHEUS
|
|
)
|
|
|
|
def service_failure_hook(self, payload: ServiceLoggerPayload):
|
|
if self.mock_testing:
|
|
self.mock_testing_failure_calls += 1
|
|
|
|
if payload.service.value in self.payload_to_prometheus_map:
|
|
prom_objects: Final = self.payload_to_prometheus_map[payload.service.value]
|
|
for obj in prom_objects:
|
|
if isinstance(obj, self.Counter):
|
|
self.increment_counter(
|
|
counter=obj,
|
|
labels=payload.service.value,
|
|
amount=1, # LOG ERROR COUNT / TOTAL REQUESTS TO PROMETHEUS
|
|
)
|
|
|
|
async def async_service_success_hook(self, payload: ServiceLoggerPayload):
|
|
"""
|
|
Log successful call to prometheus
|
|
"""
|
|
if self.mock_testing:
|
|
self.mock_testing_success_calls += 1
|
|
|
|
if payload.service.value in self.payload_to_prometheus_map:
|
|
prom_objects: Final = self.payload_to_prometheus_map[payload.service.value]
|
|
for obj in prom_objects:
|
|
if isinstance(obj, self.Histogram):
|
|
self.observe_histogram(
|
|
histogram=obj,
|
|
labels=payload.service.value,
|
|
amount=payload.duration,
|
|
)
|
|
elif isinstance(obj, self.Counter) and "total_requests" in obj._name: # pyright: ignore[reportPrivateUsage] # Metric names have no public accessor
|
|
self.increment_counter(
|
|
counter=obj,
|
|
labels=payload.service.value,
|
|
amount=1, # LOG TOTAL REQUESTS TO PROMETHEUS
|
|
)
|
|
elif isinstance(obj, self.Gauge):
|
|
if payload.event_metadata:
|
|
self.update_gauge(
|
|
gauge=obj,
|
|
labels=payload.event_metadata.get("gauge_labels") or "",
|
|
amount=payload.event_metadata.get("gauge_value") or 0,
|
|
)
|
|
|
|
async def async_service_failure_hook(
|
|
self,
|
|
payload: ServiceLoggerPayload,
|
|
error: str | Exception,
|
|
):
|
|
if self.mock_testing:
|
|
self.mock_testing_failure_calls += 1
|
|
error_class: Final = error.__class__.__name__
|
|
function_name: Final = payload.call_type
|
|
|
|
if payload.service.value in self.payload_to_prometheus_map:
|
|
prom_objects: Final = self.payload_to_prometheus_map[payload.service.value]
|
|
for obj in prom_objects:
|
|
# increment both failed and total requests
|
|
if isinstance(obj, self.Counter):
|
|
if "failed_requests" in obj._name: # pyright: ignore[reportPrivateUsage] # Metric names have no public accessor
|
|
self.increment_counter(
|
|
counter=obj,
|
|
labels=payload.service.value,
|
|
# log additional_labels=["error_class", "function_name"], used for debugging what's going wrong with the DB
|
|
additional_labels=[error_class, function_name],
|
|
amount=1, # LOG ERROR COUNT TO PROMETHEUS
|
|
)
|
|
else:
|
|
self.increment_counter(
|
|
counter=obj,
|
|
labels=payload.service.value,
|
|
amount=1, # LOG TOTAL REQUESTS TO PROMETHEUS
|
|
)
|