o
    i                     @   sx   d dl mZ d dlmZ zd dlmZ W n ey#   d dlmZ Y nw ddlmZ G dd de	Z
G d	d
 d
e
ZdS )    )wraps)iscoroutinefunction)	monotonic)time   )_get_wrapped_coc                   @   sJ   e Zd ZdZdddZdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dS )TimedContextManagerDecoratorzx
    A context manager and a decorator which will report the elapsed time in
    the context OR in a function call.
    Nr   c                 C   s0   || _ |j| _|| _|| _|| _|| _d | _d S N)statsdtimingtiming_funcmetrictagssample_rateuse_mselapsedselfr
   r   r   r   r    r   T/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/vendor/dogstatsd/context.py__init__   s   
z%TimedContextManagerDecorator.__init__c                    sD   j sd j jf _ t rt S t  fdd}|S )z
        Decorator which returns the elapsed time of the function call.

        Default to the function name if metric was not provided.
        z%s.%sc               	      s.   t  }z | i |W | S | w r	   )r   _send)argskwargsstartfuncr   r   r   wrapped.   s   z6TimedContextManagerDecorator.__call__.<locals>.wrapped)r   
__module____name__r   r   r   )r   r   r   r   r   r   __call__    s   
z%TimedContextManagerDecorator.__call__c                 C   s   | j stdt | _| S )Nz#Cannot used timed without a metric!)r   	TypeErrorr   _startr   r   r   r   	__enter__8   s   z&TimedContextManagerDecorator.__enter__c                 C   s   |  | j d S r	   )r   r"   )r   typevalue	tracebackr   r   r   __exit__>   s   z%TimedContextManagerDecorator.__exit__c                 C   sZ   t  | }| jd ur| jn| jj}|rttd| n|}| | j|| j| j || _	d S )Ni  )
r   r   r
   introundr   r   r   r   r   )r   r   r   r   r   r   r   r   B   s
   

z"TimedContextManagerDecorator._sendc                 C   s   |    d S r	   )r$   r#   r   r   r   r   I   s   z"TimedContextManagerDecorator.startc                 C   s   |  d d d  d S r	   )r(   r#   r   r   r   stopL   s   z!TimedContextManagerDecorator.stopNNr   N)r   r   __qualname____doc__r   r    r$   r(   r   r   r+   r   r   r   r   r      s    
	r   c                       s"   e Zd ZdZd fdd	Z  ZS )"DistributedContextManagerDecoratorz
    A context manager and a decorator which will report the elapsed time in
    the context OR in a function call using the custom distribution metric.
    Nr   c                    s$   t t| ||||| |j| _d S r	   )superr/   r   distributionr   r   	__class__r   r   r   V   s   z+DistributedContextManagerDecorator.__init__r,   )r   r   r-   r.   r   __classcell__r   r   r2   r   r/   P   s    r/   N)	functoolsr   inspectr   r   r   ImportErrorcontext_asyncr   objectr   r/   r   r   r   r   <module>   s   ?