관리-도구
편집 파일: asgi.cpython-37.pyc
B T a� � @ s� d Z ddlZddlmZmZ ddlmZ ddlmZ erhddl m Z ddl mZ ddl mZ dd l mZ d d� Zdd � Zdd� Zdd� Zdd� ZdS )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)�Hub� _functools)�MYPY)�SentryAsgiMiddleware)�Any)�Union)�Callable)�HttpResponsec s, ddl m� �j�� ��fdd�}|�_d S )Nr )�DjangoIntegrationc � sL t j�� �d kr$�| |||�I d H S t��| ��dd�j}||||�I d H S )NT)�unsafe_context_data)r �current�get_integrationr �__get__Z _run_asgi3)�self�scope�receive�send� middleware)r �cls�old_app� �T/opt/alt/python37/lib/python3.7/site-packages/sentry_sdk/integrations/django/asgi.py�sentry_patched_asgi_handler s zCpatch_django_asgi_handler_impl.<locals>.sentry_patched_asgi_handler)�sentry_sdk.integrations.djangor �__call__)r r r )r r r r �patch_django_asgi_handler_impl s r c s | j �� �fdd�}|| _ d S )Nc � s � |� �| |�I d H S )Nr )r �request)�_before_get_response�old_get_response_asyncr r �!sentry_patched_get_response_async0 s zCpatch_get_response_async.<locals>.sentry_patched_get_response_async)Zget_response_async)r r r r )r r r �patch_get_response_async, s r c sH dd l }ddlm� |jdk r<�j�� ��fdd�}|�_nt�� d S )Nr )r z3.0.0c � sP t j���d kr"�� ||�I d H S t��� fdd�dd�}|� j�||�I d H S )Nc s �� �� �S )N)r )Z_scope)r r r r r �<lambda>H � zWpatch_channels_asgi_handler_impl.<locals>.sentry_patched_asgi_handler.<locals>.<lambda>T)r )r r r r r )r r r r )r r r )r r r B s zEpatch_channels_asgi_handler_impl.<locals>.sentry_patched_asgi_handler)�channelsr r �__version__r r )r r# r r )r r r r � patch_channels_asgi_handler_impl8 s r% c s t �� �� �fdd��}|S )Nc � s4 �j d| jjd�� � | f|�|�I d H S Q R X d S )Nzdjango.view)�op�description)Z start_spanZresolver_matchZ view_name)r �args�kwargs)�callback�hubr r �sentry_wrapped_callbackW s z0wrap_async_view.<locals>.sentry_wrapped_callback)r �wraps)r+ r* r, r )r* r+ r �wrap_async_viewU s r. c s G � fdd�d�}|S )zh Mixin class factory that generates a middleware mixin for handling requests in async mode. c s8 e Zd ZerdZdd� Zdd� Zdd� Z� fdd �ZdS ) z7_asgi_middleware_mixin_factory.<locals>.SentryASGIMixinNc S s || _ d | _| �� d S )N)�get_response� _acall_method�_async_check)r r/ r r r �__init__n s z@_asgi_middleware_mixin_factory.<locals>.SentryASGIMixin.__init__c S s t �| j�rt jj| _dS )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�iscoroutinefunctionr/ Z coroutinesZ _is_coroutine)r r r r r1 t 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__ )r3 r4 r/ )r r r r �async_route_check~ s zI_asgi_middleware_mixin_factory.<locals>.SentryASGIMixin.async_route_checkc � sx | j }|d kr6t| jd�r*| jj | _ }n| j | _ }� |d�}|d krX|||�I d H S |� |||�I d H S Q R X d S )N� __acall__)Z old_method)r0 �hasattr�_innerr6 )r r( r) �fZmiddleware_span)�_check_middleware_spanr r r6 � s zA_asgi_middleware_mixin_factory.<locals>.SentryASGIMixin.__acall__) �__name__� __module__�__qualname__r r8 r2 r1 r5 r6 r )r: r r �SentryASGIMixinj s r>