o
    0 iF                     @   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typeZhandledZclient_options	mechanismhint   )
sentry_sdk
get_clientr   Z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 n/home/app/PaddleOCR-VL-test/.venv_paddleocr/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)r9   r<   r:   r8   r;   _wrap_init_error+   s   r>   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
 N |	  |	t||| |	d|jdd  |dkrp|	dd |	d| |jr|tkr|t t }t||t |	t  d	}
|
  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 sAw   Y  d S )Nr%   r   Z
aws_region:   Zbatch_requestT
batch_size)isolation_scopeZcurrent_scopeheaders)opnamesourceorigin)	aws_eventaws_context)Zcustom_sampling_contextr   Fr   r!   r#   )(r&   r'   get_integrationr=   r.   listlendictget_remaining_time_in_millisrB   r   r(   Zadd_event_processor_make_request_event_processorZset_taginvoked_function_arnsplittimeout_warningTIMEOUT_WARNING_BUFFERMILLIS_TO_SECONDSr   Zget_current_scopestartgetr   r   ZFUNCTION_AWSfunction_namer
   Z	COMPONENTrG   Zstart_transactionstop	Exceptionr)   r*   r   r,   r-   r   )rH   rI   r3   r4   r5   integrationZrequest_datarA   Zconfigured_timescopeZtimeout_threadZwaiting_timerC   Ztransactionr*   r6   r$   handlerr:   r;   sentry_handlerM   s   



"
:

 1$z%_wrap_handler.<locals>.sentry_handler)	functoolswraps)r]   r^   r:   r\   r;   _wrap_handlerK   s   ira   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'   rJ   r=   flush)r5   rZ   r:   r:   r;   _drain_queue   s   

"rd   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 rb   )rR   )selfrR   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 rb   ra   request_handlerr3   r4   old_handle_event_requestr:   r;   sentry_handle_event_request      zDAwsLambdaIntegration.setup_once.<locals>.sentry_handle_event_requestc                    ri   rb   rj   rk   )old_handle_http_requestr:   r;   sentry_handle_http_request   rp   zCAwsLambdaIntegration.setup_once.<locals>.sentry_handle_http_requestc                        t    | i |S rb   rd   r3   r4   )old_to_jsonr:   r;   sentry_to_json      z7AwsLambdaIntegration.setup_once.<locals>.sentry_to_jsonc                    s    t |} | |g|R i |S rb   rj   )Zlambda_runtime_clientrl   r3   r4   rm   r:   r;   ro     s   c                    s    fdd}|S )Nc                     rs   rb   rt   ru   fr:   r;   inner  rx   zKAwsLambdaIntegration.setup_once.<locals>._wrap_post_function.<locals>.innerr:   )rz   r{   r:   ry   r;   _wrap_post_function  s   z<AwsLambdaIntegration.setup_once.<locals>._wrap_post_function)get_lambda_bootstrapr   warninghasattrrg   rh   to_jsonr>   ZLambdaRuntimeClientZpost_init_errorZpost_invocation_resultZpost_invocation_error)Zlambda_bootstrapZpre_37ro   rr   rw   r|   r:   )rn   rq   rv   r;   
setup_once   sJ   




zAwsLambdaIntegration.setup_onceN)F)__name__
__module____qualname__
identifierrG   rf   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)rW   function_versionrP   aws_request_idZexecution_duration_in_millisZremaining_time_in_millislambda)url	log_group
log_streamzcloudwatch logsrequestZ
httpMethodmethodr   ZqueryStringParametersZquery_stringrC   useridentityZuserArnidZsourceIp
ip_addressbody data)rN   
setdefaultrW   r   rP   r   _get_cloudwatch_logs_urllog_group_namelog_stream_namerV   _get_urlr   r	   r   Zremoved_because_raw_datar   )r6   r$   
start_timeZremaining_time_in_milisZexec_durationr   r   Z	user_infor   r   iprI   rH   configured_timeoutr:   r;   event_processorR  sP   





z6_make_request_event_processor.<locals>.event_processor)r   nowr   utc)rH   rI   r   r   r   r:   r   r;   rO   N  s   =rO   c                 C   s`   |  dd }|  d}|d u ri }| dd }| dd }|r*|r*|r*d|||S d|jS )NpathrC   HostzX-Forwarded-Protoz	{}://{}{}zawslambda:///{})rV   formatrW   )rH   rI   r   rC   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:%SZZ
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   rV   r   
startswithr   r   r   strftimer   r   r   r   )rI   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varsZpre_contextZcontext_lineZpost_context)rematchstripgroupsintappend)Z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valuesZ	errorTypeZerrorMessager   Z
stackTracer   Fr   )r    valueZ
stacktracer"   )level	exception)rV   r   )Z
error_jsoneventr:   r:   r;   r2     s    

r2   )7r_   r0   r   r)   copyr   r   r   r   osr   r&   Zsentry_sdk.apir   Zsentry_sdk.constsr   Zsentry_sdk.scoper	   Zsentry_sdk.tracingr
   Zsentry_sdk.utilsr   r   r   r   r   r   r   Zsentry_sdk.integrationsr   Z$sentry_sdk.integrations._wsgi_commonr   typingr   r   r   r   r   Zsentry_sdk._typesr   r   r   r   rS   rT   r>   ra   rd   r=   r}   rO   r   r   r   r2   r:   r:   r:   r;   <module>   sF    $	 od%D 