o
    Zh2>                     @   sl  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
 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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# 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& Z0d'd( Z1dS ))    N)deepcopy)	timedelta)environ)continue_trace)OP)Hub_should_send_default_pii)TRANSACTION_SOURCE_COMPONENT)AnnotatedValuecapture_internal_exceptionsevent_from_exceptionloggerTimeoutThread)Integration)_filter_headers)datetime_utcnowreraise)TYPE_CHECKING)datetime)Any)TypeVar)Callable)Optional)EventProcessorEventHintF.)boundi  g     @@c                        fdd}|S )Nc            	   	      s   t j}|t}|d u r | i |S |j}t > | }|  W d    n1 s-w   Y  t	 }|rPt
|rPt||jdddd\}}|j||d W d    n1 sZw   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 Y/var/www/html/lang_env/lib/python3.10/site-packages/sentry_sdk/integrations/aws_lambda.pysentry_init_error(   s(   



z+_wrap_init_error.<locals>.sentry_init_errorr7   )r6   r9   r7   r5   r8   _wrap_init_error&   s   r:   c                    r   )Nc                    s  t | trt| dkr| d }t| }n| }d}t |tsi }tj}|t}|d u r7 | |g|R i |S |j}|	 }	|
 }
d }t I |
  |
t|||	 |
d|jdd  |dkrr|
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d
}|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\}}|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 )N   r   Z
aws_region:   Zbatch_requestT
batch_sizeheaders)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   r   ZFUNCTION_AWSfunction_namer	   Zstart_transactionstop	Exceptionr*   r+   r   r-   r.   r   )rC   rD   r/   r0   Zrequest_datar>   r1   r2   r(   Zconfigured_timer3   Ztimeout_threadZwaiting_timer?   Ztransactionr+   r4   r$   handlerr7   r8   sentry_handlerG   s   



 
7

 .$z%_wrap_handler.<locals>.sentry_handlerr7   )rV   rW   r7   rU   r8   _wrap_handlerE   s   grX   c                  C   s\   t  ! tj} | t}|d ur|   W d    d S W d    d S 1 s'w   Y  d S N)r   r   r%   r&   r'   flush)r1   r2   r7   r7   r8   _drain_queue   s   

"r[   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 rY   )rM   )selfrM   r7   r7   r8   __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 rY   rX   request_handlerr/   r0   old_handle_event_requestr7   r8   sentry_handle_event_request      zDAwsLambdaIntegration.setup_once.<locals>.sentry_handle_event_requestc                    r`   rY   ra   rb   )old_handle_http_requestr7   r8   sentry_handle_http_request   rg   zCAwsLambdaIntegration.setup_once.<locals>.sentry_handle_http_requestc                        t    | i |S rY   r[   r/   r0   )old_to_jsonr7   r8   sentry_to_json      z7AwsLambdaIntegration.setup_once.<locals>.sentry_to_jsonc                    s    t |} | |g|R i |S rY   ra   )Zlambda_runtime_clientrc   r/   r0   rd   r7   r8   rf      s   c                    r   )Nc                     rj   rY   rk   rl   fr7   r8   inner  ro   zKAwsLambdaIntegration.setup_once.<locals>._wrap_post_function.<locals>.innerr7   )rq   rr   r7   rp   r8   _wrap_post_function
  s   z<AwsLambdaIntegration.setup_once.<locals>._wrap_post_function)get_lambda_bootstrapr   warninghasattrr^   r_   to_jsonr:   ZLambdaRuntimeClientZpost_init_errorZpost_invocation_resultZpost_invocation_error)Zlambda_bootstrapZpre_37rf   ri   rn   rs   r7   )re   rh   rm   r8   
setup_once   sJ   




zAwsLambdaIntegration.setup_onceN)F)__name__
__module____qualname__
identifierr]   staticmethodrx   r7   r7   r7   r8   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*   modulesrv   r   r~   )moduler7   r7   r8   rt     s   




rt   c                    s   t  }|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)rR   function_versionrK   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)rI   
setdefaultrR   r   rK   r   _get_cloudwatch_logs_urllog_group_namelog_stream_namerQ   _get_urlr   r   r
   Zremoved_because_raw_datar   )r4   r$   
start_timeZremaining_time_in_milisZexec_durationr   r   Z	user_infor   r   iprD   rC   configured_timeoutr7   r8   event_processorJ  sP   





z6_make_request_event_processor.<locals>.event_processor)r   )rC   rD   r   r   r   r7   r   r8   rJ   F  s   =rJ   c                 C   s`   |  dd }|  d}|d u ri }| dd }| dd }|r*|r*|r*d|||S d|jS )Npathr?   HostzX-Forwarded-Protoz	{}://{}{}zawslambda:///{})rQ   formatrR   )rC   rD   r   r?   hostprotor7   r7   r8   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.comr;   )seconds   )domainregionr   r   r   Zend_time)	r   rQ   r   
startswithr   r   r   strftimer   )rD   r   formatstringr   r   r7   r7   r8   r     s   r   )2r*   copyr   r   r   osr   Zsentry_sdk.apir   Zsentry_sdk.constsr   Zsentry_sdk.hubr   r   Zsentry_sdk.tracingr	   Zsentry_sdk.utilsr
   r   r   r   r   Zsentry_sdk.integrationsr   Z$sentry_sdk.integrations._wsgi_commonr   Zsentry_sdk._compatr   r   Zsentry_sdk._typesr   typingr   r   r   r   r   r   r   r   rN   rO   r:   rX   r[   r'   rt   rJ   r   r   r7   r7   r7   r8   <module>   s>    lc'D