o
    i}                     @   s   d dl mZ d dlmZmZmZmZmZmZ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 d dlmZ erDd dlmZ G dd	 d	eZG d
d deZdS )    )wraps)AnyCallableListOptionalTextTYPE_CHECKINGUnion)	monotonic)time)_get_wrapped_co)iscoroutinefunction)	DogStatsdc                   @   sR   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.
    N   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   M/home/ubuntu/.local/lib/python3.10/site-packages/datadog/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__E   s   z&TimedContextManagerDecorator.__enter__c                 C   s   |  | j d S r   )r   r*   )r   typevalue	tracebackr   r   r   __exit__K   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   O   s
   

z"TimedContextManagerDecorator._sendc                 C   s   |    d S r   )r,   r+   r   r   r   r"   Y   s   z"TimedContextManagerDecorator.startc                 C   s   |  d d d  d S r   )r0   r+   r   r   r   stop\   s   z!TimedContextManagerDecorator.stopNNr   N)r'   r&   __qualname____doc__r   r(   r,   r0   r   r"   r3   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   )superr7   r   distributionr   r   	__class__r   r   r   f   s   z+DistributedContextManagerDecorator.__init__r4   )r'   r&   r5   r6   r   __classcell__r   r   r:   r   r7   `   s    r7   N)	functoolsr   typingr   r   r   r   r   r   r	   r   r
   ImportErrordatadog.dogstatsd.context_asyncr   datadog.util.compatr   datadog.dogstatsd.baser   objectr   r7   r   r   r   r   <module>   s   $K