o
    Á¿iõ  ã                   @   sˆ   d dl mZ d dl mZ d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d d	lmZ eeƒZG d
d„ deƒZdS )é    )ÚAny)ÚOptional)ÚSpan)ÚMAX_UINT_64BITS)ÚSAMPLING_HASH_MODULO)ÚSAMPLING_KNUTH_FACTOR)ÚGlobMatcher)Ú
get_logger)Úcachedmethodc                   @   sô   e Zd ZdZ					ddedee dee dee deeeef  d	ed
dfdd„Z	e
d
efdd„ƒZejded
dfdd„ƒZeƒ deee eee f d
efdd„ƒZded
efdd„Zded
efdd„Zdd„ Zdd„ Zded
efdd„ZdS ) ÚSamplingRulezl
    Definition of a sampling rule used by :class:`DatadogSampler` for applying a sample rate on a span
    NÚdefaultÚsample_rateÚserviceÚnameÚresourceÚtagsÚ
provenanceÚreturnc                 C   sz   t dtdt|ƒƒƒ| _|rdd„ | ¡ D ƒni | _|dur!t|ƒnd| _|dur,t|ƒnd| _|r5t|ƒnd| _	|| _
dS )an  
        Configure a new :class:`SamplingRule`

        .. code:: python

            DatadogSampler([
                # Sample 100% of any trace
                SamplingRule(sample_rate=1.0),

                # Sample no healthcheck traces
                SamplingRule(sample_rate=0, name='flask.request'),

                # Sample all services ending in `-db` based on a regular expression
                SamplingRule(sample_rate=0.5, service=re.compile('-db$')),

                # Sample based on service name using custom function
                SamplingRule(sample_rate=0.75, service=lambda service: 'my-app' in service),
            ])

        :param sample_rate: The sample rate to apply to any matching spans
        :type sample_rate: :obj:`float` clamped between 0.0 and 1.0 inclusive
        :param service: Rule to match the `span.service` on, default no rule defined
        :type service: :obj:`object` to directly compare, :obj:`function` to evaluate, or :class:`re.Pattern` to match
        :param name: Rule to match the `span.name` on, default no rule defined
        :type name: :obj:`object` to directly compare, :obj:`function` to evaluate, or :class:`re.Pattern` to match
        :param tags: A dictionary whose keys exactly match the names of tags expected to appear on spans, and whose
            values are glob-matches with the expected span tag values. Glob matching supports "*" meaning any
            number of characters, and "?" meaning any one character. If all tags specified in a SamplingRule are
            matches with a given span, that span is considered to have matching tags with the rule.
        g      ð?g        c                 S   s   i | ]\}}|t t|ƒƒ“qS © )r   Ústr)Ú.0ÚkÚvr   r   úP/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/_trace/sampling_rule.pyÚ
<dictcomp>?   s    z)SamplingRule.__init__.<locals>.<dictcomp>N)ÚminÚmaxÚfloatr   Úitemsr   r   r   r   r   r   )Úselfr   r   r   r   r   r   r   r   r   Ú__init__   s   '
zSamplingRule.__init__c                 C   s   | j S ©N)Ú_sample_rate©r   r   r   r   r   E   s   zSamplingRule.sample_ratec                 C   s   || _ |t | _d S r!   )r"   r   Ú_sampling_id_threshold)r   r   r   r   r   r   I   s   Úkeyc                 C   sP   |\}}}|| j f|| jf|| jffD ]\}}|d ur%| t|ƒ¡s% dS qdS )NFT)r   r   r   Úmatchr   )r   r%   r   r   r   ÚpropÚpatternr   r   r   Ú
name_matchN   s   
$€zSamplingRule.name_matchÚspanc                 C   s    |   |¡o|  |j|j|jf¡S )zì
        Return if this span matches this rule

        :param span: The span to match against
        :type span: :class:`ddtrace._trace.span.Span`
        :returns: Whether this span matches or not
        :rtype: :obj:`bool`
        )Ú
tags_matchr)   r   r   r   ©r   r*   r   r   r   ÚmatchesW   s    	zSamplingRule.matchesc                 C   sª   | j sdS |jp	i }|jpi }|s|sdS | j  ¡ D ]8\}}| || |¡¡}|d u r. dS t|tƒrH| ¡ r<t|ƒ}nt	|j
ƒddh rH dS | t|ƒ¡sR dS qdS )NTFú?Ú*)r   Ú_metaÚ_metricsr   ÚgetÚ
isinstancer   Ú
is_integerÚintÚsetr(   r&   r   )r   r*   ÚmetaÚmetricsÚtag_keyr(   Úvaluer   r   r   r+   b   s&   



ÿzSamplingRule.tags_matchc                 C   s0   | j dkrdS | j dkrdS |jt t | jkS )zó
        Return if this rule chooses to sample the span

        :param span: The span to sample against
        :type span: :class:`ddtrace._trace.span.Span`
        :returns: Whether this span was sampled
        :rtype: :obj:`bool`
        é   Tr   F)r   Ú_trace_id_64bitsr   r   r$   r,   r   r   r   Úsample   s
   
	
zSamplingRule.samplec                 C   s6   d| j › d| j› d| j› d| j› d| j› d| j› dS )NzSamplingRule(sample_rate=z
, service=z, name=z, resource=z, tags=z, provenance=ú))r   r   r   r   r   r   r#   r   r   r   Ú__repr__‘   s   ÿÿÿÿÿzSamplingRule.__repr__Úotherc                 C   sV   t |tƒsdS | j|jko*| j|jko*| j|jko*| j|jko*| j|jko*| j|jkS )NF)r3   r   r   r   r   r   r   r   )r   r@   r   r   r   Ú__eq__—   s   

ÿ
þ
ý
ü
úzSamplingRule.__eq__)NNNNr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   Údictr   r    Úpropertyr   Úsetterr
   ÚtupleÚboolr)   r   r-   r+   r=   r?   rA   r   r   r   r   r      sB    ùþýüûúù
ø0&r   N)Útypingr   r   Úddtrace._trace.spanr   Úddtrace.internal.constantsr   r   r   Úddtrace.internal.glob_matchingr   Úddtrace.internal.loggerr	   Úddtrace.internal.utils.cacher
   rB   ÚlogÚobjectr   r   r   r   r   Ú<module>   s    