o
    Zh                     @   s   d Z ddl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 ddlmZ dd	lmZ erVd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mZ dd Zdd Zdd Zdd Zdd Z dd Z!dS )z
Instrumentation for Django 3.0

Since this file contains `async def` it is conditionally imported in
`sentry_sdk.integrations.django` (depending on the existence of
`django.core.handlers.asgi`.
    N)WSGIRequest)Hub
_functools)TYPE_CHECKING)OP)_should_send_default_pii)SentryAsgiMiddleware)capture_internal_exceptions)Callable)AnyUnion)ASGIRequest)HttpResponse)EventEventProcessorc                    s    fdd}|S )Nc                    s   ddl m}m}  d u r| S t tkr| S t  | |  W d    n1 s+w   Y  t rLt  | |  W d    | S 1 sGw   Y  | S )Nr   )DjangoRequestExtractor_set_user_info)sentry_sdk.integrations.djangor   r   typer   r	   Zextract_into_eventr   )eventhintr   r   request Z/var/www/html/lang_env/lib/python3.10/site-packages/sentry_sdk/integrations/django/asgi.pyasgi_request_event_processor"   s   
zH_make_asgi_request_event_processor.<locals>.asgi_request_event_processorr   )r   r   r   r   r   "_make_asgi_request_event_processor    s   r   c                    sX   ddl m  j fdd}|_td}|r*j fdd}|_d S d S )Nr   DjangoIntegrationc                    sV   t j}| }|d u r| |||I d H S t| ddj}||||I d H S )NTZunsafe_context_data)r   currentget_integrationr   __get__Z
_run_asgi3)selfscopereceivesendhubintegration
middlewarer   clsold_appr   r   sentry_patched_asgi_handlerE   s   
zCpatch_django_asgi_handler_impl.<locals>.sentry_patched_asgi_handlercreate_requestc                    s   t j}| }|d u r| g|R i |S |  }| g|R i |\}}|t| ||fW  d    S 1 s>w   Y  d S N)r   r    r!   configure_scopeZadd_event_processorr   )r#   argskwargsr'   r(   r$   r   Zerror_response)r   old_create_requestr   r   sentry_patched_create_requestX   s   

$zEpatch_django_asgi_handler_impl.<locals>.sentry_patched_create_request)r   r   __call__hasattrr.   )r+   r-   Zmodern_django_asgi_supportr4   r   )r   r+   r,   r3   r   patch_django_asgi_handler_impl>   s   

r7   c                    s   | j  fdd}|| _ d S )Nc                    s    | | |I d H S r/   r   )r#   r   _before_get_responseZold_get_response_asyncr   r   !sentry_patched_get_response_asyncl   s   zCpatch_get_response_async.<locals>.sentry_patched_get_response_async)Zget_response_async)r+   r9   r:   r   r8   r   patch_get_response_asynch   s   
r;   c                    sJ   dd l }ddlm  |jdk rj fdd}|_d S t d S )Nr   r   z3.0.0c                    sR   t jd u r ||I d H S t fdddd}| j||I d H S )Nc                    s     S r/   )r"   )Z_scope)r+   r,   r#   r   r   <lambda>   s    zWpatch_channels_asgi_handler_impl.<locals>.sentry_patched_asgi_handler.<locals>.<lambda>Tr   )r   r    r!   r   r$   )r#   r%   r&   r)   r*   r#   r   r-   }   s   zEpatch_channels_asgi_handler_impl.<locals>.sentry_patched_asgi_handler)channelsr   r   __version__r5   r7   )r+   r>   r-   r   r*   r    patch_channels_asgi_handler_implt   s   

r@   c                    s   t   fdd}|S )Nc              	      s     B}|jd ur|j  jtj| jjd  | g|R i |I d H W  d    W  d    S 1 s:w   Y  W d    d S 1 sJw   Y  d S )N)opdescription)r0   ZprofileZupdate_active_thread_idZ
start_spanr   ZVIEW_RENDERZresolver_matchZ	view_name)r   r1   r2   Zsentry_scopecallbackr'   r   r   sentry_wrapped_callback   s   



"z0wrap_async_view.<locals>.sentry_wrapped_callback)r   wraps)r'   rD   rE   r   rC   r   wrap_async_view   s   rG   c                    s   G  fddd}|S )zh
    Mixin class factory that generates a middleware mixin for handling requests
    in async mode.
    c                       s8   e Zd ZerdZdd Zdd Zdd Z fdd	ZdS )
z7_asgi_middleware_mixin_factory.<locals>.SentryASGIMixinNc                 S   s   || _ d | _|   d S r/   )get_response_acall_method_async_check)r#   rH   r   r   r   __init__   s   z@_asgi_middleware_mixin_factory.<locals>.SentryASGIMixin.__init__c                 S   s   t | jrt jj| _dS dS )z
            If get_response is a coroutine function, turns us into async mode so
            a thread is not consumed during a whole request.
            Taken from django.utils.deprecation::MiddlewareMixin._async_check
            N)asyncioiscoroutinefunctionrH   Z
coroutinesZ_is_coroutiner=   r   r   r   rJ      s   zD_asgi_middleware_mixin_factory.<locals>.SentryASGIMixin._async_checkc                 S   s   t | jS )z
            Function that checks if we are in async mode,
            and if we are forwards the handling of requests to __acall__
            )rL   rM   rH   r=   r   r   r   async_route_check   s   zI_asgi_middleware_mixin_factory.<locals>.SentryASGIMixin.async_route_checkc                    s   | j }|d u rt| jdr| jj | _ }n| j | _ } |d}|d u r/||i |I d H S | ||i |I d H W  d    S 1 sFw   Y  d S )N	__acall__)Z
old_method)rI   r6   _innerrO   )r#   r1   r2   fZmiddleware_span_check_middleware_spanr   r   rO      s   
$zA_asgi_middleware_mixin_factory.<locals>.SentryASGIMixin.__acall__)	__name__
__module____qualname__r   rP   rK   rJ   rN   rO   r   rR   r   r   SentryASGIMixin   s    
rW   r   )rS   rW   r   rR   r   _asgi_middleware_mixin_factory   s   -rX   )"__doc__rL   Zdjango.core.handlers.wsgir   Z
sentry_sdkr   r   Zsentry_sdk._typesr   Zsentry_sdk.constsr   Zsentry_sdk.hubr   Zsentry_sdk.integrations.asgir   Zsentry_sdk.utilsr	   collections.abcr
   typingr   r   Zdjango.core.handlers.asgir   Zdjango.http.responser   r   r   r   r7   r;   r@   rG   rX   r   r   r   r   <module>   s*    *