
    g                         d Z ddlZddlmZ ddlmZ ddlmZmZmZ ddl	m
Z
 ddlmZmZ d Zd	ed
efdZd	edededededed
dfdZy)z|
Handles logging DB success/failure to ServiceLogger()

ServiceLogger() then sends DB logs to Prometheus, OTEL, Datadog etc
    N)datetimewraps)CallableDictTuple)ServiceTypes)!_get_parent_otel_span_from_kwargs get_litellm_metadata_from_kwargsc                 .     t                fd       }|S )a  
    Decorator to log the duration of a DB related function to ServiceLogger()

    Handles logging DB success/failure to ServiceLogger(), which logs to Prometheus, OTEL, Datadog

    When logging Failure it checks if the Exception is a PrismaError, httpx.ConnectError or httpx.TimeoutException and then logs that as a DB Service Failure

    Args:
        func: The function to be decorated

    Returns:
        Result from the decorated function

    Raises:
        Exception: If the decorated function raises an exception
    c                  L  K   t        j                         }	  
| i | d {   }t        j                         }ddlm} d
j                  vr~t        j                  |j                  j                  t        j                  
j                  |j                  dd       ||z
  j                         ||
j                  || d             |S | t        |       dkD  rt        | d   t              rl| d   }t!        |      }|Yt#        |      }t        j                  |j                  j                  t        j$                  
j                  |d|||             |S 7 <# t&        $ r5}	t        j                         }t)        |	
|| ||	       d {  7   |	d }	~	ww xY ww)
Nr   proxy_logging_objPROXYparent_otel_spanfunction_namefunction_kwargsfunction_args)service	call_typer   duration
start_timeend_timeevent_metadata)kwargsg        )efuncr   argsr   r   )r   nowlitellm.proxy.proxy_serverr   __name__asynciocreate_taskservice_logging_objasync_service_success_hookr	   DBgettotal_secondslen
isinstancedictr
   r   BATCH_WRITE_TO_DB	Exception_handle_logging_db_exception)r   r   r   resultr   r   passed_kwargsr   metadatar   r   s             T/var/www/openai/venv/lib/python3.12/site-packages/litellm/proxy/db/log_db_metrics.pywrapperzlog_db_metrics.<locals>.wrapper%   s      (||~
:	000F!)HDdmm+##%99TT ,"&--)/4F)M"*Z"7!F!F!H#-!)-1]]/5-1( U R M/  IMtAw- $Q#D($  $/?}UH'')==XX$0$B$B&*mm-=%('1%-+3 Y 
 M] 1^  
	!)H.%!   G
	sR   F$E# E B)E# F$BE# F$ E# #	F!,(FFFF!!F$r   )r   r4   s   ` r3   log_db_metricsr5      s#    $ 4[> >@ N    r   returnc                 \    ddl }ddlm} t        | ||j                  |j
                  f      S )z<
    Returns True if the exception is related to the DB
    r   N)PrismaError)httpxprisma.errorsr9   r+   ConnectErrorTimeoutException)r   r:   r9   s      r3   _is_exception_related_to_dbr>   i   s*    
 )a+u'9'95;Q;QRSSr6   r   r   r   r   r   c                   K   ddl m} t        |       dury |j                  j	                  | t
        j                  |j                  |j                  d      ||z
  j                         |||j                  ||d       d {    y 7 w)Nr   r   Tr   r   )errorr   r   r   r   r   r   r   )
r!   r   r>   r%   async_service_failure_hookr	   r'   r"   r(   r)   )r   r   r   r   r   r   r   s          r3   r/   r/   t   s      = #1%T1

/
/
J
J--$67Z'668!]]%!
 K   s   BBB	B)__doc__r#   r   	functoolsr   typingr   r   r   litellm._service_loggerr	   'litellm.litellm_core_utils.core_helpersr
   r   r5   r.   boolr>   r/    r6   r3   <module>rI      s       ( ( 0SlT9 T T
  	
   
r6   