o
    S۷i                     @   s  d dl Z d dlZd dlmZ d dlmZmZ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 errd dlmZ d d	lmZ d d
l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 d dl m!Z!m"Z" zd dlm#Z#m$Z$m%Z%m&Z'm(Z) W n e*y   edw dZ+G dd deZ,G dd deZ-ddde.ddddfddZ/		 		!	"			#d*d$d%Z0d&e1d'e2ddfd(d)Z3dS )+    N)SOURCE_FOR_STYLE)capture_internal_exceptionsensure_integration_enabledevent_from_exceptionparse_versiontransaction_from_function)IntegrationDidNotEnable$_DEFAULT_FAILED_REQUEST_STATUS_CODES_check_minimum_version)SentryWsgiMiddleware)RequestExtractor)TYPE_CHECKING)Set)_ScopedResponse)Any)Dict)Callable)Optional)
FileUpload	FormsDictLocalRequest)EventProcessorEvent)BottleHTTPResponseRouterequest__version__zBottle not installed)endpointurlc                   @   sL   e Zd ZdZde ZdZ	deddeddd	d
fddZe	dddZ
d
S )BottleIntegrationbottlez
auto.http. r   )failed_request_status_codestransaction_styler$   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
ValueErrorr%   r$   )selfr%   r$    r*   T/home/ubuntu/vllm_env/lib/python3.10/site-packages/sentry_sdk/integrations/bottle.py__init__8   s   
zBottleIntegration.__init__c               	      s   t t} tt|  tj tt 								d fd	d
}|t_tjt	dfdd}|t_t
jt	dddtdtddffdd}|t
_d S )Nr)   r   environDict[str, str]start_responseCallable[..., Any]r&   r   c                    s"   t  fddtjd}|||S )Nc                     s    g| R i |S Nr*   )akw)old_appr)   r*   r+   <lambda>R   s    zOBottleIntegration.setup_once.<locals>.sentry_patched_wsgi_app.<locals>.<lambda>)span_origin)r   r!   origin)r)   r-   r/   
middleware)r4   r)   r+   sentry_patched_wsgi_appM   s
   
z=BottleIntegration.setup_once.<locals>.sentry_patched_wsgi_appr   Dict[str, Any]c                    sN   t  t}|d u r | |S t  }d|_|t| t|  | |}|S )Nr"   )	
sentry_sdk
get_clientget_integrationr!   get_isolation_scope_nameadd_event_processor_make_request_event_processorbottle_request)r)   r-   integrationscoperes)
old_handler*   r+   _patched_handle\   s   


z5BottleIntegration.setup_once.<locals>._patched_handler   argskwargsc                    sP   | g|R i |t  t  d u rS dtdtddf fdd}|S )NrI   rJ   r&   r   c               
      sb   z	| i |}W n t y } zt|dd |d }~ww t|tr/|j jv r/t|dd |S )NF)handledT)	Exception_capture_exception
isinstancer   status_coder$   )rI   rJ   rF   	exceptionrD   prepared_callbackr*   r+   wrapped_callbacky   s   zUBottleIntegration.setup_once.<locals>.patched_make_callback.<locals>.wrapped_callback)r<   r=   r>   r!   object)r)   rI   rJ   rS   )old_make_callbackrQ   r+   patched_make_callbacko   s   z;BottleIntegration.setup_once.<locals>.patched_make_callback)r)   r   r-   r.   r/   r0   r&   r   )r)   r   r-   r;   r&   r   )r   BOTTLE_VERSIONr   r!   r   __call__r   _handle	functoolswrapsr   _make_callbackrT   )versionr:   rH   rV   r*   )r4   rG   rU   r+   
setup_onceF   s<   


zBottleIntegration.setup_once)r   )r&   N)__name__
__module____qualname__
identifierr7   r%   r
   strr,   staticmethodr^   r*   r*   r*   r+   r!   2   s     

r!   c                   @   sT   e Zd ZdddZdddZdefddZdd
dZdddZddde	fddZ
dS )BottleRequestExtractorr&   r.   c                 C      | j jS r1   )r   r-   r9   r*   r*   r+   env      zBottleRequestExtractor.envc                 C   rf   r1   )r   cookiesr9   r*   r*   r+   ri      rh   zBottleRequestExtractor.cookiesc                 C   s   | j j S r1   )r   bodyreadr9   r*   r*   r+   raw_data   s   zBottleRequestExtractor.raw_datar   c                 C   s   |   rd S | jj S r1   )is_jsonr   formsdecoder9   r*   r*   r+   form   s   zBottleRequestExtractor.formOptional[Dict[str, str]]c                 C   s   |   rd S | jjS r1   )rm   r   filesr9   r*   r*   r+   rr      s   zBottleRequestExtractor.filesfiler   c                 C   s   |j S r1   )content_length)r)   rs   r*   r*   r+   size_of_file   s   z#BottleRequestExtractor.size_of_fileN)r&   r.   )r&   r   )r&   rq   )r_   r`   ra   rg   ri   bytesrl   rp   rr   intru   r*   r*   r*   r+   re      s    



re   eventr   r%   r   r   r&   c                 C   s   d}|dkrz|j jpd}W n% ty   Y nw |dkr4z|j jp(t|j jp(d}W n	 ty3   Y nw || d< dt| i| d< d S )Nr#   r    r   transactionsourcetransaction_info)routeruleRuntimeErrornamer   callbackr   )rx   r%   r   r   r*   r*   r+    _set_transaction_name_and_source   s&   
r   appr   r   rD   r   c                    s   d fdd}|S )	Nrx   r   hintdict[str, Any]r&   c                    sH   t |  j t  t|  W d    | S 1 sw   Y  | S r1   )r   r%   r   re   extract_into_event)rx   r   rD   r   r*   r+   event_processor   s   
z6_make_request_event_processor.<locals>.event_processor)rx   r   r   r   r&   r   r*   )r   r   rD   r   r*   r   r+   rB      s   rB   rP   rK   c                 C   s0   t | t jd|dd\}}tj||d d S )Nr"   )typerK   )client_options	mechanism)r   )r   r<   r=   optionscapture_event)rP   rK   rx   r   r*   r*   r+   rM      s   
rM   )r   r   r   r   rD   r!   r&   r   )4rZ   r<   sentry_sdk.tracingr   sentry_sdk.utilsr   r   r   r   r   sentry_sdk.integrationsr   r	   r
   r   sentry_sdk.integrations.wsgir   $sentry_sdk.integrations._wsgi_commonr   typingr   collections.abcr   r   r   r   r   r   r"   r   r   r   sentry_sdk._typesr   r   r   r   r   r   rC   r   rW   ImportErrorr'   r!   re   rc   r   rB   BaseExceptionboolrM   r*   r*   r*   r+   <module>   sX     [

