o
    h>                     @   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typehandledclient_options	mechanismhint)r   currentget_integrationAwsLambdaIntegrationclientr   configure_scopeclear_breadcrumbssysexc_infoallr   optionscapture_event)	argskwargshubintegrationr+   scoper/   sentry_eventr'   
init_error e/var/www/html/aiguide_backend/venv/lib/python3.10/site-packages/sentry_sdk/integrations/aws_lambda.pysentry_init_error(   s(   



z+_wrap_init_error.<locals>.sentry_init_errorr;   )r:   r=   r;   r9   r<   _wrap_init_error&   s   r>   c                    r   )Nc                    sv  t | tr| d }t| }n| }d}t |tsi }tj}|t}|d u r1 | |g|R i |S |j}|	 }	|
 }
d }t I |
  |
t|||	 |
d|jdd  |dkrl|
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c zQz" | |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 s4w   Y  d S )Nr      
aws_region:   batch_requestT
batch_sizeheaders)opnamesource)	aws_eventaws_context)custom_sampling_contextr   Fr    r#   r&   )&
isinstancelistlendictr   r(   r)   r*   r+   get_remaining_time_in_millis
push_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	   start_transactionstop	Exceptionr.   r/   r   r1   r2   r   )rI   rJ   r3   r4   request_datarD   r5   r6   r+   configured_timer7   timeout_threadwaiting_timerE   transactionr/   r8   r'   handlerr;   r<   sentry_handlerG   s   




 
7

 .$z%_wrap_handler.<locals>.sentry_handlerr;   )rg   rh   r;   rf   r<   _wrap_handlerE   s   gri   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)r5   r6   r;   r;   r<   _drain_queue   s   

"rl   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 rj   )rW   )selfrW   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 rj   ri   request_handlerr3   r4   old_handle_event_requestr;   r<   sentry_handle_event_request      zDAwsLambdaIntegration.setup_once.<locals>.sentry_handle_event_requestc                    rq   rj   rr   rs   )old_handle_http_requestr;   r<   sentry_handle_http_request   rx   zCAwsLambdaIntegration.setup_once.<locals>.sentry_handle_http_requestc                        t    | i |S rj   rl   r3   r4   )old_to_jsonr;   r<   sentry_to_json      z7AwsLambdaIntegration.setup_once.<locals>.sentry_to_jsonc                    s    t |} | |g|R i |S rj   rr   )lambda_runtime_clientrt   r3   r4   ru   r;   r<   rw      s   c                    r   )Nc                     r{   rj   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hasattrro   rp   to_jsonr>   LambdaRuntimeClientpost_init_errorpost_invocation_resultpost_invocation_error)lambda_bootstrappre_37rw   rz   r   r   r;   )rv   ry   r~   r<   
setup_once   sJ   




zAwsLambdaIntegration.setup_onceN)F)__name__
__module____qualname__
identifierrn   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  }|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_versionrU   aws_request_idexecution_duration_in_millisremaining_time_in_millislambda)url	log_group
log_streamzcloudwatch logsrequest
httpMethodmethodr   queryStringParametersquery_stringrE   useridentityuserArnidsourceIp
ip_addressbody data)rP   
setdefaultr]   r   rU   r   _get_cloudwatch_logs_urllog_group_namelog_stream_namer[   _get_urlr   r   r
   removed_because_raw_datar   )r8   r'   
start_timeremaining_time_in_milisexec_durationr   r   	user_infor   r   iprJ   rI   configured_timeoutr;   r<   event_processorJ  sP   





z6_make_request_event_processor.<locals>.event_processor)r   )rI   rJ   r   r   r   r;   r   r<   rS   F  s   =rS   c                 C   s`   |  dd }|  d}|d u ri }| dd }| dd }|r*|r*|r*d|||S d|jS )NpathrE   HostzX-Forwarded-Protoz	{}://{}{}zawslambda:///{})r[   formatr]   )rI   rJ   r   rE   hostprotor;   r;   r<   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:%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   )rJ   r   formatstringr   r   r;   r;   r<   r     s   r   )2r.   copyr   r   r   osr   sentry_sdk.apir   sentry_sdk.constsr   sentry_sdk.hubr   r   sentry_sdk.tracingr	   sentry_sdk.utilsr
   r   r   r   r   sentry_sdk.integrationsr   $sentry_sdk.integrations._wsgi_commonr   sentry_sdk._compatr   r   sentry_sdk._typesr   typingr   r   r   r   r   r   r   r   rX   rY   r>   ri   rl   r*   r   rS   r   r   r;   r;   r;   r<   <module>   s>    lc'D