U 췀g-3@sTddlZddlZddlmZddlmZddlZddlmZddlm Z ddl m Z m Z ddl mZmZddlmZdd lmZmZdd lmZmZmZmZmZmZmZdd lmZerBdd lm Z dd lm!Z!ddlm"Z"ddlm#Z#ddlm$Z$ddlm%Z%ddl&m'Z'm(Z(ddl)m*Z*ddl+m,Z,m-Z-m.Z.m/Z/ddl0m1Z1z8ddl2m3Z3m4Z5ddl6m7Z7ddl0m8Z8ddl9m:Z:Wne;k re dYnXe:jZ?e8j@ZAz e3jBZCWneDk rYnXGddde ZEGdddeZFdd ZGd!d"ZHd#d$ZBd%d&ZId5d'd(ZJd)d*ZKd+d,ZLd-d.ZMd/d0ZNeeEd1d2ZOd3d4ZPdS)6N) isawaitable)urlsplit)continue_trace)OP) Integration DidNotEnable)RequestExtractor_filter_headers) ignore_logger)TRANSACTION_SOURCE_COMPONENTTRANSACTION_SOURCE_URL)capture_internal_exceptionsensure_integration_enabledevent_from_exceptionHAS_REAL_CONTEXTVARSCONTEXTVARS_ERROR_MESSAGE parse_versionreraise) TYPE_CHECKING) Container)Any)Callable)Optional)Union)Dict)RequestRequestParameters)BaseHTTPResponse)EventEventProcessorExcInfoHint)Route)Sanic __version__)SanicException)Router) ErrorHandlerzSanic not installedc@s<eZdZdZdeZdZedhfddZeddZ dS) SanicIntegrationsanicz auto.http.NicCs|pt|_dS)aG The unsampled_statuses parameter can be used to specify for which HTTP statuses the transactions should not be sent to Sentry. By default, transactions are sent for all HTTP statuses, except 404. Set unsampled_statuses to None to send transactions for all HTTP statuses, including 404. N)set_unsampled_statuses)selfZunsampled_statusesr-K/opt/hc_python/lib/python3.8/site-packages/sentry_sdk/integrations/sanic.py__init__?szSanicIntegration.__init__cCstttt_tjdkr"tdttjdkr4tdtsDtdttdrVt dtjdkrjt dSt dS)NzUnparsable Sanic version: {})rzSanic 0.8 or newer required.zUThe sanic integration for Sentry requires Python 3.7+ or the aiocontextvars package.z0.8.root ) r SANIC_VERSIONr(versionrformatrr startswithr _setup_legacy_sanic _setup_sanicr-r-r-r. setup_onceIs"     zSanicIntegration.setup_once) __name__ __module__ __qualname__ identifieroriginr6 frozensetr/ staticmethodr;r-r-r-r.r(:s   r(c@sLeZdZddZddZddZddZd d Zd d Zd dZ ddZ dS)SanicRequestExtractorcCs|jjdkrdSt|jjS)Nr)requestbodylenr,r-r-r.content_lengthqs z$SanicRequestExtractor.content_lengthcCs t|jjSN)dictrDcookiesrGr-r-r.rKwszSanicRequestExtractor.cookiescCs|jjSrI)rDrErGr-r-r.raw_data{szSanicRequestExtractor.raw_datacCs|jjSrI)rDformrGr-r-r.rMszSanicRequestExtractor.formcCs tdSrI)NotImplementedErrorrGr-r-r.is_jsonszSanicRequestExtractor.is_jsoncCs|jjSrI)rDjsonrGr-r-r.rPszSanicRequestExtractor.jsoncCs|jjSrI)rDfilesrGr-r-r.rQszSanicRequestExtractor.filescCst|jp dS)Nr-)rFrE)r,filer-r-r. size_of_filesz"SanicRequestExtractor.size_of_fileN) r<r=r>rHrKrLrMrOrPrQrSr-r-r-r.rCpsrCcCstt_tt_dSrI)_startupr#_sentry_error_handler_lookupr'lookupr-r-r-r.r:sr:cCstt_tt_tt_dSrI) _legacy_handle_requestr#handle_request_legacy_router_getr&getrUr'rVr-r-r-r.r9sr9cs<|dt|dt|dtt|IdHdS)Nzhttp.lifecycle.requestzhttp.lifecycle.responsezhttp.routing.after)signal_context_enter _context_exit_set_transaction old_startuprGr-r-r.rTsrTcsttdk |j_|jjs"dSt|}t|j_ |jj }| | t |tt|jtj|jttjd}t| |j_dS)N)opnamesourcer@) sentry_sdk get_clientget_integrationr(ctx_sentry_do_integrationweakrefrefisolation_scope _sentry_scope __enter__clear_breadcrumbsadd_event_processor_make_request_processorrrJheadersrZ HTTP_SERVERpathr r@Zstart_transaction_sentry_transaction)rD weak_requestscopeZ transactionr-r-r.r\s&   r\c st|jjsW5QRdStt}|dkr8dn|j}tH|jj ||jjj t |top||j kM_ |jj dddW5QRX|jj dddW5QRXdSrI)r rfrgrcrdrer(statusrrZset_http_statusZsampled isinstancer+__exit__rk)rDresponseZ integrationZresponse_statusr-r-r.r]s  r]c sL|jjrHt4t}|j|jjdd}|j |t dW5QRXdS)N.rb) rfrgr rcZget_current_scoperareplaceappstripset_transaction_namer )rDZroute_rtZ route_namer-r-r.r^s r^csLt|t||f||dkr&dSttdkr<Sfdd}|S)Ncs~zXz&||}t|r |IdH}|WW2Stk rTt}t|t|YnXW5tjrxtjdkrxt|IdHXdS)Nr2) r(r6r]r Exceptionsysexc_info_capture_exceptionr)rD exceptionrxrZold_error_handlerr-r.sentry_wrapped_error_handlers   zB_sentry_error_handler_lookup..sentry_wrapped_error_handler)rold_error_handler_lookuprcrdrer()r,rargskwargsrr-rr.rUs rUc sttdkr*t||f||IdHSt|}tL}|| t |t||f||}t |rx|IdH}|W5QRSQRXdSrI) rcrdrer(old_handle_requestrhrirjrmrnror)r,rDrrrsrtrxr-r-r.rWs   rWc Gst|f|}ttdk rtxt}tjrtjdkr|jj j }|dj }| d|rv|t |dd}|j |tdn|j |djtdW5QRX|S)N)r3rz%s.r{)old_router_getrcrdrer(r Zget_isolation_scoper6rfr}rar8rFrr r<)r,rrvrtZsanic_app_nameZ sanic_router-r-r.rY%s$  rYc Csttdt|tjdddd\}}|rXt|dddrX|ddjrXW5QRdStj||dW5QRXdS) Nr)F)typeZhandled)Zclient_optionsZ mechanismrrquiet)hint)r rrcrdoptionshasattrrZ capture_event)reventrr-r-r.rCs $rcsfdd}|S)Nc sz |rt|ddtrWdSWntk r4YnX}|dkrH|Stvt|}|||d}t|j}d|j|j |j f|d<|j |d<|j |d<d|j i|d <tt|j|d <W5QRX|S) NrrrDz %s://%s%surlZ query_stringmethodZ REMOTE_ADDRenvrp) issubclassr%KeyErrorr rCZextract_into_eventrrschemenetlocrqqueryrZ remote_addrr rJrp)rrrDZ extractorZ request_infourlpartsrsr-r.sanic_processorUs.      z0_make_request_processor..sanic_processorr-)rsrr-rr.roSs !ro)N)Qrrhinspectr urllib.parserrcrZsentry_sdk.constsrZsentry_sdk.integrationsrrZ$sentry_sdk.integrations._wsgi_commonrr Zsentry_sdk.integrations.loggingr Zsentry_sdk.tracingr r Zsentry_sdk.utilsr rrrrrrtypingrcollections.abcrrrrrrZ sanic.requestrrZsanic.responserZsentry_sdk._typesrrr r!Z sanic.routerr"r)r#r$r5Zsanic.exceptionsr%r&Zsanic.handlersr' ImportErrorrVrrXrrZrrTr_AttributeErrorr(rCr:r9r\r]r^rUrWrYrror-r-r-r.sf     $            6$  "