fix(prometheus): route guardrail metrics through get_labels_for_metric so prometheus_exclude_labels applies to them

This commit is contained in:
Ishaan Jaffer 2026-03-23 18:04:17 -07:00
parent 46071b92c7
commit 7d3cad1e18
2 changed files with 23 additions and 6 deletions

View file

@ -297,20 +297,24 @@ class PrometheusLogger(CustomLogger):
self.litellm_guardrail_latency_metric = self._histogram_factory(
"litellm_guardrail_latency_seconds",
"Latency (seconds) for guardrail execution",
labelnames=["guardrail_name", "status", "error_type", "hook_type"],
labelnames=self.get_labels_for_metric(
"litellm_guardrail_latency_seconds"
),
buckets=self._get_latency_buckets(),
)
self.litellm_guardrail_errors_total = self._counter_factory(
"litellm_guardrail_errors_total",
"Total number of errors encountered during guardrail execution",
labelnames=["guardrail_name", "error_type", "hook_type"],
labelnames=self.get_labels_for_metric("litellm_guardrail_errors_total"),
)
self.litellm_guardrail_requests_total = self._counter_factory(
"litellm_guardrail_requests_total",
"Total number of guardrail invocations",
labelnames=["guardrail_name", "status", "hook_type"],
labelnames=self.get_labels_for_metric(
"litellm_guardrail_requests_total"
),
)
# llm api provider budget metrics
self.litellm_provider_remaining_budget_metric = self._gauge_factory(

View file

@ -292,9 +292,22 @@ class PrometheusMetricLabels:
# Guardrail metrics - these use custom labels (guardrail_name, status, error_type, hook_type)
# which are not part of UserAPIKeyLabelNames
litellm_guardrail_latency_seconds: List[str] = []
litellm_guardrail_errors_total: List[str] = []
litellm_guardrail_requests_total: List[str] = []
litellm_guardrail_latency_seconds: List[str] = [
"guardrail_name",
"status",
"error_type",
"hook_type",
]
litellm_guardrail_errors_total: List[str] = [
"guardrail_name",
"error_type",
"hook_type",
]
litellm_guardrail_requests_total: List[str] = [
"guardrail_name",
"status",
"hook_type",
]
litellm_proxy_total_requests_metric = [
UserAPIKeyLabelNames.END_USER.value,