a
    xdu=                     @   s@  d dl m Z mZ d dlmZ d dlZd dlmZmZ d dlm	Z	m
Z
 d dl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 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Z'dd Z(dd  Z)d!d" Z*d#d$ Z+dS )%    )datetime	timedelta)environN)Hub_should_send_default_pii)TRANSACTION_SOURCE_COMPONENTTransaction)reraise)AnnotatedValuecapture_internal_exceptionsevent_from_exceptionloggerTimeoutThread)Integration)_filter_headers)MYPY)Any)TypeVar)Callable)Optional)EventProcessorEventHintF.)boundi  g     @@c                    s    fdd}|S )Nc            	   	      s   t j}|t}|d u r& | i |S |j}t | | }|  W d    n1 sZ0    Y  t	 }|rt
|rt||jdddd\}}|j||d W d    n1 s0    Y   | i |S )N
aws_lambdaFtypeZhandledZclient_optionsZ	mechanismhint)r   currentget_integrationAwsLambdaIntegrationclientr   Zconfigure_scopeclear_breadcrumbssysexc_infoallr   optionscapture_event)	argskwargshubintegrationr$   scoper'   sentry_eventr    
init_error Z/var/www/html/Ranjet/env/lib/python3.9/site-packages/sentry_sdk/integrations/aws_lambda.pysentry_init_error%   s"    

&
,z+_wrap_init_error.<locals>.sentry_init_errorr3   )r2   r5   r3   r1   r4   _wrap_init_error#   s    r6   c                    s    fdd}|S )Nc                    sl  t | tr| d }t| }n| }d}t |ts2i }tj}|t}|d u rb | |g|R i |S |j}|	 }	|
 }
d }t  |
  |
t|||	 |
d|jdd  |dkr|
dd |
d| |jr|	tkr|	t t }t||	t }|  W d    n1 s$0    Y  |d	}|d u rFi }tj|d
|jtd}|j|| |dd zzF | |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0 W |r(|  n|r&|  0 W d    n1 s>0    Y  W d    n1 s^0    Y  d S )Nr      Z
aws_region:   Zbatch_requestT
batch_sizeheaderszserverless.function)opnamesource)	aws_eventaws_context)Zcustom_sampling_contextr   Fr   r   r   )%
isinstancelistlendictr   r!   r"   r#   r$   get_remaining_time_in_millisZ
push_scoper   r%   Zadd_event_processor_make_request_event_processorZset_taginvoked_function_arnsplittimeout_warningTIMEOUT_WARNING_BUFFERMILLIS_TO_SECONDSr   startgetr   Zcontinue_from_headersfunction_namer   Zstart_transactionstop	Exceptionr&   r'   r   r)   r*   r	   )r?   r@   r+   r,   Zrequest_datar:   r-   r.   r$   Zconfigured_timer/   Ztimeout_threadZwaiting_timer;   Ztransactionr'   r0   r    handlerr3   r4   sentry_handlerD   s    



(



z%_wrap_handler.<locals>.sentry_handlerr3   )rR   rS   r3   rQ   r4   _wrap_handlerB   s    erT   c                  C   sJ   t  0 tj} | t}|d ur(|   W d    n1 s<0    Y  d S N)r   r   r!   r"   r#   flush)r-   r.   r3   r3   r4   _drain_queue   s
    
rW   c                   @   s&   e Zd ZdZdddZedd ZdS )	r#   r   Fc                 C   s
   || _ d S rU   )rI   )selfrI   r3   r3   r4   __init__   s    zAwsLambdaIntegration.__init__c                     s   t  } | std d S t| ds0td d S t| d}|r| j  fdd}|| _| jfdd}|| _| jfd	d
}|| _nPt| 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                    s   t | }  | g|R i |S rU   rT   request_handlerr+   r,   old_handle_event_requestr3   r4   sentry_handle_event_request   s    zDAwsLambdaIntegration.setup_once.<locals>.sentry_handle_event_requestc                    s   t | }  | g|R i |S rU   r\   r]   )old_handle_http_requestr3   r4   sentry_handle_http_request   s    zCAwsLambdaIntegration.setup_once.<locals>.sentry_handle_http_requestc                     s   t    | i |S rU   rW   r+   r,   )old_to_jsonr3   r4   sentry_to_json   s    z7AwsLambdaIntegration.setup_once.<locals>.sentry_to_jsonc                    s    t |} | |g|R i |S rU   r\   )Zlambda_runtime_clientr^   r+   r,   r_   r3   r4   ra      s    c                    s    fdd}|S )Nc                     s   t    | i |S rU   rd   re   fr3   r4   inner  s    zKAwsLambdaIntegration.setup_once.<locals>._wrap_post_function.<locals>.innerr3   )ri   rj   r3   rh   r4   _wrap_post_function  s    z<AwsLambdaIntegration.setup_once.<locals>._wrap_post_function)get_lambda_bootstrapr   warninghasattrrZ   r[   to_jsonr6   ZLambdaRuntimeClientZpost_init_errorZpost_invocation_resultZpost_invocation_error)Zlambda_bootstrapZpre_37ra   rc   rg   rk   r3   )r`   rb   rf   r4   
setup_once   sJ    


zAwsLambdaIntegration.setup_onceN)F)__name__
__module____qualname__
identifierrY   staticmethodrp   r3   r3   r3   r4   r#      s   
r#   c                  C   sb   dt jv rt jd S dt jv rZt jd } t| drFt| jdrF| jjS t| drV| jS | S d S d S )N	bootstrap__main__awslambdaricmain)r&   modulesrn   rx   rv   )moduler3   r3   r4   rl     s    




rl   c                    s    t  }|f fdd	}|S )Nc                    sd     }| }| di } j j j j||d|d< t | j jd|d< | 	di }dv rtd |d< t
 |d	< d
v rd
 |d< dv rtd |d< t r2| di }	d}|d u ri }|	d}	|	d ur|d|	 |	d}
|
d ur|d|
 dv rX	dd|d< n&	dd rXtddg dgi|d< || d< | S )Nextra)rN   function_versionrG   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_stringr;   useridentityZuserArnidZsourceIp
ip_addressbody datarem)z!rawxr   r   )rE   
setdefaultrN   r|   rG   r}   _get_cloudwatch_logs_urllog_group_namelog_stream_namerM   _get_urlr   r   r
   )r0   r    
start_timeZremaining_time_in_milisZexec_durationr{   r   Z	user_infor   r   ipr@   r?   configured_timeoutr3   r4   event_processorE  sN    







z6_make_request_event_processor.<locals>.event_processor)r   utcnow)r?   r@   r   r   r   r3   r   r4   rF   A  s    =rF   c                 C   s`   |  dd }|  d}|d u r"i }| dd }| dd }|rT|rT|rTd|||S d|jS )Npathr;   HostzX-Forwarded-Protoz	{}://{}{}zawslambda:///{})rM   formatrN   )r?   r@   r   r;   hostprotor3   r3   r4   r     s    
r   c              
   C   s`   d}t dd}dj|dr"dnd|| j| j|tdd	 |t	 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.comr7   )seconds   )domainregionr   r   r   Zend_time)
r   rM   r   
startswithr   r   r   strftimer   r   )r@   r   formatstringr   r   r3   r3   r4   r     s    r   ),r   r   osr   r&   Zsentry_sdk.hubr   r   Zsentry_sdk.tracingr   r   Zsentry_sdk._compatr	   Zsentry_sdk.utilsr
   r   r   r   r   Zsentry_sdk.integrationsr   Z$sentry_sdk.integrations._wsgi_commonr   Zsentry_sdk._typesr   typingr   r   r   r   r   r   r   r   rJ   rK   r6   rT   rW   r#   rl   rF   r   r   r3   r3   r3   r4   <module>   s4   jc'D