U 췀g@sddlZddlZddlmZddlmZddlmZmZm Z ddl m Z ddl m Z ddlmZmZddlmZdd lmZerdd lmZmZmZmZmZdd lmZmZGd d d eZddZGdddeZddZ GdddZ!dS)N) Integration)request_body_within_bounds)AnnotatedValuecapture_internal_exceptionsevent_from_exception)Broker)Message) Middlewaredefault_middleware)Retry) TYPE_CHECKING)AnyCallableDictOptionalUnion)EventHintc@s eZdZdZdZeddZdS)DramatiqIntegrationa? Dramatiq integration for Sentry Please make sure that you call `sentry_sdk.init` *before* initializing your broker, as it monkey patches `Broker.__init__`. This integration was originally developed and maintained by https://github.com/jacobsvante and later donated to the Sentry project. dramatiqcCs tdSN)_patch_dramatiq_brokerrrN/opt/hc_python/lib/python3.8/site-packages/sentry_sdk/integrations/dramatiq.py setup_once&szDramatiqIntegration.setup_onceN)__name__ __module__ __qualname____doc__ identifier staticmethodrrrrrrs rcstjfdd}|t_dS)Ncstt}z|d}Wn2tk rNt|dkrF|d}g}nd}YnX|dkrltddtD}nt|}|dk rdd|D}| dt ||d<|f||dS)N middlewarercss|] }|VqdSrr.0mrrr AszP_patch_dramatiq_broker..sentry_patched_broker__init__..cSsg|]}t|ts|qSr) isinstanceSentryMiddlewarer#rrr Fs zQ_patch_dramatiq_broker..sentry_patched_broker__init__..) sentry_sdk get_clientget_integrationrpopKeyErrorlenlistr insertr()selfargskw integrationr!Zoriginal_broker__init__rrsentry_patched_broker__init__0s   z=_patch_dramatiq_broker..sentry_patched_broker__init__)r__init__)r7rr6rr,s rc@s(eZdZdZddZdddddZdS)r(z A Dramatiq middleware that automatically captures and sends exceptions to Sentry. This is automatically added to every instantiated broker via the DramatiqIntegration. cCs`tt}|dkrdSt|_|jt}|j|_ | d|j | t ||dS)NZdramatiq_message_id)r*r+r,rZ new_scope_scope_manager __enter__Zget_current_scope actor_nameZ transactionZ set_extraZ message_idZadd_event_processor_make_message_event_processor)r2brokermessager5scoperrrbefore_process_messageXs  z'SentryMiddleware.before_process_messageN)result exceptionc Cstt}|dkrdS||j}|jdp<|jd}zR|dk r|rVt ||st |t st |tjtj ddd\}} tj|| dW5|j dddXdS)NthrowsF)typeZhandled)Zclient_optionsZ mechanism)hint)r*r+r,rZ get_actorr;optionsgetr9__exit__r'r rrZ capture_event) r2r=r>rArBr5ZactorrCeventrErrrafter_process_messagefs.  z&SentryMiddleware.after_process_message)rrrrr@rJrrrrr(Osr(csfdd}|S)Nc s$tt|W5QRX|Sr)rDramatiqMessageExtractorextract_into_event)rIrEr>rrinnersz,_make_message_event_processor..innerr)r>r5rNrrMrr<s r<c@s$eZdZddZddZddZdS)rKcCst||_dSr)dictasdict message_data)r2r>rrrr8sz!DramatiqMessageExtractor.__init__cCstt|jSr)r/jsondumpsrQ)r2rrrcontent_lengthsz'DramatiqMessageExtractor.content_lengthcCsbt}|sdS|di}|di}d|d<d}t||sPt}n|j}||d<dS)NcontextsrrDdata) r*r+Z is_active setdefaultrrTrZremoved_because_over_size_limitrQ)r2rIclientrUZ request_inforVrrrrLs   z+DramatiqMessageExtractor.extract_into_eventN)rrrr8rTrLrrrrrKsrK)"rRr*Zsentry_sdk.integrationsrZ$sentry_sdk.integrations._wsgi_commonrZsentry_sdk.utilsrrrZdramatiq.brokerrZdramatiq.messagerZdramatiq.middlewarer r Zdramatiq.errorsr typingr r rrrrZsentry_sdk._typesrrrrr(r<rKrrrrs"      #3