0i# ddlZddlZddlmZmZmZddlmZddlZddlm Z m Z m Z m Z ddl mZddlmZmZmZmZmZmZddlmZerddlmZmZmZdd lmZdd lmZdd lmZdd lmZdd lm Z ddlm!Z!ddlm"Z"ddlm#Z#ddlm$Z$ddlm%Z%ddlm&Z&ddl'm(Z(m)Z)e"dZ*e%dZ+ddl m,Z,ddl-m.Z.ddl/m0Z0m1Z1m2Z2m3Z3Gdde(dZ4Gdde4dZ5e(d d!e6iZ7d"Z8d#Z9Gd$d%e6eZ:e:jvgZr?r@rArCrDrErHrHls   PrDrHProfileContext profiler_idrLz sentry-tracec*eZdZdZdZdZdZdZdZdZ y) TransactionSource componentcustomroutetaskurlviewc|jSN)valueselfs rE__str__zTransactionSource.__str__s zzrDN) r<r=r> COMPONENTCUSTOMROUTETASKURLVIEWr\rCrDrErPrPs%I F E D C DrDrP) endpoint function_name handler_namemethod_and_path_patternpath route_name route_pattern uri_templaterUc\|dkrtjSd|cxkrdkrnn|dk(rtjS|dk(rtjS|dk(rtjS|dk(rtj S|dk(rtj S|dk(rtjStjSd|cxkrd krantjS|d k(rtjS|d k(rtjS|d k(rtjStjStjS) z Returns the Sentry status corresponding to the given HTTP status code. See: https://develop.sentry.dev/sdk/event-payloads/contexts/#trace-context iiiiiiiiiXiii)rOKPERMISSION_DENIED NOT_FOUNDRESOURCE_EXHAUSTEDFAILED_PRECONDITIONUNAUTHENTICATEDALREADY_EXISTSINVALID_ARGUMENTDEADLINE_EXCEEDED UNIMPLEMENTED UNAVAILABLEINTERNAL_ERROR UNKNOWN_ERROR)http_status_codes rEget_span_status_from_http_coderzs#}}  &3 & s "// /  $'' '  $00 0  $11 1  $-- -  $,, ,.. .  &3 &  # ## s "// /  $++ +  $)) ),, ,  # ##rDc eZdZdZdZdZdZy) _SpanRecorderz5Limits the number of spans recorded in a transaction.maxlenspans dropped_spansc4|dz |_g|_d|_y)Nrr}r[r~s rE__init__z_SpanRecorder.__init__sqj  rDct|j|jkDrd|_|xjdz c_y|jj |y)Nr)lenrr~_span_recorderrappend)r[spans rEaddz_SpanRecorder.addsA tzz?T[[ ("&D    ! #  JJ  d #rDN)r<r=r>__doc__ __slots__rrrCrDrEr|r|s?4I $rDr|ceZdZdZdZ d%dZdZedZejdZedZ e jd Z d Z d Z d Z ed Zej fdZedZedddZdZedZdZdZdZdZdZdZdZd&dZdZdZ dZ!dZ"d'd Z#d!Z$d"Z%d#Z&d$Z'y)(SpanakA 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/r0r1r2r3 _measurementsr8_start_timestamp_monotonic_nsr5 timestamp_tags_datarr4_context_manager_state_containing_transactionr9r:r;_flags_flags_capacityNc||_||_||_||_||_||_|xs||_| |_||_| |_ | |_ i|_ i|_ i|_ | |_i|_d|_|;t#j$dt&d|jxs |j|_ | $t)j*t,j.} n4t1| t2r$t)j4| t,j.} | |_ t9|_d|_d|_ |jC|jEtGy#t<$rYCwxYw)N z>The `hub` parameter is deprecated. Please use `scope` instead. stacklevel)$rrr/r0r1r2r3r5r4r9r:rrrrrrwarningswarnDeprecationWarningrnowrutc isinstancerB fromtimestampr8rrAttributeErrorrrupdate_active_threadset_profiler_idr )r[r-r.r/r0r1r2r3r4r5r7r8r9r:r;s rErz Span.__init__sK$" ,&<# .;     '=$ ! ? MMP"  0syyDJ  "&ll8<<8O  /&44_hllSO. 2A1BD . " !!# _./   sE"" E.-E.c>|jt||_yyrX)rr|rs rEinit_span_recorderzSpan.init_span_recorderas    &"/"7D  'rDcx|js#tjj|_|jSrX)ruuiduuid4hexrZs rEr-z Span.trace_idfs(~~!ZZ\--DN~~rDc||_yrX)rr[rYs rEr-z Span.trace_idns rDc~|js&tjjdd|_|jS)N)rrrrrZs rEr.z Span.span_idss/}} JJL,,RS1DM}}rDc||_yrX)rrs rEr.z Span.span_id{s  rDcd|jjd|jd|jd|jd|j d|j d|jd|jd S) N) __class__r<r2r3r-r.r/r1r:rZs rE__repr__z Span.__repr__sP ''    ##    rDc|jxstj}|j}||_||f|_|SrX)r9 sentry_sdkget_current_scoperr)r[r9old_spans rE __enter__zSpan.__enter__s; #>     ' ' VD,G,G,V,V     e $ rDc z|turtjdtjt |fi|S)a( 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. zXDeprecated: use Transaction.continue_from_environ instead of Span.continue_from_environ.)rrwarningr6continue_from_headersEnvironHeaders)clsenvironrs rEcontinue_from_environzSpan.continue_from_environs9& $; NN9 001HSFSSrD _sample_randc h|turtjdtj|j t |}|jt |it|j t}|!|j||jtdi|}d|_ |S)a 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) zXDeprecated: use Transaction.continue_from_headers instead of Span.continue_from_headers.rFrC) rrrrKfrom_incoming_headerrBAGGAGE_HEADER_NAMEupdateextract_sentrytrace_dataSENTRY_TRACE_HEADER_NAMEfreezer6r0)rheadersrrrLsentrytrace_kwargs transactions rErzSpan.continue_from_headerss$ $; NN9 .. KK+ ,<   *G455 KK0 1   ) MM, - NN !+F+ -2 *rDc#K|jsyt|jf|jjj }|r t |fyyw)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)r7rto_traceparent get_baggage serializer)r[rLs rE iter_headerszSpan.iter_headers$sZ** &(;(;(===--99;EEG %w. . sAAc dtjd|sy|jt|ifi|S)a DEPRECATED: Use :py:meth:`sentry_sdk.tracing.Span.continue_from_headers`. Create a ``Transaction`` with the given params, then add in data pulled from the given ``sentry-trace`` header value before returning the ``Transaction``. zwDeprecated: Use Transaction.continue_from_headers(headers, **kwargs) instead of from_traceparent(traceparent, **kwargs)N)rrrr)r traceparentrs rEfrom_traceparentzSpan.from_traceparent8sC  A (s(( %{ 3 7=  rDc|jdurd}n|jdurd}nd}|jd|j}||d|z }|S)NT1F0-)r1r-r.)r[r1rs rErzSpan.to_traceparentQsT <<4 G \\U "GG!% =   G- -KrDcP|jr|jjSy)zReturns the :py:class:`~sentry_sdk.tracing_utils.Baggage` associated with this ``Span``, if any. (Taken from the root of the span tree.) N)r7rrZs rE to_baggagezSpan.to_baggage`s%  & &..::< <rDc"||j|<yrX)rr[keyrYs rEset_tagz Span.set_tagi 3rDc"||j|<yrX)rrs rEset_dataz Span.set_datamrrDc:|jj|yrX)rrr[datas rE update_datazSpan.update_dataqs $rDcht|j|jkr||j|<yyrX)rrr)r[flagresults rEset_flagz Span.set_flagus, t{{ d22 2 &DKK  3rDc||_yrXr5rs rErzSpan.set_statuszs  rDc`tjdtd||d|j|<yz .. 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.rr)rYunitNrrrrr[r;rYr s rEset_measurementzSpan.set_measurement~0    .3D#A4 rDc|M|jtjt||!|jtj|yyyrX)rr THREAD_IDr? THREAD_NAMEr[ thread_id thread_names rE set_threadzSpan.set_threadsF  MM(,,c)n =& h22K@' !rDcJ|!|jtj|yyrX)rr PROFILER_IDr[rNs rErzSpan.set_profiler_ids!  " MM(.. < #rDc|jdt||jtj||j t |y)Nzhttp.status_code)rr?rr HTTP_STATUS_CODErrzr[ http_statuss rEset_http_statuszSpan.set_http_statuss@ K 0  h//= 6{CDrDc |jdk(S)NokrrZs rE is_successzSpan.is_successs{{d""rDc|jy |r> % 8mU3$,$:$:=(,,$WM!.)+d.P.PP!%!5!5 !(49" 7557*5$7  8%\\(,,7DN 8sA6B**1CCc |j|j|j|j|j|j |j |j|jd }|jr|j|jd<t|jdkDr|j|d<|j}|r||d<i}|j|j|j|j|r||d<|S)z5Returns a JSON-compatible representation of the span.) r-r.r/r0r2r3r8rr:r5r measurementstagsr)r-r.r/r0r2r3r8rr:r5rrrrrr)r[rvr)rs rEto_jsonz Span.to_jsons  ||"11&*&A&A''++#33kk   ;;#';;DJJx t!! "Q &!%!3!3B~ zz BvJ DKK  DJJ BvJ rDc|j|j|j|j|j|j d}|j r|j |d<|jr+|jjj|d<i}|jjtj}|||d<|jjtj}|||d<|r||d<|S)N)r-r.r/r2r3r:r5dynamic_sampling_contextz thread.idz thread.namer)r-r.r/r2r3r:r5r7rr-rrr rr)r[r*rrrs rEget_trace_contextzSpan.get_trace_contexts ||"11''++kk   ;;;;BxL  & &++779RRT ) *JJNN8#5#56  )D jjnnX%9%9:  ""-D  BvJ rDcb|jjtj}|yd|iS)NrN)rrr rrs rEget_profile_contextzSpan.get_profile_contexts4jjnnX%9%9:   ;  rDcBt\}}|j||yrX)r rrs rErzSpan.update_active_threads!8!: ;  ;/rD)NNNTNNNNNNNNmanualNNN)(r<r=r>rrrrpropertyr-setterr.rrrr7rSENTRYr classmethodrrrrrrrrrrrrrrrr!rr+r.r0rrCrDrErrsu:I8#  # ?0F8 __ ^^  " , ,(4':':(TTT2  --^/(  0    '  BA= E#$L DB 0rDrceZdZdZdZdddej ffd ZdZdZ fdZ fd Z e d Z d Zd Z ddd fd ZddZdZfdZfdZfdZdZdZxZS)r6aiThe 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;rIrJ sample_rater _contexts_profile_continuous_profile_baggagerr4Nc :t|di|||_||_d|_||_i|_i|_d|_d|_ ||_ |jdn|jj}|||_ yt|j|_ yNrC)superrr;rIr;rJrr<r=r>r?r_generate_sample_randr-)r[r;rJrLrIrbaggage_sample_randrs rErzTransaction.__init__?s "6"  , #'  MM)Dt}}/I/I/K   * 3D  5dmm DD rDcd|jjd|jd|jd|jd|j d|j d|jd|jd |jd S) Nrz(name=z, op=rrrrz , source=rr) rr<r;r2r-r.r/r1rIr:rZs rErzTransaction.__repr__\sU ''   ##     rDc>|jduxs|jduS)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)rr1rZs rE_possibly_startedzTransaction._possibly_startedms#""$.G$,,%2GGrDc|jstjdt||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.)rGrdebugrBrr=)r[rs rErzTransaction.__enter__ysL%%' LL>   == $ MM # # % rDc|j|jj||||j|jjt||||yrX)r=rr>stoprB)r[rrYrrs rErzTransaction.__exit__sR == $ MM " "2ub 1  # # /  $ $ ) ) + UB'rDc|S)znThe root element of the span tree. In the case of a transaction it is the transaction itself. rCrZs rEr7z"Transaction.containing_transactions  rDc|}|tjdtd|}t|tj r(tjdtd|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.rzDPassing a Hub to finish is deprecated. Please pass a Scope, instead.)rrrrrHubr9)r[ scope_arghub_arg scope_or_hubs rE_get_scope_from_finish_argsz'Transaction._get_scope_from_finish_argssb!   MM_"  #L lJNN 3 MMV"   %% %rDcxdj|jrd|jzdznd|jS)Nz{op}transaction <{name}>rz> r4)r2r;)formatr2r;rZs rE_get_log_representationz#Transaction._get_log_representations8)00(,dgg $Rtyy1  rDr4c v|jy|j||}|xs"|jxstj}tj }|j sy|j|jdurtjdntjd|jryt|jrd|jr|jjdkDrd}nd}|jj!|d |jj!|d y|j"stj$d d |_t&|Q|||j*j-t.j0}|||jd vrtjdj3|j5|j*t.j0|jd |jr]|jj!dd t7|jj8dz}|jj!dd |d|_|js"|jtj$dy|jj8Dcgc]}|j|j; } }t7|jj8t7| z } | |jj<z} d|_i} | j?|j@| j?d|jCi|jE} | | j?d| id|j"d|jFi| |jH|j|jJ| d}| dkDr| |d<|jL0|jLjOr|jL|d<d|_&|jP|d<|jS|Scc}w)auFinishes 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_categoryrzCTransaction has no name, falling back to ``.ztrace_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_coder[event_processorr)rZquantityz1Discarding transaction without sampling decision.traceprofilerI)typertransaction_infocontextsr)rr8r_dropped_spansr()*rrSr9rrr is_activerr1rrI transporthas_tracing_enabledrmonitordownsample_factorrecord_lost_eventr;rrBrrrr rrUrVrrr+rrr<r.r0rIrr8r=validr capture_event)r[r9r%r4clientreasonr] num_spansrfinished_spanslen_diffrrdprofile_contexteventrs rErzTransaction.finishs, >> %00<EEz'C'C'E&&(!    &||u$ MN i$7$G>>fnn&F&F&J+F*F  2262W  2262Pyy NNU 2DI um,jjnnX%>%>?  #v~~.IJJ LLqxx,0,H,H,J $ 8+D+D E.4nn3/x   22%]3  3 3 9 9:Q>   22%Vi3!DL||||#RS++11 1~~) LLN1  t**001C4GG 4#6#6#D#DD #'$"8"8":;<224  & OOY8 9"99!)4;; 7 JJ#33#   1 &3E" # == $)<)<)>#}}E)  DM $ 2 2n""5))U s>#P6c`tjdtd||d|j|<yr r r s rErzTransaction.set_measurementYrrDc"||j|<y)aSets 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<rs rE set_contextzTransaction.set_contextgs$srDcLt|||jdd|iy)zySets the status of the Transaction according to the given HTTP status. :param http_status: The HTTP status code.responser]N)rBrrw)r[rrs rErzTransaction.set_http_statusrs'  , m[%ABrDc~t|}|j|d<|j|d<|j|d<|S)z">"@#6>>2 DL  << #$T\\2D  **+;<= -FNN+ ,-= > $$45A!!12^^$89 $K B NN`gg,Ch  !DL  - >>   6>>#C#C C C  LLQXX,C$FNN$6$67G$HI== Y !DL ((4+;+;; << LL>EE,CF  LLPWW,C $ 0 0W rDr5r3)r<r=r>rrrPr^rrrGrrr6r7rSrVrrrwrr+r.rr __classcell__)rs@rEr6r6s" I  '' E: " H (  B Q*  Q*f B $C  brDr6ceZdZdZedZejfdZdZ dZ dZ dZ dZ d Zd Zd Zd Zd ZdZdZdZ ddddZddZdZdZdZy)rc4d|jjzS)Nz<%s>)rr<rZs rErzNoOpSpan.__repr__s////rDcyrXrCrZs rEr7zNoOpSpan.containing_transactionsrDc tSrX)r)r[rrs rErzNoOpSpan.start_child s zrDcy)Nr4rCrZs rErzNoOpSpan.to_traceparent srDcyrXrCrZs rErzNoOpSpan.to_baggagerDcyrXrCrZs rErzNoOpSpan.get_baggagerrDctdSrA)iterrZs rErzNoOpSpan.iter_headerss BxrDcyrXrCrs rErzNoOpSpan.set_tag rDcyrXrCrs rErzNoOpSpan.set_data!rrDcyrXrCrs rErzNoOpSpan.update_data%rrDcyrXrCrs rErzNoOpSpan.set_status)rrDcyrXrCrs rErzNoOpSpan.set_http_status-rrDcy)NTrCrZs rEr!zNoOpSpan.is_success1rrDciSrXrCrZs rEr+zNoOpSpan.to_json5 rDciSrXrCrZs rEr.zNoOpSpan.get_trace_context9rrDciSrXrCrZs rEr0zNoOpSpan.get_profile_context=rrDNrWcy)z_ The `hub` parameter is deprecated. Please use the `scope` parameter, instead. NrC)r[r9r%r4s rErzNoOpSpan.finishAs rDcyrXrCr s rErzNoOpSpan.set_measurementNrrDcyrXrCrs rErwzNoOpSpan.set_contextRrrDcyrXrCrs rErzNoOpSpan.init_span_recorderVrrDcyrXrC)r[rs rErz'NoOpSpan._set_initial_sampling_decisionZrrDr5r3)r<r=r>rr6r7rr8rrrrrrrrrrr!r+r.r0rrrwrrrCrDrErrs0(4':':              rDrr2r; attributestemplatecyrXrC)funcr2r;rrs rEr`r`as rDcyrXrC)rs rEr`r`is rDc>ddlm}|||||}|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)rr2r;rrr decorators rEr`r`ps3L?%  I rD)rKrrrCrhr$rX)Nrrrrrenumrrsentry_sdk.constsrrr r 'sentry_sdk.profiler.continuous_profilerr sentry_sdk.utilsr r rrrrtypingrcollections.abcrrrrrrrrrrrrrr typing_extensionsr!r"r#r$r%(sentry_sdk.profiler.transaction_profilerr&sentry_sdk._typesr'r(r)r*r,rHr?rMrrrPraLOW_QUALITY_TRANSACTION_SOURCESr]r_SOURCE_FOR_STYLErzr|rr6rDEFAULTr`rrKrrrCrhr$rCrDrErs 22NNC!AA 3#A AI@9aYe9av QJe Q 3 N ) T # "++&00%//066  ! !#--&,,%++  " " $$N$$2v0v0r]$]@] t] @  DTLDXDX     R @T@TRnrD