
    gJ-                         d dl Z d dlmZmZ d dlmZmZmZmZ d dlZd dl	m
Z
 d dlmZ ddlmZ ddlmZ dd	lmZ dd
lmZmZ erd dlmZ d dlmZ eZeZneZeZ G d de      Zy)    N)datetime	timedelta)TYPE_CHECKINGAnyOptionalUnion)verbose_logger)UserAPIKeyAuth   )CustomLoggerDataDogLogger)PrometheusServicesLogger)ServiceLoggerPayloadServiceTypes)SpanOpenTelemetryc                       e Zd ZdZddeddfdZ	 	 	 ddededed	e	e
   d
e	eeef      de	eeef      fdZdedededefdZ	 	 	 	 ddededed	e	e
   d
e	eeef      de	eeef      de	e   fdZd Zd Zd Z	 	 	 	 ddededeeef   ded	e	e
   d
e	eeef      de	eeef      de	e   fdZdededef fdZd Z xZS )ServiceLoggingzb
    Separate class used for monitoring health of litellm-adjacent services (redis/postgres).
    mock_testingreturnNc                     || _         d| _        d| _        d| _        d| _        dt
        j                  v rt               | _        y y )Nr   prometheus_system)	r   mock_testing_sync_success_hookmock_testing_async_success_hookmock_testing_sync_failure_hookmock_testing_async_failure_hooklitellmservice_callbackr   prometheusServicesLogger)selfr   s     L/var/www/openai/venv/lib/python3.12/site-packages/litellm/_service_logger.py__init__zServiceLogging.__init__   sH    (./+/0,./+/0,'":"::,D,FD) ;    serviceduration	call_typeparent_otel_span
start_timeend_timec                    | j                   r| xj                  dz  c_        	 t        j                         }|j	                         r'|j                  | j                  ||||||             y|j                  | j                  ||||||             y# t        $ r- t        j                  | j                  ||||||             Y yw xY w)z]
        Handles both sync and async monitoring by checking for existing event loop.
        r   )r&   r'   r(   r)   r*   r+   N)
r   r   asyncioget_event_loop
is_runningcreate_taskasync_service_success_hookrun_until_completeRuntimeErrorrun)r"   r&   r'   r(   r)   r*   r+   loops           r#   service_success_hookz#ServiceLogging.service_success_hook(   s     //14/'	))+D   33 '!)"+)9#-!) 4 	 ''33 '!)"+)9#-!) 4 	  	KK//#%'%5)% 0 		s   A
B .&B 3C
Cerrorc                 H    | j                   r| xj                  dz  c_        yy)zo
        [TODO] Not implemented for sync calls yet. V0 is focused on async monitoring (used by proxy).
        r   N)r   r   )r"   r&   r'   r7   r(   s        r#   service_failure_hookz#ServiceLogging.service_failure_hooka   s#     //14/ r%   event_metadatac                   K   ddl m} | j                  r| xj                  dz  c_        t	        dd|||      }	t
        j                  D ]  }
|
dk(  r=| j                          d{    | j                  j                  |	       d{    F|
d	k(  st        |
t              rA| j                          d{    | j                  j                  |	||||
       d{    |
dk(  st        |
|      sddlm} | j!                          d{    ||t        ||      s| j"                  j                  |	||||
       d{    	 y7 7 7 7 z7 K7 w)zJ
        - For counting if the redis, postgres call is successful
        r   r   r   FNis_errorr7   r&   r'   r(   r   )payloaddatadogr>   r)   r*   r+   r:   otelopen_telemetry_logger)"litellm.integrations.opentelemetryr   r   r   r   r   r    'init_prometheus_services_logger_if_noner!   r1   
isinstancer   init_datadog_logger_if_none	dd_loggerlitellm.proxy.proxy_serverrC   init_otel_logger_if_noneotel_logger)r"   r&   r(   r'   r)   r*   r+   r:   r   r>   callbackrC   s               r#   r1   z)ServiceLogging.async_service_success_hookj   s~     	E00A50&
  00H..BBDDD33NN# O    Y&*X}*M66888nn??#%5)%#1 @    V#z(M'JL33555 %0-9"#8-H**EE ')9#-!)'5 F   3 1D 9 6s   A$E!&E'#E!
E-E!8E9'E! E!E!8E!EE!E!E!)#E!E	E!E!E!E!E!E!c                    K   t        | d      st               | _        y| j                  | j                         | _        yw)zu
        initializes prometheusServicesLogger if it is None or no attribute exists on ServiceLogging Object

        r!   N)hasattrr   r!   )r"   s    r#   rE   z6ServiceLogging.init_prometheus_services_logger_if_none   sE     
 t78,D,FD) 	 **2,0,I,I,KD)s   ?Ac                 H   K   ddl m} t        | d      s |       | _        yw)zf
        initializes dd_logger if it is None or no attribute exists on ServiceLogging Object

        r   r   rH   N)$litellm.integrations.datadog.datadogr   rN   rH   )r"   r   s     r#   rG   z*ServiceLogging.init_datadog_logger_if_none   s"     
 	Gt[),9ODNs    "c                    K   ddl m} ddlm} t	        | d      s+|t        ||      r|| _        yt        j                  d       yw)zh
        initializes otel_logger if it is None or no attribute exists on ServiceLogging Object

        r   r   rB   rK   NzPServiceLogger: open_telemetry_logger is None or not an instance of OpenTelemetry)	rD   r   rI   rC   rN   rF   rK   r	   warning)r"   r   rC   s      r#   rJ   z'ServiceLogging.init_otel_logger_if_none   sS     
 	EDt]+$0Z%}6 3H 
 	 &&f 	s   AAc	           	      b  K   ddl m}	 | j                  r| xj                  dz  c_        d}
t	        |t
              rt        |      }
nt	        |t              r|}
t        d|
|||      }t        j                  D ]#  }|dk(  r>| j                          d{    | j                  j                  ||	       d{    G|d
k(  st	        |t              rB| j                          d{    | j                  j                  ||
||||       d{    |dk(  st	        ||	      sddlm} | j%                          d{    t	        |t              st        |      }||t	        ||	      s| j&                  j)                  |||||       d{    & y7 7 7 7 7 g7 w)zL
        - For counting if the redis, postgres call is unsuccessful
        r   r   r    Tr<   r   N)r>   r7   r?   )r>   r7   r)   r*   r+   r:   rA   rB   r@   )rD   r   r   r   rF   	Exceptionstrr   r   r    rE   r!   async_service_failure_hookr   rG   rH   rI   rC   rJ   rK   r1   )r"   r&   r'   r7   r(   r)   r*   r+   r:   r   error_messager>   rL   rC   s                 r#   rW   z)ServiceLogging.async_service_failure_hook   s     	E00A50eY'JMs#!M&
  00H..BBDDD33NN# O    Y&*X}*M66888nn??#'%5)%#1 @    V#z(M'JL33555!%-JE %0-9"#8-H**EE ')9#-!)'5 F   = 1D
 9 6s   BF/F"$F/;F%<-F/)F'*(F/F)F/*F/F+!F/&F/)F/6#F/F-	F/%F/'F/)F/+F/-F/request_dataoriginal_exceptionuser_api_key_dictc                 B   K   t         |   |||       d{   S 7 w)z<
        Hook to track failed litellm-service calls
        N)superasync_post_call_failure_hook)r"   rY   rZ   r[   	__class__s       r#   r^   z+ServiceLogging.async_post_call_failure_hook  s.      W9
 
 	
 
s   c                 H  K   	 ||z
  }t        |t              r|j                         }n5t        |t              rn$t	        dj                  |t        |                  | j                  t        j                  ||d          d{    y7 # t        $ r}|d}~ww xY ww)zG
        Hook to track latency for litellm proxy llm api calls
        z7Duration={} is not a float or timedelta object. type={}r(   )r&   r'   r(   N)
rF   r   total_secondsfloatrU   formattyper1   r   LITELLM)r"   kwargsresponse_objr*   r+   	_durationes          r#   async_log_success_eventz&ServiceLogging.async_log_success_event%  s     	 :-I)Y/%335	Iu-MTT!4	? 
 11$,," - 2   
  	G	s;   B"BB BB B"B 	BBBB")F)NNN)NNNN)__name__
__module____qualname____doc__boolr$   r   rb   rV   r   r   r   r   r6   rU   r9   dictr1   rE   rG   rJ   rW   r
   r^   rj   __classcell__)r_   s   @r#   r   r      s   GT Gd G ,07;5977 7 	7
 #4.7 U8U?347 5127r5#5/45=F5SV5 ,07;59)-99 9 	9
 #4.9 U8U?349 55129 !9v	
2 ,07;59)-EE E S)^$	E
 E #4.E U8U?34E 512E !EN

 &
 *	
r%   r   )r-   r   r   typingr   r   r   r   r   litellm._loggingr	   litellm.proxy._typesr
   integrations.custom_loggerr   integrations.datadog.datadogr    integrations.prometheus_servicesr   types.servicesr   r   opentelemetry.tracer   _SpanrD   r   	OTELClassr    r%   r#   <module>r}      sQ     ( 6 6  + / 4 7 F >1@DIDIa\ ar%   