a
    xd                     @   s   d dl m Z mZ d dlmZ d dlZd dlmZmZ d dlm	Z	m
Z
 d dlmZ d dlmZmZmZmZmZ d dlmZ d d	l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 )    )datetime	timedelta)environN)Hub_should_send_default_pii)TRANSACTION_SOURCE_COMPONENTTransaction)reraise)AnnotatedValuecapture_internal_exceptionsevent_from_exceptionloggerTimeoutThread)Integration)_filter_headers)MYPYg      ?g     @@)Any)TypeVar)Callable)Optional)EventProcessorEventHintF.)boundc                    s    fdd}|S )Nc                    s  t j}|t}|d u r0 | |g|R i |S |j}td}|sftd  | |g|R i |S t	|}t
 }| }	t h |	  |	t||| |	dtd d }
|jr|tkr|t }t||}
|
  W d    n1 s0    Y  i }t|dr|j}tj|dtddtd	}tdtd
tdtdtdd|d}|j||d zzN | |g|R i |W W |
r|
  |  W  d    W  d    S  ty   t  }t!||j"dddd\}}|j#||d t$|  Y n0 W |
r"|
  |  n|
r:|
  |  0 W d    n1 sZ0    Y  W d    n1 sz0    Y  d S )NZFUNCTION_TIMEOUT_SECzOThe configured timeout could not be fetched from Cloud Functions configuration.Z
gcp_regionFUNCTION_REGIONheaderszserverless.functionFUNCTION_NAME )opnamesource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)%r   currentZget_integrationGcpIntegrationclientr   getr   debugintr   utcnowZ
push_scoper   Zclear_breadcrumbsZadd_event_processor_make_request_event_processorZset_tagtimeout_warningTIMEOUT_WARNING_BUFFERr   starthasattrr   r   Zcontinue_from_headersr   Zstart_transactionstopflush	Exceptionsysexc_infor   optionsZcapture_eventr	   )Zfunctionhandlerr*   argskwargsZhubZintegrationr0   Zconfigured_timeinitial_timeZscopeZtimeout_threadZwaiting_timer   ZtransactionZsampling_contextr>   Zsentry_eventr-   func S/var/www/html/Ranjet/env/lib/python3.9/site-packages/sentry_sdk/integrations/gcp.pysentry_func%   s    


&



z_wrap_func.<locals>.sentry_funcrE   )rD   rG   rE   rC   rF   
_wrap_func#   s    RrH   c                   @   s&   e Zd ZdZdddZedd ZdS )	r/   r+   Fc                 C   s
   || _ d S )N)r6   )selfr6   rE   rE   rF   __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__r9   r   warningrK   rH   ZFunctionHandlerZinvoke_user_function)Zgcp_functionsZworker1rE   rE   rF   
setup_once   s    
zGcpIntegration.setup_onceN)F)__name__
__module____qualname__
identifierrJ   staticmethodrN   rE   rE   rE   rF   r/   z   s   
r/   c                    s    fdd}|S )Nc                    s  t  }| }|jt }| 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rj
|d< t	drjd|d< t	drtj|d< t rt	drj|d< n$t	drtddg dgi|d< || d< | S )N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   datar   rem)z!rawxr   r   )r   r4   microsecondsMILLIS_TO_SECONDS
setdefaultr   r1   _get_google_cloud_logs_urlformatr9   rX   rY   decoder   r   r   rZ   r
   )eventr-   
final_timeZ	time_diffrU   rT   rW   configured_timeoutr*   rB   rE   rF   event_processor   s:    






z6_make_request_event_processor.<locals>.event_processorrE   )r*   rf   rB   rg   rE   re   rF   r5      s    1r5   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.
       )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%   ZregionZtimestamp_endZtimestamp_start)r   ra   r   r1   strftime)rd   Zhour_agoformatstringrV   rE   rE   rF   r`      s    	r`   )(r   r   osr   r=   Zsentry_sdk.hubr   r   Zsentry_sdk.tracingr   r   Zsentry_sdk._compatr	   Zsentry_sdk.utilsr
   r   r   r   r   Zsentry_sdk.integrationsr   Z$sentry_sdk.integrations._wsgi_commonr   Zsentry_sdk._typesr   r7   r^   typingr   r   r   r   r   r   r   r   rH   r/   r5   r`   rE   rE   rE   rF   <module>   s,   W7