U 췀g)@sddlZddlmZddlmZmZddlmZddlm Z ddl m Z m Z ddl mZmZmZzhddlmZmZmZdd lmZdd lmZdd lmZdd lmZdd lmZmZm Z ddl!m"Z"Wne#k redYnXddl$m%Z%e%rTddl$m&Z&m'Z'm(Z(ddl)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z2m3Z3m4Z4ddlm5Z5ddl6m7Z7dZ8GdddeZ9GdddeZ:ddZ;ddZd"d#Z?ee9d$d%Z@dS)&N)OP) DidNotEnable Integration)SentryAsgiMiddleware)should_send_default_pii)SOURCE_FOR_STYLETRANSACTION_SOURCE_ROUTE)ensure_integration_enabledevent_from_exceptiontransaction_from_function)RequestStarliteState)BaseRouteHandler)DefineMiddleware)get_plugin_for_value) HTTPRoute)ConnectionDataExtractoris_async_callableRef) BaseModelzStarlite is not installed) TYPE_CHECKING)AnyOptionalUnion) ASGIAppHintHTTPReceiveMessage HTTPScopeMessage MiddlewareReceiveScopeSendWebSocketReceiveMessage)MiddlewareProtocol)Eventzgeneric Starlite requestc@s&eZdZdZdeZeddZdS)StarliteIntegrationstarlitez auto.http.cCstttdS)N)patch_app_initpatch_middlewarespatch_http_route_handler,r,N/opt/hc_python/lib/python3.8/site-packages/sentry_sdk/integrations/starlite.py setup_once3szStarliteIntegration.setup_onceN)__name__ __module__ __qualname__ identifierorigin staticmethodr.r,r,r,r-r'/s r'cs"eZdZejffdd ZZS)SentryStarliteASGIMiddlewarecstj|ddd|ddS)NFendpointZasgi)appZunsafe_context_dataZtransaction_styleZmechanism_type span_origin)super__init__)selfr7r8 __class__r,r-r:<sz%SentryStarliteASGIMiddleware.__init__)r/r0r1r'r3r: __classcell__r,r,r<r-r5;sr5cs&tjttfdd}|t_dS)a Replaces the Starlite class's `__init__` function in order to inject `after_exception` handlers and set the `SentryStarliteASGIMiddleware` as the outmost middleware in the stack. See: - https://starlite-api.github.io/starlite/usage/0-the-starlite-app/5-application-hooks/#after-exception - https://starlite-api.github.io/starlite/usage/7-middleware/0-middleware-intro/ csf|dg}|jtft|tr"|n|gdtjt_|dpBg}tf||d<|f||dS)Nafter_exception)r? middleware) popupdateexception_handler isinstancelistr5Z _run_asgi3__call__get)r;argskwargsr?r@Z old__init__r,r-injection_wrapperRs  z)patch_app_init..injection_wrapperN)r r:r r')rKr,rJr-r)Gs r)cs&tjttfdd}|t_dS)Ncsdd|DS)NcSsg|] }t|qSr,)enable_span_for_middleware).0r@r,r,r- pszIpatch_middlewares..resolve_middleware_wrapper..r,)r;Zold_resolve_middleware_stackr,r-resolve_middleware_wrappermsz5patch_middlewares..resolve_middleware_wrapper)rZresolve_middlewarer r')rPr,rOr-r*isr*csht|dr|tkr|St|tr*|jjn|jfdd}jdk}|rdt|tr^||j_n||_|S)NrFc sttdkr&||IdHS|jjtjtjtj d}| dfdd}t dt }|dk}|s~|n}fdd} t dt } | d k} | s| n} |||| IdHW5QRSQRXdS) Nopnamer3starlite.middleware_namec svttdkr"||IdHStjtjtdttj d*}| d||IdHW5QRSQRXdSNr1rQrT) sentry_sdk get_clientget_integrationr' start_spanrZMIDDLEWARE_STARLITE_RECEIVEgetattrstrr3set_tag)rHrIspan)middleware_namereceiver,r-_sentry_receives zNenable_span_for_middleware.._create_span_call.._sentry_receiver/r`c srttdkr |IdHStjtjtdttj d(}| d|IdHW5QRSQRXdSrU) rVrWrXr'rYrZMIDDLEWARE_STARLITE_SENDrZr[r3r\)messager])r^sendr,r- _sentry_sends zKenable_span_for_middleware.._create_span_call.._sentry_sendrc) rVrWrXr'r=r/rYrZMIDDLEWARE_STARLITEr3r\rZr[) r;scoper_rbZmiddleware_spanr`Z receive_nameZreceive_patchedZ new_receivercZ send_nameZ send_patchedZnew_sendZold_call)r^r_rbr-_create_span_calls&     z5enable_span_for_middleware.._create_span_call)rf)hasattrr5rDrr@rFr/)r@rfZnot_yet_patchedr,rer-rLxs   1   rLcstjfdd}|t_dS)Ncsttdkr&|||IdHSt}dj||d}tddd|d}|IdHfdd}tj|_ | ||||IdHS)Nr7)rdr_rbT)Z parse_bodyZ parse_querybodycsd}|di}tdd|d<tr<d|d<dk rL|d<d}|jdk rb|j}nt|jtrx|jj}n|j}|dk rt|}dt d i}|st }dt i}| |||d |S) N route_handlerrequest_bodycontent_lengthcookiesdatasourcer6)rjZ transactionZtransaction_info) rGlenrrSrDfnrvaluer r_DEFAULT_TRANSACTION_NAMErrB)event_riZ request_infofuncZtx_nameZtx_infoZextracted_request_dataZ request_datardr,r-event_processors4       zHpatch_http_route_handle..handle_wrapper..event_processor) rVrWrXr'get_isolation_scopeZ request_classrrAr2_nameZadd_event_processor)r;rdr_rb sentry_scoperjrhryZ old_handlerxr-handle_wrappers(  $ z/patch_http_route_handle..handle_wrapper)rhandle)r~r,r}r-r+s 9r+cCsh|d}|sdSt|tr |St|tr2|St|drD|St|}|rdt|jsd||SdS)Nuserasdict) rGrDdictrrgrrrto_dict)rdZ scope_userpluginr,r,r-retrieve_user_from_scopes     rcCsdd}trt|}|r2t|tr2t}||t|tj t j ddd\}}tj ||ddS)NF)typeZhandled)Zclient_optionsZ mechanism)hint) rrrDrrVrzZset_userr rWoptionsr'r2Z capture_event)excrdrvZ user_infor|rurr,r,r-rCs  rC)ArVZsentry_sdk.constsrZsentry_sdk.integrationsrrZsentry_sdk.integrations.asgirZsentry_sdk.scoperZsentry_sdk.tracingrrZsentry_sdk.utilsr r r r(r r rZstarlite.handlers.baserZstarlite.middlewarerZstarlite.plugins.baserZstarlite.routes.httprZstarlite.utilsrrrZpydanticr ImportErrortypingrrrrZstarlite.typesrrrrrr r!r"Z StarliteScoper#r$r%Zsentry_sdk._typesr&rtr'r5r)r*rLr+rrCr,r,r,r-s@        0    "I@