관리-도구
편집 파일: asgi.cpython-37.pyc
B T a! � @ s� d Z ddlZddlZddlZddlmZ ddlmZ ddlm Z m Z ddlmZ ddl mZmZmZmZmZ ddlmZ er�dd lmZ dd lmZ ddlmZ ddlmZ dd lmZ ddlmZmZ ed�ZdZdd� Z dd� Z!G dd� d�Z"dS )zf An ASGI middleware. Based on Tom Christie's `sentry-asgi <https://github.com/encode/sentry-asgi>`_. � N)�partial)�MYPY)�Hub�_should_send_default_pii)�_filter_headers)� ContextVar�event_from_exception�transaction_from_function�HAS_REAL_CONTEXTVARS�CONTEXTVARS_ERROR_MESSAGE)�Transaction)�Dict)�Any)�Optional)�Callable)�Literal)�Event�HintZsentry_asgi_middleware_appliedzgeneric ASGI requestc C s8 | j d k r4t|| j jddd�d�\}}| j||d� d S )N�asgiF)�typeZhandled)Zclient_optionsZ mechanism)�hint)�clientr �optionsZ capture_event)�hub�exc�eventr � r �M/opt/alt/python37/lib/python3.7/site-packages/sentry_sdk/integrations/asgi.py�_capture_exception( s r c C sB t �| �rt| d�S t �| �r(t�| �S t| dd�}t�|�S dS )z� Try to figure out if an application object supports ASGI3. This is how uvicorn figures out the application version as well. � __await__�__call__N)�inspect�isclass�hasattr� isfunction�asyncio�iscoroutinefunction�getattr)�app�callr r r �_looks_like_asgi35 s r* c @ sR e Zd ZdZddd�Zdd� Zdd� Zd d � Zdd� Zd d� Z dd� Z dd� ZdS )�SentryAsgiMiddleware)r( r Fc C s8 |st stdt ��|| _t|�r,| j| _n| j| _dS )ab Instrument an ASGI application with Sentry. Provides HTTP/websocket data to sent events and basic handling for exceptions bubbling up through the middleware. :param unsafe_context_data: Disable errors when a proper contextvars installation could not be found. We do not recommend changing this from the default. zRThe ASGI middleware for Sentry requires Python 3.7+ or the aiocontextvars package.N)r �RuntimeErrorr r( r* � _run_asgi3r � _run_asgi2)�selfr( Zunsafe_context_datar r r �__init__H s zSentryAsgiMiddleware.__init__c s � �fdd�}|S )Nc � s �� �� ���fdd��I d H S )Nc s �� ��� ��S )N)r( r )�receive�scoper/ �sendr r �<lambda>d � z@SentryAsgiMiddleware._run_asgi2.<locals>.inner.<locals>.<lambda>)�_run_app)r1 r3 )r2 r/ )r1 r3 r �innerb s z.SentryAsgiMiddleware._run_asgi2.<locals>.innerr )r/ r2 r7 r )r2 r/ r r. ` s zSentryAsgiMiddleware._run_asgi2c � s �� �� ���fdd��I d H S )Nc s �� �� ��S )N)r( r )r1 r2 r/ r3 r r r4 j r5 z1SentryAsgiMiddleware._run_asgi3.<locals>.<lambda>)r6 )r/ r2 r1 r3 r )r1 r2 r/ r3 r r- h s zSentryAsgiMiddleware._run_asgi3c � sn t �d�}|rPy|� I d H S tk rN } zttj|� |d �W d d }~X Y nX t �d� �z ttj�}|�� |�� �,}|�� d|_ t | j|d�}|�|� W d Q R X |d }|dkr�t j| �|�d�|�d�} n t d d�} t| _| �d |� |j| d|id��H y|� I d H S tk �rD } zt||� |d �W d d }~X Y nX W d Q R X W d Q R X W d t �d� X d S ) NFTr )� asgi_scoper )�http� websocketz {}.server)�opzasgi.serverz asgi.typer8 )Zcustom_sampling_context)�_asgi_middleware_applied�get� Exceptionr r �current�setZconfigure_scopeZclear_breadcrumbs�_namer �event_processorZadd_event_processorr Zcontinue_from_headers�_get_headers�format�_DEFAULT_TRANSACTION_NAME�nameZset_tagZstart_transaction) r/ r2 �callbackZis_recursive_asgi_middlewarer r Zsentry_scope� processor�ty�transactionr r r r6 l s@ 0zSentryAsgiMiddleware._run_appc C s� |� di �}|d }|dkrr|� d�|d<