
    h-                         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eef   ZeZneZeZeZ G d de      Zy)    N)datetime	timedelta)TYPE_CHECKINGAnyOptionalUnion)verbose_logger   )CustomLoggerDataDogLogger)OpenTelemetry)PrometheusServicesLogger)ServiceLoggerPayloadServiceTypes)Span)UserAPIKeyAuthc                       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dededede	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     S/var/www/Befach/backend/env/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&   r6   r'   s        r"   service_failure_hookz#ServiceLogging.service_failure_hookb   s#     //14/ r$   event_metadatac                   K   | 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        |	t              sd
dlm}
 | j                          d{    ||
t        |
t              s| j                   j                  |||||       d{     y7 7 7 7 7 O7 w)zJ
        - For counting if the redis, postgres call is successful
        r
   FNis_errorr6   r%   r&   r'   r9   r   )payloaddatadogr=   r(   r)   r*   r9   otelr   open_telemetry_logger)r   r   r   r   r   'init_prometheus_services_logger_if_noner    r0   
isinstancer   init_datadog_logger_if_none	dd_loggerr   litellm.proxy.proxy_serverrB   init_otel_logger_if_noneotel_logger)r!   r%   r'   r&   r(   r)   r*   r9   r=   callbackrB   s              r"   r0   z)ServiceLogging.async_service_success_hookk   s}     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$3E4'E$EE$7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"   rC   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   rF   N)$litellm.integrations.datadog.datadogr   rL   rF   )r!   r   s     r"   rE   z*ServiceLogging.init_datadog_logger_if_none   s"     
 	Gt[),9ODNs    "c                    K   ddl m} t        | d      s/|t        |t              r|| _        yt        j                  d       yw)zh
        initializes otel_logger if it is None or no attribute exists on ServiceLogging Object

        r   rA   rI   NzPServiceLogger: open_telemetry_logger is None or not an instance of OpenTelemetry)rG   rB   rL   rD   r   rI   r	   warning)r!   rB   s     r"   rH   z'ServiceLogging.init_otel_logger_if_none   sP     
 	Et]+$0Z%}6 3H 
 	 &&f 	s   AAc	           	      j  K   | 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        |t              sddlm} | j#                          d{    t        |t              st	        |      }||t        |t              s| j$                  j'                  |
||||       d{    / y7 7 7 7 7 l7 w)zL
        - For counting if the redis, postgres call is unsuccessful
        r
    Tr;   r   N)r=   r6   r>   )r=   r6   r(   r)   r*   r9   r@   r   rA   r?   )r   r   rD   	Exceptionstrr   r   r   rC   r    async_service_failure_hookr   rE   rF   r   rG   rB   rH   rI   r0   )r!   r%   r&   r6   r'   r(   r)   r*   r9   error_messager=   rJ   rB   s                r"   rU   z)ServiceLogging.async_service_failure_hook   s     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F3F&$F36F)7-F3$F+%(F3F-F3)F3F/!F3%F3(F3:#F3F1	F3)F3+F3-F3/F31F3request_dataoriginal_exceptionuser_api_key_dicttraceback_strc                 B   K   t         |   |||       d{   S 7 w)z<
        Hook to track failed litellm-service calls
        N)superasync_post_call_failure_hook)r!   rW   rX   rY   rZ   	__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)
rD   r   total_secondsfloatrS   formattyper0   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)N)__name__
__module____qualname____doc__boolr#   r   ra   rT   r   r   r   r   r5   rS   r8   dictr0   rC   rE   rH   rU   r   r]   ri   __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)-88 8 	8
 #4.8 U8U?348 55128 !8t	
0 ,07;59)-DD D S)^$	D
 D #4.D U8U?34D 512D !DV (,

 &
 *	

  }
 r$   r   )r,   r   r   typingr   r   r   r   r   litellm._loggingr	   integrations.custom_loggerr   integrations.datadog.datadogr   integrations.opentelemetryr    integrations.prometheus_servicesr   types.servicesr   r   opentelemetry.tracer   _Spanlitellm.proxy._typesr   	OTELClassr    r$   r"   <module>r}      s_     ( 6 6  + 4 7 5 F >13DIDIN_\ _r$   