o
    S۷i                      @   s8  d Z ddlZddlZddlZddlm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mZ ddlmZ erddd	lmZmZmZmZ dd
lmZ ddlmZ ddlmZmZ ededef dZeedrpejZej Z nejZd)ddZ d*ddZ!d+ddZ"d,ddZ#d+d d!Z$d-d#d$Z%	%	&		d.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)OP)SentryAsgiMiddleware)should_send_default_pii)capture_internal_exceptionsensure_integration_enabled)TYPE_CHECKING)AnyCallableUnionTypeVar)ASGIRequest)HttpResponse)EventEventProcessor_F.)boundmarkcoroutinefunctionfuncreturnc                 C   s   t jj| _| S N)asyncio
coroutines_is_coroutine)r    r   Y/home/ubuntu/vllm_env/lib/python3.10/site-packages/sentry_sdk/integrations/django/asgi.pyr   1   s   
requestr   r   c                    s   d fdd}|S )	Neventr   hintdict[str, Any]r   c                    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   )r   r   r    r!   r   r   r   asgi_request_event_processor7   s   
zH_make_asgi_request_event_processor.<locals>.asgi_request_event_processor)r   r   r   r   r   r   r   )r   r&   r   r%   r   "_make_asgi_request_event_processor6   s   r'   clsr	   c                    s   ddl m  j										d fd	d
}|_td}|rBjt 								dfdd}|_d S d S )Nr   DjangoIntegrationselfr	   scopereceivesendr   c                    s\   t   }|d u r| |||I d H S t| d j|jdj}||||I d H S )NTunsafe_context_dataspan_originhttp_methods_to_capture)
sentry_sdk
get_clientget_integrationr   __get__originr2   
_run_asgi3)r+   r,   r-   r.   integration
middlewarer*   r(   old_appr   r   sentry_patched_asgi_handlerW   s   
zCpatch_django_asgi_handler_impl.<locals>.sentry_patched_asgi_handlercreate_requestargskwargsc                    s8    | g|R i |\}}t  }|t| ||fS r   )r3   get_isolation_scopeadd_event_processorr'   )r+   r?   r@   r   error_responser,   )old_create_requestr   r   sentry_patched_create_requestm   s   zEpatch_django_asgi_handler_impl.<locals>.sentry_patched_create_request)
r+   r	   r,   r	   r-   r	   r.   r	   r   r	   )r+   r	   r?   r	   r@   r	   r   r	   )r"   r*   __call__hasattrr>   r   )r(   r=   modern_django_asgi_supportrE   r   )r*   r(   r<   rD   r   patch_django_asgi_handler_implR   s:   

	rI   _before_get_responsec                    s,   | j 						d fdd}|| _ d S )	Nr+   r	   r   r   "Union[HttpResponse, BaseException]c                    s    | | |I d H S r   r   )r+   r   rJ   old_get_response_asyncr   r   !sentry_patched_get_response_async}   s   zCpatch_get_response_async.<locals>.sentry_patched_get_response_async)r+   r	   r   r	   r   rK   )get_response_async)r(   rJ   rN   r   rL   r   patch_get_response_asyncz   s   
rP   c                    s\   dd l }ddlm  |jdk r(j								d fd	d
}|_d S t d S )Nr   r)   z3.0.0r+   r	   r-   r.   r   c                    s`   t  }|d u r ||I d H S t fdddj|jd}| j||I d H S )Nc                    s     S r   )r6   )_scope)r(   r<   r+   r   r   <lambda>   s    zWpatch_channels_asgi_handler_impl.<locals>.sentry_patched_asgi_handler.<locals>.<lambda>Tr/   )r3   r4   r5   r   r7   r2   r,   )r+   r-   r.   r9   r:   r;   r+   r   r=      s   zEpatch_channels_asgi_handler_impl.<locals>.sentry_patched_asgi_handler)r+   r	   r-   r	   r.   r	   r   r	   )channelsr"   r*   __version__rF   rI   )r(   rT   r=   r   r;   r    patch_channels_asgi_handler_impl   s   

rV   callbackc                    s:   ddl m  t								d
 fdd	}|S )Nr   r)   r   r	   r?   r@   r   c                    s   t  }|jd ur|j  t  }|jd ur|j  t   }|r)|j	s7| g|R i |I d H S t j
tj| jj jd | g|R i |I d H W  d    S 1 s\w   Y  d S )N)opnamer7   )r3   get_current_scopetransactionupdate_active_threadrA   profileupdate_active_thread_idr4   r5   middleware_spans
start_spanr   VIEW_RENDERresolver_match	view_namer7   )r   r?   r@   current_scopesentry_scoper9   r*   rW   r   r   sentry_wrapped_callback   s"   




$z0wrap_async_view.<locals>.sentry_wrapped_callback)r   r	   r?   r	   r@   r	   r   r	   )r"   r*   	functoolswraps)rW   rg   r   rf   r   wrap_async_view   s   rj   _check_middleware_spanCallable[..., Any]c                    s   G  fddd}|S )zh
    Mixin class factory that generates a middleware mixin for handling requests
    in async mode.
    c                       sD   e Zd ZerdZdddZdddZdefd	d
Zd fddZ	dS )z7_asgi_middleware_mixin_factory.<locals>.SentryASGIMixinNget_responserl   r   c                 S   s   || _ d | _|   d S r   )rm   _acall_method_async_check)r+   rm   r   r   r   __init__   s   z@_asgi_middleware_mixin_factory.<locals>.SentryASGIMixin.__init__c                 S   s   t | jrt|  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)iscoroutinefunctionrm   r   rS   r   r   r   ro      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__
            )rq   rm   rS   r   r   r   async_route_check   s   
zI_asgi_middleware_mixin_factory.<locals>.SentryASGIMixin.async_route_checkr?   r	   r@   c                    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)rn   rG   _innerrs   )r+   r?   r@   fmiddleware_spanrk   r   r   rs      s   
$zA_asgi_middleware_mixin_factory.<locals>.SentryASGIMixin.__acall__)rm   rl   r   N)r   N)r?   r	   r@   r	   r   r	   )
__name__
__module____qualname__r   ru   rp   ro   boolrr   rs   r   rx   r   r   SentryASGIMixin   s    

	r}   r   )rk   r}   r   rx   r   _asgi_middleware_mixin_factory   s   )r~   )r   r   r   r   )r   r   r   r   )r(   r	   r   N)r(   r	   rJ   r	   r   N)rW   r	   r   r	   )rk   rl   r   r	   )'__doc__r   rh   inspectdjango.core.handlers.wsgir   r3   sentry_sdk.constsr   sentry_sdk.integrations.asgir   sentry_sdk.scoper   sentry_sdk.utilsr   r   typingr   r	   r
   r   r   django.core.handlers.asgir   django.http.responser   sentry_sdk._typesr   r   r   rG   rq   r   r'   rI   rP   rV   rj   r~   r   r   r   r   <module>   s@    




(

 