o
    :lij                     @   s  d dl m Z  d dlmZ d dlmZ 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mZ d dlmZmZmZ d d	lmZ d d
lmZmZ d dlmZ erd dlmZ d dlmZ d dlmZ d dlmZ d dlm Z  d dlm!Z! d dlm"Z" d dlm#Z# d dlm$Z$ d dlm%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+ d dl,m-Z- d dlm.Z. d dl/m0Z0 e$de"def dZ1e$dZ2g Z3dd Z4dd  Z5d!d" Z6G d#d$ d$e7Z8dS )%    )copy)deque)chainN)
Attachmentwraps)Baggageextract_sentrytrace_datahas_tracing_enablednormalize_incoming_data)BAGGAGE_HEADER_NAMESENTRY_TRACE_HEADER_NAMETransaction)TYPE_CHECKING)loggercapture_internal_exceptions)FALSE_VALUES)Any)Dict)Iterator)Optional)Deque)List)Callable)Tuple)TypeVar)
BreadcrumbEventEventProcessorErrorProcessorExcInfoHintType)Profile)Span)SessionF.)boundTc                 C   s   t |  d S N)global_event_processorsappend)	processor r-   `/var/www/html/tatsat2dev/dashboard-backend/venv/lib/python3.10/site-packages/sentry_sdk/scope.pyadd_global_event_processor:      r/   c                 C   s   t | | jdS )N)fsetdoc)property__doc__fnr-   r-   r.   _attr_setter?   r0   r7   c                    s   t   fdd}|S )Nc                    s8   | j sd S zd| _  | g|R i |W d| _ S d| _ w )NFT)_should_capture)selfargskwargsr5   r-   r.   wrapperF   s   z!_disable_capture.<locals>.wrapperr   )r6   r<   r-   r5   r.   _disable_captureD   s   
r=   c                   @   s  e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dbddZ
dd Zdd Zdd Zdd Zdd Zdd Zedd Zdd Zed d! Zed"d# Zejd$d# Zdbd%d&Zed'd( Zd)d* Zed+d, Zejd-d, Zed.d/ Zejd0d/ Zd1d2 Zd3d4 Zd5d6 Zd7d8 Z d9d: Z!d;d< Z"d=d> Z#					?dcd@dAZ$dBdC Z%	dbdDdEZ&dFdG Z'dHdI Z(dJdK Z)dLdM Z*dNdO Z+dPdQ Z,dRdS Z-dTdU Z.dVdW Z/e0	dbdXdYZ1dZd[ Z2						ddd\d]Z3d^d_ Z4d`da Z5dS )eScopezaThe scope holds extra information that should be sent with all
    events that belong to it.
    )_level_name_fingerprint_transaction_transaction_info_user_tags	_contexts_extras_breadcrumbs_event_processors_error_processorsr8   _span_session_attachments_force_auto_session_tracking_profile_propagation_contextc                 C   s8   g | _ g | _d | _d | _|   |  }| j|d d S )N)incoming_data)rI   rJ   r@   rP   clear_load_trace_data_from_envgenerate_propagation_context)r9   incoming_trace_informationr-   r-   r.   __init__w   s   zScope.__init__c                 C   sp   d}t jdp	d }|tv}|r4i }t jdr$t jdp!d|t< t jdr4t jdp1d|t< |p7dS )z
        Load Sentry trace id and baggage from environment variables.
        Can be disabled by setting SENTRY_USE_ENVIRONMENT to "false".
        NSENTRY_USE_ENVIRONMENT SENTRY_TRACESENTRY_BAGGAGE)osenvirongetlowerr   r   r   )r9   rU   sentry_use_environmentuse_environmentr-   r-   r.   rS      s   zScope._load_trace_data_from_envc                 C   s   i }t |}|t}|rt| |d< |t}|r*t|}|d ur*|| d|v o1d|v}|r;|| 	  |rO|dsMt
 jdd  |d< |S d S )Ndynamic_sampling_contexttrace_idspan_id   )r   r]   r   r   from_incoming_headerra   r   r	   update_create_new_propagation_contextuuiduuid4hex)r9   datacontextnormalized_databaggage_headersentry_trace_headersentrytrace_dataonly_baggage_no_sentry_tracer-   r-   r.   _extract_propagation_context   s.   



z"Scope._extract_propagation_contextc                 C   s"   t  jt  jdd  d d dS )Nrd   rb   rc   parent_span_idra   )rh   ri   rj   r9   r-   r-   r.   rg      s
   z%Scope._create_new_propagation_contextc                 C   s   |   | _td| j dS )zt
        Creates a new propagation context and sets it as `_propagation_context`. Overwriting existing one.
        z,[Tracing] Create new propagation context: %sN)rg   rP   r   debugru   r-   r-   r.   set_new_propagation_context   s
   
z!Scope.set_new_propagation_contextNc                 C   sD   |r|  |}|dur|| _td| j | jdu r |   dS dS )z
        Makes sure `_propagation_context` is set.
        If there is `incoming_data` overwrite existing `_propagation_context`.
        if there is no `incoming_data` create new `_propagation_context`, but do NOT overwrite if already existing.
        Nz>[Tracing] Extracted propagation context from incoming data: %s)rr   rP   r   rv   rw   )r9   rQ   rl   r-   r-   r.   rT      s   

z"Scope.generate_propagation_contextc                 C   s6   | j du rdS |  }|dur| | j d< | j d S )z
        Returns the Dynamic Sampling Context from the Propagation Context.
        If not existing, creates a new one.
        Nra   )rP   get_baggagera   )r9   baggager-   r-   r.   get_dynamic_sampling_context   s   

z"Scope.get_dynamic_sampling_contextc                 C   s*   | j du rdS d| j d | j d f }|S )zn
        Returns the Sentry "sentry-trace" header (aka the traceparent) from the Propagation Context.
        Nz%s-%srb   rc   )rP   )r9   traceparentr-   r-   r.   get_traceparent   s   
zScope.get_traceparentc                 C   s4   | j d u rd S | j d}|d u rt| S t|S )Nra   )rP   r]   r   from_options)r9   ra   r-   r-   r.   rx     s   

zScope.get_baggagec                 C   s6   | j du rdS | j d | j d | j d |  d}|S )zR
        Returns the Sentry "trace" context from the Propagation Context.
        Nrb   rc   rt   rs   )rP   rz   )r9   trace_contextr-   r-   r.   get_trace_context  s   
zScope.get_trace_contextc                 c   sX    | j dur(|  }|durt|fV  |  }|dur*t| }t|fV  dS dS dS )zz
        Creates a generator which returns the `sentry-trace` and `baggage` headers from the Propagation Context.
        N)rP   r|   r   rz   r   	serializer   )r9   r{   dscry   r-   r-   r.   iter_headers#  s   

zScope.iter_headersc                 C   sf   d| _ d| _d| _i | _d| _i | _i | _i | _g | _| 	  d| _
d| _d| _d| _d| _d| _dS )zClears the entire scope.NT)r?   rA   rB   rC   rD   rE   rF   rG   rM   clear_breadcrumbsr8   rK   rL   rN   rO   rP   ru   r-   r-   r.   rR   2  s    
zScope.clearc                 C   
   || _ dS )zDWhen set this overrides the level. Deprecated in favor of set_level.Nr?   r9   valuer-   r-   r.   levelK     
zScope.levelc                 C   r   )zSets the level for the scope.Nr   r   r-   r-   r.   	set_levelQ  s   
zScope.set_levelc                 C   r   )z0When set this overrides the default fingerprint.N)rA   r   r-   r-   r.   fingerprintV  r   zScope.fingerprintc                 C   s&   | j du rdS | j jdu rdS | j jS )z8Return the transaction (root span) in the scope, if any.N)rK   containing_transactionru   r-   r-   r.   transaction\  s
   
zScope.transactionc                 C   s4   t d || _| jr| jjr|| jj_dS dS dS )zrWhen set this forces a specific transaction name to be set.

        Deprecated: use set_transaction_name instead.z`Assigning to scope.transaction directly is deprecated: use scope.set_transaction_name() instead.N)r   warningrB   rK   r   namer   r-   r-   r.   r   n  s   c                 C   sB   || _ | jr| jjr|| jj_|r|| jj_|r|| jd< dS dS )z?Set the transaction name and optionally the transaction source.sourceN)rB   rK   r   r   r   rC   )r9   r   r   r-   r-   r.   set_transaction_name  s   

zScope.set_transaction_namec                 C   s   |  | dS )zPWhen set a specific user is bound to the scope. Deprecated in favor of set_user.N)set_userr   r-   r-   r.   user  s   z
Scope.userc                 C   s&   || _ | jdur| jj|d dS dS )zSets a user for the scope.N)r   )rD   rL   rf   r   r-   r-   r.   r     s   
zScope.set_userc                 C      | j S )z,Get/set current tracing span or transaction.)rK   ru   r-   r-   r.   span  s   z
Scope.spanc                 C   sD   || _ t|tr|}|jr|j| _|jr |j| jd< d S d S d S d S )Nr   )rK   
isinstancer   r   rB   r   rC   )r9   r   r   r-   r-   r.   r     s   
c                 C   r   r)   rO   ru   r-   r-   r.   profile  s   zScope.profilec                 C   s
   || _ d S r)   r   )r9   r   r-   r-   r.   r     r   c                 C      || j |< dS )z)Sets a tag for a key to a specific value.N)rE   r9   keyr   r-   r-   r.   set_tag     zScope.set_tagc                 C      | j |d dS )zRemoves a specific tag.N)rE   popr9   r   r-   r-   r.   
remove_tag     zScope.remove_tagc                 C   r   )z5Binds a context at a certain key to a specific value.N)rF   r   r-   r-   r.   set_context  r   zScope.set_contextc                 C   r   )zRemoves a context.N)rF   r   r   r-   r-   r.   remove_context  r   zScope.remove_contextc                 C   r   )z&Sets an extra key to a specific value.N)rG   r   r-   r-   r.   	set_extra  r   zScope.set_extrac                 C   r   )zRemoves a specific extra key.N)rG   r   r   r-   r-   r.   remove_extra  r   zScope.remove_extrac                 C   s   t  | _dS )zClears breadcrumb buffer.N)r   rH   ru   r-   r-   r.   r     s   zScope.clear_breadcrumbsFc              	   C   s   | j t|||||d dS )z)Adds an attachment to future events sent.)bytespathfilenamecontent_typeadd_to_transactionsN)rM   r+   r   )r9   r   r   r   r   r   r-   r-   r.   add_attachment  s   
zScope.add_attachmentc                 C   s8   t | jdkrtd| j | jdd= | j| dS )z}Register a scope local event processor on the scope.

        :param func: This function behaves like `before_send.`
           zLToo many event processors on scope! Clearing list to free up some memory: %rN)lenrI   r   r   r+   )r9   funcr-   r-   r.   add_event_processor  s   zScope.add_event_processorc                    s.   |dur| | fdd}| j | dS )a  Register a scope local error processor on the scope.

        :param func: A callback that works similar to an event processor but is invoked with the original exception info triple as second argument.

        :param cls: Optionally, only process exceptions of this type.
        Nc                    s<   z	t |d  }W n ty   d}Y nw |r| |S | S )N   F)r   	Exception)eventexc_infois_instcls_	real_funcr-   r.   r   )  s   
z'Scope.add_error_processor.<locals>.func)rJ   r+   )r9   r   clsr-   r   r.   add_error_processor  s
   
zScope.add_error_processorc                 C   s   | j d ur| j |d< d S d S )Nr   r   r9   r   hintoptionsr-   r-   r.   _apply_level_to_event5  s   
zScope._apply_level_to_eventc                 C   s    | di  dg | j d S )Nbreadcrumbsvalues)
setdefaultextendrH   r   r-   r-   r.   _apply_breadcrumbs_to_event:  s   z!Scope._apply_breadcrumbs_to_eventc                 C   .   | dd u r| jd ur| j|d< d S d S d S )Nr   )r]   rD   r   r-   r-   r.   _apply_user_to_event@     zScope._apply_user_to_eventc                 C   r   )Nr   )r]   rB   r   r-   r-   r.    _apply_transaction_name_to_eventE  r   z&Scope._apply_transaction_name_to_eventc                 C   r   )Ntransaction_info)r]   rC   r   r-   r-   r.    _apply_transaction_info_to_eventJ  r   z&Scope._apply_transaction_info_to_eventc                 C   r   )Nr   )r]   rA   r   r-   r-   r.   _apply_fingerprint_to_eventO  r   z!Scope._apply_fingerprint_to_eventc                 C   "   | j r|di | j  d S d S )Nextra)rG   r   rf   r   r-   r-   r.   _apply_extra_to_eventT     zScope._apply_extra_to_eventc                 C   r   )Ntags)rE   r   rf   r   r-   r-   r.   _apply_tags_to_eventY  r   zScope._apply_tags_to_eventc              	   C   s   | j r|di | j  |di }|dd u r1t|r+| jd ur+| j |d< n|  |d< z
|d d d }W n ttfyH   d }Y nw |d urUd|i|d< d S d S )Ncontextstracera   	replay_idreplay)	rF   r   rf   r]   r
   rK   r   KeyError	TypeError)r9   r   r   r   r   r   r-   r-   r.   _apply_contexts_to_event^  s    zScope._apply_contexts_to_eventc              	   C   s  | d}|dk}|dk}| dpg }| jD ]}|r|jr#|| q||d< | ||| |r?d|di  di i|d< |sr| ||| | ||| | ||| | 	||| | 
||| | ||| | ||| |s}|s}| ||| dd }	| d	}
|
d
ur| jD ]}|||
}|d
u r|	|d  S |}q|stt| jD ])}|}t  |||}W d
   n1 sw   Y  |d
u r|	|d  S |}q|S )zBApplies the information contained on the scope to the given event.typer   check_inattachmentsr   r   c                 S   s   t d||  d S )Nz%s (%s) dropped event)r   info)causetyr-   r-   r.   _drop  s   z#Scope.apply_to_event.<locals>._dropr   Nzerror processorzevent processor)r]   rM   r   r+   r   r   r   r   r   r   r   r   r   r   rJ   r   r*   rI   r   )r9   r   r   r   r   is_transactionis_check_inattachments_to_send
attachmentr   r   error_processor	new_eventevent_processorr-   r-   r.   apply_to_eventw  sV   
	





zScope.apply_to_eventc                 C   s   |j dur	|j | _ |jdur|j| _|jdur|j| _|jdur'| j|j |jdur0|j| _|jr:| j|j |jrD| j|j |jrN| j|j |j	rX| j	
|j	 |jr_|j| _|jri| j
|j |jrp|j| _|jry|j| _dS dS )z+Update the scope with another scope's data.N)r?   rA   rB   rC   rf   rD   rE   rF   rG   rH   r   rK   rM   rO   rP   )r9   scoper-   r-   r.   update_from_scope  s6   




zScope.update_from_scopec                 C   sn   |dur|| _ |dur|| _|dur| j| |dur"| j| |dur,| j| |dur5|| _dS dS )zUpdate the scope's attributes.N)r?   rD   rG   rf   rF   rE   rA   )r9   r   r   extrasr   r   r   r-   r-   r.   update_from_kwargs  s   
zScope.update_from_kwargsc                 C   s   t | j}| j|_| j|_| j|_| j|_t| j|_| j	|_	t| j
|_
t| j|_t| j|_t| j|_t| j|_t| j|_| j|_| j|_| j|_| j|_| j|_t| j|_| j|_|S r)   )object__new__	__class__r?   r@   rA   rB   dictrC   rD   rE   rF   rG   r   rH   listrI   rJ   rP   r8   rK   rL   rN   rM   rO   )r9   rvr-   r-   r.   __copy__  s*   zScope.__copy__c                 C   s   d| j jtt| | jf S )Nz<%s id=%s name=%s>)r   __name__rj   idr@   ru   r-   r-   r.   __repr__  s
   
zScope.__repr__r)   )NNNNF)NNNNNN)6r   
__module____qualname__r4   	__slots__rV   rS   rr   rg   rw   rT   rz   r|   rx   r   r   rR   r7   r   r   r   r3   r   setterr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r=   r   r   r   r   r   r-   r-   r-   r.   r>   T   s    
	










			

F 
r>   )9r   collectionsr   	itertoolsr   r[   rh   sentry_sdk.attachmentsr   sentry_sdk._functoolsr   sentry_sdk.tracing_utilsr   r	   r
   r   sentry_sdk.tracingr   r   r   sentry_sdk._typesr   sentry_sdk.utilsr   r   sentry_sdk.constsr   typingr   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   sentry_sdk.profilerr#   r$   sentry_sdk.sessionr%   r&   r(   r*   r/   r7   r=   r   r>   r-   r-   r-   r.   <module>   sB    $
