o
    h                     @   s   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rVd d
lmZmZmZmZmZ d dlmZmZ G dd deZdd ZG dd deZdd Z G dd dZ!dS )    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                   @   s    e Zd ZdZdZed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.
    dramatiqc                   C   s
   t   d S N)_patch_dramatiq_broker r   r   b/var/www/html/pro-man-master/venv/lib/python3.10/site-packages/sentry_sdk/integrations/dramatiq.py
setup_once&   s   
zDramatiqIntegration.setup_onceN)__name__
__module____qualname____doc__
identifierstaticmethodr   r   r   r   r   r      s
    r   c                     s   t j  fdd} | t _d S )Nc                    s   t  t}z|d}W n ty&   t|dkr"|d }g }nd }Y nw |d u r5tdd tD }nt|}|d urKdd |D }|	dt
  ||d<  | g|R i | d S )N
middleware   r   c                 s   s    | ]}| V  qd S r   r   .0mr   r   r   	<genexpr>A   s    zP_patch_dramatiq_broker.<locals>.sentry_patched_broker__init__.<locals>.<genexpr>c                 S   s   g | ]	}t |ts|qS r   )
isinstanceSentryMiddlewarer#   r   r   r   
<listcomp>F   s    zQ_patch_dramatiq_broker.<locals>.sentry_patched_broker__init__.<locals>.<listcomp>)
sentry_sdk
get_clientget_integrationr   popKeyErrorlenlistr
   insertr(   )selfargskwintegrationr!   original_broker__init__r   r   sentry_patched_broker__init__0   s$   
z=_patch_dramatiq_broker.<locals>.sentry_patched_broker__init__)r   __init__)r8   r   r6   r   r   ,   s   
r   c                   @   s(   e Zd ZdZdd Zd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.
    c                 C   sd   t  t}|d u rd S t  |_|j  t  }||j	 |
d|j |t|| d S )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)r2   brokermessager5   scoper   r   r   before_process_messageX   s   

z'SentryMiddleware.before_process_messageN)result	exceptionc          
      C   s   t  t}|d u rd S ||j}|jdp|jd}zS|d urR|r+t||sht|t	s]t
|t  jtjddd\}}	t j||	d W |jd d d  d S W |jd d d  d S W |jd d d  d S W |jd d d  d S |jd d d  w )NthrowsF)typehandled)client_options	mechanism)hint)r*   r+   r,   r   	get_actorr@   optionsgetr'   r   r   r   capture_eventr<   __exit__)
r2   rE   rF   rI   rJ   r5   actorrK   eventrP   r   r   r   after_process_messagef   s8   
&z&SentryMiddleware.after_process_message)r   r   r   r   rH   rX   r   r   r   r   r(   O   s    r(   c                    s    fdd}|S )Nc                    s:   t   t |  W d    | S 1 sw   Y  | S r   )r   DramatiqMessageExtractorextract_into_event)rW   rP   rF   r   r   inner   s   
z,_make_message_event_processor.<locals>.innerr   )rF   r5   r\   r   r[   r   rD      s   rD   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )rY   c                 C   s   t | | _d S r   )dictasdictmessage_data)r2   rF   r   r   r   r9      s   z!DramatiqMessageExtractor.__init__c                 C   s   t t| jS r   )r/   jsondumpsr_   )r2   r   r   r   content_length   s   z'DramatiqMessageExtractor.content_lengthc                 C   sb   t  }| s
d S |di }|di }d|d< d }t||  s(t }n| j}||d< d S )Ncontextsr   rL   data)	r*   r+   	is_active
setdefaultr   rb   r   removed_because_over_size_limitr_   )r2   rW   clientrc   request_inford   r   r   r   rZ      s   
z+DramatiqMessageExtractor.extract_into_eventN)r   r   r   r9   rb   rZ   r   r   r   r   rY      s    rY   )"r`   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   r(   rD   rY   r   r   r   r   <module>   s$    #3