U 췀grW@sddlZddlZddlZddlZddlZddlmZddlmZddl m Z ddl m Z m Z ddlZddlZddlmZmZddlmZmZmZmZmZmZmZmZmZmZmZddlm Z e r dd lm!Z!dd lm"Z"dd lm#Z#dd lm$Z$dd lm%Z%ddl&m'Z'e(dZ)dZ*GdddeZ+ddZ,ej-d3ddZ.ddZ/ddZ0ddZ1dd Z2d!d"Z3d#d$Z4Gd%d&d&Z5Gd'd(d(Z6d)d*Z7d+d,Z8d-d.Z9d4d/d0Z:dd1l;mZ>e rdd2l;m?Z?dS)5N)Mapping) timedelta)wraps)quoteunquote)OPSPANDATA) capture_internal_exceptionsfilename_for_moduleDsnloggermatch_regex_listqualname_from_function to_string is_sentry_url_is_external_source_is_in_project_root_module_in_list) TYPE_CHECKING)Any)Dict) Generator)Optional)Union) FrameTypez5^[ ]*([0-9a-f]{32})?-?([0-9a-f]{16})?-?([01])?[ ]*$z(([a-zA-Z0-9+/]{4})*([a-zA-Z0-9+/]{2,3})?c@s.eZdZd ddZddZddZdd Zd S) EnvironHeadersHTTP_cCs||_||_dSN)environprefix)selfrrr!F/opt/hc_python/lib/python3.8/site-packages/sentry_sdk/tracing_utils.py__init__=szEnvironHeaders.__init__cCs|j|j|ddSN-_)rrreplaceupper)r keyr!r!r" __getitem__FszEnvironHeaders.__getitem__cCstddt|DS)Ncss|] }dVqdS)Nr!).0r&r!r!r" Lsz)EnvironHeaders.__len__..)sumiterr r!r!r"__len__JszEnvironHeaders.__len__ccsN|jD]B}t|tsq|dd}||js4q|t|jdVqdSr$)r isinstancestrr'r( startswithrlen)r kr!r!r"__iter__Ns   zEnvironHeaders.__iter__N)r)__name__ __module__ __qualname__r#r*r1r7r!r!r!r"r<s  rcCs:|dkr dSt|ddk o6|ddk p6|ddk S)z} Returns True if either traces_sample_rate or traces_sampler is defined and enable_tracing is set and not false. NFZenable_tracingtraces_sample_rateZtraces_sampler)boolget)optionsr!r!r"has_tracing_enabled[s r?Fmanualc cstjdddr6|r$|dgkr(d}|dkr>d}nd}d}t||}i}|dk r\||d<|dk rl||d<|rxd|d <|r|dk r||d <ttj|d |d W5QRXtjtj ||d *}| D]\} } | | | q|VW5QRXdS)NZ _experimentsZrecord_sql_paramsFZpyformatformatz db.paramsz db.paramstyleTzdb.executemanyz db.cursorquery)messagecategorydata)opnameorigin) sentry_sdk get_clientr>r= _format_sqlr add_breadcrumbZ start_spanrZDBitemsset_data) cursorrBZ params_listZ paramstyleZ executemanyZrecord_cursor_reprZ span_originrEspanr6vr!r!r"record_sql_queriesms8   rRcCsh|jtjkr$|j|jdd|jdn@|jtjkrD|jdd|jdn |jdkrd|jdd|j|jddS)NZredis)rCtyperDrEhttphttplib)rSrDrE subprocess)rSrDrCrE)rFrZDB_REDISrL descriptionZ_tagsZ HTTP_CLIENT_data)scoperPr!r!r""maybe_create_breadcrumbs_from_spans    rZcCs&z |jjWStk r YdSXdSr)f_code co_filename Exception)framer!r!r"_get_frame_module_abs_paths r_cCs6t||}t|pt||}| o4|p4t||o4| Sr)rrr)is_sentry_sdk_frame namespacein_app_includein_app_excludeabs_path project_rootshould_be_includedZshould_be_excludedr!r!r"_should_be_includeds  rgcCsTt}|sdS|jdks(|jdkr,dS|jdd}|sBdS|j|j}|jdd}|tdd|k}|svdS|jd}|jd }|jd }t } | dk rt | } z| j d } Wnt k rd} YnX| dk o| d } t| | ||| |d } | r q| j} qd} | dk rPz | j}Wnt k rFd}YnX|dk rb|tj| jz| j d } Wnt k rd} YnX| dk r|tj| t | }|dk r | dk rt| |}n0|dk r| |r||dtj}n|}|tj|z | jj}Wnt k r2d}YnX|dk rP|tj| jjdS)zB Adds OTel compatible source code information to the span NZenable_db_query_sourceTZdb_query_source_threshold_msrr+)Z millisecondsrerbrcr8z sentry_sdk.)r`rarbrcrdre)rIrJ is_active timestampZstart_timestampr>r=rsys _getframer_ f_globalsr]r4rgf_backf_linenorNrZ CODE_LINENOZCODE_NAMESPACEr r'lstripossepZ CODE_FILEPATHr[co_nameZ CODE_FUNCTION)rPclientZshould_add_query_sourceduration thresholdZ slow_queryrerbrcr^rdrar`rflinenofilepathZ in_app_pathZ code_functionr!r!r"add_query_sources                   rycCs|sdS|dr(|dr(|dd}t|}|s:dS|\}}}d}|r`dt|d}|rtdt|d}|r|d k}|||d S) zL Given a `sentry-trace` header string, return a dictionary of data. Nz00-z-00z{:032x}z{:016x}0)trace_idparent_span_idparent_sampled)r4endswithSENTRY_TRACE_REGEXmatchgroupsrAint)headerrr~rZ sampled_strrr!r!r"extract_sentrytrace_data&s&  rcCsZd}z0t|dr2||}t|tr2||jj}Wntk rLd}YnX|pXt|S)Nmogrify) hasattrrr2bytesdecode connectionencodingr]r)rOsqlZreal_sqlr!r!r"rKFs    rKc@sneZdZdZdZdddZeddZedd Z e j d d Z ed d Z e j d d Z ddZ ddZ dS)PropagationContextzJ The PropagationContext represents the data of a trace in Sentry.  _trace_id_span_idrrdynamic_sampling_contextNcCs"||_||_||_||_||_dSrr)r r~span_idrrrr!r!r"r#gs zPropagationContext.__init__cCsjd}t|}|t}|r0t}t||_|t}|rft|}|dk rf|dkr\t}| ||Sr) normalize_incoming_datar=BAGGAGE_HEADER_NAMErBaggagefrom_incoming_headerrSENTRY_TRACE_HEADER_NAMErupdate)cls incoming_datapropagation_contextZnormalized_dataZbaggage_headerZsentry_trace_headerZsentrytrace_datar!r!r"from_incoming_datas     z%PropagationContext.from_incoming_datacCs|jstj|_|jS)z!The trace id of the Sentry trace.)ruuiduuid4hexr0r!r!r"r~s zPropagationContext.trace_idcCs ||_dSr)rr valuer!r!r"r~scCs |jstjdd|_|jS)z+The span id of the currently executed span.r|N)rrrrr0r!r!r"rszPropagationContext.span_idcCs ||_dSr)rrr!r!r"rsc Cs<|D].\}}zt|||Wqtk r4YqXqdS)zU Updates the PropagationContext with data from the given dictionary. N)rMsetattrAttributeError)r other_dictr)rr!r!r"rs zPropagationContext.updatecCsd|j|j|j|j|jS)Nzm)rArrrrrr0r!r!r"__repr__szPropagationContext.__repr__)NNNNN)r8r9r:__doc__ __slots__r# classmethodrpropertyr~setterrrrr!r!r!r"rZs(        rc@sveZdZdZdZdZedZdddZ e d d Z e d d Z e d dZ ddZddZdddZeddZdS)rzR The W3C Baggage header information (see https://www.w3.org/TR/baggage/).  sentry_itemsthird_party_itemsmutablezsentry-z^sentry-rhTcCs||_||_||_dSrr)r rrrr!r!r"r#szBaggage.__init__c Csi}d}d}|r|dD]z}d|kr(qt`|}|d\}}tj|rvt|dd}t|||<d}n||rdnd|7}W5QRXqt|||S)zF freeze if incoming header already has sentry baggage rhT,=r%r+F)splitr striprSENTRY_PREFIX_REGEXrr) rrrrritemr)valZ baggage_keyr!r!r"rs    zBaggage.from_incoming_headercCsi}d}d}t}|r&|jdkr.t|S|j}|j}|dk rL|j|d<|drb|d|d<|drx|d|d<|drt|dj |d<|drt |d|d <t|||S) NrhFr~ environmentreleasedsn public_keyr; sample_rate) rIrJriZ_propagation_contextrr>r~r=r rr3)rrYrrrrtr>rr!r!r" from_optionss&       zBaggage.from_optionscCst}i}|st|S|jp$i}|j|d<|drF|d|d<|dr\|d|d<|drxt|dj|d<|j r|j t kr|j |d<|j dk rt |j |d<|jdk r|jrd nd |d <|jr|jjr||jjt|d d S)z Populate fresh baggage entry with sentry_items and make it immutable if this is the head SDK which originates traces. r~rrrr transactionNrtruefalsesampledFr)rIrJrirr>r~r=r rrGsourceLOW_QUALITY_TRANSACTION_SOURCESrr3rZ_baggagerr)rrrtrr>r!r!r"populate_from_transactions0          z!Baggage.populate_from_transactioncCs d|_dS)NFrr0r!r!r"freezeIszBaggage.freezecCs$i}|jD]\}}|||<q|Sr)rrM)r rr)rr!r!r"rMs z Baggage.dynamic_sampling_contextFc Cslg}|jD]B\}}t.tjt|dtt|}||W5QRXq|rb||jd |S)Nrr) rrMr r SENTRY_PREFIXrr3appendrjoin)r Zinclude_third_partyrMr)rrr!r!r" serializeVs zBaggage.serializecCsddd|dDS)zRemove Sentry baggage from the given header. Given a Baggage header, return a new Baggage header with all Sentry baggage items removed. rcss"|]}tj|s|VqdSr)rrrr)r,rr!r!r"r-lsz/Baggage.strip_sentry_baggage..)rr)rr!r!r"strip_sentry_baggageds zBaggage.strip_sentry_baggageN)rhT)F)r8r9r:rrrrecompilerr#rrrrrrr staticmethodrr!r!r!r"rs$     - rcCs&|jd}t||rdSt||ddS)zy Returns True if url matches trace_propagation_targets configured in the given client. Otherwise, returns False. trace_propagation_targetsFT)Zsubstring_matching)r>rr )rturlrr!r!r"should_propagate_tracets  rcCsHi}|D]6\}}|dr*|dd}|dd}|||<q |S)z Normalizes incoming data so the keys are all lowercase with dashes instead of underscores and stripped from known prefixes. rNr&r%)rMr4r'lower)rrEr)rr!r!r"rs   rcstrFtfdd}zt|_Wqtk rBYqXn:tfdd}zt|_Wntk r~YnX|S)zc Decorator to add child spans for functions. See also ``sentry_sdk.tracing.trace()``. c sjt}|dkr.tdt||IdHS|jtjtd||IdHW5QRSQRXdSNzbCannot create a child span for %s. Please start a Sentry transaction before calling this function.)rFrGget_current_spanr debugrZ start_childrZFUNCTIONargskwargsrPfuncr!r"func_with_tracingsz5start_child_span_decorator..func_with_tracingc s^t}|dkr(tdt||S|jtjtd||W5QRSQRXdSrrrrr!r"rs )inspectiscoroutinefunctionr signature __signature__r])rrr!rr"start_child_span_decorators rcCs|p t}|j}|S)zU Returns the currently active span if there is one running, otherwise `None` )rIZget_current_scoperP)rYZ current_spanr!r!r"rs r)rrr)Span)Fr@)N)@ contextlibrrqrrkcollections.abcrdatetimer functoolsr urllib.parserrrrIZsentry_sdk.constsrrZsentry_sdk.utilsr r r r r rrrrrrtypingrrrrrrtypesrrrZbase64_strippedrr?contextmanagerrRrZr_rgryrrKrrrrrrZsentry_sdk.tracingrrrrr!r!r!r"s^   4          /[ q* B