The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(prometheus.py): pass custom metadata labels in litellm_total_toke… (#11414)
Krish Dholakia69c9d75f2024
diff --git a/litellm/integrations/prometheus.py b/litellm/integrations/prometheus.pyindex a66b1e755f6..c33dcc5b553 100644--- a/litellm/integrations/prometheus.py+++ b/litellm/integrations/prometheus.py@@ -117,15 +117,9 @@ class PrometheusLogger(CustomLogger):self.litellm_tokens_metric = Counter("litellm_total_tokens","Total number of input + output tokens from LLM requests",- labelnames=[- "end_user",- "hashed_api_key",- "api_key_alias",- "model",- "team",- "team_alias",- "user",- ],+ labelnames=PrometheusMetricLabels.get_labels(+ label_name="litellm_total_tokens_metric"+ ),)self.litellm_input_tokens_metric = Counter(@@ -550,21 +544,22 @@ class PrometheusLogger(CustomLogger):enum_values: UserAPIKeyLabelValues,):# token metrics- self.litellm_tokens_metric.labels(- end_user_id,- user_api_key,- user_api_key_alias,- model,- user_api_team,- user_api_team_alias,- user_id,- ).inc(standard_logging_payload["total_tokens"])if standard_logging_payload is not None and isinstance(standard_logging_payload, dict):_tags = standard_logging_payload["request_tags"]+ _labels = prometheus_label_factory(+ supported_enum_labels=PrometheusMetricLabels.get_labels(+ label_name="litellm_proxy_total_requests_metric"+ ),+ enum_values=enum_values,+ )+ self.litellm_proxy_total_requests_metric.labels(**_labels).inc(+ standard_logging_payload["total_tokens"]+ )+_labels = prometheus_label_factory(supported_enum_labels=PrometheusMetricLabels.get_labels(label_name="litellm_input_tokens_metric"diff --git a/litellm/llms/openai/realtime/handler.py b/litellm/llms/openai/realtime/handler.pyindex 099eeab7e52..a865de41184 100644--- a/litellm/llms/openai/realtime/handler.py+++ b/litellm/llms/openai/realtime/handler.py@@ -17,9 +17,12 @@ class OpenAIRealtime(OpenAIChatCompletion):Example output:"BACKEND_WS_URL = "wss://localhost:8080/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01"";"""+ from httpx import URL+api_base = api_base.replace("https://", "wss://")api_base = api_base.replace("http://", "ws://")- return f"{api_base}/v1/realtime?model={model}"+ url = URL(api_base).join("/v1/realtime")+ return str(url.copy_add_param("model", model))async def async_realtime(self,diff --git a/litellm/proxy/_new_secret_config.yaml b/litellm/proxy/_new_secret_config.yamlindex 584d8c9c107..2f605bc76c3 100644--- a/litellm/proxy/_new_secret_config.yaml+++ b/litellm/proxy/_new_secret_config.yaml@@ -91,6 +91,7 @@ litellm_settings:ttl: 600password: os.environ/REDIS_PASSWORDsupported_call_types: ["acompletion", "completion"]+ callbacks: ["prometheus"]router_settings:redis_password: os.environ/REDIS_PASSWORDdiff --git a/litellm/types/integrations/prometheus.py b/litellm/types/integrations/prometheus.pyindex 0155a0630c0..49009f6e95f 100644--- a/litellm/types/integrations/prometheus.py+++ b/litellm/types/integrations/prometheus.py@@ -80,6 +80,7 @@ DEFINED_PROMETHEUS_METRICS = Literal["litellm_proxy_failed_requests_metric","litellm_deployment_latency_per_output_token","litellm_requests_metric",+ "litellm_total_tokens_metric","litellm_input_tokens_metric","litellm_output_tokens_metric","litellm_deployment_successful_fallbacks",@@ -175,6 +176,17 @@ class PrometheusMetricLabels:UserAPIKeyLabelNames.REQUESTED_MODEL.value,]+ litellm_total_tokens_metric = [+ UserAPIKeyLabelNames.END_USER.value,+ UserAPIKeyLabelNames.API_KEY_HASH.value,+ UserAPIKeyLabelNames.API_KEY_ALIAS.value,+ UserAPIKeyLabelNames.v1_LITELLM_MODEL_NAME.value,+ UserAPIKeyLabelNames.TEAM.value,+ UserAPIKeyLabelNames.TEAM_ALIAS.value,+ UserAPIKeyLabelNames.USER.value,+ UserAPIKeyLabelNames.REQUESTED_MODEL.value,+ ]+litellm_output_tokens_metric = [UserAPIKeyLabelNames.END_USER.value,UserAPIKeyLabelNames.API_KEY_HASH.value,diff --git a/tests/test_litellm/llms/openai/realtime/test_openai_realtime_handler.py b/tests/test_litellm/llms/openai/realtime/test_openai_realtime_handler.pynew file mode 100644index 00000000000..0681be6c22d--- /dev/null+++ b/tests/test_litellm/llms/openai/realtime/test_openai_realtime_handler.py@@ -0,0 +1,27 @@+import json+import os+import sys+from unittest.mock import AsyncMock, MagicMock, patch++import httpx+import pytest++sys.path.insert(+ 0, os.path.abspath("../../../../..")+) # Adds the parent directory to the system path+++ "api_base", ["https://api.openai.com/v1", "https://api.openai.com"]+)+def test_openai_realtime_handler_url_construction(api_base):+ from litellm.llms.openai.realtime.handler import OpenAIRealtime++ handler = OpenAIRealtime()+ url = handler._construct_url(+ api_base=api_base, model="gpt-4o-realtime-preview-2024-10-01"+ )+ assert (+ url+ == f"wss://api.openai.com/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01"+ )