o
    ưi                     @   s   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d	 Zd
edefddZd
ededededededdfddZdS )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                    s   t   fd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            
         s0  t  }zs | i |I d H }t  }ddlm} d jvrAt|jjt	j
 j|dd ||  || j|| dd |W S | d urvt| dkrvt| d trv| d }t|d}|d urvt|d}t|jjt	j j|d	|||d |W S  ty }	 zt  }t|	 || ||d
I d H  |	d }	~	ww )Nr   proxy_logging_objZPROXYparent_otel_spanZfunction_nameZfunction_kwargsZ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_objZasync_service_success_hookr   DBgettotal_secondslen
isinstancedictr	   r
   ZBATCH_WRITE_TO_DB	Exception_handle_logging_db_exception)
r   r   r   resultr   r   Zpassed_kwargsr   metadatar   r    V/home/app/Keep/.python/lib/python3.10/site-packages/litellm/proxy/db/log_db_metrics.pywrapper%   sr   


)
zlog_db_metrics.<locals>.wrapperr   )r   r-   r+   r*   r,   log_db_metrics   s   ?r.   r   returnc                 C   s(   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)httpxZprisma.errorsr0   r$   ZConnectErrorZTimeoutException)r   r1   r0   r+   r+   r,   _is_exception_related_to_dbh   s   r2   r   r   r   r   r   c                    s`   ddl m} t| durd S |jj| tj|j|d|| 	 |||j||ddI d H  d S )Nr   r   Tr   r   )errorr   r   r   r   r   r   r   )
r   r   r2   r   Zasync_service_failure_hookr   r    r   r!   r"   )r   r   r   r   r   r   r   r+   r+   r,   r'   s   s"   
r'   )__doc__r   r   	functoolsr   typingr   r   r   Zlitellm._service_loggerr   Z'litellm.litellm_core_utils.core_helpersr	   r
   r.   r&   boolr2   r'   r+   r+   r+   r,   <module>   s0    U