o
    iu                     @   sn   d dl mZ d dlmZmZ d dlmZ d dlZd dlZ	 G dd de	Z
dae
Zdd Zd	d
 Zdd ZdS )    )ThreadStats)LockThread)apiNc                   @   sL   e Zd ZdZdZe Ze ZdZdd Z	e
dd Ze
dd	 Zd
d ZdS )_LambdaDecoratorzY DEPRECATED Decorator to automatically init & flush metrics, created for Lambda functionsr   Fc                 C   s
   || _ d S )N)func)selfr    r	   R/home/ubuntu/.local/lib/python3.10/site-packages/datadog/threadstats/aws_lambda.py__init__   s   
z_LambdaDecorator.__init__c                 C   s   | j 5 | js*d| _tjdtjdt_tjddt_tt	d}|
  t  | jd | _W d    d S 1 s;w   Y  d S )NTDATADOG_API_KEY
DD_API_KEYDATADOG_HOSTzhttps://api.datadoghq.com)target   )_counter_lock_was_initializedosenvirongetr   _api_key	_api_hostr   _init_api_clientstart_get_lambda_stats_counter)clstr	   r	   r
   _enter"   s   
"z_LambdaDecorator._enterc              	   C   s   d}| j  | jd | _| jdkrd}W d    n1 sw   Y  |rf| j5 | j  | jdkr3d}W d    n1 s=w   Y  |rTt td W d    d S W d    d S 1 s_w   Y  d S d S )NFr   r   Tinf)r   r   _flush_lockr   flushfloat)r   should_flushr	   r	   r
   _close4   s(   

"z_LambdaDecorator._closec                 O   s:   t dt t  z| j|i |W t  S t  w )Nz=datadog_lambda_wrapper() is relocated to https://git.io/fjy8o)warningswarnDeprecationWarningr   r   r   r$   )r   argskwr	   r	   r
   __call__G   s
   z_LambdaDecorator.__call__N)__name__
__module____qualname____doc__r   r   r   r    r   r   classmethodr   r$   r*   r	   r	   r	   r
   r      s    

r   c                   C   s    t d u rt a t jddd t S )NF)flush_in_greenletflush_in_thread)_lambda_statsr   r   r	   r	   r	   r
   r   T   s   r   c                  O   s   t  j| i | dS )z8 Alias to expose only distributions for lambda functionsN)r   distribution)r(   r)   r	   r	   r
   lambda_metric]   s   r4   c                   C   s,   zt jjdd W dS  ty   Y dS w )a  No-op GET to initialize the requests connection with DD's endpoints

    The goal here is to make the final flush faster:
    we keep alive the Requests session, this means that we can re-use the connection
    The consequence is that the HTTP Handshake, which can take hundreds of ms,
    is now made at the beginning of a lambda instead of at the end.

    By making the initial request async, we spare a lot of execution time in the lambdas.
    GETvalidateN)r   
api_client	APIClientsubmit	Exceptionr	   r	   r	   r
   r   b   s
   
r   )datadog.threadstatsr   	threadingr   r   datadogr   r   r%   objectr   r2   datadog_lambda_wrapperr   r4   r   r	   r	   r	   r
   <module>   s   :	