o
    0 i!                     @   sH  d dl Z d dlZd dlmZ d dlmZmZmZ d dlmZ d dl	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 d dlmZmZmZmZmZmZ d dlmZ dZdZ erd 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( e"de#de!f dZ)dd Z*G dd deZ+dd Z,dd Z-dS )    N)deepcopy)datetime	timedeltatimezone)environ)continue_trace)OP)Integration)_filter_headers)should_send_default_pii)TransactionSource)AnnotatedValuecapture_internal_exceptionsevent_from_exceptionloggerTimeoutThreadreraise)TYPE_CHECKINGg      ?g     @@)Any)TypeVar)Callable)Optional)EventProcessorEventHintF.)boundc                    s   t   fdd}|S )Nc                    s  t  }|t}|d u r | |g|R i |S td}|s1td  | |g|R i |S t|}t	
tj}t  }t 9 |  |t||| |dtd d }	|jrv|tkrv|t }
t|
||t  d}	|	  W d    n1 sw   Y  i }t|dr|j}t|tjtddtjtjd	}tdtd
tdtdtdd|d}t j ||do zZz& | |g|R i |W W |	r|	!  |"  W  d    W  d    S  t#y   t$% }t&||j'dddd\}}t j(||d t)|  Y nw W |	r|	!  |"  n|	r#|	!  |"  w W d    n1 s3w   Y  W d    d S W d    d S 1 sLw   Y  d S )NZFUNCTION_TIMEOUT_SECzOThe configured timeout could not be fetched from Cloud Functions configuration.Z
gcp_regionFUNCTION_REGION)isolation_scopeZcurrent_scopeheadersFUNCTION_NAME )opnamesourceoriginENTRY_POINTFUNCTION_IDENTITYGCP_PROJECT)function_namefunction_entry_pointfunction_identityfunction_regionfunction_project)Zgcp_env	gcp_event)Zcustom_sampling_contextgcpF)typeZhandled)Zclient_optionsZ	mechanism)hint)*
sentry_sdkZ
get_clientZget_integrationGcpIntegrationr   getr   debugintr   nowr   utcr   r   Zclear_breadcrumbsZadd_event_processor_make_request_event_processorZset_tagtimeout_warningTIMEOUT_WARNING_BUFFERr   Zget_current_scopestarthasattrr   r   r   ZFUNCTION_GCPr   Z	COMPONENTr%   Zstart_transactionstopflush	Exceptionsysexc_infor   optionsZcapture_eventr   )Zfunctionhandlerr.   argskwargsclientZintegrationZconfigured_timeinitial_timescopeZtimeout_threadZwaiting_timer   ZtransactionZsampling_contextrB   Zsentry_eventr1   func g/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/sentry_sdk/integrations/gcp.pysentry_func*   s   



	
4

 /$z_wrap_func.<locals>.sentry_func)	functoolswraps)rJ   rM   rK   rI   rL   
_wrap_func(   s   VrP   c                   @   s0   e Zd ZdZde Zd	ddZedd ZdS )
r3   r/   zauto.function.Fc                 C   s
   || _ d S )N)r:   )selfr:   rK   rK   rL   __init__   s   
zGcpIntegration.__init__c                  C   s:   dd l } t| dstd d S | j}t|jj|j_d S )Nr   	worker_v1zFGcpIntegration currently supports only Python 3.7 runtime environment.)__main__r=   r   warningrS   rP   ZFunctionHandlerZinvoke_user_function)Zgcp_functionsZworker1rK   rK   rL   
setup_once   s   
zGcpIntegration.setup_onceN)F)__name__
__module____qualname__
identifierr%   rR   staticmethodrV   rK   rK   rK   rL   r3      s    

r3   c                    s    fdd}|S )Nc                    s  t tj}| }|tdd }| di }tdtdtdtdtd| d	|d
< dt|i|d< | di }d	td|d< t
drTj|d< t
drajd|d< t
drmtj|d< t r{t
drzj|d< nt
drt |d< t|| d< | S )N   )Zmillisecondsextrar    r&   r'   r   r(   )r)   r*   r+   r,   r-   execution_duration_in_millisZconfigured_timeout_in_secondszgoogle cloud functionsurlzgoogle cloud logsrequestz	gcp:///{}methodquery_stringzutf-8r   data)r   r7   r   r8   r   
setdefaultr   r4   _get_google_cloud_logs_urlformatr=   ra   rb   decoder
   r   r   rc   r   Zremoved_because_raw_datar   )eventr1   
final_timeZ	time_diffr^   r]   r`   configured_timeoutr.   rG   rK   rL   event_processor   s<   







z6_make_request_event_processor.<locals>.event_processorrK   )r.   rk   rG   rl   rK   rj   rL   r9      s   1r9   c                 C   sH   | t dd }d}djtdtdtd| |||d}|S )	z
    Generates a Google Cloud Logs console URL based on the environment variables
    Arguments:
        final_time {datetime} -- Final time
    Returns:
        str -- Google Cloud Logs Console URL to logs.
    r\   )hoursz%Y-%m-%dT%H:%M:%SZaY  https://console.cloud.google.com/logs/viewer?project={project}&resource=cloud_function%2Ffunction_name%2F{function_name}%2Fregion%2F{region}&minLogLevel=0&expandAll=false&timestamp={timestamp_end}&customFacets=&limitCustomFacetWidth=true&dateRangeStart={timestamp_start}&dateRangeEnd={timestamp_end}&interval=PT1H&scrollTimestamp={timestamp_end}r(   r    r   )projectr)   regionZtimestamp_endZtimestamp_start)r   rf   r   r4   strftime)ri   Zhour_agoformatstringr_   rK   rK   rL   re      s   	re   ).rN   rA   copyr   r   r   r   osr   r2   Zsentry_sdk.apir   Zsentry_sdk.constsr   Zsentry_sdk.integrationsr	   Z$sentry_sdk.integrations._wsgi_commonr
   Zsentry_sdk.scoper   Zsentry_sdk.tracingr   Zsentry_sdk.utilsr   r   r   r   r   r   typingr   r;   ZMILLIS_TO_SECONDSr   r   r   r   Zsentry_sdk._typesr   r   r   r   rP   r3   r9   re   rK   rK   rK   rL   <module>   s6     	\7