a
    xdX                     @   s  d dl Z d dl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 d dlmZ d d	lmZmZmZmZ d d
lmZ er`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  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* d dl+m,Z, e!dZ-ndd Z"edZ.dd Z/d d! Z0G d"d# d#e1Z2d$d% Z3d&d' Z4d d
lmZ erG d(d) d)e,e2Z5n
d*d+  Z5G d,d- d-e6Z7G d.d/ d/e1Z8G d0d1 d1ee7Z9e9 Z:e.;e: dS )2    N)datetime)contextmanager)with_metaclassScope)Client)SpanTransaction)Session)exc_info_from_errorevent_from_exceptionlogger
ContextVar)MYPY)Union)Any)Optional)Tuple)Dict)List)Callable)	Generator)Type)TypeVar)overload)ContextManager)Integration)EventHint
BreadcrumbBreadcrumbHintExcInfo)ClientConstructorTc                 C   s   | S N )xr%   r%   F/var/www/html/Ranjet/env/lib/python3.9/site-packages/sentry_sdk/hub.pyr   1   s    r   Zsentry_current_hubc                 C   sh   |r|rt d|d ur@t| }t|r4|| qd|| n$|r`t| }|jf i | n| }|S )Nzcannot provide scope and kwargs)	TypeErrorcopycallableZupdate_from_scopeZupdate_from_kwargs)baseZscope_changeZscope_kwargsZfinal_scoper%   r%   r'   _update_scope9   s    


r,   c                  C   s   t jj} | sdS | jd S )NFZsend_default_pii)Hubcurrentclientoptions)r/   r%   r%   r'   _should_send_default_piiK   s    r1   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )
_InitGuardc                 C   s
   || _ d S r$   )_client)selfr/   r%   r%   r'   __init__T   s    z_InitGuard.__init__c                 C   s   | S r$   r%   r4   r%   r%   r'   	__enter__X   s    z_InitGuard.__enter__c                 C   s   | j }|d ur|  d S r$   )r3   close)r4   exc_type	exc_valuetbcr%   r%   r'   __exit__\   s    z_InitGuard.__exit__N__name__
__module____qualname__r5   r7   r=   r%   r%   r%   r'   r2   S   s   r2   c                  C   s>   t jd d } | dks| dkr:tddj|   td d S )N   )      )rC      z1sentry-sdk 2.0.0 will drop support for Python %s.z{}.{}zQPlease upgrade to the latest version to continue receiving upgrades and bugfixes.)sysversion_infor   warningformat)versionr%   r%   r'   _check_python_deprecationsc   s    rK   c                  O   s,   t | i |}tj| t  t|}|S )zsInitializes the SDK and optionally integrations.

    This takes the same arguments as the client constructor.
    )r   r-   r.   bind_clientrK   r2   )argskwargsr/   rvr%   r%   r'   _initq   s
    rP   c                   @   s   e Zd ZdS )initN)r?   r@   rA   r%   r%   r%   r'   rQ      s   rQ   c                   C   s   t S r$   )rP   r%   r%   r%   r'   <lambda>       rR   c                   @   s$   e Zd Zedd Zedd ZdS )HubMetac                 C   s(   t d}|du r$tt}t | |S )z(Returns the current instance of the hub.N)_localgetr-   
GLOBAL_HUBset)clsrO   r%   r%   r'   r.      s
    

zHubMeta.currentc                 C   s   t S )z%Returns the main instance of the hub.)rW   )rY   r%   r%   r'   main   s    zHubMeta.mainN)r?   r@   rA   propertyr.   rZ   r%   r%   r%   r'   rT      s   
	rT   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )_ScopeManagerc                 C   s"   || _ t|j| _|jd | _d S N)_hublen_stack_original_len_layer)r4   hubr%   r%   r'   r5      s    z_ScopeManager.__init__c                 C   s   | j d }|d usJ |S )N   )rc   )r4   scoper%   r%   r'   r7      s    
z_ScopeManager.__enter__c                 C   s   t | jj}|| jk r,td| j|  d S || jkrXtd|| j | jj| jd   | jj| jd  }| jj| jd d = |d | jd krtd|d | jd  n |d | jd krd}t| d S )Nz1Scope popped too soon. Popped %s scopes too many.zLeaked %s scopes: %sre   z2Wrong scope found. Meant to pop %s, but popped %s.r   zinit() called inside of pushed scope. This might be entirely legitimate but usually occurs when initializing the SDK inside a request handler or task/job function. Try to initialize the SDK as early as possible instead.)r`   r_   ra   rb   r   errorrH   rc   )r4   r9   r:   r;   Zcurrent_lenZlayerrH   r%   r%   r'   r=      s2    

z_ScopeManager.__exit__Nr>   r%   r%   r%   r'   r\      s   r\   c                   @   s<  e Zd ZdZdZerdZdZd:ddZdd Z	dd Z
d	d
 Zdd Zedd Zedd Zdd Zdd Zd;ddZd<ddZd=ddZdd Zd>ddZd?dd Zd@d!d"ZedAd#d$Zed%d$ ZdBd&d$Zd'd( ZedCd)d*Zed+d* ZdDd,d*ZdEd.d/Zd0d1 Zd2d3 Zd4d5 Z dFd6d7Z!dGd8d9Z"dS )Hr-   zThe hub wraps the concurrency management of the SDK.  Each thread has
    its own hub but the hub might transfer with the flow of execution if
    context vars are available.

    If the hub is used with a with statement it's temporarily activated.
    Nc                 C   s^   t |tr0|}|jd \}}|d u r4t|}n|}|d u rBt }||fg| _d | _g | _d S r]   )
isinstancer-   ra   r)   r   _last_event_id	_old_hubs)r4   Zclient_or_hubrf   rd   r/   Zother_scoper%   r%   r'   r5      s    
zHub.__init__c                 C   s   | j tj t|  | S r$   )rj   appendr-   r.   rU   rX   r6   r%   r%   r'   r7      s    
zHub.__enter__c                 C   s   | j  }t| d S r$   )rj   poprU   rX   )r4   r9   r:   r;   oldr%   r%   r'   r=      s    
zHub.__exit__c                 C   s.   |  | W  d   S 1 s 0    Y  dS )z~Runs a callback in the context of the hub.  Alternatively the
        with statement can be used on the hub directly.
        Nr%   r4   callbackr%   r%   r'   run  s    zHub.runc                 C   sT   t |tr|}n|jdur"|j}ntd| j}|durP|j|}|durP|S dS )a  Returns the integration for this hub by name or class.  If there
        is no client bound or the client does not have that integration
        then `None` is returned.

        If the return value is not `None` the hub is guaranteed to have a
        client attached.
        NzIntegration has no name)rh   str
identifier
ValueErrorr/   ZintegrationsrV   )r4   Zname_or_classZintegration_namer/   rO   r%   r%   r'   get_integration  s    

zHub.get_integrationc                 C   s   | j d d S )z&Returns the current client on the hub.r^   r   ra   r6   r%   r%   r'   r/   *  s    z
Hub.clientc                 C   s   | j d d S )z%Returns the current scope on the hub.r^   re   ru   r6   r%   r%   r'   rf   0  s    z	Hub.scopec                 C   s   | j S )zReturns the last event ID.)ri   r6   r%   r%   r'   last_event_id6  s    zHub.last_event_idc                 C   s    | j d }||d f| j d< dS )zBinds a new client to the hub.r^   re   Nru   )r4   newtopr%   r%   r'   rL   ;  s    
zHub.bind_clientc           	      K   sX   | j d \}}t|||}|durT|ddk}||||}|durP|sP|| _|S dS )zGCaptures an event. Alias of :py:meth:`sentry_sdk.Client.capture_event`.r^   Ntypetransaction)ra   r,   rV   capture_eventri   )	r4   eventhintrf   
scope_argsr/   Z	top_scopeZis_transactionrO   r%   r%   r'   r{   C  s    	zHub.capture_eventc                 K   s6   | j du rdS |du rd}| j||dfd|i|S )zCaptures a message.  The message is just a string.  If no level
        is provided the default level is `info`.

        :returns: An `event_id` if the SDK decided to send the event (see :py:meth:`sentry_sdk.Client.capture_event`).
        Ninfo)messagelevelrf   )r/   r{   )r4   r   r   rf   r~   r%   r%   r'   capture_messageV  s    
zHub.capture_messagec                 K   s~   | j }|du rdS |dur$t|}nt }t||jd\}}z| j|f||d|W S  tyx   | t  Y n0 dS )zCaptures an exception.

        :param error: An exception to catch. If `None`, `sys.exc_info()` will be used.

        :returns: An `event_id` if the SDK decided to send the event (see :py:meth:`sentry_sdk.Client.capture_event`).
        N)Zclient_options)r}   rf   )	r/   r   rF   exc_infor   r0   r{   	Exception_capture_internal_exception)r4   rg   rf   r~   r/   r   r|   r}   r%   r%   r'   capture_exceptionk  s    
zHub.capture_exceptionc                 C   s   t jd|d dS )z
        Capture an exception that is likely caused by a bug in the SDK
        itself.

        These exceptions do not end up in Sentry and are just logged instead.
        zInternal error in sentry_sdk)r   N)r   rg   )r4   r   r%   r%   r'   r     s    
zHub._capture_internal_exceptionc                 K   s   | j d \}}|du r$td dS t|p,d}|| |sBdS t|pJd}|ddu rht |d< |ddu r~d|d< |jd dur|jd ||}n|}|dur|j	
| ntd	| |jd
 }t|j	|kr|j	  qdS )a  
        Adds a breadcrumb.

        :param crumb: Dictionary with the data as the sentry v7/v8 protocol expects.

        :param hint: An optional value that can be used by `before_breadcrumb`
            to customize the breadcrumbs that are emitted.
        r^   Nz*Dropped breadcrumb because no client boundr%   	timestampry   defaultZbefore_breadcrumbz)before breadcrumb dropped breadcrumb (%s)max_breadcrumbs)ra   r   r   dictupdaterV   r   utcnowr0   Z_breadcrumbsrk   r`   popleft)r4   Zcrumbr}   rN   r/   rf   Z	new_crumbr   r%   r%   r'   add_breadcrumb  s,    


zHub.add_breadcrumbc                 K   s   t |tsd|v rdd}t |tr4t| | |S d|v rdt| |d}| jf d|i|S |durp|S |d|  | jj}|dur|j	f i |S t
f i |S )a"  
        Create and start timing a new span whose parent is the currently active
        span or transaction, if any. The return value is a span instance,
        typically used as a context manager to start and stop timing in a `with`
        block.

        Only spans contained in a transaction are sent to Sentry. Most
        integrations start a transaction at the appropriate time, for example
        for every incoming HTTP request. Use `start_transaction` to start a new
        transaction when one is not already in progress.
        rz   zcDeprecated: use start_transaction to start transactions and Transaction.start_child to start spans.nameNrd   )rh   r	   r   rH   start_transactionrl   
setdefaultrf   spanZstart_childr   )r4   r   rN   Zdeprecation_msgr   r%   r%   r'   
start_span  s"    




zHub.start_spanc                 K   s   | di }|du r.|d|  tf i |}| |jd}|| |j|d |jr| jrr| jj	d 
dptd}|j|d	 |S )
a  
        Start and return a transaction.

        Start an existing transaction if given, otherwise create and start a new
        transaction with kwargs.

        This is the entry point to manual tracing instrumentation.

        A tree structure can be built by adding child spans to the transaction,
        and child spans to other spans. To start a new child span within the
        transaction or any span, call the respective `.start_child()` method.

        Every child span must be finished before the transaction is finished,
        otherwise the unfinished spans are discarded.

        When used as context managers, spans and transactions are automatically
        finished at the end of the `with` block. If not using context managers,
        call the `.finish()` method.

        When the transaction is finished, it will be sent to Sentry with all its
        finished child spans.
        custom_sampling_contextNrd   )Ztransaction_contextparent_sampled)sampling_contextZ_experiments	max_spansi  )maxlen)rl   r   r	   to_jsonr   r   Z_set_initial_sampling_decisionZsampledr/   r0   rV   Zinit_span_recorder)r4   rz   rN   r   r   r   r%   r%   r'   r     s     
zHub.start_transactionc                 C   s   d S r$   r%   rn   r%   r%   r'   
push_scope$  s    zHub.push_scopec                 C   s   d S r$   r%   rn   r%   r%   r'   r   +  s    c                 C   sl   |dur<|   }|| W d   n1 s.0    Y  dS | jd \}}|t|f}| j| t| S )a.  
        Pushes a new layer on the scope stack.

        :param callback: If provided, this method pushes a scope, calls
            `callback`, and pops the scope again.

        :returns: If no `callback` is provided, a context manager that should
            be used to pop the scope again.
        Nr^   )r   ra   r)   rk   r\   )r4   ro   rf   r/   Z	new_layerr%   r%   r'   r   2  s    
&c                 C   s   | j  }| j sJ d|S )z{
        Pops a scope layer from the stack.

        Try to use the context manager :py:meth:`push_scope` instead.
        z"stack must have at least one layer)ra   rl   )r4   rO   r%   r%   r'   pop_scope_unsafeJ  s    
zHub.pop_scope_unsafec                 C   s   d S r$   r%   rn   r%   r%   r'   configure_scopeU  s    zHub.configure_scopec                 C   s   d S r$   r%   rn   r%   r%   r'   r   \  s    c                    sB   | j d \ |dur* dur&| dS t fdd}| S )z
        Reconfigures the scope.

        :param callback: If provided, call the callback with the current scope.

        :returns: If no callback is provided, returns a context manager that returns the scope.
        r^   Nc                   3   s    d urV  nt  V  d S r$   r   r%   r/   rf   r%   r'   innerw  s    z"Hub.configure_scope.<locals>.inner)ra   r   )r4   ro   r   r%   r   r'   r   c  s    applicationc                 C   sJ   |    | jd \}}t|r&|jd nd|r6|jd nd|j|d|_dS )zStarts a new session.r^   releaseNenvironment)r   r   usersession_mode)end_sessionra   r
   r0   _user_session)r4   r   r/   rf   r%   r%   r'   start_session  s    zHub.start_sessionc                 C   sB   | j d \}}|j}d| j_|dur>|  |dur>|| dS )z)Ends the current session if there is one.r^   N)ra   r   rf   r8   Zcapture_session)r4   r/   rf   sessionr%   r%   r'   r     s    zHub.end_sessionc                 C   s    |    | jd \}}d|_dS )zStops automatic session tracking.

        This temporarily session tracking for the current scope when called.
        To resume session tracking call `resume_auto_session_tracking`.
        r^   FN)r   ra   _force_auto_session_trackingr4   r/   rf   r%   r%   r'   stop_auto_session_tracking  s    zHub.stop_auto_session_trackingc                 C   s   | j d \}}d|_dS )zResumes automatic session tracking for the current scope if
        disabled earlier.  This requires that generally automatic session
        tracking is enabled.
        r^   N)ra   r   r   r%   r%   r'   resume_auto_session_tracking  s    z Hub.resume_auto_session_trackingc                 C   s(   | j d \}}|dur$|j||dS dS )z>
        Alias for :py:meth:`sentry_sdk.Client.flush`
        r^   N)timeoutro   )ra   flush)r4   r   ro   r/   rf   r%   r%   r'   r     s    	z	Hub.flushc                 c   sP   |p
| j j}|sdS | jd d }|o.|jd }|s8dS | D ]
}|V  q@dS )z
        Return HTTP headers which allow propagation of trace data. Data taken
        from the span representing the request, if available, or the current
        span on the scope if not.
        Nr^   r   propagate_traces)rf   r   ra   r0   Ziter_headers)r4   r   r/   r   headerr%   r%   r'   iter_trace_propagation_headers  s    z"Hub.iter_trace_propagation_headers)NN)NN)NN)NN)NN)N)N)N)N)N)N)r   )NN)N)#r?   r@   rA   __doc__ra   r   r.   rZ   r5   r7   r=   rp   rt   r[   r/   rf   rv   rL   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r%   r%   r%   r'   r-      sv     




  
  
  
  
0 
. 
6 
 
 
 
 
  
r-   )<r)   rF   r   
contextlibr   Zsentry_sdk._compatr   Zsentry_sdk.scoper   Zsentry_sdk.clientr   Zsentry_sdk.tracingr   r	   Zsentry_sdk.sessionr
   Zsentry_sdk.utilsr   r   r   r   Zsentry_sdk._typesr   typingr   r   r   r   r   r   r   r   r   r   r   r   Zsentry_sdk.integrationsr   r   r   r   r    r!   Zsentry_sdk.constsr"   r#   rU   r,   r1   objectr2   rK   rP   rQ   ry   rT   r\   r-   rW   rX   r%   r%   r%   r'   <module>   s^   

0    