o
    ZhX                     @   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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 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) d dlm*Z* e dZ+ndd ZedZ,dd Z-G dd  d e.Z/d!d" Z0d#d$ Z1d d	lmZ erG d%d& d&e*e/Z2nd'd(  Z2G d)d* d*e3Z4G d+d, d,e.Z5G d-d. d.ee4Z6e6 Z7e,8e7 dS )/    N)contextmanager)with_metaclass)INSTRUMENTERScope)Client)NoOpSpanSpanTransaction)logger
ContextVar)TYPE_CHECKING)Any)Callable)ContextManager)Dict)	Generator)List)Optional)overload)Tuple)Type)TypeVar)Union)Integration)EventHint
BreadcrumbBreadcrumbHintExcInfoLogLevelStr)ClientConstructorTc                 C      | S N )xr%   r%   E/var/www/html/lang_env/lib/python3.10/site-packages/sentry_sdk/hub.pyr   3      r   Zsentry_current_hubc                  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_pii;   s   
r.   c                   @   $   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__D   s   
z_InitGuard.__init__c                 C   r#   r$   r%   r2   r%   r%   r'   	__enter__H   r(   z_InitGuard.__enter__c                 C   s   | j }|d ur|  d S d S r$   )r1   close)r2   exc_type	exc_valuetbcr%   r%   r'   __exit__L   s   z_InitGuard.__exit__N__name__
__module____qualname__r3   r5   r;   r%   r%   r%   r'   r0   C   s    r0   c                  C   sB   t jd d } | dks| dkrtddj|   td d S d S )N   )      )rA      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_deprecationsS   s   rI   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_clientrI   r0   )argskwargsr+   rvr%   r%   r'   _inita   s
   rN   c                   @   s   e Zd ZdS )initN)r=   r>   r?   r%   r%   r%   r'   rO   w   s    rO   c                   C      t S r$   )rN   r%   r%   r%   r'   <lambda>   s    rQ   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)clsrM   r%   r%   r'   r*      s
   

zHubMeta.currentc                 C   rP   )z%Returns the main instance of the hub.)rU   )rW   r%   r%   r'   main   s   zHubMeta.mainN)r=   r>   r?   propertyr*   rX   r%   r%   r%   r'   rR      s
    
	rR   c                   @   r/   )_ScopeManagerc                 C   s"   || _ t|j| _|jd | _d S N)_hublen_stack_original_len_layer)r2   hubr%   r%   r'   r3      s   z_ScopeManager.__init__c                 C   s   | j d }|d usJ |S )N   )ra   r2   scoper%   r%   r'   r5      s   
z_ScopeManager.__enter__c                 C   s   t | jj}|| jk rtd| j|  d S || jkr,td|| j | jj| jd   | jj| jd  }| jj| jd d = |d | jd krVtd|d | jd  d S |d | jd krhd}t| d S d S )Nz1Scope popped too soon. Popped %s scopes too many.zLeaked %s scopes: %src   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]   r_   r`   r   errorrF   ra   )r2   r7   r8   r9   Zcurrent_lenlayerrF   r%   r%   r'   r;      s4   

z_ScopeManager.__exit__Nr<   r%   r%   r%   r'   rZ      s    rZ   c                   @   s  e Zd ZdZdZerdZdZ		dCd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dCddZdCddZdCddZdd ZdCddZdejfdd Zdejfd!d"ZdDd#d$Ze	dEd%d&Zed'd& Z		(dFd)d&Zd*d+ Ze	dEd,d-Zed.d- Z		(dFd/d-Z	0dGd1d2Z d3d4 Z!d5d6 Z"d7d8 Z#		dCd9d:Z$d;d< Z%d=d> Z&dEd?d@Z'dEdAdB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r|}|jd \}}|d u rt|}n|}|d u r!t }||fg| _d | _g | _d S r[   )
isinstancer)   r_   copyr   _last_event_id	_old_hubs)r2   Zclient_or_hubre   rb   r+   Zother_scoper%   r%   r'   r3      s   


zHub.__init__c                 C   s   | j tj t|  | S r$   )rk   appendr)   r*   rS   rV   r4   r%   r%   r'   r5      s   
zHub.__enter__c                 C   s   | j  }t| d S r$   )rk   poprS   rV   )r2   r7   r8   r9   oldr%   r%   r'   r;      s   
zHub.__exit__c                 C   s.   |  | W  d   S 1 sw   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%   r2   callbackr%   r%   r'   run   s   $zHub.runc                 C   s   | j }|dur||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.
        N)r+   get_integration)r2   Zname_or_classr+   r%   r%   r'   rr     s   
zHub.get_integrationc                 C      | j d d S )z&Returns the current client on the hub.r\   r   r_   r4   r%   r%   r'   r+        z
Hub.clientc                 C   rs   )z%Returns the current scope on the hub.r\   rc   rt   r4   r%   r%   r'   re     ru   z	Hub.scopec                 C   s   t d | jS )a  
        Returns the last event ID.

        .. deprecated:: 1.40.5
            This function is deprecated and will be removed in a future release. The functions `capture_event`, `capture_message`, and `capture_exception` return the event ID directly.
        zDeprecated: last_event_id is deprecated. This will be removed in the future. The functions `capture_event`, `capture_message`, and `capture_exception` return the event ID directly.)r   rF   rj   r4   r%   r%   r'   last_event_id  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\   rc   Nrt   )r2   newtopr%   r%   r'   rJ   *  s   
zHub.bind_clientc           	      K   sX   | j d \}}|du rdS |j||f||d|}|ddk}|dur*|s*|| _|S )a  
        Captures an event.

        Alias of :py:meth:`sentry_sdk.Scope.capture_event`.

        :param event: A ready-made event that can be directly sent to Sentry.

        :param hint: Contains metadata about the event that can be read from `before_send`, such as the original exception object or a HTTP request object.

        :param scope: An optional :py:class:`sentry_sdk.Scope` to apply to events.
            The `scope` and `scope_kwargs` parameters are mutually exclusive.

        :param scope_kwargs: Optional data to apply to event.
            For supported `**scope_kwargs` see :py:meth:`sentry_sdk.Scope.update_from_kwargs`.
            The `scope` and `scope_kwargs` parameters are mutually exclusive.
        r\   Nr+   re   typetransaction)r_   capture_eventrT   rj   )	r2   eventhintre   scope_kwargsr+   	top_scoperv   Zis_transactionr%   r%   r'   r|   2  s   zHub.capture_eventc                 K   sF   | j d \}}|du rdS |j|f|||d|}|dur!|| _|S )a  
        Captures a message.

        Alias of :py:meth:`sentry_sdk.Scope.capture_message`.

        :param message: The string to send as the message to Sentry.

        :param level: If no level is provided, the default level is `info`.

        :param scope: An optional :py:class:`sentry_sdk.Scope` to apply to events.
            The `scope` and `scope_kwargs` parameters are mutually exclusive.

        :param scope_kwargs: Optional data to apply to event.
            For supported `**scope_kwargs` see :py:meth:`sentry_sdk.Scope.update_from_kwargs`.
            The `scope` and `scope_kwargs` parameters are mutually exclusive.

        :returns: An `event_id` if the SDK decided to send the event (see :py:meth:`sentry_sdk.Client.capture_event`).
        r\   N)levelr+   re   )r_   capture_messagerj   )r2   messager   re   r   r+   r   rv   r%   r%   r'   r   R  s   zHub.capture_messagec                 K   sD   | j d \}}|du rdS |j|f||d|}|dur || _|S )a  Captures an exception.

        Alias of :py:meth:`sentry_sdk.Scope.capture_exception`.

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

        :param scope: An optional :py:class:`sentry_sdk.Scope` to apply to events.
            The `scope` and `scope_kwargs` parameters are mutually exclusive.

        :param scope_kwargs: Optional data to apply to event.
            For supported `**scope_kwargs` see :py:meth:`sentry_sdk.Scope.update_from_kwargs`.
            The `scope` and `scope_kwargs` parameters are mutually exclusive.

        :returns: An `event_id` if the SDK decided to send the event (see :py:meth:`sentry_sdk.Client.capture_event`).
        r\   Nry   )r_   capture_exceptionrj   )r2   rf   re   r   r+   r   rv   r%   r%   r'   r   s  s   zHub.capture_exceptionc                 C   s   t jd|d dS )a   
        Capture an exception that is likely caused by a bug in the SDK
        itself.

        Duplicated in :py:meth:`sentry_sdk.Client._capture_internal_exception`.

        These exceptions do not end up in Sentry and are just logged instead.
        zInternal error in sentry_sdk)exc_infoN)r   rf   )r2   r   r%   r%   r'   _capture_internal_exception  s   zHub._capture_internal_exceptionc                 K   sD   | j d \}}|du rtd dS ||d< |j||fi | d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+   )r_   r   infoadd_breadcrumb)r2   Zcrumbr~   rL   r+   re   r%   r%   r'   r     s   

zHub.add_breadcrumbc                 K   4   | j d \}}| |d< ||d< |jd||d|S )a  
        Start a span whose parent is the currently active span or transaction, if any.

        The return value is a :py:class:`sentry_sdk.tracing.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
        :py:meth:`sentry_sdk.start_transaction` to start a new transaction when
        one is not already in progress.

        For supported `**kwargs` see :py:class:`sentry_sdk.tracing.Span`.
        r\   rb   r+   )spaninstrumenterNr%   )r_   
start_span)r2   r   r   rL   r+   re   r%   r%   r'   r     s   zHub.start_spanc                 K   r   )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.

        For supported `**kwargs` see :py:class:`sentry_sdk.tracing.Transaction`.
        r\   rb   r+   )r{   r   Nr%   )r_   start_transaction)r2   r{   r   rL   r+   re   r%   r%   r'   r     s   zHub.start_transactionc                 C   s    | j d d }|j||||dS )ze
        Sets the propagation context from environment or headers and returns a transaction.
        r\   rc   )environ_or_headersopnamesource)r_   continue_trace)r2   r   r   r   r   re   r%   r%   r'   r     s   zHub.continue_tracec                 C      d S r$   r%   ro   r%   r%   r'   
push_scope     zHub.push_scopec                 C   r   r$   r%   ro   r%   r%   r'   r     r   Tc                 C   s~   |dur|   }|| W d   dS 1 sw   Y  dS | jd \}}t|}|r1|  ||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   r_   ri   generate_propagation_contextrl   rZ   )r2   rp   r   re   r+   Z	new_scopeZ	new_layerr%   r%   r'   r     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)r_   rm   )r2   rM   r%   r%   r'   pop_scope_unsafe(  s   
zHub.pop_scope_unsafec                 C   r   r$   r%   ro   r%   r%   r'   configure_scope3  r   zHub.configure_scopec                 C   r   r$   r%   ro   r%   r%   r'   r   :  r   c                    sN   | j d \ |r  |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  d S t  V  d S r$   r   r%   ry   r%   r'   inner[  s   
z"Hub.configure_scope.<locals>.inner)r_   r   r   )r2   rp   r   r   r%   ry   r'   r   A  s   applicationc                 C   s    | j d \}}|j||d dS )zStarts a new session.r\   )r+   session_modeN)r_   start_session)r2   r   r+   re   r%   r%   r'   r   e  s
   
zHub.start_sessionc                 C      | j d \}}|j|d dS )z)Ends the current session if there is one.r\   r-   N)r_   end_sessionr2   r+   re   r%   r%   r'   r   p  s   zHub.end_sessionc                 C   r   )zStops automatic session tracking.

        This temporarily session tracking for the current scope when called.
        To resume session tracking call `resume_auto_session_tracking`.
        r\   r-   N)r_   stop_auto_session_trackingr   r%   r%   r'   r   v  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\   rc   N)r_   resume_auto_session_trackingrd   r%   r%   r'   r     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)timeoutrp   )r_   flush)r2   r   rp   r+   re   r%   r%   r'   r     s   	z	Hub.flushc                 C   s   | j d \}}|j|dS )zX
        Returns the traceparent either from the active span or from the scope.
        r\   r-   )r_   get_traceparentr   r%   r%   r'   r     s   zHub.get_traceparentc                 C   s.   | j d \}}|j|d}|dur| S dS )zP
        Returns Baggage either from the active span or from the scope.
        r\   r-   N)r_   get_baggage	serialize)r2   r+   re   Zbaggager%   r%   r'   r     s
   zHub.get_baggagec                 C   s   | j d \}}|j||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.
        r\   r   r+   )r_   iter_trace_propagation_headersr2   r   r+   re   r%   r%   r'   r     s   z"Hub.iter_trace_propagation_headersc                 C   s.   |dur	t d | jd \}}|j||dS )z
        Return meta tags which should be injected into HTML templates
        to allow propagation of trace information.
        NzaThe parameter `span` in trace_propagation_meta() is deprecated and will be removed in the future.r\   r   )r   rF   r_   trace_propagation_metar   r%   r%   r'   r     s   zHub.trace_propagation_meta)NN)NNNr$   )NT)r   ))r=   r>   r?   __doc__r_   r   r*   rX   r3   r5   r;   rq   rr   rY   r+   re   rv   rJ   r|   r   r   r   r   r   ZSENTRYr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r%   r%   r%   r'   r)      sr    






 
!


%

 

%



r)   )9ri   rD   
contextlibr   Zsentry_sdk._compatr   Zsentry_sdk.constsr   Zsentry_sdk.scoper   Zsentry_sdk.clientr   Zsentry_sdk.tracingr   r	   r
   Zsentry_sdk.utilsr   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   r    r!   r"   rS   r.   objectr0   rI   rN   rO   rz   rR   rZ   r)   rU   rV   r%   r%   r%   r'   <module>   s\     

0    