
    lh                         d dl Z d dlZd dlmZ d dlmZ d dlmZmZm	Z	 d dl
mZ d dlmZ d dlmZmZ d dlmZ d d	lmZ erd d
lmZmZmZmZmZ d dlmZmZ  G d de      Zd Z G d de      Zd Z  G d d      Z!y)    N)Integration)request_body_within_bounds)AnnotatedValuecapture_internal_exceptionsevent_from_exception)Broker)Message)
Middlewaredefault_middleware)Retry)TYPE_CHECKING)AnyCallableDictOptionalUnion)EventHintc                   $    e Zd ZdZdZed        Zy)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.
    dramatiqc                      t                y N)_patch_dramatiq_broker     r/var/www/pru.catia.catastroantioquia-mas.com/tasa/lib/python3.12/site-packages/sentry_sdk/integrations/dramatiq.py
setup_oncezDramatiqIntegration.setup_once&   s
     	 r   N)__name__
__module____qualname____doc__
identifierstaticmethodr   r   r   r   r   r      s     	 J! !r   r   c                  F    t         j                  fd} | t         _        y )Nc                    t        j                         j                  t              }	 |j	                  d      }|t        d t        D              }nt        |      }|:|D cg c]  }t        |t              r| }}|j                  dt                      ||d<    | g|i | y # t
        $ r t        |      dk(  r|d   }g }nd }Y w xY wc c}w )N
middleware   r   c              3   *   K   | ]  } |         y wr   r   ).0ms     r   	<genexpr>zP_patch_dramatiq_broker.<locals>.sentry_patched_broker__init__.<locals>.<genexpr>A   s     >aac>s   )
sentry_sdk
get_clientget_integrationr   popKeyErrorlenlistr   
isinstanceSentryMiddlewareinsert)selfargskwintegrationr'   r+   original_broker__init__s         r   sentry_patched_broker__init__z=_patch_dramatiq_broker.<locals>.sentry_patched_broker__init__0   s     ++-==>QR
	"-J >+=>>Jj)J"%/Wz!EU7V!WJWa!1!34%<2t2r2)  	" 4yA~!!W
!
	"  Xs   B- %C;C-!CC)r   __init__)r<   r;   s    @r   r   r   ,   s    $oo38 4FOr   c                   $    e Zd ZdZd ZddddZy)r5   z
    A Dramatiq middleware that automatically captures and sends
    exceptions to Sentry.

    This is automatically added to every instantiated broker via the
    DramatiqIntegration.
    c                    t        j                         j                  t              }|y t        j                         |_        |j
                  j                          t        j                         }|j                  |j                         |j                  d|j                         |j                  t        ||             y )Ndramatiq_message_id)r-   r.   r/   r   	new_scope_scope_manager	__enter__get_current_scopeset_transaction_name
actor_name	set_extra
message_idadd_event_processor_make_message_event_processor)r7   brokermessager:   scopes        r   before_process_messagez'SentryMiddleware.before_process_messageX   s     ++-==>QR!+!5!5!7((*,,.""7#5#56-w/A/AB!!"?"UVr   N)result	exceptionc                h   t        j                         j                  t              }|y |j	                  |j
                        }|j                  j                  d      xs |j                  j                  d      }	 |s|rt        ||      set        |t              sUt        |t        j                         j                  t        j                  dd      \  }}	t        j                  ||	       |j                  j                  d d d        y # |j                  j                  d d d        w xY w)NthrowsF)typehandled)client_options	mechanism)hint)r-   r.   r/   r   	get_actorrF   optionsgetr4   r   r   r#   capture_eventrB   __exit__)
r7   rK   rL   rO   rP   r:   actorrR   eventrW   s
             r   after_process_messagez&SentryMiddleware.after_process_messagef   s     ++-==>QR  !3!34$$X.M%--2C2CH2M	>%Jy&$A"9e42#-#8#8#:#B#B 3 > >#(t ((T:""++D$=G""++D$=s   ?A5D D1)r   r    r!   r"   rN   r_   r   r   r   r5   r5   O   s    W @Dt >r   r5   c                       fd}|S )Nc                 |    t               5  t              j                  |        d d d        | S # 1 sw Y   | S xY wr   )r   DramatiqMessageExtractorextract_into_event)r^   rW   rL   s     r   innerz,_make_message_event_processor.<locals>.inner   s=    (* 	H$W-@@G	H 	H s   1;r   )rL   r:   rd   s   `  r   rJ   rJ      s     Lr   c                       e Zd Zd Zd Zd Zy)rb   c                 @    t        |j                               | _        y r   )dictasdictmessage_data)r7   rL   s     r   r=   z!DramatiqMessageExtractor.__init__   s     !12r   c                 R    t        t        j                  | j                              S r   )r2   jsondumpsri   )r7   s    r   content_lengthz'DramatiqMessageExtractor.content_length   s    4::d//011r   c                 $   t        j                         }|j                         sy |j                  di       }|j                  di       }d|d<   d }t	        || j                               st        j                         }n| j                  }||d<   y )Ncontextsr   rS   data)	r-   r.   	is_active
setdefaultr   rm   r   removed_because_over_size_limitri   )r7   r^   clientro   request_inforp   s         r   rc   z+DramatiqMessageExtractor.extract_into_event   s    &&(!##J3**:r:)V)&$2E2E2GH!AACD$$D#Vr   N)r   r    r!   r=   rm   rc   r   r   r   rb   rb      s    32$r   rb   )"rk   r-   sentry_sdk.integrationsr   $sentry_sdk.integrations._wsgi_commonr   sentry_sdk.utilsr   r   r   dramatiq.brokerr   dramatiq.messager	   dramatiq.middlewarer
   r   dramatiq.errorsr   typingr   r   r   r   r   r   sentry_sdk._typesr   r   r   r   r5   rJ   rb   r   r   r   <module>r      sf      / K  # $ > !  ;;-!+ !( 4F0>z 0>f
$ $r   