o
    h                     @   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rTd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 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)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	   extract_into_eventr   )eventhintr   r   request f/var/www/html/aiguide_backend/venv/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unsafe_context_data)r   currentget_integrationr   __get__
_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_scopeadd_event_processorr   )r%   argskwargsr)   r*   r&   r   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__hasattrr0   )r-   r/   modern_django_asgi_supportr8   r   )r   r-   r.   r7   r   patch_django_asgi_handler_impl>   s   

r<   c                    s   | j  fdd}|| _ d S )Nc                    s    | | |I d H S r1   r   )r%   r   _before_get_responseold_get_response_asyncr   r   !sentry_patched_get_response_asyncl   s   zCpatch_get_response_async.<locals>.sentry_patched_get_response_async)get_response_async)r-   r>   r@   r   r=   r   patch_get_response_asynch   s   
rB   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 r1   )r#   )_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__r9   r<   )r-   rF   r/   r   r,   r    patch_channels_asgi_handler_implt   s   

rH   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)r2   profileupdate_active_thread_id
start_spanr   VIEW_RENDERresolver_match	view_name)r   r4   r5   sentry_scopecallbackr)   r   r   sentry_wrapped_callback   s   



"z0wrap_async_view.<locals>.sentry_wrapped_callback)r   wraps)r)   rS   rT   r   rR   r   wrap_async_view   s   rV   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 r1   )get_response_acall_method_async_check)r%   rW   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iscoroutinefunctionrW   
coroutines_is_coroutinerE   r   r   r   rY      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__
            )r[   r\   rW   rE   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__)
old_method)rX   r:   _innerr`   )r%   r4   r5   fmiddleware_span_check_middleware_spanr   r   r`      s   
$zA_asgi_middleware_mixin_factory.<locals>.SentryASGIMixin.__acall__)	__name__
__module____qualname__r   rb   rZ   rY   r_   r`   r   re   r   r   SentryASGIMixin   s    
rj   r   )rf   rj   r   re   r   _asgi_middleware_mixin_factory   s   -rk   )!__doc__r[   django.core.handlers.wsgir   
sentry_sdkr   r   sentry_sdk._typesr   sentry_sdk.constsr   sentry_sdk.hubr   sentry_sdk.integrations.asgir   sentry_sdk.utilsr	   collections.abcr
   typingr   r   django.core.handlers.asgir   django.http.responser   r   r   r<   rB   rH   rV   rk   r   r   r   r   <module>   s*    *