
    fi                        d dl Z d dlZd dlmZmZ d dlmZmZmZ d dl	m
Z
mZ d dlmZ d dlmZmZmZ d dlmZmZmZ d dlmZ  ed	          Z	 d d
lmZ d dlmZmZ d dlmZ d dl m!Z! n# e"$ r  ed          w xY w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          Z0dS )    N)OP
SPANSTATUS)continue_traceget_baggageget_traceparent)IntegrationDidNotEnable)request_body_within_bounds)BAGGAGE_HEADER_NAMESENTRY_TRACE_HEADER_NAMETransactionSource)AnnotatedValuecapture_internal_exceptionsevent_from_exception)TypeVarR)Broker)
Middlewaredefault_middleware)Retry)MessagezDramatiq is not installed)TYPE_CHECKING)AnyCallableDictOptionalUnion)EventHintc                   6    e Zd ZdZdZde Zed             ZdS )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.
    dramatiqzauto.queue.c                  "    t                       d S N)_patch_dramatiq_broker     p/var/www/html/Pagina-Ingenieria-Requisitos/venv/lib/python3.11/site-packages/sentry_sdk/integrations/dramatiq.py
setup_oncezDramatiqIntegration.setup_once4   s     	     r'   N)__name__
__module____qualname____doc__
identifieroriginstaticmethodr)   r&   r'   r(   r!   r!   %   sK        	 	 J':''F! ! \! ! !r'   r!   c                  B    t           j        fd} | t           _        d S )Nc                    t          j                                        t                    }	 |                    d          }n0# t
          $ r# t          |          dk    r|d         }g }nd }Y nw xY w|t          d t          D                       }nt          |          }|.d |D             }|	                    dt                                 ||d<    | g|R i | d S )N
middleware   r   c              3   *   K   | ]} |            V  d S r$   r&   .0ms     r(   	<genexpr>zP_patch_dramatiq_broker.<locals>.sentry_patched_broker__init__.<locals>.<genexpr>P   s(      >>aaacc>>>>>>r'   c                 <    g | ]}t          |t                    |S r&   )
isinstanceSentryMiddlewarer6   s     r(   
<listcomp>zQ_patch_dramatiq_broker.<locals>.sentry_patched_broker__init__.<locals>.<listcomp>U   s(    WWWz!EU7V7VW!WWWr'   )
sentry_sdk
get_clientget_integrationr!   popKeyErrorlenlistr   insertr<   )selfargskwintegrationr3   original_broker__init__s        r(   sentry_patched_broker__init__z=_patch_dramatiq_broker.<locals>.sentry_patched_broker__init__?   s    +--==>QRR
	"--JJ 	" 	" 	" 4yyA~~!!W
!
	" >>+=>>>>>JJj))J"WWZWWWJa!1!3!3444%<2t222r22222s   A *A10A1)r   __init__)rK   rJ   s    @r(   r%   r%   ;   s1    $o3 3 3 3 38 4FOOOr'   c                   0    e Zd ZdZdZd Zd ZddddZdS )r<   z
    A Dramatiq middleware that automatically captures and sends
    exceptions to Sentry.

    This is automatically added to every instantiated broker via the
    DramatiqIntegration.
    _sentry_headersc                     t          j                                        t                    }|d S t          t                      t          t                      i|j        | j	        <   d S r$   )
r>   r?   r@   r!   r   r   r   r   optionsSENTRY_HEADERS_NAME)rF   brokermessagedelayrI   s        r(   before_enqueuezSentryMiddleware.before_enqueuei   sW     +--==>QRRF  $o&7&75
0111r'   c                    t          j                                        t                    }|d S t          j                    |_        |j                                        }|                                 |                    d|j	                   |
                    t          ||                     |j                            | j                  pi }d|j        v ri }t          ||j        t"          j        t&          j        t          j                  }|                    t.          j                   t          j        ||j        t"          j        t&          j                   |                                 d S )Ndramatiq_message_idretries)nameopsourcer/   )rY   rZ   r[   )r>   r?   r@   r!   isolation_scope_scope_manager	__enter__clear_breadcrumbs	set_extra
message_idadd_event_processor_make_message_event_processorrP   getrQ   r   
actor_namer   QUEUE_TASK_DRAMATIQr   TASKr/   
set_statusr   OKstart_transaction)rF   rR   rS   rI   scopesentry_headerstransactions          r(   before_process_messagez'SentryMiddleware.before_process_messaget   sW    +--==>QRRF!+!;!=!=&0022!!!-w/ABBB!!"?"U"UVVV ,,T-EFFL"''N$#%$)&-
 
 
 	z}---$#%$)		
 	
 	
 	
 	r'   N)result	exceptionc                0   t          j                                        t                    }|d S |                    |j                  }|j                            d          p|j                            d          }|j        }t          j	                    j
        }	|	sd S |d uo(|ot          ||           ot          |t                     }
|
s0|	                    d d d            |                    d d d            d S t          |t          j                    j        t          j        dd          \  }}t          j        ||           |	                    t#          |          |d            |                    t#          |          |d            d S )NthrowsF)typehandled)client_options	mechanism)hint)r>   r?   r@   r!   	get_actorre   rP   rd   r]   get_current_scoperm   r;   r   __exit__r   r.   capture_eventrs   )rF   rR   rS   ro   rp   rI   actorrr   scope_managerrm   is_event_capture_requiredeventrw   s                r(   after_process_messagez&SentryMiddleware.after_process_message   s    +--==>QRRF  !344$$X..M%-2C2CH2M2M. 244@ 	4 T! 1=
9f = =>1y%000 	"
 ) 	  tT222""4t444F*%022:+6  
 
 
t 	 T2222T)__i>>>tI	4@@@@@r'   )r*   r+   r,   r-   rQ   rU   rn   r   r&   r'   r(   r<   r<   ^   sn          ,	
 	
 	
     B @Dt $A $A $A $A $A $A $Ar'   r<   c                       fd}|S )Nc                     t                      5  t                                        |            d d d            n# 1 swxY w Y   | S r$   )r   DramatiqMessageExtractorextract_into_event)r   rw   rS   s     r(   innerz,_make_message_event_processor.<locals>.inner   s    (** 	H 	H$W--@@GGG	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H s   #?AAr&   )rS   rI   r   s   `  r(   rc   rc      s#         Lr'   c                        e Zd Zd Zd Zd ZdS )r   c                 R    t          |                                          | _        d S r$   )dictasdictmessage_data)rF   rS   s     r(   rL   z!DramatiqMessageExtractor.__init__   s"     !1!122r'   c                 N    t          t          j        | j                            S r$   )rC   jsondumpsr   )rF   s    r(   content_lengthz'DramatiqMessageExtractor.content_length   s    4:d/00111r'   c                 B   t          j                    }|                                sd S |                    di           }|                    di           }d|d<   d }t	          ||                                           st          j                    }n| j        }||d<   d S )Ncontextsr"   rs   data)	r>   r?   	is_active
setdefaultr
   r   r   removed_because_over_size_limitr   )rF   r   clientr   request_infor   s         r(   r   z+DramatiqMessageExtractor.extract_into_event   s    &((!! 	F##J33**:r::)V)&$2E2E2G2GHH 	%!ACCDD$D#Vr'   N)r*   r+   r,   rL   r   r   r&   r'   r(   r   r      sA        3 3 32 2 2$ $ $ $ $r'   r   )1r   r>   sentry_sdk.constsr   r   sentry_sdk.apir   r   r   sentry_sdk.integrationsr   r	   $sentry_sdk.integrations._wsgi_commonr
   sentry_sdk.tracingr   r   r   sentry_sdk.utilsr   r   r   typingr   r   dramatiq.brokerr   dramatiq.middlewarer   r   dramatiq.errorsr   dramatiq.messager   ImportErrorr   r   r   r   r   r   sentry_sdk._typesr   r   r!   r%   r<   rc   r   r&   r'   r(   <module>r      s        , , , , , , , , G G G G G G G G G G = = = = = = = = K K K K K K         
         
      GCLL4&&&&&&BBBBBBBB%%%%%%((((((( 4 4 4
,2
3
334 !           .;;;;;;;;;;;;;;--------! ! ! ! !+ ! ! !, 4  4  4F[A [A [A [A [Az [A [A [A|
 
 
$ $ $ $ $ $ $ $ $ $s   A* *A;