ó
    Ð]j/  ã                  óH  • S SK Jr  S SKrS SKJs  Jr  S SKJ	r	  S SK
Jr  S SKJr  S SKJrJr  S SKJr  S SKJrJrJr  S S	KJrJr  S S
KJr  S SKJrJr  S SKJ r   S SK!J"r"  S SK#J$r$  \	 " S S5      5       r%\	 " S S5      5       r&\	 " S S5      5       r'SS jr( " S S\ 5      r)g)é    )ÚannotationsN)Ú	dataclass)Úcached_property)ÚLock)ÚCallableÚLiteral)Úcontext)ÚReadableSpanÚSpanÚSpanProcessor)ÚSamplerÚTraceIdRatioBased)ÚSelf)ÚONE_SECOND_IN_NANOSECONDSÚ	LevelName)ÚWrapperSpanProcessor)Úsuppress_instrumentation)Ú	SpanLevelc                  óL   • \ rS rSr% SrS\S'   S\S'   S\S'   \SS	 j5       rS
rg)ÚTraceBufferé   zÅArguments of `SpanProcessor.on_start` and `SpanProcessor.on_end` for spans in a single trace.

These are stored until either the trace is included by tail sampling or it's completed and discarded.
z)list[tuple[Span, context.Context | None]]Ústartedzlist[ReadableSpan]Úendedr   Ú
first_spanc                ó¬  • U R                   R                  nS o!ULo3(       d¥  [        R                  " SU4SX45      S[        R
                  " 5       ;   d  [        R                  " U5      (       a  [        R                  " U5      OS[        R                  " U5      S.-  nSSU0-  n[        [        R                  " U5      5      eS =p2UR                  $ )N)úis not)z%(py0)s is not %(py3)sÚspan_context)Úpy0Úpy3zassert %(py5)sÚpy5)r   r	   Ú
@pytest_arÚ_call_reprcompareÚ@py_builtinsÚlocalsÚ_should_repr_global_nameÚ	_safereprÚAssertionErrorÚ_format_explanationÚtrace_id)Úselfr   Ú@py_assert2Ú@py_assert1Ú@py_format4Ú@py_format6s         Ú\/home/mande/repo/quber/.venv/lib/python3.13/site-packages/logfire/sampling/_tail_sampling.pyr)   ÚTraceBuffer.trace_id!   sx   € à—‘×.Ñ.ˆØ#'Ð' 4Ð'×'×'×'Ò'ˆ|×'×'×'×'×'Õ'ˆ|×'×'×'Ñ'ˆ|×'×'Õ' 4×'×'×'×'×'×'Ñ'Ø×$Ñ$Ð$ó    © N)ÚreturnÚint)	Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ú__annotations__r   r)   Ú__static_attributes__r2   r1   r/   r   r      s-   ‡ ñð
 7Ó6ØÓØÓàó%ó ó%r1   r   c                  óp   • \ rS rSr% SrS\S'    S\S'    S\S'    S	\S
'   \SS j5       r\SS j5       rSr	g)ÚTailSamplingSpanInfoé(   z`Argument passed to the [`SamplingOptions.tail`][logfire.sampling.SamplingOptions.tail] callback.r
   Úspanúcontext.Context | Noner	   zLiteral['start', 'end']Úeventr   Úbufferc                óB   • [         R                  " U R                  5      $ )zThe log level of the span.)r   Ú	from_spanr?   ©r*   s    r/   ÚlevelÚTailSamplingSpanInfo.level>   s   € ô ×"Ò" 4§9¡9Ó-Ð-r1   c                óî   • U R                   R                  =(       d    U R                   R                  =(       d    SU R                  R                  R                  =(       d    [        S5      -
  [        -  $ )zRThe time in seconds between the start of the trace and the start/end of this span.r   Úinf)r?   Úend_timeÚ
start_timerB   r   Úfloatr   rE   s    r/   ÚdurationÚTailSamplingSpanInfo.durationC   sS   € ð
 �Y‰Y×Ñ×< 4§9¡9×#7Ñ#7×<¸1ÀÇÁ×AWÑAW×AbÑAb×ArÔfkÐlqÓfrÑsÜ%ñ&ð 	&r1   r2   N)r3   r   )r3   rL   )
r5   r6   r7   r8   r9   r:   ÚpropertyrF   rM   r;   r2   r1   r/   r=   r=   (   sX   ‡ ájà
Óðð $Ó#ðð #Ó"ðð
 Óàó.ó ð.ð ó&ó ó&r1   r=   c                  ón   • \ rS rSr% SrSrS\S'    SrS\S'    \SS	S
SS.         SS jj5       r	Sr
g)ÚSamplingOptionséL   z¦Options for [`logfire.configure(sampling=...)`][logfire.configure(sampling)].

See the [sampling guide](https://logfire.pydantic.dev/docs/guides/advanced/sampling/).
ç      ð?úfloat | SamplerÚheadNz.Callable[[TailSamplingSpanInfo], float] | NoneÚtailÚnoticeg      @ç        )rU   Úlevel_thresholdÚduration_thresholdÚbackground_ratec               ó¨   ^^^• [        U[        [        45      (       a  UOSnSTs=::  a  Us=::  a  S::  d  O  [        S5      eSUUU4S jjnU " XS9$ )aÃ  Returns a `SamplingOptions` instance that tail samples traces based on their log level and duration.

If a trace has at least one span/log that has a log level greater than or equal to `level_threshold`,
or if the duration of the whole trace is greater than `duration_threshold` seconds,
then the whole trace will be included.
Otherwise, the probability is `background_rate`.

The `head` parameter is the same as in the `SamplingOptions` constructor.
rS   rX   zEInvalid sampling rates, must be 0.0 <= background_rate <= head <= 1.0c                óX   >• Tb  U R                   T:”  a  gTb  U R                  T:¼  a  gT$ )NrS   )rM   rF   )Ú	span_infor[   rZ   rY   s    €€€r/   Úget_tail_sample_rateÚ?SamplingOptions.level_or_duration.<locals>.get_tail_sample_rate�   s4   ø€ Ø!Ñ-°)×2DÑ2DÐGYÓ2YØàÑ*¨y¯©À/Ó/QØà"Ð"r1   )rU   rV   )r^   r=   r3   rL   )Ú
isinstancerL   r4   Ú
ValueError)ÚclsrU   rY   rZ   r[   Úhead_sample_rater_   s     ```  r/   Úlevel_or_durationÚ!SamplingOptions.level_or_durationj   sS   ú€ ô$ $.¨d´U¼C°L×#AÑ#A™4ÀsÐà�ÕAÐ*:ÕA¸cÕAÜÐdÓeÐe÷	#ñ 	#ñ ˜Ñ8Ð8r1   r2   )
rU   rT   rY   zLevelName | NonerZ   zfloat | Noner[   rL   r3   r   )r5   r6   r7   r8   r9   rU   r:   rV   Úclassmethodre   r;   r2   r1   r/   rQ   rQ   L   s   ‡ ñð
  €Dˆ/Óðð <@€DÐ
8Ó?ð	ð ð !$Ø,4Ø+.Ø!$ñ9ð ð9ð *ð	9ð
 )ð9ð ð9ð 
ô9ó ó9r1   rQ   c                óV   • U [         R                  -  [         R                  " U5      :  $ ©N)r   ÚTRACE_ID_LIMITÚget_bound_for_rate)r)   Úrates     r/   Úcheck_trace_id_ratiorm   �   s&   € àÔ(×7Ñ7Ñ7Ô;L×;_Ò;_Ð`dÓ;eÑeÐer1   c                  ó°   ^ • \ rS rSrSr S       SU 4S jjjrSSU 4S jjjrSU 4S jjrSS jrSS jr	SU 4S jjr
SU 4S	 jjrSSU 4S
 jjjrSrU =r$ )ÚTailSamplingProcessoré’   aI  Buffers spans until a span in the trace meets the sampling criteria.

The wrapped `processor` receives `on_start` calls immediately (most processors just set attributes,
which is safe before the sampling decision). Only `on_end` is buffered.

The optional `deferred_processor` has both `on_start` and `on_end` buffered until the sampling
decision. Use this for processors like `PendingSpanProcessor` that create new spans in `on_start`.
Advanced users can mark their own processors for deferral by setting the
`tail_sampling_defer_on_start` attribute to `True` on the processor class.
c                óh   >• [         TU ]  U5        X l        X0l        0 U l        [        5       U l        g ri   )ÚsuperÚ__init__r_   Údeferred_processorÚtracesr   Úlock)r*   Ú	processorr_   rt   Ú	__class__s       €r/   rs   ÚTailSamplingProcessor.__init__ž   s2   ø€ ô 	‰Ñ˜Ô#Ø$8Ô!Ø"4Ôð /1ˆŒô “Fˆ�	r1   c           	     ób  >• SnS nU R                      UR                  (       až  UR                  R                  nUR                  c  [	        / / US9U R
                  U'   U R
                  R                  U5      nUbE  U R                  b  UR                  R                  X45        U R                  [        XSU5      5      nS S S 5        [        TU ]5  X5        Uc*  U R                  b  U R                  R                  X5        g g U(       a  U R                  U5        g g ! , (       d  f       Nd= f)NF)r   r   r   Ústart)rv   r	   r)   Úparentr   ru   Úgetrt   r   ÚappendÚ
check_spanr=   rr   Úon_startÚpush_buffer)r*   r?   Úparent_contextÚdroppedrB   r)   rx   s         €r/   r€   ÚTailSamplingProcessor.on_start±   sÿ   ø€ ØˆØˆà�Y‹Yà�|�|ØŸ<™<×0Ñ0�à—;‘;Ñ&Ü,7ÀÈ"ÐY]Ñ,^�D—K‘K Ñ)àŸ™Ÿ™¨Ó2�ØÑ%ð ×.Ñ.Ñ:ØŸ™×-Ñ-¨tÐ.DÔEØ"Ÿo™oÔ.BÀ4ÐY`ÐbhÓ.iÓj�G÷ ô* 	‰Ñ˜Ô.à‰>ð ×&Ñ&Ñ2Ø×'Ñ'×0Ñ0°ÕFð 3æØ×Ñ˜VÕ$ð ÷9 �Yús   ’B0D Ä 
D.c           	     óP  >• SnS nU R                      UR                  (       a•  UR                  R                  nU R                  R	                  U5      nUba  UR
                  R                  U5        U R                  [        US SU5      5      nUR                  c  U R                  R                  US 5        S S S 5        Uc9  [        TU ]1  U5        U R                  b  U R                  R                  U5        g g U(       a  U R                  U5        g g ! , (       d  f       Nd= f)NFÚend)rv   r	   r)   ru   r}   r   r~   r   r=   r|   Úpoprr   Úon_endrt   r�   )r*   r?   rƒ   rB   r)   rx   s        €r/   rˆ   ÚTailSamplingProcessor.on_endÔ   sì   ø€ ð ˆØˆà�Y‹YØ�|�|ØŸ<™<×0Ñ0�ØŸ™Ÿ™¨Ó2�ØÑ%Ø—L‘L×'Ñ'¨Ô-Ø"Ÿo™oÔ.BÀ4ÈÈuÐV\Ó.]Ó^�GØ—{‘{Ñ*ð Ÿ™Ÿ™¨°$Ô7÷ ð ‰>ô ‰G‰N˜4Ô Ø×&Ñ&Ñ2Ø×'Ñ'×.Ñ.¨tÕ4ð 3æØ×Ñ˜VÕ$ð ÷+ �Yús   ’B'DÄ
D%c                ó¬   • U R                  U5      n[        UR                  R                  U5      =n(       a  U R	                  UR                  5        U$ )z³If the span meets the sampling criteria, drop the buffer and return True. Otherwise, return False.

Must be called within self.lock since it modifies self.traces via drop_buffer.
)r_   rm   rB   r)   Údrop_buffer)r*   r^   Úsample_rateÚsampleds       r/   r   Ú TailSamplingProcessor.check_spanó   sK   € ð
 ×/Ñ/°	Ó:ˆÜ*¨9×+;Ñ+;×+DÑ+DÀkÓRÐRˆ7ÕRØ×Ñ˜Y×-Ñ-Ô.àˆr1   c                ó2   • U R                   UR                  	 g ri   )ru   r)   )r*   rB   s     r/   r‹   Ú!TailSamplingProcessor.drop_bufferþ   s   € Ø�K‰K˜Ÿ™Ñ(r1   c                ó  >• U R                   bZ  UR                   H  nU R                   R                  " U6   M     UR                   H  nU R                   R	                  U5        M      UR                   H  n[
        TU ]  U5        M     g ri   )rt   r   r€   r   rˆ   rr   )r*   rB   r   r?   rx   s       €r/   r�   Ú!TailSamplingProcessor.push_buffer  so   ø€ à×"Ñ"Ñ.Ø!Ÿ>œ>�Ø×'Ñ'×0Ò0°'Ó:ñ *àŸœ�Ø×'Ñ'×.Ñ.¨tÖ4ñ %ð
 —L”LˆDÜ‰G‰N˜4Ö ò !r1   c                óº   >• [         TU ]  5         U R                  b.  [        5          U R                  R                  5         S S S 5        g g ! , (       d  f       g = fri   )rr   Úshutdownrt   r   )r*   rx   s    €r/   r”   ÚTailSamplingProcessor.shutdown  sJ   ø€ Ü‰ÑÔØ×"Ñ"Ñ.ô *Õ+Ø×'Ñ'×0Ñ0Ô2÷ ,Ð+ð	 /÷ ,Õ+ús   §AÁ
Ac                óÖ   >• [         TU ]  U5      nU R                  b9  [        5          U R                  R                  U5      =(       a    UnS S S 5        U$ U$ ! , (       d  f       U$ = fri   )rr   Úforce_flushrt   r   )r*   Útimeout_millisÚresultrx   s      €r/   r—   Ú!TailSamplingProcessor.force_flush  s\   ø€ Ü‘Ñ$ ^Ó4ˆØ×"Ñ"Ñ.ä)Õ+Ø×0Ñ0×<Ñ<¸^ÓL×WÐQW�÷ ,àˆˆvˆ÷ ,Ô+àˆús   ¨%AÁ
A()rt   r_   rv   ru   ri   )rw   r   r_   z'Callable[[TailSamplingSpanInfo], float]rt   zSpanProcessor | Noner3   ÚNone)r?   r   r‚   r@   r3   r›   )r?   r
   r3   r›   )r^   r=   r3   Úbool)rB   r   r3   r›   )r3   r›   )i0u  )r˜   r4   r3   rœ   )r5   r6   r7   r8   r9   rs   r€   rˆ   r   r‹   r�   r”   r—   r;   Ú__classcell__)rx   s   @r/   ro   ro   ’   sl   ø† ñ	ð 48ð	à ðð Fðð 1ð	ð
 
÷ð ÷&!%ñ !%÷F%ô>	ô)÷!÷3÷÷ r1   ro   )r)   r4   rl   rL   r3   rœ   )*Ú
__future__r   Úbuiltinsr#   Ú_pytest.assertion.rewriteÚ	assertionÚrewriter!   Údataclassesr   Ú	functoolsr   Ú	threadingr   Útypingr   r   Úopentelemetryr	   Úopentelemetry.sdk.tracer
   r   r   Ú opentelemetry.sdk.trace.samplingr   r   Útyping_extensionsr   Úlogfire._internal.constantsr   r   Ú#logfire._internal.exporters.wrapperr   Úlogfire._internal.utilsr   Úlogfire.typesr   r   r=   rQ   rm   ro   r2   r1   r/   Ú<module>r¯      s›   ðÝ "ç  „ Õ !Ý %Ý ß $å !ß EÑ Eß GÝ "÷õ EÝ <Ý #ð ÷%ð %ó ð%ð" ÷ &ð  &ó ð &ðF ÷=9ð =9ó ð=9ô@fô
KÐ0õ Kr1   