
    \jy              
          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rwd 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e'd      Z3 G d de3d      Z4 e'dde5i      Z6dZ7dZ8 G d de5e      Z9e9jt                  gZ;e9jx                  e9jx                  e9jx                  e9jz                  e9jt                  e9jx                  e9jz                  e9jz                  e9jz                  d	Z>de?de5fdZ@ G d  d!      ZA G d" d#      ZB G d$ d%eB      ZC G d& d'eB      ZDer/e%	 d-dddej                  d(	 	 	 	 	 	 	 	 	 	 	 d.d)       ZFe%d/d*       ZF	 d-dddej                  d(	 	 	 	 	 	 	 	 	 	 	 d0d+ZFd d,lGmHZHmIZImJZJmKZKmLZLmMZM y)1    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                       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<   y)
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     </root/env/lib/python3.12/site-packages/sentry_sdk/tracing.pyr+   r+   0   s    	
 T< $$J	
 	
  	Y''sr 778;;	
 "!X	 	`rE   r+   F)totalc                   0    e Zd ZU eed<   	 eed<   	 ded<   y)TransactionKwargssourceparent_sampledBaggagebaggageNr=   rD   rE   rF   rI   rI   k   s     	 PrE   rI   ProfileContextprofiler_idrM   zsentry-tracec                   0    e Zd ZdZdZdZdZdZdZde	fdZ
y	)
TransactionSource	componentcustomroutetaskurlviewreturnc                     | j                   S N)valueselfs    rF   __str__zTransactionSource.__str__   s    zzrE   N)r>   r?   r@   	COMPONENTCUSTOMROUTETASKURLVIEWrA   r^   rD   rE   rF   rQ   rQ      s,    IFED
CD rE   rQ   )	endpointfunction_namehandler_namemethod_and_path_patternpath
route_nameroute_patternuri_templaterV   http_status_coderX   c                 \   | dk  rt         j                  S d| cxk  rdk  rn 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(  rt         j                  S | dk(  rt         j                  S t         j                  S d| cxk  rd	k  ran t         j                  S | d
k(  rt         j                  S | dk(  rt         j                  S | dk(  rt         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)rm   s    rF   get_span_status_from_http_coder|      s    #}}	 	&3	&s"///$'''$000$111$---$,,,...	 	&3	& ### s"///$+++$))),,,###rE   c                   ,    e Zd ZdZdZdeddfdZddZy)	_SpanRecorderz5Limits the number of spans recorded in a transaction.maxlenspansdropped_spansr   rX   Nc                 4    |dz
  | _         g | _        d| _        y )N   r   r   r]   r   s     rF   __init__z_SpanRecorder.__init__   s     qj#%
"#rE   c                     t        | j                        | j                  kD  rd |_        | xj                  dz  c_        y | j                  j                  |       y )Nr   )lenr   r   _span_recorderr   append)r]   spans     rF   addz_SpanRecorder.add   sA    tzz?T[[("&D!#JJd#rE   )r   SpanrX   N)r>   r?   r@   __doc__	__slots__intr   r   rD   rE   rF   r~   r~      s"    ?4I$s $t $$rE   r~   c                      e Zd ZdZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 dEd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ZdeddfdZ	e
defd       Zej                  deddfd       Ze
defd       Zej                  deddfd        Zdefd!ZdFd"Z	 	 	 	 	 	 	 	 dGd#Ze
dHd$       Zej&                  fd%ed&d'dd fd(Ze	 	 	 	 	 	 dId)       Zedd*	 	 	 	 	 	 	 dJd+       ZdKd,Ze	 	 	 	 	 	 dLd-       Zdefd.ZdMd/Zd0edd'ddfd1Zd0edd'ddfd2ZdNd3Zd4ed5eddfd6Zdeddfd7Z 	 dOdede!d8d9ddfd:Z"	 	 	 	 	 	 dPd;Z#dQd<Z$d=eddfd>Z%defd?Z&	 	 dR	 	 	 	 	 dSd@Z'dTdAZ(dUdBZ)dVdCZ*dWdDZ+eZ,eZ-eZ.e)Z/y)Xr   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/   r0   r1   r2   _measurementsr9   _start_timestamp_monotonic_nsr5   	timestamp_tags_datar   r4   _context_manager_state_containing_transactionr:   r;   r<   _flags_flags_capacityNr,   Optional[str]r-   r.   r/   r0   Optional[bool]r1   r2   r4   r3   r5   r7   r6   r9   r8   r:   Optional[sentry_sdk.Scope]r;   r<   rX   c                    || _         || _        || _        || _        || _        || _        |xs || _        |	| _        || _        || _	        || _
        i | _        i | _        i | _        |
| _        i | _        d| _        |;t#        j$                  dt&        d       | j                  xs |j                  | _	        |$t)        j*                  t,        j.                        }n4t1        |t2              r$t)        j4                  |t,        j.                        }|| _        	 t9               | _        d | _        d | _         | jC                          | jE                  tG                      y # t<        $ r Y Cw xY w)N
   z>The `hub` parameter is deprecated. Please use `scope` instead.   
stacklevel)$r   r   r.   r/   r0   r1   r2   r5   r4   r:   r;   r   r   r   r   r   r   warningswarnDeprecationWarningr   nowr   utc
isinstancefloatfromtimestampr9   r   r   AttributeErrorr   r   update_active_threadset_profiler_idr   )r]   r,   r-   r.   r/   r0   r1   r2   r4   r5   r7   r9   r:   r;   r<   s                  rF   r   zSpan.__init__  sK   " ",&<#.;
<>13
')
'=$)+!?MMP" 0syyDJ"&ll8<<8O/&44_hllSO.	 2A1BD.
 049=!!#_./  		s   E" "	E.-E.r   c                 >    | j                   t        |      | _         y y rZ   )r   r~   r   s     rF   init_span_recorderzSpan.init_span_recorder[  s     &"/"7D 'rE   c                 x    | j                   s#t        j                         j                  | _         | j                   S rZ   )r   uuiduuid4hexr\   s    rF   r,   zSpan.trace_id_  s&    ~~!ZZ\--DN~~rE   r[   c                     || _         y rZ   )r   r]   r[   s     rF   r,   zSpan.trace_idf  s	    rE   c                 ~    | j                   s&t        j                         j                  dd  | _         | j                   S )N   )r   r   r   r   r\   s    rF   r-   zSpan.span_idj  s-    }} JJL,,RS1DM}}rE   c                     || _         y rZ   )r   r   s     rF   r-   zSpan.span_idq  s	    rE   c                     d| j                   j                  d| j                  d| j                  d| j                  d| j
                  d| j                  d| j                  d| j                  d	S )
N<z(op=z, description:, trace_id=
, span_id=, parent_span_id=
, sampled=	, origin=)>)		__class__r>   r1   r2   r,   r-   r.   r0   r;   r\   s    rF   __repr__zSpan.__repr__u  sP     ''  ##
	
rE   c                     | j                   xs t        j                         }|j                  }| |_        ||f| _        | S rZ   )r:   
sentry_sdkget_current_scoper   r   )r]   r:   old_spans      rF   	__enter__zSpan.__enter__  s;    

<j::<::
',h&7#rE   c                     |+t        ||      r| j                  t        j                         t	               5  | j
                  \  }}| `| j                  |       ||_        d d d        y # 1 sw Y   y xY wrZ   )r   
set_statusr
   rz   r   r   finishr   )r]   tyr[   tbr:   r   s         rF   __exit__zSpan.__exit__  si     !;B!FOOJ556(* 	""99OE8+KK!EJ		" 	" 	"s   *A++A4c                     | j                   S )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   r\   s    rF   r7   zSpan.containing_transaction  s     +++rE   instrumenterkwargsr   c                    |j                  d      t        j                  dt        d       t	        j
                         j                  d   }||k7  r
t               S |j                  d| j                         t        d| j                  | j                  | j                  d|}| j                  xr | j                  j                  }|r|j                  |       |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.
        r2   zEThe `description` parameter is deprecated. Please use `name` instead.r   r   r   r0   )r,   r.   r7   rD   )getr   r   r   r   
get_clientoptionsNoOpSpan
setdefaultr0   r   r,   r-   r7   r   r   )r]   r   r   configuration_instrumenterchildspan_recorders         rF   start_childzSpan.start_child  s     ::m$0MMW" &0%:%:%<%D%D^%T"55:)T\\2 
]]<<#'#>#>
 	
 ''VD,G,G,V,V 	 e$rE   c                 @    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.
        )Transactioncontinue_from_headersEnvironHeaders)clsenvironr   s      rF   continue_from_environzSpan.continue_from_environ  s    ( 001HSFSSrE   _sample_randc                X   t        j                  d       t        j                  |j	                  t
              |      }|j                  t
        |i       t        |j	                  t                    }|!|j                  |       |j                          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   FrD   )r   warningrL   from_incoming_headerr   BAGGAGE_HEADER_NAMEupdateextract_sentrytrace_dataSENTRY_TRACE_HEADER_NAMEfreezer   r/   )r   headersr   r   rM   sentrytrace_kwargstransactions          rF   r   zSpan.continue_from_headers  s    $ 	KL ..KK+,<
 	*G455KK01
 )MM,-
 NN!+F+-2*rE   c              #      K   | j                   syt        | j                         f | j                   j                         j	                         }|rt
        |f yyw)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)r7   r   to_traceparentget_baggage	serializer   )r]   rM   s     rF   iter_headerszSpan.iter_headers  sZ      **
 &(;(;(===--99;EEG%w.. s   AAc                 :    |sy | 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   traceparentr   s      rF   from_traceparentzSpan.from_traceparent"  s/     (s((%{3
7=
 	
rE   c                     | j                   du rd}n| j                   du rd}nd }| j                  d| j                  }||d|z  }|S )NT1F0-)r0   r,   r-   )r]   r0   r   s      rF   r   zSpan.to_traceparent5  sT    <<4G\\U"GG!%=G--KrE   c                 P    | j                   r| j                   j                         S y)zReturns the :py:class:`~sentry_sdk.tracing_utils.Baggage`
        associated with this ``Span``, if any. (Taken from the root of the span tree.)
        N)r7   r   r\   s    rF   
to_baggagezSpan.to_baggageC  s%     &&..::<<rE   keyc                 "    || j                   |<   y rZ   )r   r]   r   r[   s      rF   set_tagzSpan.set_tagK      

3rE   c                 "    || j                   |<   y rZ   )r   r  s      rF   set_datazSpan.set_dataN  r  rE   c                 :    | j                   j                  |       y rZ   )r   r   r]   datas     rF   update_datazSpan.update_dataQ  s    

$rE   flagresultc                 h    t        | j                        | j                  k  r|| j                  |<   y y rZ   )r   r   r   )r]   r
  r  s      rF   set_flagzSpan.set_flagT  s,    t{{d222 &DKK 3rE   c                     || _         y rZ   r5   r   s     rF   r   zSpan.set_statusX  s	    rE   unitr%   c                 `    t        j                  dt        d       ||d| j                  |<   y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   )r[   r  Nr   r   r   r   r]   r<   r[   r  s       rF   set_measurementzSpan.set_measurement[  0     		

 .3D#A4 rE   c                     |M| j                  t        j                  t        |             |!| j                  t        j                  |       y y y rZ   )r  r	   	THREAD_IDrA   THREAD_NAMEr]   	thread_idthread_names      rF   
set_threadzSpan.set_threadj  sF      MM(,,c)n=&h22K@ ' !rE   c                 J    |!| j                  t        j                  |       y y rZ   )r  r	   PROFILER_IDr]   rO   s     rF   r   zSpan.set_profiler_ids  s!    "MM(..< #rE   http_statusc                     | j                  dt        |             | j                  t        j                  |       | j                  t        |             y )Nzhttp.status_code)r  rA   r  r	   HTTP_STATUS_CODEr   r|   r]   r!  s     rF   set_http_statuszSpan.set_http_statusw  s@    K 0	
 	h//=6{CDrE   c                      | j                   dk(  S )Nokr  r\   s    rF   
is_successzSpan.is_success~  s    {{d""rE   c                    | j                   y	 |r<t        |t              r$t        j                  |t
        j                        }|| _         n8t               | j                  z
  }| j                  t        |dz        z   | _         |xs t        j                         }|j                         }|rzt         j"                  | j$                  v }| j&                  duxr8 | j&                  j)                  d      xs | j&                  j)                  d      }|s|r| j+                  d|       t-        ||        y# t        $ r+ t        j                  t
        j                        | _         Y w xY w)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   r9   r   r   r   r   r   get_conversation_idr	   GEN_AI_OPERATION_NAMEr   r1   
startswithr  "maybe_create_breadcrumbs_from_span)r]   r:   end_timestampelapsedconversation_id	has_ai_opis_ai_span_ops          rF   r   zSpan.finish  s3   & >>%	8mU3$,$:$:=(,,$WM!.)+d.P.PP!%!5!5	!(49 " 7557  335 66$**DI GG4/ ""5)JTWW-?-?	-J  M6H*5$7#  	8%\\(,,7DN	8s   A6D6 61E*)E*c           
      ,   | j                   | j                  | j                  | j                  | j                  | j
                  | j                  | j                  | j                  d	}| j                  r(| j                  |d<   | j                  | j                  d<   t        | j                        dkD  r| j                  |d<   | j                  }|r||d<   i }|j                  | j                         |j                  | j                         |r||d<   |S )z5Returns a JSON-compatible representation of the span.)	r,   r-   r.   r/   r1   r2   r9   r   r;   r5   r   measurementstagsr  )r,   r-   r.   r/   r1   r2   r9   r   r;   r5   r   r   r   r   r   r   )r]   rvr6  r  s       rF   to_jsonzSpan.to_json  s     ||"11&*&A&A''++#33kk
 
 ;;;;BxL#';;DJJx t!!"Q&!%!3!3B~zzBvJDKK DJJBvJ	rE   c                    | j                   | j                  | j                  | j                  | j                  | j
                  d}| j                  r| j                  |d<   | j                  r+| j                  j                         j                         |d<   i }| j                  j                  t        j                        }|||d<   | j                  j                  t        j                        }|||d<   |r||d<   |S )N)r,   r-   r.   r1   r2   r;   r5   dynamic_sampling_contextz	thread.idzthread.namer  )r,   r-   r.   r1   r2   r;   r5   r7   r   r:  r   r   r	   r  r  )r]   r7  r  r  r  s        rF   get_trace_contextzSpan.get_trace_context  s    ||"11''++kk 
 ;;;;BxL&&++779RRT )* JJNN8#5#56	  )DjjnnX%9%9:""-DBvJ	rE   c                 b    | j                   j                  t        j                        }|y d|iS )NrO   )r   r   r	   r  r   s     rF   get_profile_contextzSpan.get_profile_context  s4    jjnnX%9%9: ;
 	
rE   c                 B    t               \  }}| j                  ||       y rZ   )r   r  r  s      rF   r   zSpan.update_active_thread  s    !8!:	;	;/rE   )NNNTNNNNNNNNmanualN)rX   r   r   Optional[Any]r[   rA  r   rA  rX   NrX   r6   )r   Mapping[str, str]r   r   rX   r   )r   rC  r   r   r   r   rX   r   rX   zIterator[Tuple[str, str]])r   r   r   r   rX   r6   rX   Optional[Baggage]r  Dict[str, Any]rX   N )r  zOptional[int]r  r   rX   N)rO   r   rX   NNN)r:   r   r/   Optional[Union[float, datetime]]rX   r   rX   rH  rX   r   )rX   zOptional[ProfileContext])rX   N)0r>   r?   r@   r   r   rC   rA   r   r   r   propertyr,   setterr-   r   r   r   r7   r   SENTRYr   classmethodr   r   r   r   r   r   r  r  r	  r  r   r   r  r  r   r%  r(  r   r8  r;  r=  r   _to_traceparent_to_baggage_iter_headers_get_trace_contextrD   rE   rF   r   r      s   :I8 %)#'*.'+$("'+*."&:>>B.2 $>0!>0 !>0 (	>0
 !%>0 ">0 >0 %>0 (>0  >0 !8>0 <>0 ,>0 >0 >0  
!>0D8 8 8 #   __c d      ^^S T  
# 

"!
"*9
"?N
"	
" , , #/"5"5))AF)	)V T$T T 
	T T* 
 )-	)$) &	)
 ) 
) )V/& 
$
 
 
!	
 
$  3  u     C    $   'S '$ '4 '   BDBB %B->B	BA(A7FA	A=E3 E4 E#D #
 /3<@2+2 :2 
	2h!F@
0
 %OK M*rE   r   c                   r    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ZdefdZ	de
fdZd% fdZ	 	 	 	 	 	 	 	 d& fdZed%d       Z	 	 	 	 	 	 d'dZdefdZ	 	 d(dd	 	 	 	 	 	 	 d) fdZ	 d*dededdddfdZdeddddfdZdeddf fd Zd+ fd!Zd, fd"Zd-d#Z	 	 	 	 d.d$ZeZeZ xZS )/r   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<   rJ   rK   sample_rater   	_contexts_profile_continuous_profile_baggager   rJ  Nr<   rK   r   rM   rF  rJ   r   zUnpack[SpanKwargs]rX   c                 :   t        |   di | || _        || _        d | _        || _        i | _        i | _        d | _        d | _	        || _
        | j                  d n| j                  j                         }||| _        y t        | j                        | _        y NrD   )superr   r<   rJ   rX  rK   r   rY  rZ  r[  r\  r   _generate_sample_randr,   )r]   r<   rK   rM   rJ   r   baggage_sample_randr   s          rF   r   zTransaction.__init__,  s     	"6"	.2,<>+--1BF  MM)Dt}}/I/I/K 	 * 3D 5dmm DDrE   c                    d| j                   j                  d| j                  d| j                  d| j                  d| j
                  d| j                  d| j                  d| j                  d	| j                  d
S )Nr   z(name=z, op=r   r   r   r   z	, source=r   r   )
r   r>   r<   r1   r,   r-   r.   r0   rJ   r;   r\   s    rF   r   zTransaction.__repr__H  sU     ''		##	
rE   c                 >    | j                   duxs | 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   r0   r\   s    rF   _possibly_startedzTransaction._possibly_startedX  s#     ""$.G$,,%2GGrE   c                     | j                         st        j                  d       t        |           | j
                  | j
                  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.)rd  r   debugr_  r   rZ  )r]   r   s    rF   r   zTransaction.__enter__c  sL    %%'LL> 	==$MM##%rE   r[   c                     | j                   | j                   j                  |||       | j                  | j                  j                          t        |   |||       y rZ   )rZ  r   r[  stopr_  )r]   r   r[   r   r   s       rF   r   zTransaction.__exit__r  sT     ==$MM""2ub1##/$$))+UB'rE   c                     | S )znThe root element of the span tree.
        In the case of a transaction it is the transaction itself.
        rD   r\   s    rF   r7   z"Transaction.containing_transaction}  s	     rE   c                     |}|t        j                  dt        d       |}t        |t        j
                        r(t        j                  dt        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.
        zMThe `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   Hubr:   )r]   	scope_arghub_argscope_or_hubs       rF   _get_scope_from_finish_argsz'Transaction._get_scope_from_finish_args  sb     !MM_" #LlJNN3MMV"  %%%rE   c                 x    dj                  | j                  rd| j                  z   dz   nd| j                        S )Nz{op}transaction <{name}>r   z> rJ  )r1   r<   )formatr1   r<   r\   s    rF   _get_log_representationz#Transaction._get_log_representation  s8    )00(,dgg$Rtyy 1 
 	
rE   r4   c          	      v   | j                   y| j                  ||      }|xs" | j                  xs t        j                         }t        j
                         }|j                         sy| j                  | j                  du rt        j                  d       nt        j                  d       |j                  ryt        |j                        rd|j                  r|j                  j                  dkD  rd}nd}|j                  j!                  |d	       |j                  j!                  |d
	       y| j"                  st        j$                  d       d| _        t&        | Q  ||       | j*                  j-                  t.        j0                        }|||j                  d   v rt        j                  dj3                  | j5                         | j*                  t.        j0                     |j                  d                |j                  r]|j                  j!                  dd	       t7        | j                  j8                        dz   }|j                  j!                  dd
|       d| _        | j                  s"| j                  t        j$                  d       y| j                  j8                  D cg c]  }|j                   |j;                           }	}t7        | j                  j8                        t7        |	      z
  }
|
| j                  j<                  z   }d| _        i }|j?                  | j@                         |j?                  d| jC                         i       | jE                         }||j?                  d|i       d| j"                  d| jF                  i|| jH                  | j                   | jJ                  |	d}|dkD  r||d<   | jL                  0| jL                  jO                         r| jL                  |d<   d| _&        | jP                  |d<   |jS                  |      S c c}w )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   backpressurerX  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_coderx  event_processorr   )rw  quantityz1Discarding transaction without sampling decision.traceprofilerJ   )typer   transaction_infocontextsr6  r   r9   r   _dropped_spansr5  )*r   rp  r:   r   r   r   	is_activer   r0   r   rf  	transporthas_tracing_enabledr   monitordownsample_factorrecord_lost_eventr<   r   r_  r   r   r   r	   r#  rr  rs  r   r   r8  r   r   rY  r;  r=  rJ   r   r9   rZ  validr   capture_event)r]   r:   r/  r4   clientreasonrz  	num_spansr   finished_spanslen_diffr   r  profile_contexteventr   s                  rF   r   zTransaction.finish  s   * >>% /3.N.N3/
 EEz'C'C'E&&(!&||u$MNi $7$G>>fnn&F&F&J+F*F  2262W   2262PyyNNU 2DIum,jjnnX%>%>?#v~~.IJJLL q  x  x,0,H,H,J $

8+D+D E.4nn3/ x    22%] 3    3 3 9 9:Q>	  22%Vi 3  !DL|| ||#RS ++11
~~) LLN
 
 t**001C4GG 4#6#6#D#DD #'$"8"8":;<224&OOY89 "99!)4;; 7 JJ#33#	
 1&3E"#==$)<)<)>#}}E) DM $ 2 2n""5))U
s   >#P6r  r%   c                 `    t        j                  dt        d       ||d| j                  |<   yr  r  r  s       rF   r  zTransaction.set_measurementA  r  rE   r   dict[str, Any]c                 "    || j                   |<   y)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)rY  r  s      rF   set_contextzTransaction.set_contextP  s     $srE   r!  c                 L    t         |   |       | j                  dd|i       y)zySets the status of the Transaction according to the given HTTP status.

        :param http_status: The HTTP status code.responserz  N)r_  r%  r  )r]   r!  r   s     rF   r%  zTransaction.set_http_statusZ  s'     	,m[%ABrE   c                 ~    t         |          }| j                  |d<   | j                  |d<   | j                  |d<   |S )z<Returns a JSON-compatible representation of the transaction.r<   rJ   r0   )r_  r8  r<   rJ   r0   )r]   r7  r   s     rF   r8  zTransaction.to_jsona  s;    W_YY6
{{89	rE   c                 Z    t         |          }| j                  r| j                  |d<   |S )Nr  )r_  r;  r   )r]   trace_contextr   s     rF   r;  zTransaction.get_trace_contextk  s+    13::$(JJM&!rE   c                     | j                   r| j                   j                  rt        j                  |       | _         | 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.)r\  mutablerL   populate_from_transactionr\   s    rF   r   zTransaction.get_baggages  s3     }} 5 5#==dCDM}}rE   c           
      <   t        j                         }| j                         }t        |j                        sd| _        y| j
                  t        | j
                        | _        yt        |j                  j                  d            r |j                  d   |      n|d   |d   n|j                  d   }t        |d      s-t        j                  dj                  |	             d| _        yt        |      | _        |j                  r,| xj                  d
|j                  j                  z  z  c_        | j                  sTt        j                   dj                  |t        |j                  j                  d            rdnd             d| _        y| j"                  | j                  k  | _        | j
                  r&t        j                   dj                  |	             yt        j                   dj                  || j                               y)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_samplerrK   traces_sample_rateTracing)rJ   zN[Tracing] Discarding {transaction_description} because of invalid sample rate.)ry  r   z?[Tracing] Discarding {transaction_description} because {reason}z"traces_sampler returned 0 or Falseztraces_sample_rate is set to 0)ry  r  z,[Tracing] Starting {transaction_description}z}[Tracing] Discarding {transaction_description} because it's not included in the random sample (sampling rate = {sample_rate}))ry  rX  )r   r   rs  r  r   r0   r   rX  callabler   r   r   r   rr  r  r  rf  r   )r]   sampling_contextr  ry  rX  s        rF   _set_initial_sampling_decisionz*Transaction._set_initial_sampling_decision~  s   , &&("&">">"@ #6>>2 DL <<#$T\\2D **+;<= -FNN+,-=>
 $$45A !!12^^$89 	 $K	BNN`gg,C h 
 !DL ->>6>>#C#C CC LLQXX,C $FNN$6$67G$HI == Y 	 !DL ((4+;+;;<<LL>EE,C F  LL P  W  W,C $ 0 0 W rE   )rX   r   r@  )rm  1Optional[Union[sentry_sdk.Scope, sentry_sdk.Hub]]rn  r  rX   r   rK  r:   r   r/  rL  r4   r3   rX   r   rI  rM  rN  )rX   rL   r  r'   rX   N)r>   r?   r@   r   r   rQ   r`   rA   r   r   rC   rd  r   r   rO  r7   rp  rs  r   r   r  r  r   r%  r8  r;  r   r  _get_baggagerV  __classcell__)r   s   @rF   r   r     s   "I  +/'+'..EE )E %	E
 E 'E 
E8
# 
 	H4 	H	(!	(*9	(?N	(		(  F E 
&	@
 
 /3<@R*
 +/R*+R* :R*
 (R* 
R*j BDBB %B->B	B$s $+; $ $C3 C4 C	c 1c	cL L*rE   r   c            	       t   e Zd ZdefdZed$d       Zej                  fdedddd fdZ	defdZ
d%d	Zd%d
Zd&dZdeddddfdZdeddddfdZd'dZdeddfdZdeddfdZdefdZd(dZd)dZd)dZ	 	 d*dd	 	 	 	 	 	 	 d+dZ	 d,dededdddfdZdeddddfd Zd!eddfd"Z	 	 	 	 d-d#Ze
ZeZ eZ!eZ"eZ#y).r   rX   c                 4    d| j                   j                  z  S )Nz<%s>)r   r>   r\   s    rF   r   zNoOpSpan.__repr__  s    ////rE   c                      y rZ   rD   r\   s    rF   r7   zNoOpSpan.containing_transaction  s    rE   r   r   r   c                     t               S rZ   )r   )r]   r   r   s      rF   r   zNoOpSpan.start_child  s     zrE   c                      y)NrJ  rD   r\   s    rF   r   zNoOpSpan.to_traceparent  s    rE   c                      y rZ   rD   r\   s    rF   r   zNoOpSpan.to_baggage      rE   c                      y rZ   rD   r\   s    rF   r   zNoOpSpan.get_baggage  r  rE   c                     t        d      S r^  )iterr\   s    rF   r   zNoOpSpan.iter_headers  s    BxrE   r   r[   Nc                      y rZ   rD   r  s      rF   r  zNoOpSpan.set_tag      rE   c                      y rZ   rD   r  s      rF   r  zNoOpSpan.set_data  r  rE   c                      y rZ   rD   r  s     rF   r	  zNoOpSpan.update_data  r  rE   c                      y rZ   rD   r   s     rF   r   zNoOpSpan.set_status
  r  rE   r!  c                      y rZ   rD   r$  s     rF   r%  zNoOpSpan.set_http_status  r  rE   c                      y)NTrD   r\   s    rF   r(  zNoOpSpan.is_success  r  rE   c                     i S rZ   rD   r\   s    rF   r8  zNoOpSpan.to_json      	rE   c                     i S rZ   rD   r\   s    rF   r;  zNoOpSpan.get_trace_context  r  rE   c                     i S rZ   rD   r\   s    rF   r=  zNoOpSpan.get_profile_context  r  rE   rt  c                     y)z_
        The `hub` parameter is deprecated. Please use the `scope` parameter, instead.
        NrD   )r]   r:   r/  r4   s       rF   r   zNoOpSpan.finish       	rE   r<   r  r%   c                      y rZ   rD   r  s       rF   r  zNoOpSpan.set_measurement(       	rE   r  c                      y rZ   rD   r  s      rF   r  zNoOpSpan.set_context-  r  rE   r   c                      y rZ   rD   r   s     rF   r   zNoOpSpan.init_span_recorder0  r  rE   c                      y rZ   rD   )r]   r  s     rF   r  z'NoOpSpan._set_initial_sampling_decision3  r  rE   rB  rE  rD  rG  rM  rN  rK  r  rI  r  )$r>   r?   r@   rA   r   rO  r7   r   rQ  r   r   r   r   r   r  r  r	  r   r   r%  rC   r(  r8  r;  r=  r   r   r  r  r   r  rS  rT  r  rU  rV  rD   rE   rF   r   r     s   0# 0   #/"5"5AF	
 3 u  C  $   3 4 D 
 /3<@

 +/
+
 :

 (
 

 BD %->	
s +;     1	 %OKL M*rE   r   r1   r<   
attributestemplatec                     y rZ   rD   )funcr1   r<   r  r  s        rF   r}  r}  B  r  rE   c                      y rZ   rD   )r  s    rF   r}  r}  N  r  rE   c                >    ddl m}  |||||      }| 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_decoratorr  )sentry_sdk.tracing_utilsr  )r  r1   r<   r  r  r  	decorators          rF   r}  r}  T  s3    T ?%	I rE   )rL   r   r`  r   r  r.  rZ   )r  Nr1   r   r<   r   r  Optional[dict[str, Any]]r  r   rX   z*Callable[[Callable[P, R]], Callable[P, R]])r  Callable[P, R]rX   r  )r  zOptional[Callable[P, R]]r1   r   r<   r   r  r  r  r   rX   zAUnion[Callable[P, R], Callable[[Callable[P, R]], Callable[P, 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+   rI   rA   rN   r   r   rQ   rc   LOW_QUALITY_TRANSACTION_SOURCESr_   ra   SOURCE_FOR_STYLEr   r|   r~   r   r   r   DEFAULTr}  r  rL   r   r`  r   r  r.  rD   rE   rF   <module>r     su     2 2     N N C  AA   4#AA  J@9aYe 9avQJe Q 3	
N   ) 
	T 	 # 
 "++&00%//066!!#--&,,%++""
 #$S #$S #$L$ $.h+ h+VY+$ Y+xU+t U+p 	 # $15#/#7#7		 	 		
 /	 !	 
6	 	   (,V  -1+33V
$V 	V 	V
 +V V IVv  rE   