o
    aiy                  
   @   s  d dl Z d dlZd dlmZmZmZ d dlmZ d dlmZ d dl	Z	d dl
mZmZmZmZ d dlmZ d dlmZmZmZmZmZmZ erd dlmZmZmZ d d	lmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z% d d
l&m'Z'm(Z( e!dZ)e#dZ*d dl+m,Z,m-Z-m.Z.m/Z/ d dlm0Z0 d dl1m2Z2 G dd de'ddZ3G dd de3ddZ4e'dde5iZ6dZ7dZ8G dd de5eZ9e9j:gZ;e9j<e9j<e9j<e9j=e9j:e9j<e9j=e9j=e9j=d	Z>de?de5fdd Z@G d!d" d"ZAG d#d$ d$ZBG d%d& d&eBZCG d'd( d(eBZDere%	d;dddejEd)		+	,	-	,	.	/	0	1		2d<d3d4ZFe%d=d6d4ZF	d;dddejEd)	7	+	,	-	,	.	/	0	1		8d>d9d4ZFd d:lGmHZHmIZImJZJmKZKmLZLmMZM dS )?    N)datetime	timedeltatimezone)Enum)TYPE_CHECKING)INSTRUMENTERSPANDATA
SPANSTATUSSPANTEMPLATE)get_profiler_id)capture_internal_exceptionsget_current_thread_metais_valid_sample_rateloggernanosecond_timeshould_be_treated_as_error)CallableMappingMutableMapping)
AnyDictIteratorListOptional	ParamSpecTupleTypeVarUnionoverload)	TypedDictUnpackPR)EventMeasurementUnitMeasurementValueSamplingContext)ContinuousProfile)Profilec                   @   s   e Zd ZU eed< 	 eed< 	 eed< 	 eed< 	 eed< 	 eed< 	 eed< 	 ded	< 	 eed
< 	 ded< 	 ded< 	 ded< 	 eed< 	 eed< dS )
SpanKwargstrace_idspan_idparent_span_idsame_process_as_parentsampledopdescriptionOptional[sentry_sdk.Hub]hubstatusOptional[Transaction]containing_transaction Optional[Union[datetime, float]]start_timestampzsentry_sdk.ScopescopeoriginnameN__name__
__module____qualname__str__annotations__bool rB   rB   P/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/sentry_sdk/tracing.pyr)   0   s:   
 r)   F)totalc                   @   s*   e Zd ZU eed< 	 eed< 	 ded< dS )TransactionKwargssourceparent_sampledBaggagebaggageNr;   rB   rB   rB   rC   rE   k   s   
 rE   ProfileContextprofiler_idrI   zsentry-tracec                   @   s2   e Zd ZdZdZdZdZdZdZde	fdd	Z
d
S )TransactionSource	componentcustomroutetaskurlviewreturnc                 C      | j S N)valueselfrB   rB   rC   __str__   s   zTransactionSource.__str__N)r<   r=   r>   	COMPONENTCUSTOMROUTETASKURLVIEWr?   rY   rB   rB   rB   rC   rL      s    rL   )	endpointfunction_namehandler_namemethod_and_path_patternpath
route_nameroute_patternuri_templaterQ   http_status_coderS   c                 C   s   | dk rt jS d|   krdk r@n n-| dkrt jS | dkr!t jS | dkr(t jS | dkr/t jS | dkr6t jS | dkr=t jS t jS d|   krJd	k rfn t jS | d
krUt j	S | dkr\t j
S | dkrct jS t jS t jS )z
    Returns the Sentry status corresponding to the given HTTP status code.

    See: https://develop.sentry.dev/sdk/event-payloads/contexts/#trace-context
    i  i  i  i  i  i  i  i  iX  i  i  i  )r	   OKPERMISSION_DENIED	NOT_FOUNDRESOURCE_EXHAUSTEDFAILED_PRECONDITIONUNAUTHENTICATEDALREADY_EXISTSINVALID_ARGUMENTDEADLINE_EXCEEDEDUNIMPLEMENTEDUNAVAILABLEINTERNAL_ERRORUNKNOWN_ERROR)rh   rB   rB   rC   get_span_status_from_http_code   s4   
rv   c                   @   s0   e Zd ZdZdZdeddfddZdd
dZdS )_SpanRecorderz5Limits the number of spans recorded in a transaction.maxlenspansdropped_spansry   rS   Nc                 C   s   |d | _ g | _d| _d S )N   r   rx   rX   ry   rB   rB   rC   __init__   s   

z_SpanRecorder.__init__spanSpanc                 C   s8   t | j| jkrd |_|  jd7  _d S | j| d S )Nr|   )lenrz   ry   _span_recorderr{   append)rX   r   rB   rB   rC   add   s   z_SpanRecorder.add)r   r   rS   N)r<   r=   r>   __doc__	__slots__intr~   r   rB   rB   rB   rC   rw      s
    
rw   c                   @   s  e Zd ZdZdZ														dzddddd	dd
edddddddddddddddddeddddfddZ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fd$d%Zejd"eddfd&d%Zdefd'd(Zd{d)d*Z	+	,	"	,	-	,		d|d.d/Ze
d}d0d1Zejfd2ed3d4dd fd5d6Ze	7	8	3	4		9d~d:d;Zedd<	8	>		3	4		9dd?d@ZddBdCZe	D		3	4		ddEdFZdefdGdHZddJdKZdLed"d4ddfdMdNZdLed"d4ddfdOdPZddSdTZdUedVeddfdWdXZd"eddfdYdZZ 	[dded"e!d\d]ddfd^d_Z"	`	a	b			ddcddZ#ddfdgZ$dheddfdidjZ%defdkdlZ&		d		m	n		ddodpZ'ddqdrZ(ddsdtZ)ddvdwZ*ddxdyZ+eZ,eZ-eZ.e)Z/dS )r   ak  A span holds timing information of a block of code.
    Spans can have multiple child spans thus forming a span tree.

    :param trace_id: The trace ID of the root span. If this new span is to be the root span,
        omit this parameter, and a new trace ID will be generated.
    :param span_id: The span ID of this span. If omitted, a new span ID will be generated.
    :param parent_span_id: The span ID of the parent span, if applicable.
    :param same_process_as_parent: Whether this span is in the same process as the parent span.
    :param sampled: Whether the span should be sampled. Overrides the default sampling decision
        for this span when provided.
    :param op: The span's operation. A list of recommended values is available here:
        https://develop.sentry.dev/sdk/performance/span-operations/
    :param description: A description of what operation is being performed within the span.

        .. deprecated:: 2.15.0
            Please use the `name` parameter, instead.
    :param name: A string describing what operation is being performed within the span.
    :param hub: The hub to use for this span.

        .. deprecated:: 2.0.0
            Please use the `scope` parameter, instead.
    :param status: The span's status. Possible values are listed at
        https://develop.sentry.dev/sdk/event-payloads/span/
    :param containing_transaction: The transaction that this span belongs to.
    :param start_timestamp: The timestamp when the span started. If omitted, the current time
        will be used.
    :param scope: The scope to use for this span. If not provided, we use the current scope.
    )	_trace_id_span_idr,   r-   r.   r/   r0   _measurementsr7   _start_timestamp_monotonic_nsr3   	timestamp_tags_datar   r2   _context_manager_state_containing_transactionr8   r9   r:   _flags_flags_capacityNTmanualr*   Optional[str]r+   r,   r-   r.   Optional[bool]r/   r0   r2   r1   r3   r5   r4   r7   r6   r8   Optional[sentry_sdk.Scope]r9   r:   rS   c                 C   s  || _ || _|| _|| _|| _|| _|p|| _|	| _|| _|| _	|| _
i | _i | _i | _|
| _i | _d| _|d urHtjdtdd | j	pF|j	| _	|d u rSttj}nt|tr_t|tj}|| _zt | _W n	 tyq   Y nw d | _d | _ | !  | "t#  d S )N
   z>The `hub` parameter is deprecated. Please use `scope` instead.   
stacklevel)$r   r   r,   r-   r.   r/   r0   r3   r2   r8   r9   r   r   r   r   r   r   warningswarnDeprecationWarningr   nowr   utc
isinstancefloatfromtimestampr7   r   r   AttributeErrorr   r   update_active_threadset_profiler_idr   )rX   r*   r+   r,   r-   r.   r/   r0   r2   r3   r5   r7   r8   r9   r:   rB   rB   rC   r~     sL   

zSpan.__init__ry   c                 C   s   | j d u rt|| _ d S d S rU   )r   rw   r}   rB   rB   rC   init_span_recorder[  s   
zSpan.init_span_recorderc                 C   s   | j s	t j| _ | j S rU   )r   uuiduuid4hexrW   rB   rB   rC   r*   _  s   zSpan.trace_idrV   c                 C   
   || _ d S rU   )r   rX   rV   rB   rB   rC   r*   f     
c                 C   s    | j st jdd  | _ | j S )N   )r   r   r   r   rW   rB   rB   rC   r+   j  s   zSpan.span_idc                 C   r   rU   )r   r   rB   rB   rC   r+   q  r   c              	   C   s*   d| j j| j| j| j| j| j| j| jf S )Nz^<%s(op=%r, description:%r, trace_id=%r, span_id=%r, parent_span_id=%r, sampled=%r, origin=%r)>)		__class__r<   r/   r0   r*   r+   r,   r.   r9   rW   rB   rB   rC   __repr__u  s   zSpan.__repr__c                 C   s(   | j pt }|j}| |_||f| _| S rU   )r8   
sentry_sdkget_current_scoper   r   )rX   r8   old_spanrB   rB   rC   	__enter__  s
   
zSpan.__enter__tyOptional[Any]tbc                 C   sh   |d urt ||r| tj t  | j\}}| `| | ||_W d    d S 1 s-w   Y  d S rU   )r   
set_statusr	   rt   r   r   finishr   )rX   r   rV   r   r8   r   rB   rB   rC   __exit__  s   

"zSpan.__exit__c                 C   rT   )zThe ``Transaction`` that this span belongs to.
        The ``Transaction`` is the root of the span tree,
        so one could also think of this ``Transaction`` as the "root span".)r   rW   rB   rB   rC   r5     s   	zSpan.containing_transactioninstrumenterkwargsr   c                 K   s   | ddurtjdtdd t jd }||krt S |d| j	 t
d	| j| j| jd|}| jo8| jj}|r@|| |S )
a  
        Start a sub-span from the current span or transaction.

        Takes the same arguments as the initializer of :py:class:`Span`. The
        trace id, sampling decision, transaction pointer, and span recorder are
        inherited from the current span/transaction.

        The instrumenter parameter is deprecated for user code, and it will
        be removed in the next major version. Going forward, it should only
        be used by the SDK itself.
        r0   NzEThe `description` parameter is deprecated. Please use `name` instead.r   r   r   r.   )r*   r,   r5   rB   )getr   r   r   r   
get_clientoptionsNoOpSpan
setdefaultr.   r   r*   r+   r5   r   r   )rX   r   r   configuration_instrumenterchildspan_recorderrB   rB   rC   start_child  s,   
zSpan.start_childenvironMapping[str, str]Transactionc                 K   s   t jt|fi |S )ag  
        DEPRECATED: Use :py:meth:`sentry_sdk.continue_trace`.

        Create a Transaction with the given params, then add in data pulled from
        the ``sentry-trace`` and ``baggage`` headers from the environ (if any)
        before returning the Transaction.

        This is different from :py:meth:`~sentry_sdk.tracing.Span.continue_from_headers`
        in that it assumes header names in the form ``HTTP_HEADER_NAME`` -
        such as you would get from a WSGI/ASGI environ -
        rather than the form ``header-name``.

        :param environ: The ASGI/WSGI environ to pull information from.
        )r   continue_from_headersEnvironHeaders)clsr   r   rB   rB   rC   continue_from_environ  s   zSpan.continue_from_environ_sample_randheadersr   c                K   sl   t d tj|t|d}|t|i t|t}|dur*|| |	  t
di |}d|_|S )a  
        DEPRECATED: Use :py:meth:`sentry_sdk.continue_trace`.

        Create a transaction with the given params (including any data pulled from
        the ``sentry-trace`` and ``baggage`` headers).

        :param headers: The dictionary with the HTTP headers to pull information from.
        :param _sample_rand: If provided, we override the sample_rand value from the
            incoming headers with this value. (internal use only)
        z2Deprecated: use sentry_sdk.continue_trace instead.r   NFrB   )r   warningrH   from_incoming_headerr   BAGGAGE_HEADER_NAMEupdateextract_sentrytrace_dataSENTRY_TRACE_HEADER_NAMEfreezer   r-   )r   r   r   r   rI   sentrytrace_kwargstransactionrB   rB   rC   r     s   


zSpan.continue_from_headersIterator[Tuple[str, str]]c                 c   s>    | j sdS t|  fV  | j   }|rt|fV  dS dS )z
        Creates a generator which returns the span's ``sentry-trace`` and ``baggage`` headers.
        If the span's containing transaction doesn't yet have a ``baggage`` value,
        this will cause one to be generated and stored.
        N)r5   r   to_traceparentget_baggage	serializer   )rX   rI   rB   rB   rC   iter_headers  s   zSpan.iter_headerstraceparentc                 K   s   |sdS | j t|ifi |S )z
        DEPRECATED: Use :py:meth:`sentry_sdk.continue_trace`.

        Create a ``Transaction`` with the given params, then add in data pulled from
        the given ``sentry-trace`` header value before returning the ``Transaction``.
        N)r   r   )r   r   r   rB   rB   rC   from_traceparent"  s   zSpan.from_traceparentc                 C   sN   | j du rd}n
| j du rd}nd }d| j| jf }|d ur%|d|f 7 }|S )NT1F0z%s-%sz-%s)r.   r*   r+   )rX   r.   r   rB   rB   rC   r   5  s   

zSpan.to_traceparentOptional[Baggage]c                 C   s   | j r| j  S dS )zReturns the :py:class:`~sentry_sdk.tracing_utils.Baggage`
        associated with this ``Span``, if any. (Taken from the root of the span tree.)
        N)r5   r   rW   rB   rB   rC   
to_baggageC  s   
zSpan.to_baggagekeyc                 C      || j |< d S rU   )r   rX   r   rV   rB   rB   rC   set_tagK     zSpan.set_tagc                 C   r   rU   )r   r   rB   rB   rC   set_dataN  r   zSpan.set_datadataDict[str, Any]c                 C   s   | j | d S rU   )r   r   rX   r   rB   rB   rC   update_dataQ  s   zSpan.update_dataflagresultc                 C   s"   t | j| jk r|| j|< d S d S rU   )r   r   r   )rX   r   r   rB   rB   rC   set_flagT  s   zSpan.set_flagc                 C   r   rU   r3   r   rB   rB   rC   r   X     
zSpan.set_status unitr$   c                 C   $   t jdtdd ||d| j|< dS z
        .. deprecated:: 2.28.0
            This function is deprecated and will be removed in the next major release.
        zq`set_measurement()` is deprecated and will be removed in the next major version. Please use `set_data()` instead.r   r   )rV   r   Nr   r   r   r   rX   r:   rV   r   rB   rB   rC   set_measurement[     zSpan.set_measurement	thread_idOptional[int]thread_namec                 C   s<   |d ur|  tjt| |d ur|  tj| d S d S d S rU   )r   r   	THREAD_IDr?   THREAD_NAMErX   r   r   rB   rB   rC   
set_threadj  s   zSpan.set_threadrK   c                 C   s   |d ur|  tj| d S d S rU   )r   r   PROFILER_IDrX   rK   rB   rB   rC   r   s  s   zSpan.set_profiler_idhttp_statusc                 C   s0   |  dt| | tj| | t| d S )Nzhttp.status_code)r   r?   r   r   HTTP_STATUS_CODEr   rv   rX   r  rB   rB   rC   set_http_statusw  s
   zSpan.set_http_statusc                 C   s
   | j dkS )Nokr   rW   rB   rB   rC   
is_success~  r   zSpan.is_successend_timestamp Optional[Union[float, datetime]]c                 C   s   | j durdS z%|rt|trt|tj}|| _ nt | j }| j	t
|d d | _ W n ty<   ttj| _ Y nw |pBt }| }|rjtj| jv }| jduo_| jdp_| jd}|sd|rj| d| t||  dS )aD  
        Sets the end timestamp of the span.

        Additionally it also creates a breadcrumb from the span,
        if the span represents a database or HTTP request.

        :param scope: The scope to use for this transaction.
            If not provided, the current scope will be used.
        :param end_timestamp: Optional timestamp that should
            be used as timestamp instead of the current time.

        :return: Always ``None``. The type is ``Optional[str]`` to match
            the return value of :py:meth:`sentry_sdk.tracing.Transaction.finish`.
        Ni  )microsecondszai.zgen_ai.zgen_ai.conversation.id)r   r   r   r   r   r   r   r   r   r7   r   r   r   r   r   get_conversation_idr   GEN_AI_OPERATION_NAMEr   r/   
startswithr   "maybe_create_breadcrumbs_from_span)rX   r8   r	  elapsedconversation_id	has_ai_opis_ai_span_oprB   rB   rC   r     s4   




zSpan.finishc              
   C   s   | j | j| j| j| j| j| j| j| jd	}| j	r#| j	|d< | j	| j
d< t| jdkr/| j|d< | j
}|r8||d< i }|| j || j |rL||d< |S )z5Returns a JSON-compatible representation of the span.)	r*   r+   r,   r-   r/   r0   r7   r   r9   r3   r   measurementstagsr   )r*   r+   r,   r-   r/   r0   r7   r   r9   r3   r   r   r   r   r   r   )rX   rvr  r   rB   rB   rC   to_json  s0   

zSpan.to_jsonc                 C   s   | j | j| j| j| j| jd}| jr| j|d< | jr#| j 	 |d< i }| j
tj}|d ur4||d< | j
tj}|d urC||d< |rI||d< |S )N)r*   r+   r,   r/   r0   r9   r3   dynamic_sampling_contextz	thread.idzthread.namer   )r*   r+   r,   r/   r0   r9   r3   r5   r   r  r   r   r   r   r   )rX   r  r   r   r   rB   rB   rC   get_trace_context  s,   
zSpan.get_trace_contextOptional[ProfileContext]c                 C   s"   | j tj}|d u rd S d|iS )NrK   )r   r   r   r  r  rB   rB   rC   get_profile_context  s
   zSpan.get_profile_contextc                 C   s   t  \}}| || d S rU   )r   r   r   rB   rB   rC   r     s   
zSpan.update_active_thread)NNNTNNNNNNNNr   N)rS   r   r   r   rV   r   r   r   rS   NrS   r4   )r   r   r   r   rS   r   )r   r   r   r   r   r   rS   r   rS   r   )r   r   r   r   rS   r4   rS   r   r   r   rS   Nr   )r   r   r   r   rS   N)rK   r   rS   NNN)r8   r   r	  r
  rS   r   rS   r   rS   r   )rS   r  )rS   N)0r<   r=   r>   r   r   rA   r?   r~   r   r   propertyr*   setterr+   r   r   r   r5   r   SENTRYr   classmethodr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r   r  r  r  r   _to_traceparent_to_baggage_iter_headers_get_trace_contextrB   rB   rB   rC   r      s,   	

B


+
+




	

4
#
 
	r   c                       sv  e Zd ZdZdZdddejfdedddd	d
eddddf fddZdefddZ	de
fddZdG fddZ								dH fddZedGddZ			 			!dId"d#Zdefd$d%Z		dJdd&	!	(	)	*	+		,dK fd-d.Z	dLdeded/d0ddfd1d2Zd3edd4ddfd5d6Zd7eddf fd8d9ZdM fd;d<ZdN fd>d?ZdOdAdBZ	C	D		dPdEdFZeZeZ  ZS )Qr   ai  The Transaction is the root element that holds all the spans
    for Sentry performance instrumentation.

    :param name: Identifier of the transaction.
        Will show up in the Sentry UI.
    :param parent_sampled: Whether the parent transaction was sampled.
        If True this transaction will be kept, if False it will be discarded.
    :param baggage: The W3C baggage header value.
        (see https://www.w3.org/TR/baggage/)
    :param source: A string describing the source of the transaction name.
        This will be used to determine the transaction's type.
        See https://develop.sentry.dev/sdk/event-payloads/transaction/#transaction-annotations
        for more information. Default "custom".
    :param kwargs: Additional arguments to be passed to the Span constructor.
        See :py:class:`sentry_sdk.tracing.Span` for available arguments.
    )
r:   rF   rG   sample_rater   	_contexts_profile_continuous_profile_baggager   r   Nr:   rG   r   rI   r   rF   r   zUnpack[SpanKwargs]rS   c                    s   t  jdi | || _|| _d | _|| _i | _i | _d | _d | _	|| _
| j
d u r+d n| j
 }|d ur9|| _d S t| j| _d S NrB   )superr~   r:   rF   r-  rG   r   r.  r/  r0  r1  r   _generate_sample_randr*   )rX   r:   rG   rI   rF   r   baggage_sample_randr   rB   rC   r~   ,  s   
zTransaction.__init__c              
   C   s.   d| j j| j| j| j| j| j| j| j| j	f	 S )Nzb<%s(name=%r, op=%r, trace_id=%r, span_id=%r, parent_span_id=%r, sampled=%r, source=%r, origin=%r)>)
r   r<   r:   r/   r*   r+   r,   r.   rF   r9   rW   rB   rB   rC   r   H  s   zTransaction.__repr__c                 C   s   | j dup	| jdu S )zReturns whether the transaction might have been started.

        If this returns False, we know that the transaction was not started
        with sentry_sdk.start_transaction, and therefore the transaction will
        be discarded.
        NF)r   r.   rW   rB   rB   rC   _possibly_startedX  s   	zTransaction._possibly_startedc                    s4   |   s	td t   | jd ur| j  | S )NzTransaction was entered without being started with sentry_sdk.start_transaction.The transaction will not be sent to Sentry. To fix, start the transaction bypassing it to sentry_sdk.start_transaction.)r7  r   debugr3  r   r/  rW   r6  rB   rC   r   c  s   


zTransaction.__enter__r   r   rV   r   c                    sB   | j d ur| j ||| | jd ur| j  t ||| d S rU   )r/  r   r0  stopr3  )rX   r   rV   r   r6  rB   rC   r   r  s
   


zTransaction.__exit__c                 C   s   | S )znThe root element of the span tree.
        In the case of a transaction it is the transaction itself.
        rB   rW   rB   rB   rC   r5   }  s   	z"Transaction.containing_transaction	scope_arg1Optional[Union[sentry_sdk.Scope, sentry_sdk.Hub]]hub_argr   c                 C   sF   |}|durt jdtdd |}t|tjr!t jdtdd |jS |S )z
        Logic to get the scope from the arguments passed to finish. This
        function exists for backwards compatibility with the old finish.

        TODO: Remove this function in the next major version.
        NzMThe `hub` parameter is deprecated. Please use the `scope` parameter, instead.   r   zDPassing a Hub to finish is deprecated. Please pass a Scope, instead.)r   r   r   r   r   Hubr8   )rX   r:  r<  scope_or_hubrB   rB   rC   _get_scope_from_finish_args  s    z'Transaction._get_scope_from_finish_argsc                 C   s$   dj | jrd| j d nd| jdS )Nz{op}transaction <{name}><z> r   )r/   r:   )formatr/   r:   rW   rB   rB   rC   _get_log_representation  s   z#Transaction._get_log_representationr2   r8   r	  r
  r2   r1   r   c             	      sp  | j durdS | ||}|p| jpt }t }| s dS | jdu r]| jdu r0t	
d nt	
d |jr[t|jr[|jrI|jjdkrId}nd}|jj|dd	 |jj|d
d	 dS | jsht	d d| _t || | jtj}|dur||jd v rt	
dj|  | jtj |jd d |jr|jjddd	 t| jjd }|jjdd
|d d| _| js| jdu rt	d dS dd | jjD }t| jjt| }	|	| jj }
d| _i }|| j |d|   i | ! }|dur|d|i d| jd| j"i|| j#| j | j$|d}|
dkr|
|d< | j%dur.| j%& r.| j%|d< d| _%| j'|d< |(|S )au  Finishes the transaction and sends it to Sentry.
        All finished spans in the transaction will also be sent to Sentry.

        :param scope: The Scope to use for this transaction.
            If not provided, the current Scope will be used.
        :param end_timestamp: Optional timestamp that should
            be used as timestamp instead of the current time.
        :param hub: The hub to use for this transaction.
            This argument is DEPRECATED. Please use the `scope`
            parameter, instead.

        :return: The event ID if the transaction was sent to Sentry,
            otherwise None.
        NFz.Discarding transaction because sampled = FalsezSDiscarding transaction because it was not started with sentry_sdk.start_transactionr   backpressurer-  r   )data_categoryr   zCTransaction has no name, falling back to `<unlabeled transaction>`.z<unlabeled transaction>trace_ignore_status_codesz[Tracing] Discarding {transaction_description} because the HTTP status code {status_code} is matched by trace_ignore_status_codes: {trace_ignore_status_codes})transaction_descriptionstatus_coderG  event_processorr|   )rF  quantityz1Discarding transaction without sampling decision.c                 S   s   g | ]}|j d ur| qS rU   )r   r  ).0r   rB   rB   rC   
<listcomp>  s
    
z&Transaction.finish.<locals>.<listcomp>traceprofilerF   )typer   transaction_infocontextsr  r   r7   rz   _dropped_spansr  ))r   r@  r8   r   r   r   	is_activer   r.   r   r8  	transporthas_tracing_enabledr   monitordownsample_factorrecord_lost_eventr:   r   r3  r   r   r   r   r  rB  rC  r   rz   r{   r   r.  r  r  rF   r   r7   r/  validr   capture_event)rX   r8   r	  r2   clientreasonrI  	num_spansfinished_spanslen_diffr{   rR  profile_contexteventr6  rB   rC   r     s   



	





zTransaction.finishr   r$   c                 C   r   r   r   r   rB   rB   rC   r   A  r   zTransaction.set_measurementr   dict[str, Any]c                 C   s   || j |< dS )a  Sets a context. Transactions can have multiple contexts
        and they should follow the format described in the "Contexts Interface"
        documentation.

        :param key: The name of the context.
        :param value: The information about the context.
        N)r.  r   rB   rB   rC   set_contextP  s   zTransaction.set_contextr  c                    s    t  | | dd|i dS )zySets the status of the Transaction according to the given HTTP status.

        :param http_status: The HTTP status code.responserI  N)r3  r  rd  r  r6  rB   rC   r  Z  s   zTransaction.set_http_statusr   c                    s,   t   }| j|d< | j|d< | j|d< |S )z<Returns a JSON-compatible representation of the transaction.r:   rF   r.   )r3  r  r:   rF   r.   )rX   r  r6  rB   rC   r  a  s
   



zTransaction.to_jsonr   c                    s   t   }| jr| j|d< |S )Nr   )r3  r  r   )rX   trace_contextr6  rB   rC   r  k  s   

zTransaction.get_trace_contextrH   c                 C   s    | j r| j jrt| | _ | j S )zReturns the :py:class:`~sentry_sdk.tracing_utils.Baggage`
        associated with the Transaction.

        The first time a new baggage with Sentry items is made,
        it will be frozen.)r1  mutablerH   populate_from_transactionrW   rB   rB   rC   r   s  s   zTransaction.get_baggagesampling_contextr&   c                 C   sD  t  }|  }t|jsd| _dS | jdurt| j| _dS t|j	dr.|jd |n|d dur8|d n|jd }t
|ddsQtdj|d	 d| _dS t|| _|jrd|  jd
|jj   _| jstdj|t|j	drvdndd d| _dS | j| jk | _| jrtdj|d	 dS tdj|| jd dS )aO  
        Sets the transaction's sampling decision, according to the following
        precedence rules:

        1. If a sampling decision is passed to `start_transaction`
        (`start_transaction(name: "my transaction", sampled: True)`), that
        decision will be used, regardless of anything else

        2. If `traces_sampler` is defined, its decision will be used. It can
        choose to keep or ignore any parent sampling decision, or use the
        sampling context data to make its own decision or to choose a sample
        rate for the transaction.

        3. If `traces_sampler` is not defined, but there's a parent sampling
        decision, the parent sampling decision will be used.

        4. If `traces_sampler` is not defined and there's no parent sampling
        decision, `traces_sample_rate` will be used.
        FNtraces_samplerrG   traces_sample_rateTracing)rF   zN[Tracing] Discarding {transaction_description} because of invalid sample rate.)rH  r   z?[Tracing] Discarding {transaction_description} because {reason}z"traces_sampler returned 0 or Falseztraces_sample_rate is set to 0)rH  r]  z,[Tracing] Starting {transaction_description}z}[Tracing] Discarding {transaction_description} because it's not included in the random sample (sampling rate = {sample_rate}))rH  r-  )r   r   rC  rV  r   r.   r   r-  callabler   r   r   r   rB  rW  rX  r8  r   )rX   ri  r\  rH  r-  rB   rB   rC   _set_initial_sampling_decision~  sb   



z*Transaction._set_initial_sampling_decision)rS   r   r  )r:  r;  r<  r;  rS   r   r"  r8   r   r	  r
  r2   r1   rS   r   r!  r#  r$  )rS   rH   ri  r&   rS   N)r<   r=   r>   r   r   rL   r[   r?   r~   r   rA   r7  r   r   r%  r5   r@  rC  r   r   r   rd  r   r  r  r  r   rn  _get_baggager,  __classcell__rB   rB   r6  rC   r     s    

  




fr   c                	   @   s  e Zd ZdefddZedHddZejfdedd	dd fd
dZ	defddZ
dIddZdIddZdJddZdedd	ddfddZdedd	ddfddZdKdd Zdeddfd!d"Zd#eddfd$d%Zdefd&d'ZdLd(d)ZdMd*d+ZdMd,d-Z		dNdd.	0	1	2	3	4		5dOd6d7Z	8dPd9eded:d;ddfd<d=Zdedd>ddfd?d@ZdAeddfdBdCZ	D	E		dQdFdGZe
ZeZ eZ!eZ"eZ#dS )Rr   rS   c                 C   s   d| j j S )Nz<%s>)r   r<   rW   rB   rB   rC   r     s   zNoOpSpan.__repr__r4   c                 C      d S rU   rB   rW   rB   rB   rC   r5     s   zNoOpSpan.containing_transactionr   r   r   c                 K   s   t  S rU   )r   )rX   r   r   rB   rB   rC   r     s   zNoOpSpan.start_childc                 C      dS )Nr   rB   rW   rB   rB   rC   r        zNoOpSpan.to_traceparentr   c                 C   rs  rU   rB   rW   rB   rB   rC   r     ru  zNoOpSpan.to_baggagec                 C   rs  rU   rB   rW   rB   rB   rC   r     ru  zNoOpSpan.get_baggager   c                 C   s   t dS r2  )iterrW   rB   rB   rC   r     s   zNoOpSpan.iter_headersr   rV   Nc                 C   rs  rU   rB   r   rB   rB   rC   r     ru  zNoOpSpan.set_tagc                 C   rs  rU   rB   r   rB   rB   rC   r     ru  zNoOpSpan.set_datar   r   c                 C   rs  rU   rB   r   rB   rB   rC   r     ru  zNoOpSpan.update_datac                 C   rs  rU   rB   r   rB   rB   rC   r   
  ru  zNoOpSpan.set_statusr  c                 C   rs  rU   rB   r  rB   rB   rC   r    ru  zNoOpSpan.set_http_statusc                 C   rt  )NTrB   rW   rB   rB   rC   r    ru  zNoOpSpan.is_successc                 C      i S rU   rB   rW   rB   rB   rC   r    ru  zNoOpSpan.to_jsonc                 C   rw  rU   rB   rW   rB   rB   rC   r    ru  zNoOpSpan.get_trace_contextc                 C   rw  rU   rB   rW   rB   rB   rC   r    ru  zNoOpSpan.get_profile_contextrD  r8   r   r	  r
  r2   r1   r   c                C   rt  )z_
        The `hub` parameter is deprecated. Please use the `scope` parameter, instead.
        NrB   )rX   r8   r	  r2   rB   rB   rC   r        
zNoOpSpan.finishr   r:   r   r$   c                 C   rs  rU   rB   r   rB   rB   rC   r   (     zNoOpSpan.set_measurementrc  c                 C   rs  rU   rB   r   rB   rB   rC   rd  -  ru  zNoOpSpan.set_contextry   c                 C   rs  rU   rB   r}   rB   rB   rC   r   0  ru  zNoOpSpan.init_span_recorderri  r&   c                 C   rs  rU   rB   )rX   ri  rB   rB   rC   rn  3  ry  z'NoOpSpan._set_initial_sampling_decisionr  r  r  r   r#  r$  r"  ro  r!  rp  )$r<   r=   r>   r?   r   r%  r5   r   r'  r   r   r   r   r   r   r   r   r   r   r  rA   r  r  r  r  r   r   r   rd  r   rn  r)  r*  rq  r+  r,  rB   rB   rB   rC   r     sx    










r   r/   r:   
attributestemplatefuncr/   r   r:   r{  Optional[dict[str, Any]]r|  r
   *Callable[[Callable[P, R]], Callable[P, R]]c                C   rs  rU   rB   )r}  r/   r:   r{  r|  rB   rB   rC   rN  B  rx  rN  Callable[P, R]c                 C   rs  rU   rB   )r}  rB   rB   rC   rN  N  ry  Optional[Callable[P, R]]AUnion[Callable[P, R], Callable[[Callable[P, R]], Callable[P, R]]]c                C   s,   ddl m} |||||d}| r|| S |S )a
  
    Decorator to start a child span around a function call.

    This decorator automatically creates a new span when the decorated function
    is called, and finishes the span when the function returns or raises an exception.

    :param func: The function to trace. When used as a decorator without parentheses,
        this is the function being decorated. When used with parameters (e.g.,
        ``@trace(op="custom")``, this should be None.
    :type func: Callable or None

    :param op: The operation name for the span. This is a high-level description
        of what the span represents (e.g., "http.client", "db.query").
        You can use predefined constants from :py:class:`sentry_sdk.consts.OP`
        or provide your own string. If not provided, a default operation will
        be assigned based on the template.
    :type op: str or None

    :param name: The human-readable name/description for the span. If not provided,
        defaults to the function name. This provides more specific details about
        what the span represents (e.g., "GET /api/users", "process_user_data").
    :type name: str or None

    :param attributes: A dictionary of key-value pairs to add as attributes to the span.
        Attribute values must be strings, integers, floats, or booleans. These
        attributes provide additional context about the span's execution.
    :type attributes: dict[str, Any] or None

    :param template: The type of span to create. This determines what kind of
        span instrumentation and data collection will be applied. Use predefined
        constants from :py:class:`sentry_sdk.consts.SPANTEMPLATE`.
        The default is `SPANTEMPLATE.DEFAULT` which is the right choice for most
        use cases.
    :type template: :py:class:`sentry_sdk.consts.SPANTEMPLATE`

    :returns: When used as ``@trace``, returns the decorated function. When used as
        ``@trace(...)`` with parameters, returns a decorator function.
    :rtype: Callable or decorator function

    Example::

        import sentry_sdk
        from sentry_sdk.consts import OP, SPANTEMPLATE

        # Simple usage with default values
        @sentry_sdk.trace
        def process_data():
            # Function implementation
            pass

        # With custom parameters
        @sentry_sdk.trace(
            op=OP.DB_QUERY,
            name="Get user data",
            attributes={"postgres": True}
        )
        def make_db_query(sql):
            # Function implementation
            pass

        # With a custom template
        @sentry_sdk.trace(template=SPANTEMPLATE.AI_TOOL)
        def calculate_interest_rate(amount, rate, years):
            # Function implementation
            pass
    r   )create_span_decoratorrz  )sentry_sdk.tracing_utilsr  )r}  r/   r:   r{  r|  r  	decoratorrB   rB   rC   rN  T  s   J)rH   r   r4  r   rV  r  rU   )r}  Nr/   r   r:   r   r{  r~  r|  r
   rS   r  )r}  r  rS   r  )r}  r  r/   r   r:   r   r{  r~  r|  r
   rS   r  )Nr   r   r   r   r   enumr   typingr   r   sentry_sdk.constsr   r   r	   r
   'sentry_sdk.profiler.continuous_profilerr   sentry_sdk.utilsr   r   r   r   r   r   collections.abcr   r   r   r   r   r   r   r   r   r   r   r   r   typing_extensionsr   r    r!   r"   sentry_sdk._typesr#   r$   r%   r&   r'   (sentry_sdk.profiler.transaction_profilerr(   r)   rE   r?   rJ   r   r   rL   r^   LOW_QUALITY_TRANSACTION_SOURCESrZ   r\   SOURCE_FOR_STYLEr   rv   rw   r   r   r   DEFAULTrN  r  rH   r   r4  r   rV  r  rB   rB   rB   rC   <module>   s     	0;&    /   _X
$[