U \@sddlZddlZddlmZddlmZmZddlmZddl m Z m Z m Z ddl mZddlmZmZddlmZdd lmZmZdd lmZdd lmZdd lmZGd ddeZ&ddZ'ddZ(GdddeZ)dS)N) isawaitable)urlparsereraise)Hub)capture_internal_exceptionsevent_from_exceptionHAS_REAL_CONTEXTVARS) Integration)RequestExtractor_filter_headers) ignore_logger)Sanic __version__)SanicException)Router) ErrorHandlerc@seZdZdZeddZdS)SanicIntegrationsaniccsjts tdtdrtdtjfdd}|t_tjfdd}|t_t j fdd }|t _ dS) NzPThe sanic integration for Sentry requires Python 3.7+ or aiocontextvars packagez0.8.rootc stj}|tdkr&||f||St|}t|`}|}||t |W5QRX||f||}t |r|IdH}|W5QRSQRXdSN) rcurrentget_integrationrweakrefrefconfigure_scopeZclear_breadcrumbsZadd_event_processor_make_request_processorr)selfrequestargskwargshub weak_requestscoperesponse)old_handle_request /sanic.pysentry_handle_request=s    z:SanicIntegration.setup_once..sentry_handle_requestc sT||}tj}|tdk rPt&|}|dj|_W5QRXW5QRX|SNr)rrrrrr__name__Z transaction)rrrvr r")old_router_getr%r&sentry_router_getTs   z6SanicIntegration.setup_once..sentry_router_getcsBt|||dkrdStjtdkr2Sfdd}|S)NcsTz"||}t|r|IdH}|WStk rNt}t|t|YnXdSr)r Exceptionsysexc_info_capture_exceptionr)r exceptionr#r/Zold_error_handlerr%r&sentry_wrapped_error_handlerms  zfSanicIntegration.setup_once..sentry_error_handler_lookup..sentry_wrapped_error_handler)r0rrrr)rr1r3)old_error_handler_lookupr2r&sentry_error_handler_lookupbs  z@SanicIntegration.setup_once..sentry_error_handler_lookup) r RuntimeErrorVERSION startswithr r Zhandle_requestrgetrlookup)r'r,r5r%)r4r$r+r& setup_once$s    zSanicIntegration.setup_onceN)r) __module__ __qualname__Z identifier staticmethodr;r%r%r%r&r!src Cs\tj}|t}|dkrdSt0t||jjdddd\}}|j||dW5QRXdS)NrF)typeZhandled)Zclient_optionsZ mechanism)hint) rrrrrrZclientZoptionsZ capture_event)r1r Z integrationeventr@r%r%r&r0s  r0csfdd}|S)Nc szt|ddtrWdSWntk r0YnX}|dkrD|Stxt|}|||d}t|j}d|j |j |j f|d<|j |d<|j |d<d|ji|d <tt|j|d <W5QRX|S) Nr/rrz %s://%s%surlZ query_stringmethodZ REMOTE_ADDRenvheaders) issubclassrKeyErrorrSanicRequestExtractorZextract_into_eventrZurlsplitrBZschemeZnetlocpathZqueryrCZ remote_addrr dictrE)rAr@rZ extractorZ request_infoZurlpartsr!r%r&sanic_processors.      z0_make_request_processor..sanic_processorr%)r!rLr%rKr&rs !rc@sLeZdZddZddZddZddZd d Zd d Zd dZ ddZ dS)rHcCs|jjdkrdSt|jjSr()rbodylenrr%r%r&content_lengths z$SanicRequestExtractor.content_lengthcCs t|jjSr)rJrcookiesrOr%r%r&rQszSanicRequestExtractor.cookiescCs|jjSr)rrMrOr%r%r&raw_dataszSanicRequestExtractor.raw_datacCs|jjSr)rformrOr%r%r&rSszSanicRequestExtractor.formcCs tdSr)NotImplementedErrorrOr%r%r&is_jsonszSanicRequestExtractor.is_jsoncCs|jjSr)rjsonrOr%r%r&rVszSanicRequestExtractor.jsoncCs|jjSr)rfilesrOr%r%r&rWszSanicRequestExtractor.filescCst|jp dS)Nr%)rNrM)rfiler%r%r& size_of_filesz"SanicRequestExtractor.size_of_fileN) r)r<r=rPrQrRrSrUrVrWrYr%r%r%r&rHsrH)*r.rinspectrZsentry_sdk._compatrrZsentry_sdk.hubrZsentry_sdk.utilsrrrZsentry_sdk.integrationsr Z$sentry_sdk.integrations._wsgi_commonr r Zsentry_sdk.integrations.loggingr rr rr7Zsanic.exceptionsrZ sanic.routerrZsanic.handlersrZRequestZAnyCallableZDictZOptionalZUnionZTupleZRequestParametersrr0rrHr%r%r%r&s        `&