From 1429bcc2dead21219c5254ba20f7c74d2c82ef8f Mon Sep 17 00:00:00 2001 From: Krrish Dholakia Date: Mon, 4 Nov 2024 15:00:03 +0530 Subject: [PATCH] perf(litellm_pre_call_utils.py): add otel tracing for pre-call utils adds 200ms on calls with pgdb connected --- litellm/proxy/litellm_pre_call_utils.py | 18 ++++++++++++++++++ litellm/types/services.py | 1 + 2 files changed, 19 insertions(+) diff --git a/litellm/proxy/litellm_pre_call_utils.py b/litellm/proxy/litellm_pre_call_utils.py index a34dffccdc1..f57eaaf1f00 100644 --- a/litellm/proxy/litellm_pre_call_utils.py +++ b/litellm/proxy/litellm_pre_call_utils.py @@ -1,4 +1,5 @@ import copy +import time from typing import TYPE_CHECKING, Any, Dict, Optional, Union from fastapi import Request @@ -6,6 +7,7 @@ from starlette.datastructures import Headers import litellm from litellm._logging import verbose_logger, verbose_proxy_logger +from litellm._service_logger import ServiceLogging from litellm.proxy._types import ( AddTeamCallback, CommonProxyErrors, @@ -16,11 +18,15 @@ from litellm.proxy._types import ( UserAPIKeyAuth, ) from litellm.proxy.auth.auth_utils import get_request_route +from litellm.types.services import ServiceTypes from litellm.types.utils import ( StandardLoggingUserAPIKeyMetadata, SupportedCacheControls, ) +service_logger_obj = ServiceLogging() # used for tracking latency on OTEL + + if TYPE_CHECKING: from litellm.proxy.proxy_server import ProxyConfig as _ProxyConfig @@ -293,6 +299,8 @@ async def add_litellm_data_to_request( # noqa: PLR0915 """ from litellm.proxy.proxy_server import llm_router, premium_user + start_time = time.time() + safe_add_api_version_from_query_params(data, request) _headers = clean_headers( @@ -539,6 +547,16 @@ async def add_litellm_data_to_request( # noqa: PLR0915 verbose_proxy_logger.debug( f"[PROXY]returned data from litellm_pre_call_utils: {data}" ) + + end_time = time.time() + await service_logger_obj.async_service_success_hook( + service=ServiceTypes.PROXY_PRE_CALL, + duration=end_time - start_time, + call_type="completion", + start_time=start_time, + end_time=end_time, + parent_otel_span=user_api_key_dict.parent_otel_span, + ) return data diff --git a/litellm/types/services.py b/litellm/types/services.py index 5f690f328b2..cfa427ebc3d 100644 --- a/litellm/types/services.py +++ b/litellm/types/services.py @@ -16,6 +16,7 @@ class ServiceTypes(str, enum.Enum): LITELLM = "self" ROUTER = "router" AUTH = "auth" + PROXY_PRE_CALL = "proxy_pre_call" class ServiceLoggerPayload(BaseModel):