o
    i                      @   sf  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dZ*G dd deZ+					 			!d)d"d#Z,d$d%de-f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.)boundfuncreturnc                    s0   t  										d	 fdd}|S )
Nfunctionhandlerr   	gcp_eventargskwargsr   c                    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 )NFUNCTION_TIMEOUT_SECzOThe configured timeout could not be fetched from Cloud Functions configuration.
gcp_regionFUNCTION_REGION)isolation_scope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)gcp_envr    )custom_sampling_contextgcpF)typehandled)client_options	mechanism)hint)*
sentry_sdk
get_clientget_integrationGcpIntegrationr   getr   debugintr   nowr   utcr&   r   clear_breadcrumbsadd_event_processor_make_request_event_processorset_tagtimeout_warningTIMEOUT_WARNING_BUFFERr   get_current_scopestarthasattrr(   r   r   FUNCTION_GCPr   	COMPONENTr.   start_transactionstopflush	Exceptionsysexc_infor   optionscapture_eventr   )r   r    r!   r"   clientintegrationconfigured_timeinitial_timescopetimeout_threadwaiting_timer(   transactionsampling_contextrX   sentry_eventr>   r    X/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/sentry_sdk/integrations/gcp.pysentry_func)   s   



	
4

 /$z_wrap_func.<locals>.sentry_func)
r   r   r    r   r!   r   r"   r   r   r   )	functoolswraps)r   rh   rf   re   rg   
_wrap_func(   s   Wrk   c                   @   s<   e Zd ZdZde ZddeddfddZedd	d
ZdS )rB   r9   zauto.function.FrL   r   Nc                 C   s
   || _ d S )N)rL   )selfrL   rf   rf   rg   __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__rP   r   warningrn   rk   FunctionHandlerinvoke_user_function)gcp_functionsworker1rf   rf   rg   
setup_once   s   
zGcpIntegration.setup_once)F)r   N)	__name__
__module____qualname__
identifierr.   boolrm   staticmethodru   rf   rf   rf   rg   rB      s    
rB   r    r   configured_timeoutr^   r   c                    s   d	 fdd}|S )
Neventr   r>   r   r   Optional[Event]c                    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   )millisecondsextrar)   r/   r0   r%   r1   )r2   r3   r4   r5   r6   execution_duration_in_millisconfigured_timeout_in_secondszgoogle cloud functionsurlzgoogle cloud logsrequestz	gcp:///{}methodquery_stringzutf-8r(   data)r   rF   r   rG   r   
setdefaultr   rC   _get_google_cloud_logs_urlformatrP   r   r   decoder
   r(   r   r   r   removed_because_raw_datar   )r}   r>   
final_time	time_diffr   r   r   r|   r    r^   rf   rg   event_processor   s<   







z6_make_request_event_processor.<locals>.event_processor)r}   r   r>   r   r   r~   rf   )r    r|   r^   r   rf   r   rg   rJ      s   /rJ   r   r   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}r1   r)   r%   )projectr2   regiontimestamp_endtimestamp_start)r   r   r   rC   strftime)r   hour_agoformatstringr   rf   rf   rg   r      s   r   )r   r   r   r   )r    r   r|   r   r^   r   r   r   )/ri   rW   copyr   r   r   r   osr   r?   sentry_sdk.apir   sentry_sdk.constsr   sentry_sdk.integrationsr	   $sentry_sdk.integrations._wsgi_commonr
   sentry_sdk.scoper   sentry_sdk.tracingr   sentry_sdk.utilsr   r   r   r   r   r   typingr   rM   MILLIS_TO_SECONDSr   r   r   r   sentry_sdk._typesr   r   r   r   rk   rB   rJ   strr   rf   rf   rf   rg   <module>   sF     	
\
5