Ë
    uPêjü8  ã                   óä   — d dl Z d dlmZmZ d dl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z  ZeZneZeZeZdedz  fd„Z G d„ de«      Zy)é    N)ÚdatetimeÚ	timedelta)ÚTYPE_CHECKINGÚAnyÚFinal)Úverbose_loggeré   )ÚCustomLogger©ÚDataDogLogger)ÚOpenTelemetry)ÚPrometheusServicesLogger)ÚServiceLoggerPayloadÚServiceTypes)ÚSpan)ÚUserAPIKeyAuthÚreturnc                  ó2   — 	 ddl m}  | S # t        $ r Y yw xY w)a•  Return the ``OpenTelemetryV2`` class, or ``None`` if the OTel SDK is absent.

    Imported lazily: ``litellm.integrations.otel.logger`` imports the OpenTelemetry
    SDK at module scope, so importing it eagerly would break installs without the
    SDK. The V2 logger only exists when ``LITELLM_OTEL_V2`` is enabled (which
    requires the SDK), so a failed import simply means "no V2 logger in play".
    r   ©ÚOpenTelemetryV2N)Ú litellm.integrations.otel.loggerr   Ú	Exceptionr   s    ú\/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/_service_logger.pyÚ_get_otel_v2_classr      s"   € ðÝDàÐøÜò Ùðús   ‚
 Š	•c                   óŠ  ‡ — e Zd ZdZddeddfd„Zdededz  fd„Z	 	 	 dded	e	d
e
dedz  dee	z  dz  de	ez  dz  fd„Zded	e	ded
e
fd„Z	 	 	 	 dded
e
d	e	dedz  dee	z  dz  dee	z  dz  dedz  fd„Zd„ Zd„ Zd„ Z	 	 	 	 dded	e	de
ez  d
e
dedz  dee	z  dz  de	ez  dz  dedz  fd„Z	 d dededede
dz  fˆ fd„Zd„ Zˆ xZS )!ÚServiceLoggingzb
    Separate class used for monitoring health of litellm-adjacent services (redis/postgres).
    Úmock_testingr   Nc                 óŽ   — || _         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     r   Ú__init__zServiceLogging.__init__0   sH   € Ø(ˆÔØ./ˆÔ+Ø/0ˆÔ,Ø./ˆÔ+Ø/0ˆÔ,Ø¤'×":Ñ":Ñ:Ü,DÓ,FˆDÕ)ð ;ó    Úcallbackc                 ó€   ‡— t        «       Šdt        dt        fˆfd„} ||«      r|S |dk(  rddlm} |�
 ||«      r|S y)uÖ  Resolve the OTel logger (legacy or V2) to emit a service span on.

        Returns the logger instance whose ``async_service_*_hook`` should fire for
        this ``callback``, or ``None`` when ``callback`` is not an OTel callback.

        The V2 ``OpenTelemetryV2`` logger is a plain ``CustomLogger`` and is NOT a
        subclass of the legacy ``OpenTelemetry``, so the legacy ``isinstance``
        check alone misses it â€” which is why redis/postgres service spans never
        showed up under ``LITELLM_OTEL_V2``. Match both the legacy and V2 types,
        whether the callback is the logger instance itself or the ``"otel"`` string
        (which routes to the proxy's registered ``open_telemetry_logger``).
        Úobjr   c                 óJ   •— t        | t        «      ry‰d uxr t        | ‰«      S )NT)Ú
isinstancer   )r,   Úotel_v2_clss    €r   Ú_is_otel_loggerzDServiceLogging._resolve_otel_service_logger.<locals>._is_otel_loggerH   s'   ø€ Ü˜#œ}Ô-ØØ dÐ*ÒK¬z¸#¸{Ó/KÐKr)   Úotelr   ©Úopen_telemetry_loggerN)r   r   ÚboolÚlitellm.proxy.proxy_serverr3   )r'   r*   r0   r3   r/   s       @r   Ú_resolve_otel_service_loggerz+ServiceLogging._resolve_otel_service_logger9   sT   ø€ ô 0Ó1ˆð	L¤ð 	L¬õ 	Lñ
 ˜8Ô$ØˆOØ�vÒÝHà$Ð0±_ÐEZÔ5[Ø,Ð,Ør)   Ú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	   )r7   r8   r9   r:   r;   r<   N)
r   r    ÚasyncioÚget_event_loopÚ
is_runningÚcreate_taskÚasync_service_success_hookÚrun_until_completeÚRuntimeErrorÚrun)r'   r7   r8   r9   r:   r;   r<   Úloops           r   Úservice_success_hookz#ServiceLogging.service_success_hookV   så   € ð ×ÒØ×/Ò/°1Ñ4Õ/ð'	ä!×0Ñ0Ó2ˆDà�‰Ô à× Ñ Ø×3Ñ3Ø 'Ø!)Ø"+Ø)9Ø#-Ø!)ð 4ó õ	ð ×'Ñ'Ø×3Ñ3Ø 'Ø!)Ø"+Ø)9Ø#-Ø!)ð 4ó õ	øô ò 	ä�K‰KØ×/Ñ/Ø#Ø%Ø'Ø%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'   r7   r8   rH   r9   s        r   Úservice_failure_hookz#ServiceLogging.service_failure_hook�   s#   € ð ×ÒØ×/Ò/°1Ñ4Ö/ð r)   Úevent_metadatac              ƒ   ó¦  K  — | j                   r| xj                  dz  c_        t        dd||||¬«      }t        «       }	t        j
                  D ]ô  }
|
dk(  r=| j                  «       ƒ d{  –—†  | j                  j                  |¬«      ƒ d{  –—†  ŒE|
dk(  st        |
t        «      rA| j                  «       ƒ d{  –—†  | j                  j                  |||||¬«      ƒ d{  –—†  Œ›| j                  |
«      }|€Œ¯t        |«      |	vsŒ½|	j                  t        |«      «       |j                  |||||¬«      ƒ d{  –—†  Œö y7 ŒÜ7 Œº7 ŒŽ7 Œh7 Œ­w)	zJ
        - For counting if the redis, postgres call is successful
        r	   FN©Úis_errorrH   r7   r8   r9   rK   r   )ÚpayloadÚdatadog)rO   r:   r;   r<   rK   )r   r!   r   Úsetr$   r%   Ú'init_prometheus_services_logger_if_noner&   rB   r.   r   Úinit_datadog_logger_if_noneÚ	dd_loggerr6   ÚidÚadd)r'   r7   r9   r8   r:   r;   r<   rK   rO   Úemitted_otel_logger_idsr*   Ú_otel_logger_to_uses               r   rB   z)ServiceLogging.async_service_success_hook–   sk  è ø€ ð ×ÒØ×0Ò0°AÑ5Õ0ä-ØØØØØØ)ô
ˆô /2«eÐÜ×0Ñ0ò 	ˆHØÐ.Ò.Ø×BÑBÓD×DÐDØ×3Ñ3×NÑNÐW^ÐNÓ_×_Ñ_Ø˜YÒ&¬*°X¼}Ô*MØ×6Ñ6Ó8×8Ð8Ø—n‘n×?Ñ?Ø#Ø%5Ø)Ø%Ø#1ð @ó ÷ ñ ð '+×&GÑ&GÈÓ&QÐ#ð 'Ñ2´rÐ:MÓ7NÐVmÒ7mØ+×/Ñ/´Ð3FÓ0GÔHØ-×HÑHØ 'Ø)9Ø#-Ø!)Ø'5ð Ió ÷ ñ ñ-	àDøØ_øà8øðøð úsf   ‚A(EÁ*EÁ+#EÂE	Â-EÂ<EÂ='EÃ$EÃ%EÃ>EÄ3EÄ?EÅ EÅ	EÅEÅEÅEc              ƒ   óˆ   K  — t        | d«      st        «       | _        y| j                  €| j                  «       | _        yy­w)zu
        initializes prometheusServicesLogger if it is None or no attribute exists on ServiceLogging Object

        r&   N)Úhasattrr   r&   )r'   s    r   rR   z6ServiceLogging.init_prometheus_services_logger_if_noneÔ   s@   è ø€ ô
 �tÐ7Ô8Ü,DÓ,FˆDÕ)Ø×*Ñ*Ð2Ø,0×,IÑ,IÓ,KˆDÕ)ð 3ùs   ‚A Ac              ƒ   óJ   K  — ddl m} t        | d«      s |«       | _        yy­w)zf
        initializes dd_logger if it is None or no attribute exists on ServiceLogging Object

        r   r   rT   N)Ú$litellm.integrations.datadog.datadogr   rZ   rT   )r'   r   s     r   rS   z*ServiceLogging.init_datadog_logger_if_noneÞ   s$   è ø€ õ
 	Gä�t˜[Ô)Ù,9«OˆD�Nð *ùs   ‚!#c              ƒ   ó�   K  — ddl m} t        | d«      s0|�t        |t        «      r|| _        yt        j                  d«       yy­w)zh
        initializes otel_logger if it is None or no attribute exists on ServiceLogging Object

        r   r2   Úotel_loggerNzPServiceLogger: open_telemetry_logger is None or not an instance of OpenTelemetry)r5   r3   rZ   r.   r   r^   r   Úwarning)r'   r3   s     r   Úinit_otel_logger_if_nonez'ServiceLogging.init_otel_logger_if_noneè   sE   è ø€ õ
 	Eä�t˜]Ô+Ø$Ð0´ZÐ@UÔWdÔ5eØ2G�Õ ä×&Ñ&Øfõð	 ,ùs   ‚AAc	           	   ƒ   óF  K  — | j                   r| xj                  dz  c_        d}	t        |t        «      rt	        |«      }	nt        |t        «      r|}	t        d|	||||¬«      }
t        «       }t        j                  D �]  }|dk(  r>| j                  «       ƒ d{  –—†  | j                  j                  |
|¬«      ƒ d{  –—†  ŒG|dk(  st        |t        «      rB| j                  «       ƒ d{  –—†  | j                  j                  |
|	||||¬	«      ƒ d{  –—†  Œž| j                  |«      }t        |t        «      st	        |«      }|€ŒÍt!        |«      |vsŒÛ|j#                  t!        |«      «       |j                  |
|||||¬	«      ƒ d{  –—†  �Œ y7 Œû7 ŒØ7 Œ¬7 Œ…7 Œ­w)
zL
        - For counting if the redis, postgres call is unsuccessful
        r	   Ú TrM   r   N)rO   rH   rP   )rO   rH   r:   r;   r<   rK   )r   r#   r.   r   Ústrr   rQ   r$   r%   rR   r&   Úasync_service_failure_hookr   rS   rT   r6   rU   rV   )r'   r7   r8   rH   r9   r:   r;   r<   rK   Úerror_messagerO   rW   r*   rX   s                 r   rd   z)ServiceLogging.async_service_failure_hook÷   s½  è ø€ ð ×ÒØ×0Ò0°AÑ5Õ0àˆÜ�eœYÔ'Ü ›J‰MÜ˜œsÔ#Ø!ˆMä-ØØØØØØ)ô
ˆô /2«eÐÜ×0Ñ0ó "	ˆHØÐ.Ò.Ø×BÑBÓD×DÐDØ×3Ñ3×NÑNØ#Øð Oó ÷ ñ ð ˜YÒ&¬*°X¼}Ô*MØ×6Ñ6Ó8×8Ð8Ø—n‘n×?Ñ?Ø#Ø'Ø%5Ø)Ø%Ø#1ð @ó ÷ ñ ð '+×&GÑ&GÈÓ&QÐ#ä! %¬Ô-Ü ›J�Eð 'Ñ2´rÐ:MÓ7NÐVmÒ7mØ+×/Ñ/´Ð3FÓ0GÔHØ-×HÑHØ 'Ø#Ø)9Ø#-Ø!)Ø'5ð Ió ÷ ò ñ7"	àDøðøð
 9øðøð$úsf   ‚BF!ÂFÂ$F!Ã FÃ-F!Ã.FÃ/(F!ÄFÄ3F!ÅF!Å4F!ÆFÆ	F!ÆF!ÆF!ÆF!ÆF!Ú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'   rf   rg   rh   ri   Ú	__class__s        €r   rl   z+ServiceLogging.async_post_call_failure_hook>  s.   øè ø€ ô ‘WÑ9ØØØó
÷ 
ð 	
ð 
ús   ƒ˜™c              ƒ   óR  K  — 	 ||z
  }t        |t        «      r|j                  «       }n+t        |t        «      rnt	        d|› dt        |«      › �«      ‚| j                  t        j                  ||j                  dd«      ||¬«      ƒ d{  –—†  y7 Œ# t        $ r}|‚d}~ww xY w­w)zG
        Hook to track latency for litellm proxy llm api calls
        z	Duration=z* is not a float or timedelta object. type=r9   Úunknown)r7   r8   r9   r;   r<   N)
r.   r   Útotal_secondsÚfloatr   ÚtyperB   r   ÚLITELLMÚget)r'   ÚkwargsÚresponse_objr;   r<   Ú	_durationÚes          r   Úasync_log_success_eventz&ServiceLogging.async_log_success_eventN  sµ   è ø€ ð	Ø  :Ñ-ˆIÜ˜)¤YÔ/Ø%×3Ñ3Ó5‘	Ü˜I¤uÔ-ØäØ 	˜{Ð*TÔUYÐZcÓUdÐTeÐfóð ð
 ×1Ñ1Ü$×,Ñ,Ø"Ø Ÿ*™* [°)Ó<Ø%Ø!ð 2ó ÷ ò ùô ò 	ØˆGûð	üs;   ‚B'„BB ÂBÂB ÂB'ÂB Â	B$ÂBÂB$Â$B')F)NNN)NNNN)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r4   r(   r   r6   r   rq   rc   r   r   rG   r   rJ   ÚdictrB   rR   rS   r`   rd   r   rl   ry   Ú__classcell__)rm   s   @r   r   r   +   s  ø„ ññG Tð G°dó Gð°Sð ¸SÀ4¹Zó ðD )-Ø.2Ø,0ñ7àð7ð ð7ð ð	7ð
  ™+ð7ð ˜uÑ$ tÑ+ð7ð ˜(Ñ" TÑ)ó7ðr5¨Lð 5ÀEð 5ÐR[ð 5Ðhkó 5ð )-Ø.2Ø,0Ø&*ñ<àð<ð ð<ð ð	<ð
  ™+ð<ð ˜uÑ$ tÑ+ð<ð ˜UÑ" TÑ)ð<ð ˜t™ó<ò|Lò<òð* )-Ø.2Ø,0Ø&*ñEàðEð ðEð �Y‰ð	Eð
 ðEð  ™+ðEð ˜uÑ$ tÑ+ðEð ˜(Ñ" TÑ)ðEð ˜t™óEðX %)ñ
àð
ð &ð
ð *ð	
ð
 ˜T‘zõ
ö r)   r   )r>   r   r   Útypingr   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   Ú	OTELClassrr   r   r   © r)   r   ú<module>rŒ      sj   ðÛ ß (ß ,Ñ ,ã Ý +å 4Ý 7Ý 5Ý Fß >áÝ1å3à�3‰;€DØ�Ià€DØ€IØ€Nð˜D 4™Kó ô {�\õ {r)   