
    i                     x   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! 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      Z0y# e"$ r	  ed      w xY w)    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                   .    e Zd ZdZdZde 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.
    dramatiqzauto.queue.c                      t                y N)_patch_dramatiq_broker     /var/www/html/hwPaymentPortal-be-dev/hw-payment-portal-api/venv/lib/python3.12/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!   %   s,    	 J:,'F! !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>P   s     >aac>s   )
sentry_sdk
get_clientget_integrationr!   popKeyErrorlenlistr   
isinstanceSentryMiddlewareinsert)selfargskwintegrationr3   r7   original_broker__init__s         r(   sentry_patched_broker__init__z=_patch_dramatiq_broker.<locals>.sentry_patched_broker__init__?   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__)rH   rG   s    @r(   r%   r%   ;   s    $oo38 4FOr'   c                   .    e Zd ZdZdZd Zd ZddddZy)rA   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                         j                  t              }|y t        t               t        t               i|j                  | j                  <   y r$   )
r9   r:   r;   r!   r   r   r   r   optionsSENTRY_HEADERS_NAME)rC   brokermessagedelayrF   s        r(   before_enqueuezSentryMiddleware.before_enqueuei   sL     ++-==>QR  $o&75
001r'   c                 "   t        j                         j                  t              }|y t        j                         |_        |j
                  j                         }|j                          |j                  d|j                         |j                  t        ||             |j                  j                  | j                        xs i }d|j                  v ri }t        ||j                   t"        j$                  t&        j(                  t        j*                        }|j-                  t.        j0                         t        j2                  ||j                   t"        j$                  t&        j(                         |j                          y )Ndramatiq_message_idretries)nameopsourcer/   )rV   rW   rX   )r9   r:   r;   r!   isolation_scope_scope_manager	__enter__clear_breadcrumbs	set_extra
message_idadd_event_processor_make_message_event_processorrM   getrN   r   
actor_namer   QUEUE_TASK_DRAMATIQr   TASKr/   
set_statusr   OKstart_transaction)rC   rO   rP   rF   scopesentry_headerstransactions          r(   before_process_messagez'SentryMiddleware.before_process_messaget   s0    ++-==>QR!+!;!;!=&&002!-w/A/AB!!"?"UV ,,T-E-EFL"'N$##%%$))&--
 	z}}-$$##%%$))		
 	r'   N)result	exceptionc                   t        j                         j                  t              }|y |j	                  |j
                        }|j                  j                  d      xs |j                  j                  d      }|j                  }t        j                         j                  }	|	sy |d uxr$ |xr t        ||       xr t        |t               }
|
s'|	j                  d d d        |j                  d d d        y t        |t        j                         j                  t        j                  dd      \  }}t        j                   ||       |	j                  t#        |      |d        |j                  t#        |      |d        y )NthrowsF)typehandled)client_options	mechanism)hint)r9   r:   r;   r!   	get_actorrb   rM   ra   rZ   get_current_scoperj   r@   r   __exit__r   r.   capture_eventrp   )rC   rO   rP   rl   rm   rF   actorro   scope_managerrj   is_event_capture_requiredeventrt   s                r(   after_process_messagez&SentryMiddleware.after_process_message   s^    ++-==>QR  !3!34$$X.M%--2C2CH2M.. 224@@ T! 1=
9f =>1y%00 	"
 )  tT2""4t4*%002::+66 
t 	  T2T)_i>tI	4@r'   )r*   r+   r,   r-   rN   rR   rk   r}   r&   r'   r(   rA   rA   ^   s)     ,	
 B @Dt $Ar'   rA   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|   rt   rP   s     r(   innerz,_make_message_event_processor.<locals>.inner   s=    (* 	H$W-@@G	H 	H s   1;r&   )rP   rF   r   s   `  r(   r`   r`      s     Lr'   c                       e Zd Zd Zd Zd Zy)r   c                 @    t        |j                               | _        y r$   )dictasdictmessage_data)rC   rP   s     r(   rI   z!DramatiqMessageExtractor.__init__   s     !12r'   c                 R    t        t        j                  | j                              S r$   )r>   jsondumpsr   )rC   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"   rp   data)	r9   r:   	is_active
setdefaultr
   r   r   removed_because_over_size_limitr   )rC   r|   clientr   request_infor   s         r(   r   z+DramatiqMessageExtractor.extract_into_event   s    &&(!##J3**:r:)V)&$2E2E2GH!AACD$$D#Vr'   N)r*   r+   r,   rI   r   r   r&   r'   r(   r   r      s    32$r'   r   )1r   r9   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%   rA   r`   r   r&   r'   r(   <module>r      s      , G G = K 
 
 CL4&B%( !;;-!+ !, 4F[Az [A|
$ $]  4
2
334s   B+ +B9