o
    h"F                     @   s  d dl Z d dlZ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mZ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! 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Z+dZ,dd Z-dd Z.dd Z/G dd deZ0dd  Z1d!d" Z2d#d$ Z3d%d& Z4d'd( Z5d)d* Z6dS )+    N)deepcopy)datetime	timedeltatimezone)environ)continue_trace)OP)should_send_default_pii)TransactionSource)AnnotatedValuecapture_internal_exceptionsensure_integration_enabledevent_from_exceptionloggerTimeoutThreadreraise)Integration)_filter_headers)TYPE_CHECKING)Any)TypeVar)Callable)Optional)EventProcessorEventHintF.)boundi  g     @@c                    s   t t  fdd}|S )Nc                     s   t  }t D t    t }|r-t|r-t||j	dddd\}}t j
||d n| d }t|tr;t|}t|}t 
| W d    n1 sNw   Y   | i |S )N
aws_lambdaFtypehandledclient_options	mechanismhint   )
sentry_sdk
get_clientr   get_isolation_scopeclear_breadcrumbssysexc_infoallr   optionscapture_event
isinstancestrjsonloads_event_from_error_json)argskwargsclientr-   sentry_eventr&   
error_info
init_error d/var/www/html/pro-man-master/venv/lib/python3.10/site-packages/sentry_sdk/integrations/aws_lambda.pysentry_init_error-   s&   



z+_wrap_init_error.<locals>.sentry_init_error)r   AwsLambdaIntegration)r<   r?   r=   r;   r>   _wrap_init_error+   s   rA   c                    s   t   fdd}|S )Nc                    s  t  }|t}|d u r | |g|R i |S t| tr-t| dkr-| d }t| }n| }d}t|ts8i }| }t 	 }	d }
t
 I |	  |	t||| |	d|jdd  |dkrp|	dd |	d| |jr|tkr|t t }t||t }
|
  W d    n1 sw   Y  |d	i }t|tsi }t|tj|jtjtjd
}t j|| |ddd zRz" | |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 W d    n1 s#w   Y  W d    d S W d    d S 1 s<w   Y  d S )Nr'   r   
aws_region:   batch_requestT
batch_sizeheaders)opnamesourceorigin)	aws_eventaws_context)custom_sampling_contextr   Fr   r"   r%   )'r(   r)   get_integrationr@   r1   listlendictget_remaining_time_in_millisisolation_scoper   r+   add_event_processor_make_request_event_processorset_taginvoked_function_arnsplittimeout_warningTIMEOUT_WARNING_BUFFERMILLIS_TO_SECONDSr   startgetr   r   FUNCTION_AWSfunction_namer
   	COMPONENTrK   start_transactionstop	Exceptionr,   r-   r   r/   r0   r   )rL   rM   r6   r7   r8   integrationrequest_datarF   configured_timescopetimeout_threadwaiting_timerG   transactionr-   r9   r&   handlerr=   r>   sentry_handlerM   s   



 
8

 /$z%_wrap_handler.<locals>.sentry_handler)	functoolswraps)rm   rn   r=   rl   r>   _wrap_handlerK   s   grq   c                  C   s^   t  " t } | t}|d ur|   W d    d S W d    d S 1 s(w   Y  d S N)r   r(   r)   rO   r@   flush)r8   re   r=   r=   r>   _drain_queue   s   

"rt   c                   @   s0   e Zd ZdZde Zd	ddZedd ZdS )
r@   r   zauto.function.Fc                 C   s
   || _ d S rr   )rZ   )selfrZ   r=   r=   r>   __init__   s   
zAwsLambdaIntegration.__init__c                     s   t  } | std d S t| dstd d S t| d}|rE| j  fdd}|| _| jfdd}|| _| jfd	d
}|| _d S t| jj	| j_	| j  fdd}|| _dd }|| jj
| j_
|| jj| j_d S )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_requestc                       t | }  | g|R i |S rr   rq   request_handlerr6   r7   old_handle_event_requestr=   r>   sentry_handle_event_request      zDAwsLambdaIntegration.setup_once.<locals>.sentry_handle_event_requestc                    ry   rr   rz   r{   )old_handle_http_requestr=   r>   sentry_handle_http_request   r   zCAwsLambdaIntegration.setup_once.<locals>.sentry_handle_http_requestc                        t    | i |S rr   rt   r6   r7   )old_to_jsonr=   r>   sentry_to_json      z7AwsLambdaIntegration.setup_once.<locals>.sentry_to_jsonc                    s    t |} | |g|R i |S rr   rz   )lambda_runtime_clientr|   r6   r7   r}   r=   r>   r     s   c                    s    fdd}|S )Nc                     r   rr   r   r   fr=   r>   inner  r   zKAwsLambdaIntegration.setup_once.<locals>._wrap_post_function.<locals>.innerr=   )r   r   r=   r   r>   _wrap_post_function  s   z<AwsLambdaIntegration.setup_once.<locals>._wrap_post_function)get_lambda_bootstrapr   warninghasattrrw   rx   to_jsonrA   LambdaRuntimeClientpost_init_errorpost_invocation_resultpost_invocation_error)lambda_bootstrappre_37r   r   r   r   r=   )r~   r   r   r>   
setup_once   sJ   




zAwsLambdaIntegration.setup_onceN)F)__name__
__module____qualname__
identifierrK   rv   staticmethodr   r=   r=   r=   r>   r@      s    

r@   c                  C   s^   dt jv r
t jd S dt jv r-t jd } t| dr#t| jdr#| jjS t| dr+| jS | S d S )N	bootstrap__main__awslambdaricmain)r,   modulesr   r   r   )moduler=   r=   r>   r   '  s   




r   c                    s$   t tj}|f fdd	}|S )Nc                    sT     }| }| di } j j j j||d|d< t | j jd|d< | 	di }dv r:d |d< t
 |d	< d
v rKd
 |d< dv rWtd |d< t r| di }	d}|d u rki }|	d}	|	d urz|d|	 |	d}
|
d ur|d|
 dv r	dd|d< n	dd rt |d< t|| d< | S )Nextra)r`   function_versionrX   aws_request_idexecution_duration_in_millisremaining_time_in_millislambda)url	log_group
log_streamzcloudwatch logsrequest
httpMethodmethodr   queryStringParametersquery_stringrG   useridentityuserArnidsourceIp
ip_addressbody data)rS   
setdefaultr`   r   rX   r   _get_cloudwatch_logs_urllog_group_namelog_stream_namer^   _get_urlr   r	   r   removed_because_raw_datar   )r9   r&   
start_timeremaining_time_in_milisexec_durationr   r   	user_infor   r   iprM   rL   configured_timeoutr=   r>   event_processorP  sP   





z6_make_request_event_processor.<locals>.event_processor)r   nowr   utc)rL   rM   r   r   r   r=   r   r>   rV   L  s   =rV   c                 C   s`   |  dd }|  d}|d u ri }| dd }| dd }|r*|r*|r*d|||S d|jS )NpathrG   HostzX-Forwarded-Protoz	{}://{}{}zawslambda:///{})r^   formatr`   )rL   rM   r   rG   hostprotor=   r=   r>   r     s   
r   c              
   C   sd   d}t dd}dj|drdnd|| j| j|tdd	 |t	t
jtd
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:%SZ
AWS_REGIONr   zhttps://console.{domain}/cloudwatch/home?region={region}#logEventViewer:group={log_group};stream={log_stream};start={start_time};end={end_time}zcn-zamazonaws.cnzaws.amazon.comr'   )seconds   )domainregionr   r   r   end_time)r   r^   r   
startswithr   r   r   strftimer   r   r   r   )rM   r   formatstringr   r   r=   r=   r>   r     s   r   c                 C   sV   g }| D ]$}t d| }|r(| \}}}t|}||||d d d d d q|S )Nz File "(.+)", line (\d+), in (.+))filenamefunctionlinenovarspre_contextcontext_linepost_context)rematchstripgroupsintappend)formatted_tbframesframer   	file_nameline_number	func_namer=   r=   r>   _parse_formatted_traceback  s$   r   c              
   C   s@   dd|  d|  ddt|  dg iddd	d
gid}|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
    errorvalues	errorTypeerrorMessager   
stackTracer   Fr   )r    value
stacktracer$   )level	exception)r^   r   )
error_jsoneventr=   r=   r>   r5     s    

r5   )7ro   r3   r   r,   copyr   r   r   r   osr   r(   sentry_sdk.apir   sentry_sdk.constsr   sentry_sdk.scoper	   sentry_sdk.tracingr
   sentry_sdk.utilsr   r   r   r   r   r   r   sentry_sdk.integrationsr   $sentry_sdk.integrations._wsgi_commonr   typingr   r   r   r   r   sentry_sdk._typesr   r   r   r   r[   r\   rA   rq   rt   r@   r   rV   r   r   r   r5   r=   r=   r=   r>   <module>   sF    $	 md%D 