o
    i                     @   s&   d dl mZ d dlZG dd dZdS )    )LockNc                   @   s.   e Zd Zdd Zdd Zd
ddZdd	 ZdS )MaxSampleMetricContextsc                 C   s   t  | _i | _|| _d S N)r   lockvaluesmax_sample_metric_type)selfr    r	   _/home/ubuntu/.local/lib/python3.10/site-packages/datadog/dogstatsd/max_sample_metric_context.py__init__   s   
z MaxSampleMetricContexts.__init__c                 C   s\   g }	 | j  | j}i | _W d    n1 sw   Y  | D ]\}}||  q |S r   )r   r   itemsappendflush)r   metricstemp_metricr	   r	   r
   r      s   zMaxSampleMetricContexts.flushNc           
      C   s   |  |}| j# || jvr| j||||d| j|< | j| }	|	j  W d   n1 s.w   Y  |r;|	| n|	  |	j  dS )z6Sample a metric and store it if it meets the criteria.)cardinalityN)should_sampler   r   r   acquiremaybe_keep_sample_work_unsafeskip_samplerelease)
r   namevaluetagsratecontext_keymax_samples_per_contextr   keeping_sampler   r	   r	   r
   sample   s   


zMaxSampleMetricContexts.samplec                 C   s   |dkrdS t   |k S )zADetermine if a sample should be kept based on the specified rate.   T)random)r   r   r	   r	   r
   r   &   s   z%MaxSampleMetricContexts.should_sampler   )__name__
__module____qualname__r   r   r    r   r	   r	   r	   r
   r      s
    

r   )	threadingr   r"   r   r	   r	   r	   r
   <module>   s    