o
    ii>                      @   sL  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mZ d dlmZ d d	lmZ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% 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)	timedelta)environ)continue_trace)OP)Hub_should_send_default_pii)TRANSACTION_SOURCE_COMPONENT)datetime_utcnowreraise)AnnotatedValuecapture_internal_exceptionsevent_from_exceptionloggerTimeoutThread)Integration)_filter_headers)TYPE_CHECKINGg      ?g     @@)datetime)Any)TypeVar)Callable)Optional)EventProcessorEventHintF.)boundc                    s    fdd}|S )Nc                    s  t j}|t}|d u r | |g|R i |S |j}td}|s3td  | |g|R i |S t	|}t
 }|  }	t 4 |	  |	t||| |	dtd d }
|jrp|tkrp|t }t||}
|
  W d    n1 szw   Y  i }t|dr|j}t|tjtddtd}tdtd	td
tdtdd|d}|j||do zZz& | |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w W |
r|
  |  n|
r|
  |  w W d    n1 s*w   Y  W d    d S W d    d S 1 sCw   Y  d S )NFUNCTION_TIMEOUT_SECzOThe configured timeout could not be fetched from Cloud Functions configuration.
gcp_regionFUNCTION_REGIONheadersFUNCTION_NAME )opnamesourceENTRY_POINTFUNCTION_IDENTITYGCP_PROJECT)function_namefunction_entry_pointfunction_identityfunction_regionfunction_project)gcp_env	gcp_event)custom_sampling_contextgcpF)typehandled)client_options	mechanism)hint)%r   currentget_integrationGcpIntegrationclientr   getr   debugintr
   
push_scoper   clear_breadcrumbsadd_event_processor_make_request_event_processorset_tagtimeout_warningTIMEOUT_WARNING_BUFFERr   starthasattrr!   r   r   FUNCTION_GCPr	   start_transactionstopflush	Exceptionsysexc_infor   optionscapture_eventr   )functionhandlerr0   argskwargshubintegrationr;   configured_timeinitial_timescopetimeout_threadwaiting_timer!   transactionsampling_contextrN   sentry_eventr7   func Z/var/www/html/pca-backend/venv/lib/python3.10/site-packages/sentry_sdk/integrations/gcp.pysentry_func)   s   





.

 )$z_wrap_func.<locals>.sentry_funcr`   )r_   rb   r`   r^   ra   
_wrap_func'   s   Src   c                   @   s&   e Zd ZdZdddZedd ZdS )	r:   r2   Fc                 C   s
   || _ d S )N)rD   )selfrD   r`   r`   ra   __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__rG   r   warningrf   rc   FunctionHandlerinvoke_user_function)gcp_functionsworker1r`   r`   ra   
setup_once   s   
zGcpIntegration.setup_onceN)F)__name__
__module____qualname__
identifierre   staticmethodrm   r`   r`   r`   ra   r:      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rOj	|d< tdr\j
d|d< tdrhtj|d< t rvtdruj|d< ntdrt |d< t|| d< | S )Nextrar"   r'   r(   r    r)   )r*   r+   r,   r-   r.   execution_duration_in_millisconfigured_timeout_in_secondszgoogle cloud functionsurlzgoogle cloud logsrequestz	gcp:///{}methodquery_stringzutf-8r!   data)r
   microsecondsMILLIS_TO_SECONDS
setdefaultr   r<   _get_google_cloud_logs_urlformatrG   rx   ry   decoder   r!   r   rz   r   removed_because_raw_datar   )eventr7   
final_time	time_diffrt   rs   rw   configured_timeoutr0   rW   r`   ra   event_processor   s<   








z6_make_request_event_processor.<locals>.event_processorr`   )r0   r   rW   r   r`   r   ra   rB      s   1rB   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*   regiontimestamp_endtimestamp_start)r   r   r   r<   strftime)r   hour_agoformatstringrv   r`   r`   ra   r~      s   	r~   ).rM   copyr   r   r   osr   sentry_sdk.apir   sentry_sdk.constsr   sentry_sdk.hubr   r   sentry_sdk.tracingr	   sentry_sdk._compatr
   r   sentry_sdk.utilsr   r   r   r   r   sentry_sdk.integrationsr   $sentry_sdk.integrations._wsgi_commonr   sentry_sdk._typesr   rE   r|   typingr   r   r   r   r   r   r   r   rc   r:   rB   r~   r`   r`   r`   ra   <module>   s6    X7