o
    h_2                     @   s  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m	Z	m
Z
 d 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 d d	lmZmZmZ d d
lmZ d dlmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z) zd dl*Z*d dl+m,Z- d dl+m.Z.m/Z/ d dl0m1Z1m2Z2m3Z3 W n e4y   edw d dl5m6Z6 e6rd dl7m8Z8 d dl9m:Z: d dl+m;Z;m<Z< d dl=m>Z> d dl?m@Z@ d dl5mAZA d dl5mBZB d dl5mCZC d dl5mDZD d dlmEZE d dlFmGZGmHZH dZIG dd deZJdd  ZKd!d" ZLd#d$ ZMd%ZNd&d' ZOdS )(    N)wraps)continue_trace)OP
SPANSTATUSSPANDATA)$_DEFAULT_FAILED_REQUEST_STATUS_CODES_check_minimum_versionIntegrationDidNotEnable)ignore_logger)track_session)_filter_headersrequest_body_within_bounds)BAGGAGE_HEADER_NAMESOURCE_FOR_STYLETransactionSource)should_propagate_trace)capture_internal_exceptionsensure_integration_enabledevent_from_exceptionlogger	parse_urlparse_versionreraisetransaction_from_functionHAS_REAL_CONTEXTVARSCONTEXTVARS_ERROR_MESSAGESENSITIVE_DATA_SUBSTITUTEAnnotatedValue)__version__)ClientSessionTraceConfig)ApplicationHTTPExceptionUrlDispatcherzAIOHTTP not installed)TYPE_CHECKING)Request)UrlMappingMatchInfo)TraceRequestStartParamsTraceRequestEndParams)Set)SimpleNamespace)Any)Optional)Tuple)Union)ExcInfo)EventEventProcessor)handler_namemethod_and_path_patternc                   @   s8   e Zd ZdZde Z	d
edddZedd Zd	S )AioHttpIntegrationaiohttpz
auto.http.r3   )failed_request_status_codesc                C   s(   |t vrtd|t f || _|| _d S )Nz7Invalid value for transaction_style: %s (must be in %s))TRANSACTION_STYLE_VALUES
ValueErrortransaction_style_failed_request_status_codes)selfr:   r7    r=   a/var/www/html/pro-man-master/venv/lib/python3.10/site-packages/sentry_sdk/integrations/aiohttp.py__init__K   s   
zAioHttpIntegration.__init__c                     s   t t} tt|  tstdt td tj	fdd}|t_	t
jtfdd}|t
_tj tt  fdd}|t_d S )	NzSThe aiohttp integration for Sentry requires Python 3.7+  or aiocontextvars package.zaiohttp.serverc                    s  t  t}|d u r | |g|R i |I d H S t|}t  }t|dd |  |	  |
t| t|j}t|tjdtjtjd}t j|d|idp z
 | |I d H }	W n9 ty| }
 z||
j |
j|jv rwt   d }
~
w tjtfy   |tj   t y   t!t   Y nw z|	j"}W n	 t#y   Y nw || |	W  d    W  d    W  d    S 1 sw   Y  W d    n1 sw   Y  W d    d S W d    d S 1 sw   Y  d S )Nrequest)session_modezgeneric AIOHTTP request)opnamesourceoriginaiohttp_request)custom_sampling_context)$
sentry_sdk
get_clientget_integrationr5   weakrefrefisolation_scoper   generate_propagation_contextclear_breadcrumbsadd_event_processor_make_request_processordictheadersr   r   HTTP_SERVERr   ROUTErE   start_transactionr#   set_http_statusstatus_coder;   _capture_exceptionasyncioCancelledErrorConnectionResetError
set_statusr   	CANCELLED	Exceptionr   statusAttributeError)r<   r@   argskwargsintegrationweak_requestscoperS   transactionresponseeresponse_status)
old_handler=   r>   sentry_app_handlem   sj   


	

"z8AioHttpIntegration.setup_once.<locals>.sentry_app_handlec                    s    | |I d H }t  t}|d u r|S d }z'|jdkr$t|j}n|jdkr>| }|dp6|d}d	|j
|}W n	 tyH   Y nw |d urYt  j|t|j d |S )Nr3   r4   path	formatterz{} {})rD   )rH   rI   rJ   r5   r:   r   handlerget_infogetformatmethodr_   get_current_scopeset_transaction_namer   )r<   r@   rvrd   rC   
route_infopattern)old_urldispatcher_resolver=   r>   sentry_urldispatcher_resolve   s.   

zCAioHttpIntegration.setup_once.<locals>.sentry_urldispatcher_resolvec                     s8   t |dpd}t }|| ||d<  | i |S )Ntrace_configsr=   )listrq   create_trace_configappend)rb   rc   client_trace_configstrace_config)old_client_session_initr=   r>   init   s
   
z+AioHttpIntegration.setup_once.<locals>.init)r   AIOHTTP_VERSIONr   r5   r   r
   r   r   r"   _handler$   resolver   r    r?   r   )versionrl   rz   r   r=   )r   rk   ry   r>   
setup_onceZ   s(   
?
	zAioHttpIntegration.setup_onceN)r3   )	__name__
__module____qualname__
identifierrE   r   r?   staticmethodr   r=   r=   r=   r>   r5   G   s    
r5   c                  C   s2   dd } dd }t  }|j|  |j| |S )Nc           	         sR  t  td u rd S |j }d }t  tt|j	dd}W d    n1 s*w   Y  t j
tjd||r:|j	ntf tjd}|tj| |d urd|d|j	 |tj|j |tj|j t  }t|t|j	rt  j|dD ]+\}}tdj|||j	d |tkr|jtr|j|  d	| 7  < qx||j|< qx||_d S )
NF)sanitizez%s %s)rB   rC   rE   url)spanzE[Tracing] Adding `{key}` header {value} to outgoing request to {url}.)keyvaluer   ,) rH   rI   rJ   r5   rs   upperr   r   strr   
start_spanr   HTTP_CLIENTr   rE   set_datar   HTTP_METHOD
HTTP_QUERYqueryHTTP_FRAGMENTfragmentr   rt   iter_trace_propagation_headersr   debugrr   r   rS   rq   r   )	sessiontrace_config_ctxparamsrs   
parsed_urlr   clientr   r   r=   r=   r>   on_request_start   sN   

z-create_trace_config.<locals>.on_request_startc                    sD   |j d u rd S |j }|t|jj |d|jj |  d S )Nreason)r   rW   intrh   r`   r   r   finish)r   r   r   r   r=   r=   r>   on_request_end  s   
z+create_trace_config.<locals>.on_request_end)r!   r   r~   r   )r   r   r   r=   r=   r>   r}      s   /
r}   c                    s    fdd}|S )Nc                    s     }|d u r	| S t  ; | di }d|j|j|jf |d< |j|d< |j|d< d|ji|d< tt	|j
|d< t||d	< W d    | S 1 sJw   Y  | S )
Nr@   z	%s://%s%sr   query_stringrs   REMOTE_ADDRenvrS   data)r   
setdefaultschemehostrm   r   rs   remoter   rR   rS   get_aiohttp_request_data)eventhintr@   request_infore   r=   r>   aiohttp_processor$  s&   



z2_make_request_processor.<locals>.aiohttp_processorr=   )re   r   r=   r   r>   rQ   "  s   rQ   c                  C   s8   t  } t| t jdddd\}}tj||d | S )Nr6   F)typehandled)client_options	mechanism)r   )sysexc_infor   rH   rI   optionscapture_event)r   r   r   r=   r=   r>   rY   E  s   
rY   z8[Can't show request body due to implementation details.]c                 C   sL   | j }|d urtt t|st S | jpd}||dS | j	r$t
S d S )Nzutf-8replace)_read_bytesr   rH   rI   lenr   removed_because_over_size_limitcharsetdecodecan_read_bodyBODY_NOT_READ_MESSAGE)r@   
bytes_bodyencodingr=   r=   r>   r   T  s   
r   )Pr   rK   	functoolsr   rH   sentry_sdk.apir   sentry_sdk.constsr   r   r   sentry_sdk.integrationsr   r   r	   r
   sentry_sdk.integrations.loggingr   sentry_sdk.sessionsr   $sentry_sdk.integrations._wsgi_commonr   r   sentry_sdk.tracingr   r   r   sentry_sdk.tracing_utilsr   sentry_sdk.utilsr   r   r   r   r   r   r   r   r   r   r   r   rZ   r6   r   r   r    r!   aiohttp.webr"   r#   r$   ImportErrortypingr%   aiohttp.web_requestr&   aiohttp.web_urldispatcherr'   r(   r)   collections.abcr*   typesr+   r,   r-   r.   r/   r0   sentry_sdk._typesr1   r2   r8   r5   r}   rQ   rY   r   r   r=   r=   r=   r>   <module>   sT    8 D#