o
    ٰi2                     @   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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/m0Z0 d dl1m2Z2m3Z3m4Z4 W n e5y   edw d dl6m7Z7 e7rd dl8m9Z9 d dl:m;Z; d dl,m<Z<m=Z= d dl>m?Z? d dl@mAZA d dl6mBZB d dl6mCZC d dl6mDZD d dl6mEZE d dlmFZF d dlGmHZHmIZI dZJG dd deZKd1d!d"ZL	#	$		%d2d&d'ZMd3d)d*ZNd+ZO	,	-		.d4d/d0ZPdS )5    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add_http_request_source)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                   @   sH   e Zd ZdZde Z	deddedddd	fd
dZedddZ	d	S )AioHttpIntegrationaiohttpz
auto.http.r4   )failed_request_status_codestransaction_styler8   zSet[int]returnNc                C   s(   |t vrtd|t f || _|| _d S )Nz7Invalid value for transaction_style: %s (must be in %s))TRANSACTION_STYLE_VALUES
ValueErrorr9   _failed_request_status_codes)selfr9   r8    r?   S/home/ubuntu/.local/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												dfd
d}|t_	t
jt							dfdd}|t
_tj tt d fdd}|t_d S )NzSThe aiohttp integration for Sentry requires Python 3.7+  or aiocontextvars package.zaiohttp.serverr>   r-   requestr'   argskwargsr:   c                    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 )NrB   )session_modezgeneric AIOHTTP request)opnamesourceoriginaiohttp_request)custom_sampling_context)$
sentry_sdk
get_clientget_integrationr6   weakrefrefisolation_scoper   generate_propagation_contextclear_breadcrumbsadd_event_processor_make_request_processordictheadersr   r   HTTP_SERVERr   ROUTErI   start_transactionr$   set_http_statusstatus_coder=   _capture_exceptionasyncioCancelledErrorConnectionResetError
set_statusr   	CANCELLED	Exceptionr   statusAttributeError)r>   rB   rC   rD   integrationweak_requestscoperW   transactionresponseeresponse_status)
old_handler?   r@   sentry_app_handlej   sj   


	

"z8AioHttpIntegration.setup_once.<locals>.sentry_app_handler%   r(   c                    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 )Nr4   r5   path	formatterz{} {})rH   )rL   rM   rN   r6   r9   r   handlerget_infogetformatmethodrc   get_current_scopeset_transaction_namer   )r>   rB   rvrf   rG   
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?   )listrs   create_trace_configappend)rC   rD   client_trace_configstrace_config)old_client_session_initr?   r@   init   s
   
z+AioHttpIntegration.setup_once.<locals>.init)
r>   r-   rB   r'   rC   r-   rD   r-   r:   r-   )r>   r%   rB   r'   r:   r(   )rC   r-   rD   r-   r:   N)r   AIOHTTP_VERSIONr   r6   r   r
   r   r   r#   _handler%   resolver   r!   rA   r   )versionrn   r|   r   r?   )r   rm   r{   r@   
setup_onceY   sH   
@
zAioHttpIntegration.setup_once)r4   )r:   N)
__name__
__module____qualname__
identifierrI   r   strrA   staticmethodr   r?   r?   r?   r@   r6   G   s    

r6   r:   r"   c                  C   sV   								 ddd	} 						
		 ddd}t  }|j|  |j| |S )Nsessionr!   trace_config_ctxr,   paramsr)   r:   c           	         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)rF   rG   rI   url)spanzE[Tracing] Adding `{key}` header {value} to outgoing request to {url}.)keyvaluer   ,) rL   rM   rN   r6   ru   upperr   r   r   r   
start_spanr   HTTP_CLIENTr   rI   set_datar   HTTP_METHOD
HTTP_QUERYqueryHTTP_FRAGMENTfragmentr   rv   iter_trace_propagation_headersr   debugrt   r   rW   rs   r   )	r   r   r   ru   
parsed_urlr   clientr   r   r?   r?   r@   on_request_start   sN   

z-create_trace_config.<locals>.on_request_startr*   c                    st   |j d u rd S |j }|t|jj |d|jj |  t  t	| W d    d S 1 s3w   Y  d S )Nreason)
r   r[   intrj   rd   r   r   finishr   r   )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:   N)r   r!   r   r,   r   r*   r:   N)r"   r   r   r   )r   r   r   r?   r?   r@   r      s,   
2
r   rg   weakref.ReferenceType[Request]r3   c                    s   						d fdd}|S )	Neventr2   hint*dict[str, Tuple[type, BaseException, Any]]r:   c                    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 )
NrB   z	%s://%s%sr   query_stringru   REMOTE_ADDRenvrW   data)r   
setdefaultschemehostro   r   ru   remoter   rV   rW   get_aiohttp_request_data)r   r   rB   request_inforg   r?   r@   aiohttp_processor*  s&   



z2_make_request_processor.<locals>.aiohttp_processor)r   r2   r   r   r:   r2   r?   )rg   r   r?   r   r@   rU   '  s   rU   r1   c                  C   s8   t  } t| t jdddd\}}tj||d | S )Nr7   F)typehandled)client_options	mechanism)r   )sysexc_infor   rL   rM   optionscapture_event)r   r   r   r?   r?   r@   r]   J  s   
r]   z8[Can't show request body due to implementation details.]rB   r'   $Union[Optional[str], AnnotatedValue]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   rL   rM   lenr   removed_because_over_size_limitcharsetdecodecan_read_bodyBODY_NOT_READ_MESSAGE)rB   
bytes_bodyencodingr?   r?   r@   r   X  s   
r   )r:   r"   )rg   r   r:   r3   )r:   r1   )rB   r'   r:   r   )Qr   rO   	functoolsr   rL   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   r   sentry_sdk.utilsr   r   r   r   r   r   r   r   r   r   r   r   r^   r7   r    r   r!   r"   aiohttp.webr#   r$   r%   ImportErrortypingr&   aiohttp.web_requestr'   aiohttp.web_urldispatcherr(   r)   r*   collections.abcr+   typesr,   r-   r.   r/   r0   r1   sentry_sdk._typesr2   r3   r;   r6   r   rU   r]   r   r   r?   r?   r?   r@   <module>   sd    8 
K

#