
    uYf(                         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rd d	l mZ d d
l mZ d dl mZ d dlmZ  e
e      ZdZ G d de      Zy)    )TYPE_CHECKING)pattern_type)MAX_UINT_64BITS)GlobMatcher)
get_logger)cachedmethod)DDTraceDeprecationWarning)	deprecate)Any)Optional)Tuple)Spanl   qWV[{c                       e Zd ZdZ e       ZeeeefdZed        Zej                  d        Zd Z
 e       d        Zd Zd Zd	 Zd
 Zd Zd Zd ZeZd Zy)SamplingRulezl
    Definition of a sampling rule used by :class:`DatadogSampler` for applying a sample rate on a span
    c           
         d|cxk  rdk  sn t        dj                  |            t        |      | _        |t        j
                  k7  r6|j                         D ci c]  \  }}|t        t        |             c}}ni | _	        || _
        | j                  |      | _        | j                  |      | _        | j                  |      | _        yc c}}w )a  
        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` greater than or equal to 0.0 and less than or equal to 1.0
        :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      ?z_SamplingRule(sample_rate={}) must be greater than or equal to 0.0 and less than or equal to 1.0N)
ValueErrorformatfloatsample_rater   NO_RULEitemsr   str_tag_value_matcherstagschoose_matcherservicenameresource)selfr   r   r   r   r   kvs           U/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/ddtrace/sampling_rule.py__init__zSamplingRule.__init__   s    P k(S(u&% 
 !- BFI]I]A]

=1QCF##=ce 	  	**73''-	++H5 >s   Cc                     | j                   S N)_sample_rater   s    r"   r   zSamplingRule.sample_rateW   s            c                 .    || _         |t        z  | _        y r%   )r&   _MAX_UINT_64BITS_sampling_id_threshold)r   r   s     r"   r   zSamplingRule.sample_rate\   s     (&14D&D#r(   c                    || j                   u ryt        |t              r|j                  t	        |            S t        |      r	 t         ||            S t        |t              r$	 t        |j                  t	        |                  S ||k(  S # t        $ r t        j                  d| ||d       Y yw xY w# t        t        f$ r t        j                  d| ||d       Y yw xY w)NTz%r pattern %r failed with %r)exc_infoF)r   
isinstancer   matchr   callablebool	Exceptionlogwarningr   r   	TypeError)r   proppatterns      r"   _pattern_matcheszSamplingRule._pattern_matchesb   s     dll"g{+==T++ GGDM** g|,GMM#d)455 w  :D'4Z^_ 	* :D'4Z^_s$   B ("B9 #B65B69)C%$C%c                     |\  }}}|| j                   f|| j                  f|| j                  ffD ]  \  }}| j                  ||      r y y)NFT)r   r   r   r8   )r   keyr   r   r   r6   r7   s          r"   _matcheszSamplingRule._matches   s`     #&x&5dii7H8UYUbUbJcd 	MD'((w7	 r(   c                     | j                  |      }|xr2 | j                  |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   spanr=   s      r"   matcheszSamplingRule.matches   s9     __T*
UdmmT\\499dmm,TUUr(   c                 ~    d}| j                   r.| j                  |j                         |j                               }|S )NT)r   
check_tagsget_tagsget_metrics)r   r>   	tag_matchs      r"   r=   zSamplingRule.tags_match   s5    	##9I9I9KLIr(   c                    ||yd}| j                   j                         D ]  }|j                  |      }| j                   |   j                  t	        |            }|du r|j                  |      }t        |t              r=|j                         s"| j                   |   j                  dk(  rd}n yt        |      }| j                   |   j                  t	        |            }n|du s y |S )NF*T)
r   keysgetr/   r   r.   r   
is_integerr7   int)r   metametricsrD   tag_keyvalues         r"   rA   zSamplingRule.check_tags   s    <GO	//446 	GHHW%E009??E
KIE!G, eU+ ++-33G<DDK(,I#(  #E
 44W=CCCJO	E!1	4 r(   c                     | j                   dk(  ry| j                   dk(  ry|j                  t        z  t        z  | 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_64bitsKNUTH_FACTORr*   r+   )r   r>   s     r"   samplezSamplingRule.sample   sH     q "&&59IIdNiNiiir(   c                 f    || j                   u ry|yt        |      t        k(  r|j                  S |S )Nr   None)r   typer   r7   )r   vals     r"   _no_rule_or_selfzSamplingRule._no_rule_or_self   s4    $,,[#Y+%;;Jr(   c                     t        |      st        |t              rt        dddt               |S |d}y |t
        j                  k7  rt        |      S t
        j                  S )NzNUsing methods or regular expressions for SamplingRule matching is deprecated. z5Please move to passing in a string for Glob matching.z3.0.0)messageremoval_versioncategoryrU   )r0   r.   r   r
   r	   r   r   r   )r   r6   s     r"   r   zSamplingRule.choose_matcher   s^     D>Zl;`O '2	 K \D(,0D0D(D;t$^,J^J^^r(   c           
      2   dj                  | j                  j                  | j                  | j	                  | j
                        | j	                  | j                        | j	                  | j                        | j	                  | j                              S )NzG{}(sample_rate={!r}, service={!r}, name={!r}, resource={!r}, tags={!r}))	r   	__class____name__r   rX   r   r   r   r   r'   s    r"   __repr__zSamplingRule.__repr__   ss    X__NN##!!$,,/!!$)),!!$--0!!$)),
 	
r(   c                     t        |t              s#t        dj                  t	        |                  t        |       t        |      k(  S )Nz!Cannot compare SamplingRule to {})r.   r   r5   r   rV   r   )r   others     r"   __eq__zSamplingRule.__eq__   s:    %.?FFtE{STT4yCJ&&r(   N)r_   
__module____qualname____doc__objectr   r#   propertyr   setterr8   r   r;   r?   r=   rA   rS   rX   r   r`   __str__rc    r(   r"   r   r      s     hG
 76r ! ! E E
> ^ VBj _&
 G'r(   r   N)typingr   ddtrace.internal.compatr   ddtrace.internal.constantsr   r*   ddtrace.internal.glob_matchingr   ddtrace.internal.loggerr   ddtrace.internal.utils.cacher   #ddtrace.internal.utils.deprecationsr	   ddtrace.vendor.debtcollectorr
   r   r   r   ddtrace._trace.spanr   r_   r3   rR   rg   r   rk   r(   r"   <module>ru      sK      0 J 6 . 5 I 2 ("g'6 g'r(   