U 췀g^ @sJddlZddlZddlmZddlmZmZmZddlmZddl Z ddl m Z ddl m Z ddlmZddlmZdd lmZdd lmZdd lmZmZmZmZmZmZdd lmZd ZdZ erddlm!Z!ddlm"Z"ddlm#Z#ddlm$Z$ddl%m&Z&m'Z'm(Z(e"de#de!fdZ)ddZ*GdddeZ+ddZ,ddZ-dS)N)deepcopy)datetime timedeltatimezone)environ)continue_trace)OP) Integration)_filter_headers)should_send_default_pii)TRANSACTION_SOURCE_COMPONENT)AnnotatedValuecapture_internal_exceptionsevent_from_exceptionlogger TimeoutThreadreraise) TYPE_CHECKINGg?g@@)Any)TypeVar)Callable)Optional)EventProcessorEventHintF.)boundcstfdd}|S)Nc s t}|t}|dkr,||f||Std}|sVtd||f||St|}t t j }t }t^||t||||dtdd} |jr|tkr|t} t| |} | W5QRXi} t|dr|j} t| tjtddttjd} tdtd td tdtd d |d } tj| | dzz0||f||WWlW5QRW5QRSt!k rt"#}t$||j%dddd\}}tj&||dt'|YnXW5| r| | XW5QRXW5QRXdS)NZFUNCTION_TIMEOUT_SECzOThe configured timeout could not be fetched from Cloud Functions configuration.Z gcp_regionFUNCTION_REGIONheaders FUNCTION_NAME)opnamesourceorigin ENTRY_POINTFUNCTION_IDENTITY GCP_PROJECT) function_namefunction_entry_pointfunction_identityfunction_regionfunction_project)Zgcp_env gcp_event)Zcustom_sampling_contextgcpF)typeZhandled)Zclient_optionsZ mechanism)hint)( sentry_sdkZ get_clientZget_integrationGcpIntegrationrgetrdebugintrnowrutcZisolation_scoperZclear_breadcrumbsZadd_event_processor_make_request_event_processorZset_tagtimeout_warningTIMEOUT_WARNING_BUFFERrstarthasattrrrrZ FUNCTION_GCPr r$Zstart_transactionstopflush Exceptionsysexc_inforoptionsZ capture_eventr)Zfunctionhandlerr-argskwargsclientZ integrationZconfigured_time initial_timescopeZtimeout_threadZ waiting_timerZ transactionZsampling_contextrAZ sentry_eventr0funcI/opt/hc_python/lib/python3.8/site-packages/sentry_sdk/integrations/gcp.py sentry_func*s         0 z_wrap_func..sentry_func) functoolswraps)rIrLrJrHrK _wrap_func(sQrOc@s0eZdZdZdeZd ddZeddZdS) r2r.zauto.function.FcCs ||_dS)N)r9)selfr9rJrJrK__init__szGcpIntegration.__init__cCs:ddl}t|ds tddS|j}t|jj|j_dS)Nr worker_v1zFGcpIntegration currently supports only Python 3.7 runtime environment.)__main__r<rwarningrRrOZFunctionHandlerZinvoke_user_function)Z gcp_functionsZworker1rJrJrK setup_onces zGcpIntegration.setup_onceN)F)__name__ __module__ __qualname__ identifierr$rQ staticmethodrUrJrJrJrKr2s   r2csfdd}|S)Ncsttj}|}|tdd}|di}tdtdtdtdtd|d |d <d t|i|d <|d i}d td|d <t drj |d<t drˆj d|d<t drtj|d<trt drj|d<nt drt|d<t||d <|S)N)Z millisecondsextrarr%r&rr')r(r)r*r+r,execution_duration_in_millisZconfigured_timeout_in_secondszgoogle cloud functionsurlzgoogle cloud logsrequestz gcp:///{}method query_stringzutf-8rdata)rr6rr7r setdefaultrr3_get_google_cloud_logs_urlformatr<r`radecoder rr rbr Zremoved_because_raw_datar)eventr0 final_timeZ time_diffr]r\r_configured_timeoutr-rFrJrKevent_processors<             z6_make_request_event_processor..event_processorrJ)r-rjrFrkrJrirKr8s1r8cCsH|tdd}d}djtdtdtd||||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:%SZaYhttps://console.cloud.google.com/logs/viewer?project={project}&resource=cloud_function%2Ffunction_name%2F{function_name}%2Fregion%2F{region}&minLogLevel=0&expandAll=false×tamp={timestamp_end}&customFacets=&limitCustomFacetWidth=true&dateRangeStart={timestamp_start}&dateRangeEnd={timestamp_end}&interval=PT1H&scrollTimestamp={timestamp_end}r'rr)projectr(regionZ timestamp_endZtimestamp_start)rrerr3strftime)rhZhour_ago formatstringr^rJrJrKrds rd).rMr@copyrrrrosrr1Zsentry_sdk.apirZsentry_sdk.constsrZsentry_sdk.integrationsr Z$sentry_sdk.integrations._wsgi_commonr Zsentry_sdk.scoper Zsentry_sdk.tracingr Zsentry_sdk.utilsr rrrrrtypingrr:ZMILLIS_TO_SECONDSrrrrZsentry_sdk._typesrrrrrOr2r8rdrJrJrJrKs4             W7