
    tni=$                     n   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rd dlmZ d d	lmZ d d
lmZ d dlmZmZ 	 d dlZd dlmZ n# e$ r  ed          w xY w	 d dlZej        Zej        Z dZ!n# e$ r d dl"Zej#        Zej$        Z dZ!Y nw xY wdZ%de&d<   e!r( e            5  d dl'm(Z% ddd           n# 1 swxY w Y    G d de          Z) G d d          Z*dZ+ G d de          Z,d-dZ-d-dZ.d-dZ/de0dd de1fd!Z2dd de1fd"Z3d#d$d%e4d&d'ddfd(Z5	 	 	 	 	 	 d.d,Z6dS )/    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                   >    e Zd ZddZddZddZddZdd	ZddZdS )FalconRequestExtractorreturnDict[str, Any]c                     | j         j        S N)requestenvselfs    [/var/www/html/gpu-tools/venv/lib/python3.11/site-packages/sentry_sdk/integrations/falcon.pyr   zFalconRequestExtractor.env5   s    |    c                     | j         j        S r   )r   cookiesr   s    r    r#   zFalconRequestExtractor.cookies8   s    |##r!   Nc                     d S r    r   s    r    formzFalconRequestExtractor.form;       tr!   c                     d S r   r%   r   s    r    fileszFalconRequestExtractor.files>   r'   r!   Optional[str]c                 >    |                                  }|dk    rdS d S )Nr   z[REQUEST_CONTAINING_RAW_DATA])content_length)r   r,   s     r    raw_datazFalconRequestExtractor.raw_dataA   s+    
 ,,..A224r!   Optional[Dict[str, Any]]c                     d }t                      5  | j        j        }d d d            n# 1 swxY w Y   |t          ur|S d S r   )r	   r   _mediar   )r   cached_medias     r    jsonzFalconRequestExtractor.jsonL   s    (** 	/ 	/  <.L	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ },,ts   *..)r   r   r   N)r   r*   )r   r.   )	__name__
__module____qualname__r   r#   r&   r)   r-   r2   r%   r!   r    r   r   4   s               $ $ $ $      	 	 	 	     r!   r   c                   .    e Zd ZdZ	 	 	 	 	 	 	 	 	 	 d
d	ZdS )SentryFalconMiddlewarez9Captures exceptions in Falcon requests and send to Sentryreqr   respargskwargsr   Nc                     t          j                                        t                    }|d S t          j                    }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   r9   r:   r;   r<   integrationscopes          r    process_requestz&SentryFalconMiddleware.process_requesta   sf     !+--==>OPPF.00!!"?["Q"QRRRRRr!   )
r9   r   r:   r   r;   r   r<   r   r   N)r4   r5   r6   __doc__rI   r%   r!   r    r8   r8   ^   sa        CC	S	S %	S.3	S?D	S		S 	S 	S 	S 	S 	Sr!   r8   uri_templatepathc                   J    e Zd ZdZde ZdZd
deddfdZedd	            Z	dS )rB   r>   z
auto.http. rL   transaction_styler   Nc                 \    |t           vrt          d|dt           d          || _        d S )Nz%Invalid value for transaction_style: z (must be in ))TRANSACTION_STYLE_VALUES
ValueErrorrP   )r   rP   s     r    __init__zFalconIntegration.__init__v   sJ    $<<<*$$$&>&>&>@   "3r!   c                      t          t                    } t          t          |            t	                       t                       t                       d S r   )r   FALCON_VERSIONr   rB   _patch_wsgi_app_patch_handle_exception_patch_prepare_middleware)versions    r    
setup_oncezFalconIntegration.setup_once~   sJ    //0':::!!!!#####r!   )rL   r3   )
r4   r5   r6   
identifieroriginrP   strrU   staticmethodr\   r%   r!   r    rB   rB   p   ss        J&*&&F3 3# 34 3 3 3 3 $ $ $ \$ $ $r!   rB   r   c                  T    t           j        	 	 	 	 	 	 	 	 dfd} | t           _        d S )	Nr   
falcon.APIr   r   start_responser   c                      t          j                                        t                    }|  ||          S t	           fdt          j                  } |||          S )Nc                      | |          S r   r%   )envi
start_resporiginal_wsgi_appr   s     r    <lambda>zB_patch_wsgi_app.<locals>.sentry_patched_wsgi_app.<locals>.<lambda>   s    %6%6tT:%N%N r!   )span_origin)r?   r@   rA   rB   r   r^   )r   r   rc   rG   sentry_wrappedrh   s   `    r    sentry_patched_wsgi_appz0_patch_wsgi_app.<locals>.sentry_patched_wsgi_app   s{     !+--==>OPP$$T3???-NNNNN)0
 
 

 ~c>222r!   )r   rb   r   r   rc   r   r   r   )falcon_app_class__call__)rl   rh   s    @r    rX   rX      s[    (133!&38=3	3 3 3 3 3 3 !8r!   c                  z    t           j        t          t                    dfd            } | t           _        d S )Nr   rb   r;   r   r   c                    d x}}t                      5  t          d |D                       }t          d |D                       }d d d            n# 1 swxY w Y    | g|R  }|||S t          ||          rDt          |t	          j                    j        ddd          \  }}t	          j        ||           |S )Nc              3   D   K   | ]}t          |t                    |V  d S r   )
isinstance	Exception.0arguments     r    	<genexpr>zS_patch_handle_exception.<locals>.sentry_patched_handle_exception.<locals>.<genexpr>   s1      WW8z(I7V7VWhWWWWWWr!   c              3   N   K   | ] }t          |t          j                  |V  !d S r   )rr   r>   Responsert   s     r    rw   zS_patch_handle_exception.<locals>.sentry_patched_handle_exception.<locals>.<genexpr>   sG        %Hfo1V1V     r!   r>   F)typehandled)client_options	mechanism)hint)r	   next_exception_leads_to_http_5xxr   r?   r@   optionscapture_event)r   r;   exresponsewas_handledeventr~   original_handle_exceptions          r    sentry_patched_handle_exceptionz@_patch_handle_exception.<locals>.sentry_patched_handle_exception   sH   
 X(** 	 	WWtWWWWWB  )-    H	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 0/<t<<<:) 'H55 	7.)466>#+>>  KE4
 $U6666s   3AAA)r   rb   r;   r   r   r   )rm   _handle_exceptionr
   rB   )r   r   s    @r    rY   rY      sT     0 B 13LMM     NM: *I&&&r!   c            	      f    t           j        	 	 	 ddddddt          ddffd} | t           _        d S )	NF
middlewarer   independent_middlewareasgir   c                     |r | ||          S t          j                                        t                    }|t	                      g| pg z   }  | |          S r   )r?   r@   rA   rB   r8   )r   r   r   rG   original_prepare_middlewares       r    !sentry_patched_prepare_middlewarezD_patch_prepare_middleware.<locals>.sentry_patched_prepare_middleware   sv    
  	Y..z;QSWXXX +--==>OPP"0223z7GRHJ +*:7MNNNr!   )NFF)falcon_helpersprepare_middlewarebool)r   r   s    @r    rZ   rZ      sz    "0"C !(-O OO %O O 
	O O O O O O" )JN%%%r!   r   r   zfalcon.Responsec                     t          | t          j                  o| j        pd                    d          }t          | t          j        t          j        j        f           }|s|ot           pt          |          S )NrO   5)	rr   r>   	HTTPErrorstatus
startswithhttp_status
HTTPStatusFALCON3_has_http_5xx_status)r   r   is_server_erroris_unhandled_errors       r    r   r      s     V%566 BIO;W;W< <O (
Vv1<=   11 5+H55r!   c                 6    | j                             d          S )Nr   )r   r   )r   s    r    r   r      s    ?%%c***r!   r   r   rP   r   falcon.Requestc                 ^    |j         |j        d}||         | d<   dt          |         i| d<   d S )NrK   transactionsourcetransaction_info)rL   rM   r   )r   rP   r   name_for_styles       r     _set_transaction_name_and_sourcer      sJ      , N **;<E-!)+;<M+N OE
r!   r9   rG   r   c                      d fd}|S )Nr   r   r~   dict[str, Any]r   c                     t          | j                   t                      5  t                                        |            d d d            n# 1 swxY w Y   | S r   )r   rP   r	   r   extract_into_event)r   r~   rG   r9   s     r    event_processorz6_make_request_event_processor.<locals>.event_processor   s    (0MsSSS(** 	B 	B"3''::5AAA	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B s   #AAA)r   r   r~   r   r   r   r%   )r9   rG   r   s   `` r    rF   rF      s/           r!   r3   )r9   r   rG   rB   r   r   )7r?   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   r>   r   rW   ImportErrorfalcon.app_helpersapp_helpersr   Apprm   r   falcon.api_helpersapi_helpersAPIr   __annotations__falcon.requestr   r   r8   rS   rB   rX   rY   rZ   rs   r   r   r   r_   r   rF   r%   r!   r    <module>r      s        U U U U U U U U U U A A A A A A = = = = = = / / / / / /            !           877777777
/MMM4444444 / / /
,-
.
../'NzGG   'NzGGG %)! ( ( (
 ;	$	$	&	& ; ;::::::; ; ; ; ; ; ; ; ; ; ; ; ; ; ;' ' ' ' '- ' ' 'TS S S S S S S S 4 $ $ $ $ $ $ $ $08 8 8 8(!I !I !I !IHJ J J J.Y :K PT    $+#4 + + + + +PP'*P5EP	P P P P	(;     s0   
A A-1B B"!B"8CCC