
    tniE                        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!r7d 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                   Z*dZ+dZ,d3dZ-d4dZ.d5dZ/ G d de          Z0d6d Z1	 	 	 	 	 	 	 	 d7d&Z2d!d"d#d"de3fd'Z4d#d"d(d)de3fd*Z5d8d.Z6d9d2Z7d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     @@
init_errorreturnc                 J     t          t                     d fd            }|S )Nargsr   kwargsr    c                  (   t          j                    }t                      5  t          j                                                     t          j                    }|rCt          |          r4t          ||j	        ddd          \  }}t          j
        ||           nT| d         }t          |t                    rt          j        |          }t          |          }t          j
        |           d d d            n# 1 swxY w 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)r"   r#   clientr4   sentry_eventr-   
error_infor   s          _/var/www/html/gpu-tools/venv/lib/python3.11/site-packages/sentry_sdk/integrations/aws_lambda.pysentry_init_errorz+_wrap_init_error.<locals>.sentry_init_error,   sU   &(((** 	7 	7*,,>>@@@|~~H 7CMM 7%9#)>'3FF& & &"d
 (DAAAAA "!W
j#.. 8!%J!7!7J5jAA(666%	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7( z4*6***s   CDDDr"   r   r#   r   r    r   )r   AwsLambdaIntegration)r   rA   s   ` r@   _wrap_init_errorrD   +   s@     4jAA+ + + + + BA+2     handlerc                 \     t          j                   	 	 	 	 	 	 	 	 	 	 d fd            }|S )	N	aws_eventr   aws_contextr"   r#   r    c           
         t          j                    }|                    t                    }| | |g|R i |S t	          | t
                    r+t          |           dk    r| d         }t          |           }n| }d}t	          |t                    si }|                                }t          j	                    5 }	d }
t                      5  |	                                 |	                    t          |||                     |	                    d|j                            d          d                    |dk    r,|	                    dd           |	                    d|           |j        r]|t$          k    rR|t$          z
  t&          z  }t)          ||t&          z  |	t          j                    	          }
|
                                 d d d            n# 1 swxY w Y   |                    d
i           }t	          |t                    si }t1          |t2          j        |j        t8          j        t          j                  }t          j        || |d          5  	  | |g|R i ||
r|
                                  cd d d            cd d d            S # tB          $ rR tE          j#                    }tI          ||j%        ddd          \  }}t          j&        ||           tO          |  Y nw xY w	 |
r|
                                  n# |
r|
                                  w w xY wd d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nr.   r   
aws_region:   batch_requestT
batch_size)isolation_scopecurrent_scopeheaders)opnamesourceorigin)rH   rI   )custom_sampling_contextr%   Fr&   r)   r,   )(r/   r0   get_integrationrC   r8   listlendictget_remaining_time_in_millisrP   r   r2   add_event_processor_make_request_event_processorset_taginvoked_function_arnsplittimeout_warningTIMEOUT_WARNING_BUFFERMILLIS_TO_SECONDSr   get_current_scopestartgetr   r	   FUNCTION_AWSfunction_namer   	COMPONENTrV   start_transactionstop	Exceptionr3   r4   r   r6   r7   r   )rH   rI   r"   r#   r=   integrationrequest_datarO   configured_timescopetimeout_threadwaiting_timerR   transactionr4   r>   r-   rF   s                    r@   sentry_handlerz%_wrap_handler.<locals>.sentry_handlerJ   s    &((,,-ABB79kCDCCCFCCCi&& 	3y>>Q+>+>$Q<LYJJ$LJ,-- 	 L%BBDD')) E	.U!N,..  +  +'')))))1$k?   
  +"B"H"H"M"Ma"P   >>MM/4888MM,
;;;
  /+'*@@@ (*@@)$*L &3$'*;;(-&0&B&D&D	& & &N #((***A +  +  +  +  +  +  +  +  +  +  +  +  +  +  +D #&&y"55G gt,, (? .(2+2  K -!*#.) )   . .."79kKDKKKFKK & .&++---). . . . . . .cE	. E	. E	. E	. E	. E	. E	. E	.t ! ' ' '"|~~H)= '-~+7E"J"J* * *&L$
 ,\EEEEX&&&&' '% .&++--- & .&++----.'. . . . . . . . . . . . . . .cE	. E	. E	. E	. E	. E	. E	. E	. E	. E	. E	. E	. E	. E	. E	. E	. E	. E	.s    MDG M G$	$M'G$	(B M(M*J%6MM%AL>L LLML55M8MM	MM	MM #M )
rH   r   rI   r   r"   r   r#   r   r    r   )	functoolswraps)rF   ru   s   ` r@   _wrap_handlerrx   I   sr    _Wg.g.',g.5:g.FKg.	g. g. g. g. g. g.R rE   c                      t                      5  t          j                    } |                     t                    }||                                  d d d            d S # 1 swxY w Y   d S N)r   r/   r0   rX   rC   flush)r=   rn   s     r@   _drain_queuer|      s    	$	&	&  &((,,-ABB" LLNNN                 s   AA  A$'A$c                   F    e Zd ZdZde Zd	deddfdZed
d            ZdS )rC   r%   zauto.function.Frb   r    Nc                     || _         d S rz   )rb   )selfrb   s     r@   __init__zAwsLambdaIntegration.__init__   s    .rE   c                  T   t                      } | st          j        d           d S t          | d          st          j        d           d S t          | d          }|rN| j        	 	 	 	 	 	 	 	 dfd
}|| _        | j        	 	 	 	 	 	 	 	 dfd}|| _        | j        dfd}|| _        d S t          | j        j	                  | j        _	        | j        f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_requestrequest_handlerr   r"   r#   r    c                 :    t          |           }  | g|R i |S rz   rx   )r   r"   r#   old_handle_event_requests      r@   sentry_handle_event_requestzDAwsLambdaIntegration.setup_once.<locals>.sentry_handle_event_request   s3     #0"@"@//Q$QQQ&QQQrE   c                 :    t          |           }  | g|R i |S rz   r   )r   r"   r#   old_handle_http_requests      r@   sentry_handle_http_requestzCAwsLambdaIntegration.setup_once.<locals>.sentry_handle_http_request   s3     #0"@"@..PPPPPPPrE   c                  0    t                        | i |S rz   r|   )r"   r#   old_to_jsons     r@   sentry_to_jsonz7AwsLambdaIntegration.setup_once.<locals>.sentry_to_json   s!    "{D3F333rE   c                 <    t          |          } | |g|R i |S rz   r   )lambda_runtime_clientr   r"   r#   r   s       r@   r   zDAwsLambdaIntegration.setup_once.<locals>.sentry_handle_event_request  sF     #0"@"@//)?=A  EK  rE   fr   c                      d fd}|S )Nr"   r   r#   r    c                  0    t                        | i |S rz   r   )r"   r#   r   s     r@   innerzKAwsLambdaIntegration.setup_once.<locals>._wrap_post_function.<locals>.inner  s!     NNN1d-f---rE   rB    )r   r   s   ` r@   _wrap_post_functionz<AwsLambdaIntegration.setup_once.<locals>._wrap_post_function  s(    . . . . . . rE   )r   r   r"   r   r#   r   r    r   rB   )r   r   r    r   )get_lambda_bootstrapr   warninghasattrr   r   to_jsonrD   LambdaRuntimeClientpost_init_errorpost_invocation_resultpost_invocation_error)	lambda_bootstrappre_37r   r   r   r   r   r   r   s	         @@@r@   
setup_oncezAwsLambdaIntegration.setup_once   s3   /11 	NR   F')?@@ 	NV   F)+@AA C	'7'L$R!&R/4R@ERR R R R R R 5P1&6&J#Q!&Q/4Q@EQQ Q Q Q Q Q 4N0
 +2K4 4 4 4 4 4 (6$$$CS 4DD D0@ (8'L$     5P1
    $#$8O  0G $#$8N  0FFFrE   )Fr    N)	__name__
__module____qualname__
identifierrV   boolr   staticmethodr   r   rE   r@   rC   rC      ss        J*j**F/ / / / / / / U U U \U U UrE   rC   Optional[Any]c                     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)r3   modulesr   r   r   )modules    r@   r   r   !  s      ck!!{;''	s{	"	"Z(6-.. 	$7#[4
 4
 	$ *44V[)) 	$## trE   rH   r   rI   configured_timeoutr   c                 j     t          j        t          j                  }|f	 	 	 	 	 	 	 d
 fd	}|S )Nr>   r   r-   r   
start_timer   r    Optional[Event]c                                                     }|z
  }|                     di           }j        j        j        j        ||d|d<   t          |          j        j        d|d<   | 	                    di           }dv rd         |d<   t                    |d	<   d
v rd
         |d<   dv rt          d                   |d<   t                      r|                     di           }	                    d          }|i }|	                    d          }	|	|                    d|	           |	                    d          }
|
|                    d|
           dv r	                    dd          |d<   n,	                    dd           rt          j                    |d<   t          |          | d<   | S )Nextra)ri   function_versionr`   aws_request_idexecution_duration_in_millisremaining_time_in_millislambda)url	log_group
log_streamzcloudwatch logsrequest
httpMethodmethodr   queryStringParametersquery_stringrR   useridentityuserArnidsourceIp
ip_addressbody data)r\   
setdefaultri   r   r`   r   _get_cloudwatch_logs_urllog_group_namelog_stream_namerg   _get_urlr   r
   r   removed_because_raw_datar   )r>   r-   r   remaining_time_in_milisexec_durationr   r   	user_infor   r   iprI   rH   r   s              r@   event_processorz6_make_request_event_processor.<locals>.event_processorI  s    #."J"J"L"L*-DD''44(6 + <$/$D)8,9(?
 
h ,KDD$3%5$
 $
  ""9b119$$ ), 7GH!)[99"i//&/0G&HGN#	!!!091E!F!FGI"$$ 	L$//;;I }}Z00Hi((B~$$T2...j))B~$$\2666"""+--";";}}VT** L #1"I"K"K"*7"3"3YrE   )r>   r   r-   r   r   r   r    r   )r   nowr   utc)rH   rI   r   r   r   s   ```  r@   r^   r^   D  sp     hl++J GQ<<%+<9C<	< < < < < < < <| rE   c                 &   |                      dd           }|                      d          }|i }|                     dd           }|                     dd           }|r|r|rd                    |||          S d                    |j                  S )NpathrR   HostzX-Forwarded-Protoz	{}://{}{}zawslambda:///{})rg   formatri   )rH   rI   r   rR   hostprotos         r@   r   r     s    ==&&DmmI&&G;;vt$$DKK+T22E 5 5$ 5!!%t444##K$=>>>rE   r   r   c                 t   d}t          j        dd          }d                    |                    d          rdnd|| j        | j        |t          d	          z
                      |          t          j	        t          j                  t          d
	          z                       |                    }|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   rg   r   
startswithr   r   r   strftimer   r   r   r   )rI   r   formatstringr   r   s        r@   r   r     s     (L[r**F	- f!'!2!25!9!9O~~?O,.1!5!5!55??MM,x|,,y/C/C/CCMM
 
  	 		  JrE   formatted_tb	list[str]list[dict[str, Any]]c                     g }| D ]o}t          j        d|                                          }|rD|                                \  }}}t	          |          }|                    |||d d d d d           p|S )Nz File "(.+)", line (\d+), in (.+))filenamefunctionlinenovarspre_contextcontext_linepost_context)rematchstripgroupsintappend)r   framesframer   	file_nameline_number	func_names          r@   _parse_formatted_tracebackr     s    F  <ekkmmLL 	05-I{Ik**KMM ) )) #'$($( 
 
 
 MrE   
error_jsondict[str, Any]r   c                     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)rg   r   )r  events     r@   r<   r<     s     &NN;77'^^N;; "<&NN<<<# ## !-#(" " 
 E* LrE   )r   r   r    r   )rF   r   r    r   r   )r    r   )rH   r   rI   r   r   r   r    r   )r   r   r    r   )r  r  r    r   )8rv   r:   r   r3   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   rc   rd   rD   rx   r|   rC   r   r^   r9   r   r   r   r<   r   rE   r@   <module>r     s7        				 



       2 2 2 2 2 2 2 2 2 2           ) ) ) ) ) )             4 4 4 4 4 4 0 0 0 0 0 0                  0 / / / / / @ @ @ @ @ @             /==========8CH-...A      <k k k k\   ] ] ] ] ]; ] ] ]@       FCC#(C>CCC C C CL? ?E ?c ? ? ? ?% Z C    >   *     rE   