Ë
    tPêjñ  ã                   ó²   — d 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 d	ed
eeef   dz  fd„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)ÚCallable)Údatetime©Úwraps)ÚFinal)ÚServiceTypes)Ú!_get_parent_otel_span_from_kwargsÚkwargsÚreturnc                 óP   — | j                  d«      }t        |t        «      rd|iS dS )u"  Minimal, non-sensitive ``event_metadata`` for a DB service log.

    The raw ``kwargs``/``args`` carry live objects (Prisma client, OTel spans)
    and secrets (tokens), none of which belongs on a span â€” so we surface only
    the table name when present. Everything else is dropped.
    Ú
table_nameN)ÚgetÚ
isinstanceÚstr)r
   r   s     úd/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/proxy/db/log_db_metrics.pyÚ_safe_db_event_metadatar      s+   € ð Ÿ
™
 <Ó0€JÜ)3°JÄÔ)DˆL˜*Ð%ÐNÈ$ÐNó    c                 ó.   ‡ — 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               Ÿ   ó*  •K  — t        j                  «       }	  ‰	| i |¤Žƒ d {  –—† }t        j                  «       }ddlm} d‰	j                  vryt        j                  |j                  j                  t        j                  ‰	j                  |j                  dd «      ||z
  j                  «       ||t        |«      ¬«      «       |S | ��t        | «      dkD  rst        | d   t         «      r`| d   }t#        |¬«      }|�Mt        j                  |j                  j                  t        j$                  ‰	j                  |d||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)ÚserviceÚ	call_typer   ÚdurationÚ
start_timeÚend_timeÚevent_metadataé   )r
   g        )ÚeÚfuncr
   Úargsr   r   )r   ÚnowÚlitellm.proxy.proxy_serverr   Ú__name__ÚasyncioÚcreate_taskÚservice_logging_objÚasync_service_success_hookr   ÚDBr   Útotal_secondsr   Úlenr   Údictr	   ÚBATCH_WRITE_TO_DBÚ	ExceptionÚ_handle_logging_db_exception)
r#   r
   r   Úresultr   r   Úpassed_kwargsr   r!   r"   s
            €r   Úwrapperzlog_db_metrics.<locals>.wrapper.   s‡  øè ø€ ä&.§l¡l£nˆ
ð3	Ù"&¨Ð"7°Ñ"7×7ˆFÜ!)§¡£ˆHÝDà˜dŸm™mÑ+Ü×#Ñ#Ø%×9Ñ9×TÑTÜ ,§¡Ø"&§-¡-Ø)/¯©Ð4FÈÓ)MØ"*¨ZÑ"7×!FÑ!FÓ!HØ#-Ø!)Ü'>¸vÓ'Fð Uó ô
ðD ˆMð) Ð ¤S¨£Y°¢]´zÀ$ÀqÁ'Ì4Ô7Pà'+¨A¡w�Ü*KÐS`Ô*aÐ Ø#Ð/ô ×'Ñ'Ø)×=Ñ=×XÑXÜ$0×$BÑ$BØ&*§m¡mØ-=Ø%(Ø'1Ø%-Ø+/ð Yó ô
ð ˆMðO 8úôP ò 
	Ü!)§¡£ˆHÜ.ØØØØØ%Ø!ô÷ ñ ð ˆGûð
	üsR   ƒF™E ¤E¥B$E Ã	FÃ
BE ÅFÅE Å	FÅ(FÆFÆFÆFÆFr   )r"   r4   s   ` r   Úlog_db_metricsr5      s#   ø€ ô$ ˆ4ƒ[ó6ó ð6ðp €Nr   r!   c                 ó\   — 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.errorsr7   r   ÚConnectErrorÚTimeoutException)r!   r8   r7   s      r   Ú_is_exception_related_to_dbr<   j   s*   € ó
 Ý)ä�a˜+ u×'9Ñ'9¸5×;QÑ;QÐRÓSÐSr   r"   r#   r   r   c              ƒ   ó  K  — ddl m} t        | «      dury |j                  j	                  | t
        j                  |j                  |j                  d«      ||z
  j                  «       ||t        |«      ¬«      ƒ d {  –—†  y 7 Œ­w)Nr   r   Tr   )Ú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   r   s          r   r1   r1   u   s~   è ø€ õ =ô # 1Ó%¨TÑ1Øà
×
/Ñ
/×
JÑ
JØÜ—‘Ø—-‘-ØŸ™Ð$6Ó7Ø˜ZÑ'×6Ñ6Ó8ØØÜ.¨vÓ6ð Kó 	÷ 	ò 	ús   ‚A<BÁ>BÁ?B)Ú__doc__r'   Úcollections.abcr   r   Ú	functoolsr   Útypingr   Úlitellm._service_loggerr   Ú'litellm.litellm_core_utils.core_helpersr	   r.   r   r   r5   r0   Úboolr<   Útupler1   © r   r   ú<module>rI      s¨   ðñó Ý $Ý Ý Ý å 0Ý UðO Dð O¨T°#°s°(©^¸dÑ-Bó OòKð\T 9ð T°ó TðØðà
ðð ðð ð	ð
 ðð ðð 
ôr   