o
    پi                  	   @   sz   d Z ddlZddlZddlmZ ddlmZmZmZ ddl	m
Z
 dadd Zda	dd	ed
ee dedef fddZdS )z'
Records the latency of some functions
    N)wraps)AnyCallableOptional)exponential_bucketsFc                  C   s0   ddl m}  da| ddtdddd	d
gdad S )Nr   	HistogramTzsglang:func_latency_secondszFunction latency in secondsg?g      ?   )startwidthlengthname)buckets
labelnames)prometheus_clientr   enable_metricsr   FUNC_LATENCYr    r   Q/home/ubuntu/.local/lib/python3.10/site-packages/sglang/srt/metrics/func_timer.pyenable_func_timer   s   
r   funcr   return.c                    s6   dt dtf dt dtf f fdd}| r|| S |S )a  
    A decorator to observe the latency of a function's execution. Supports both sync and async functions.

    NOTE: We use our own implementation of a timer decorator since prometheus_client does not support async
    context manager yet.

    Overhead: The overhead introduced here in case of an async function could likely be because of `await` introduced
    which will return in another coroutine object creation and under heavy load could see longer wall time
    (scheduling delays due to introduction of another awaitable).
    r   .r   c                    sH   p j t  fdd}t  fdd}t r"|S |S )Nc               
      s   t s | i |I d H S t}t } | i |}t|tjs%t|rIz|I d H }W |jd	t |  |S |jd	t |  w |S Nr   )
r   r   time	monotonic
isinstanceasyncioFutureiscoroutinelabelsobserveargskwargsmetricr
   retr   r   r   r   async_wrapperB   s   z9time_func_latency.<locals>.measure.<locals>.async_wrapperc               
      sj   t s	 | i |S t}t }z | i |}W |jdt |  |S |jdt |  w r   )r   r   r   r   r    r!   r"   r'   r   r   sync_wrapperQ   s   z8time_func_latency.<locals>.measure.<locals>.sync_wrapper)__name__r   r   iscoroutinefunction)r   r(   r)   r   )r   r   measure=   s   

z"time_func_latency.<locals>.measure)r   r   )r   r   r,   r   r   r   time_func_latency/   s   &%r-   )NN)__doc__r   r   	functoolsr   typingr   r   r   sglang.srt.metrics.utilsr   r   r   r   strr-   r   r   r   r   <module>   s"   
