U 췀gE@sddlZddlZddlZddlZddlmZddlmZmZmZddl m Z ddl Z ddl m Z ddlmZddlmZddlmZdd lmZmZmZmZmZmZmZdd lmZdd lmZdd l m!Z!e!r*dd l m"Z"ddl m#Z#ddl m$Z$ddl m%Z%ddl&m'Z'm(Z(m)Z)e#de$de"fdZ*dZ+dZ,ddZ-ddZ.ddZ/GdddeZ0dd Z1d!d"Z2d#d$Z3d%d&Z4d'd(Z5d)d*Z6dS)+N)deepcopy)datetime timedeltatimezone)environ)continue_trace)OP)should_send_default_pii)TRANSACTION_SOURCE_COMPONENT)AnnotatedValuecapture_internal_exceptionsensure_integration_enabledevent_from_exceptionlogger TimeoutThreadreraise) Integration)_filter_headers) TYPE_CHECKING)Any)TypeVar)Callable)Optional)EventProcessorEventHintF.)boundig@@csttfdd}|S)Nc st}tltt}|rZt|rZt||j dddd\}}tj ||dnt t |d}t |W5QRX||S)N aws_lambdaFtypeZhandledZclient_options mechanismhint) sentry_sdk get_clientr Zget_isolation_scopeclear_breadcrumbssysexc_infoallroptions capture_event_event_from_error_jsonjsonloads)argskwargsclientr* sentry_eventr$ init_errorP/opt/hc_python/lib/python3.8/site-packages/sentry_sdk/integrations/aws_lambda.pysentry_init_error-s   z+_wrap_init_error..sentry_init_error)r AwsLambdaIntegration)r6r9r7r5r8_wrap_init_error+sr;cstfdd}|S)Nc st}|t}|dkr,||f||St|trTt|dkrT|d}t|}n|}d}t|tsji}|}t } d} t | | t |||| d|jdd|dkr| dd| d||jr|tkr|tt} t| |t} | W5QRX|d i} t| ts6i} t| tj|jttjd } tj| ||d d zz0||f||WWlW5QRW5QRStk rt !}t"||j#d ddd\}}tj$||dt%|YnXW5| r| XW5QRXW5QRXdS)Nr%rZ aws_region:Z batch_requestT batch_sizeheaders)opnamesourceorigin) aws_event aws_context)Zcustom_sampling_contextrFrr!r#)&r&r'get_integrationr: isinstancelistlendictget_remaining_time_in_millisZisolation_scoper r(Zadd_event_processor_make_request_event_processorZset_taginvoked_function_arnsplittimeout_warningTIMEOUT_WARNING_BUFFERMILLIS_TO_SECONDSrstartgetrrZ FUNCTION_AWS function_namer rCZstart_transactionstop Exceptionr)r*rr,r-r)rDrEr1r2r3 integrationZ request_datar>Zconfigured_timescopeZtimeout_threadZ waiting_timer?Z transactionr*r4r$handlerr7r8sentry_handlerJs        0 z%_wrap_handler..sentry_handler) functoolswraps)rZr[r7rYr8 _wrap_handlerHsgr^c Cs8t(t}|t}|dk r*|W5QRXdSN)r r&r'rFr:flush)r3rWr7r7r8 _drain_queues  rac@s0eZdZdZdeZd ddZeddZdS) r:rzauto.function.FcCs ||_dSr_)rO)selfrOr7r7r8__init__szAwsLambdaIntegration.__init__cst}|stddSt|ds0tddSt|d}|r|jfdd}||_|jfdd}||_|jfd d }||_nPt|jj |j_ |jfd d}||_d d }||jj |j_ ||jj |j_ dS)NzfNot running in AWS Lambda environment, AwsLambdaIntegration disabled (could not find bootstrap module)handle_event_requestzjNot running in AWS Lambda environment, AwsLambdaIntegration disabled (could not find handle_event_request)handle_http_requestcst|}|f||Sr_r^request_handlerr1r2old_handle_event_requestr7r8sentry_handle_event_requestszDAwsLambdaIntegration.setup_once..sentry_handle_event_requestcst|}|f||Sr_rfrg)old_handle_http_requestr7r8sentry_handle_http_requestszCAwsLambdaIntegration.setup_once..sentry_handle_http_requestcst||Sr_rar1r2) old_to_jsonr7r8sentry_to_jsonsz7AwsLambdaIntegration.setup_once..sentry_to_jsoncst|}||f||Sr_rf)Zlambda_runtime_clientrhr1r2rir7r8rkscsfdd}|S)Ncst||Sr_rnrofr7r8innerszKAwsLambdaIntegration.setup_once.._wrap_post_function..innerr7)rsrtr7rrr8_wrap_post_functions z._wrap_post_function) get_lambda_bootstraprwarninghasattrrdreto_jsonr;ZLambdaRuntimeClientZpost_init_errorZpost_invocation_resultZpost_invocation_error)Zlambda_bootstrapZpre_37rkrmrqrur7)rjrlrpr8 setup_oncesJ       zAwsLambdaIntegration.setup_onceN)F)__name__ __module__ __qualname__ identifierrCrc staticmethodrzr7r7r7r8r:s   r:cCsbdtjkrtjdSdtjkrZtjd}t|drFt|jdrF|jjSt|drV|jS|SdSdS)N bootstrap__main__awslambdaricmain)r)modulesrxrr)moduler7r7r8rv$s      rvcs$ttj}|ffdd }|S)Nc s\}|}|di}jjjj||d|d<t|jjd|d<| di}dkrtd|d<t |d <d krd |d <d krt d |d <t r2|d i} d}|dkri}| d} | dk r|d| | d} | dk r|d| dkrL dd|d<n ddrLt |d<t||d<|S)Nextra)rTfunction_versionrMaws_request_idZexecution_duration_in_millisZremaining_time_in_millislambda)url log_group log_streamzcloudwatch logsrequestZ httpMethodmethodrZqueryStringParametersZ query_stringr?useridentityZuserArnidZsourceIp ip_addressbodydata)rK setdefaultrTrrMr_get_cloudwatch_logs_urllog_group_namelog_stream_namerS_get_urlrr r Zremoved_because_raw_datar) r4r$ start_timeZremaining_time_in_milisZ exec_durationrrZ user_inforriprErDconfigured_timeoutr7r8event_processorMsN                z6_make_request_event_processor..event_processor)rnowrutc)rDrErrrr7rr8rLIs =rLcCs`|dd}|d}|dkr"i}|dd}|dd}|rT|rT|rTd|||Sd|jS)Npathr?HostzX-Forwarded-Protoz {}://{}{}zawslambda:///{})rSformatrT)rDrErr?hostprotor7r7r8rs     rc Csdd}tdd}dj|dr"dnd||j|j|tdd |t t j td d |d }|S) z Generates a CloudWatchLogs console URL based on the context object Arguments: aws_context {Any} -- context from lambda handler Returns: str -- AWS Console URL to logs. z%Y-%m-%dT%H:%M:%SZZ AWS_REGIONrzhttps://console.{domain}/cloudwatch/home?region={region}#logEventViewer:group={log_group};stream={log_stream};start={start_time};end={end_time}zcn-z amazonaws.cnzaws.amazon.comr%)seconds)domainregionrrrZend_time) rrSr startswithrrrstrftimerrrr)rEr formatstringrrr7r7r8rs  rc CsVg}|D]H}td|}|r|\}}}t|}||||dddddq|S)Nz File "(.+)", line (\d+), in (.+))filenamefunctionlinenovarsZ pre_contextZ context_lineZ post_context)rematchstripgroupsintappend)Z formatted_tbframesframer file_name line_number func_namer7r7r8_parse_formatted_tracebacks" rc Cs@dd|d|ddt|dgiddd d gid }|S) a9 Converts the error JSON from AWS Lambda into a Sentry error event. This is not a full fletched event, but better than nothing. This is an example of where AWS creates the error JSON: https://github.com/aws/aws-lambda-python-runtime-interface-client/blob/2.2.1/awslambdaric/bootstrap.py#L479 errorvaluesZ errorTypeZ errorMessagerZ stackTracerFr)r valueZ stacktracer")level exception)rSr)Z error_jsoneventr7r7r8r.s"  r.)7r\r/rr)copyrrrrosrr&Zsentry_sdk.apirZsentry_sdk.constsrZsentry_sdk.scoper Zsentry_sdk.tracingr Zsentry_sdk.utilsr r r rrrrZsentry_sdk.integrationsrZ$sentry_sdk.integrations._wsgi_commonrtypingrrrrrZsentry_sdk._typesrrrrrPrQr;r^rar:rvrLrrrr.r7r7r7r8sD      $       m d%D