o
    S۷i=$                     @   s  U d dl Z d dlmZmZmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZmZmZmZ d dlmZ erOd dlmZ d d	lmZ d d
lmZ d dlmZmZ zd dlZd dlmZ W n eyf   edw zd dlZejZejZ dZ!W n ey   d dl"Zej#Zej$Z dZ!Y nw dZ%de&d< e!re  d dl'm(Z% W d   n1 sw   Y  G dd deZ)G dd dZ*dZ+G dd deZ,d4ddZ-d4ddZ.d4dd Z/d!e0d"d#de1fd$d%Z2d"d#de1fd&d'Z3d(d)d*e4d+d,ddfd-d.Z5	/	,	0			1d5d2d3Z6dS )6    N)_check_minimum_versionIntegrationDidNotEnable)RequestExtractor)SentryWsgiMiddleware)SOURCE_FOR_STYLE)capture_internal_exceptionsensure_integration_enabledevent_from_exceptionparse_version)TYPE_CHECKING)Any)Dict)Optional)EventEventProcessor)__version__zFalcon not installedTFzOptional[object]_FALCON_UNSET)_UNSETc                   @   sH   e Z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 )FalconRequestExtractorreturnDict[str, Any]c                 C      | j jS N)requestenvself r   T/home/ubuntu/vllm_env/lib/python3.10/site-packages/sentry_sdk/integrations/falcon.pyr   5      zFalconRequestExtractor.envc                 C   r   r   )r   cookiesr   r   r   r   r!   8   r    zFalconRequestExtractor.cookiesNc                 C      d S r   r   r   r   r   r   form;      zFalconRequestExtractor.formc                 C   r"   r   r   r   r   r   r   files>   r$   zFalconRequestExtractor.filesOptional[str]c                 C   s   |   }|dkr
dS d S )Nr   z[REQUEST_CONTAINING_RAW_DATA])content_length)r   r'   r   r   r   raw_dataA   s   zFalconRequestExtractor.raw_dataOptional[Dict[str, Any]]c                 C   sB   d }t   | jj}W d    n1 sw   Y  |tur|S d S r   )r   r   _mediar   )r   cached_mediar   r   r   jsonL   s   
	zFalconRequestExtractor.json)r   r   r   N)r   r&   )r   r)   )	__name__
__module____qualname__r   r!   r#   r%   r(   r,   r   r   r   r   r   4   s    




r   c                   @   s.   e Zd ZdZ										dd	d
ZdS )SentryFalconMiddlewarez9Captures exceptions in Falcon requests and send to Sentryreqr   respargskwargsr   Nc                 O   s<   t  t}|d u rd S t  }d|_|t|| d S )Nfalcon)
sentry_sdk
get_clientget_integrationFalconIntegrationget_isolation_scope_nameadd_event_processor_make_request_event_processor)r   r2   r3   r4   r5   integrationscoper   r   r   process_requesta   s   z&SentryFalconMiddleware.process_request)
r2   r   r3   r   r4   r   r5   r   r   N)r.   r/   r0   __doc__rA   r   r   r   r   r1   ^   s    r1   uri_templatepathc                   @   s@   e Zd ZdZde ZdZddeddfdd	Zedd
dZ	dS )r:   r6   z
auto.http. rD   transaction_styler   Nc                 C   s"   |t vrtd|t f || _d S )Nz7Invalid value for transaction_style: %s (must be in %s))TRANSACTION_STYLE_VALUES
ValueErrorrG   )r   rG   r   r   r   __init__v   s   
zFalconIntegration.__init__c                  C   s(   t t} tt|  t  t  t  d S r   )r   FALCON_VERSIONr   r:   _patch_wsgi_app_patch_handle_exception_patch_prepare_middleware)versionr   r   r   
setup_once~   s
   

zFalconIntegration.setup_once)rD   r-   )
r.   r/   r0   
identifieroriginrG   strrJ   staticmethodrP   r   r   r   r   r:   p   s    
r:   r   c                     s.   t j 								d	 fdd} | t _d S )
Nr   
falcon.APIr   r   start_responser   c                    sD   t  t}|d u r ||S t fddtjd}|||S )Nc                    s    | |S r   r   )envi
start_resp)original_wsgi_appr   r   r   <lambda>   s    zB_patch_wsgi_app.<locals>.sentry_patched_wsgi_app.<locals>.<lambda>)span_origin)r7   r8   r9   r:   r   rR   )r   r   rV   r?   sentry_wrappedrY   r   r   sentry_patched_wsgi_app   s   
z0_patch_wsgi_app.<locals>.sentry_patched_wsgi_app)r   rU   r   r   rV   r   r   r   )falcon_app_class__call__)r^   r   r]   r   rL      s   
rL   c                     s(   t j tt d fdd} | t _d S )	Nr   rU   r4   r   r   c                    s   d  }}t   tdd |D }tdd |D }W d    n1 s$w   Y   | g|R  }|d u s9|d u r;|S t||rVt|t jdddd\}}tj||d |S )	Nc                 s   s    | ]
}t |tr|V  qd S r   )
isinstance	Exception.0argumentr   r   r   	<genexpr>   s    zS_patch_handle_exception.<locals>.sentry_patched_handle_exception.<locals>.<genexpr>c                 s   s     | ]}t |tjr|V  qd S r   )ra   r6   Responserc   r   r   r   rf      s    
r6   F)typehandled)client_options	mechanism)hint)r   next_exception_leads_to_http_5xxr
   r7   r8   optionscapture_event)r   r4   exresponsewas_handledeventrl   original_handle_exceptionr   r   sentry_patched_handle_exception   s$   


z@_patch_handle_exception.<locals>.sentry_patched_handle_exception)r   rU   r4   r   r   r   )r_   _handle_exceptionr	   r:   )rw   r   ru   r   rM      s   
rM   c               	      s6   t j 	 		d	dddddtddf fdd} | t _d S )
NF
middlewarer   independent_middlewareasgir   c                    s@   |r | ||S t  t}|d urt g| pg  }  | |S r   )r7   r8   r9   r:   r1   )ry   rz   r{   r?   original_prepare_middlewarer   r   !sentry_patched_prepare_middleware   s   
zD_patch_prepare_middleware.<locals>.sentry_patched_prepare_middleware)NFF)falcon_helpersprepare_middlewarebool)r~   r   r|   r   rN      s   
rN   rq   rr   zfalcon.Responsec                 C   sH   t | tjo| jp
dd}t | tjtjjf }|s|o#t p#t|S )NrF   5)	ra   r6   	HTTPErrorstatus
startswithhttp_status
HTTPStatusFALCON3_has_http_5xx_status)rq   rr   is_server_erroris_unhandled_errorr   r   r   rn      s   	rn   c                 C   s   | j dS )Nr   )r   r   )rr   r   r   r   r      s   r   rt   r   rG   r   falcon.Requestc                 C   s.   |j |jd}|| | d< dt| i| d< d S )NrC   transactionsourcetransaction_info)rD   rE   r   )rt   rG   r   name_for_styler   r   r    _set_transaction_name_and_source   s
   r   r2   r?   r   c                    s   d fdd}|S )	Nrt   r   rl   dict[str, Any]r   c                    sH   t |  j t  t|  W d    | S 1 sw   Y  | S r   )r   rG   r   r   extract_into_event)rt   rl   r?   r2   r   r   event_processor   s   
z6_make_request_event_processor.<locals>.event_processor)rt   r   rl   r   r   r   r   )r2   r?   r   r   r   r   r>      s   r>   r-   )r2   r   r?   r:   r   r   )7r7   sentry_sdk.integrationsr   r   r   $sentry_sdk.integrations._wsgi_commonr   sentry_sdk.integrations.wsgir   sentry_sdk.tracingr   sentry_sdk.utilsr   r	   r
   r   typingr   r   r   r   sentry_sdk._typesr   r   r6   r   rK   ImportErrorfalcon.app_helpersapp_helpersr   Appr_   r   falcon.api_helpersapi_helpersAPIr   __annotations__falcon.requestr   r   r1   rH   r:   rL   rM   rN   rb   r   rn   r   rS   r   r>   r   r   r   r   <module>   sv   
 *


$
