U 췀g% @sddlZddlmZmZddlmZddlmZddlm Z ddl m Z m Z m Z mZddlmZerddlmZdd lmZdd lmZdd lmZmZzddlZdd lmZWnek red YnXzddlZejZejZdZ Wn.ek rddl!Zej"Zej#ZdZ YnXdZ$e rBe ddl%m&Z$W5QRXGdddeZ'GdddZ(dZ)GdddeZ*ddZ+ddZ,ddZ-ddZ.d d!Z/d"d#Z0d$d%Z1dS)&N) Integration DidNotEnable)RequestExtractor)SentryWsgiMiddleware)SOURCE_FOR_STYLE)capture_internal_exceptionsensure_integration_enabledevent_from_exception parse_version) TYPE_CHECKING)Any)Dict)Optional)EventEventProcessor) __version__zFalcon not installedTF)_UNSETc@s<eZdZddZddZddZddZd d Zd d Zd S)FalconRequestExtractorcCs|jjSN)requestenvselfrL/opt/hc_python/lib/python3.8/site-packages/sentry_sdk/integrations/falcon.pyr5szFalconRequestExtractor.envcCs|jjSr)rcookiesrrrrr9szFalconRequestExtractor.cookiescCsdSrrrrrrform=szFalconRequestExtractor.formcCsdSrrrrrrfilesAszFalconRequestExtractor.filescCs|}|dkrdSdSdS)Nrz[REQUEST_CONTAINING_RAW_DATA])content_length)rrrrrraw_dataEszFalconRequestExtractor.raw_datac Cs.d}t|jj}W5QRX|tk r*|SdSr)rrZ_media _FALCON_UNSET)rZ cached_mediarrrjsonRs zFalconRequestExtractor.jsonN) __name__ __module__ __qualname__rrrrrr!rrrrr4s  rc@seZdZdZddZdS)SentryFalconMiddlewarez9Captures exceptions in Falcon requests and send to SentrycOs<tt}|dkrdSt}d|_|t||dS)Nfalcon) sentry_sdk get_clientget_integrationFalconIntegrationZget_isolation_scope_nameZadd_event_processor_make_request_event_processor)rreqrespargskwargs integrationscoperrrprocess_requesths z&SentryFalconMiddleware.process_requestN)r"r#r$__doc__r3rrrrr%esr% uri_templatepathc@s4eZdZdZdeZdZd ddZeddZd S) r*r&z auto.http.r6cCs"|tkrtd|tf||_dS)Nz7Invalid value for transaction_style: %s (must be in %s))TRANSACTION_STYLE_VALUES ValueErrortransaction_style)rr;rrr__init__|szFalconIntegration.__init__cCsDtt}|dkrtdt|dkr.tdtttdS)NzUnparsable Falcon version: {})zFalcon 1.4 or newer required.)r FALCON_VERSIONrformat_patch_wsgi_app_patch_handle_exception_patch_prepare_middleware)versionrrr setup_onceszFalconIntegration.setup_onceN)r6) r"r#r$ identifieroriginr;r< staticmethodrErrrrr*vs   r*cstjfdd}|t_dS)NcsDtt}|dkr"||Stfddtjd}|||S)Ncs ||Srr)ZenviZ start_resp)original_wsgi_apprrrzB_patch_wsgi_app..sentry_patched_wsgi_app..)Z span_origin)r'r(r)r*rrG)rrZstart_responser1Zsentry_wrappedrIrrsentry_patched_wsgi_apps  z0_patch_wsgi_app..sentry_patched_wsgi_app)falcon_app_class__call__)rMrrLrrAs rAcs&tjttfdd}|t_dS)Nc sd}}t*tdd|D}tdd|D}W5QRX|f|}|dks\|dkr`|St||rt|tjdddd\}}tj||d|S) Ncss|]}t|tr|VqdSr) isinstance Exception.0argumentrrr s zS_patch_handle_exception..sentry_patched_handle_exception..css|]}t|tjr|VqdSr)rPr&ResponserRrrrrUs r&F)typeZhandled)Zclient_optionsZ mechanism)hint)rnext_exception_leads_to_http_5xxr r'r(optionsZ capture_event)rr/exresponseZ was_handledeventrXZoriginal_handle_exceptionrrsentry_patched_handle_exceptions"  z@_patch_handle_exception..sentry_patched_handle_exception)rNZ_handle_exceptionrr*)r`rr_rrBsrBcstjdfdd }|t_dS)NFcs@|r|||Stt}|dk r6tg|p2g}||Sr)r'r(r)r*r%)Z middlewareZindependent_middlewareZasgir1Zoriginal_prepare_middlewarerr!sentry_patched_prepare_middlewares  zD_patch_prepare_middleware..sentry_patched_prepare_middleware)NFF)falcon_helpersZprepare_middleware)rbrrarrCs rCcCsHt|tjo|jpdd}t|tjtjjf }|s:|oFt pFt|S)Nr85) rPr& HTTPErrorstatus startswithZ http_status HTTPStatusFALCON3_has_http_5xx_status)r\r]Zis_server_errorZis_unhandled_errorrrrrZs   rZcCs |jdS)Nrd)rfrg)r]rrrrjsrjcCs.|j|jd}|||d<dt|i|d<dS)Nr5Z transactionsourceZtransaction_info)r6r7r)r^r;rZname_for_stylerrr _set_transaction_name_and_sources  rlcsfdd}|S)Nc s2t|jtt|W5QRX|Sr)rlr;rrZextract_into_event)r^rXr1r-rrevent_processor sz6_make_request_event_processor..event_processorr)r-r1rnrrmrr, s r,)2r'Zsentry_sdk.integrationsrrZ$sentry_sdk.integrations._wsgi_commonrZsentry_sdk.integrations.wsgirZsentry_sdk.tracingrZsentry_sdk.utilsrrr r typingr r r rZsentry_sdk._typesrrr&rr? ImportErrorZfalcon.app_helpersZ app_helpersrcZApprNriZfalcon.api_helpersZ api_helpersZAPIr Zfalcon.requestrrr%r9r*rArBrCrZrjrlr,rrrrsR        1 &