
    iZ                        d dl Z d dlZd dlZd dlZ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 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mZmZmZmZ d d	lmZ erad 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" 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+m,Z,m-Z-m.Z.m/Z/m0Z0  e'de1e.e2d      Z3	 d dl4m5Z5 d dl6m7Z8 e8Z7 e5dd      Z9da<d Z=d Z>d Z?d Z@d  ZAd! ZBd" ZCd# ZDd$ ZEd% ZFd& ZG G d' d(      ZH G d) d*      ZI G d+ d,eI      ZJ G d- d.eI      ZKd/ZL G d0 d1      ZM G d2 d3      ZNy# e:$ r ejv                  Z9dZ7Y yw xY w)4    N)deque)datetimetimezone)VERSION)Envelope)LRUCache)DEFAULT_SAMPLING_FREQUENCYextract_stack)capture_internal_exception	is_geventloggernowset_in_app_in_frames)TYPE_CHECKING)Any)Callable)Deque)Dict)List)Optional)Set)Type)Union)	TypedDict)ContinuousProfilerModeSDKInfo)ExtractedSampleFrameIdStackIdThreadIdProcessedFrameProcessedStackProcessedSample	timestamp	thread_idstack_id)get_original)
ThreadPooltimesleepc                    t         d u}|rt        j                  d       t                t	               rt
        j                  }nt        j                  }| j                  d      | d   }n'| j                  di       }|j                  d      xs |}t        }|t        j                  k(  rt        || ||      a n<|t
        j                  k(  rt        || ||      a nt        dj                  |            t        j                  dj                  t         j                               |st        j                  t               y)	Nz0[Profiling] Continuous Profiler is already setupprofiler_mode_experimentscontinuous_profiling_modez$Unknown continuous profiler mode: {}z9[Profiling] Setting up continuous profiler in {mode} mode)modeT)
_schedulerr   debugteardown_continuous_profilerr   GeventContinuousSchedulerr0   ThreadContinuousSchedulergetr	   
ValueErrorformatatexitregister)optionssdk_infocapture_funcalready_initializeddefault_profiler_moder-   experiments	frequencys           /var/www/html/hwPaymentPortal-be-dev/hw-payment-portal-api/venv/lib/python3.12/site-packages/sentry_sdk/profiler/continuous_profiler.pysetup_continuous_profilerrC   J   s+    %D0GH$&{
 !: > > 9 > >{{?#/0 kk."5 OO78Q<Q 	 +I1666.w,

 
388	8.w,

 ?FF}UVV
LLCJJ 	K 	
 45    c                  0    t         yt         j                  S NF)r1   sampled rD   rB   is_profile_session_sampledrI      s    rD   c                  d    t         y t         j                         sy t         j                          y N)r1   is_auto_start_enabledmanual_startrH   rD   rB   !try_autostart_continuous_profilerrN      s(    
 ++-rD   c                  8    t         y t         j                         S rK   )r1   
auto_startrH   rD   rB   !try_profile_lifecycle_trace_startrQ      s      ""rD   c                  :    t         y t         j                          y rK   )r1   rM   rH   rD   rB   start_profilerrS      s    rD   c                  P    t        j                  dt        d       t                y )NzWThe `start_profile_session` function is deprecated. Please use `start_profile` instead.   
stacklevel)warningswarnDeprecationWarningrS   rH   rD   rB   start_profile_sessionr[      s!     MMa
 rD   c                  :    t         y t         j                          y rK   )r1   manual_stoprH   rD   rB   stop_profilerr^      s    rD   c                  P    t        j                  dt        d       t                y )NzUThe `stop_profile_session` function is deprecated. Please use `stop_profile` instead.rU   rV   )rX   rY   rZ   r^   rH   rD   rB   stop_profile_sessionr`      s      MM_
 OrD   c                      t                d ay rK   )r^   r1   rH   rD   rB   r3   r3      s    O JrD   c                  0    t         y t         j                  S rK   )r1   profiler_idrH   rD   rB   get_profiler_idrd      s    !!!rD   c                 H    | syt        j                          t        |       k  S rF   )randomfloat)sample_rates    rB   +determine_profile_session_sampling_decisionri      s      ==?U;///rD   c                   "    e Zd ZU dZeed<   d Zy)ContinuousProfileTactivec                     d| _         y rF   )rl   selfs    rB   stopzContinuousProfile.stop   s	    rD   N)__name__
__module____qualname__rl   bool__annotations__rp   rH   rD   rB   rk   rk      s    FDrD   rk   c                   b    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zed        Zd Zd Zy)ContinuousSchedulerunknownc                 z   d|z  | _         || _        || _        || _        | j                  j	                  d      | _        | j                  j	                  d      }t        |      | _        | j                         | _	        d | _
        d | _        d| _        d| _        t        d      | _        t!               | _        y )Ng      ?profile_lifecycleprofile_session_sample_rateF   )maxlen)intervalr;   r<   r=   r6   	lifecycleri   rG   make_samplersamplerbufferpidrunningsoft_shutdownr   new_profilessetactive_profiles)ro   rA   r;   r<   r=   r{   s         rB   __init__zContinuousScheduler.__init__   s    i ())*=>&*ll&6&67T&U#B'
 ((*"!-"urD   c                     | j                   t        j                         k(  ry| j                  j	                  d      }|sy|j	                  d      S )NFr.   continuous_profiling_auto_start)r   osgetpidr;   r6   )ro   r@   s     rB   rL   z)ContinuousScheduler.is_auto_start_enabled   sC     88ryy{"ll&&~6@AArD   c                     | j                   sy | j                  dk7  ry t        j                  d       t	               }| j
                  j                  |       | j                          |S )Ntracez"[Profiling] Auto starting profiler)rG   r   r   r2   rk   r   appendensure_running)ro   profiles     rB   rP   zContinuousScheduler.auto_start  sU    ||>>W$9:#%  )rD   c                 ^    | j                   sy | j                  dk7  ry | j                          y Nmanual)rG   r   r   rn   s    rB   rM   z ContinuousScheduler.manual_start   s'    ||>>X%rD   c                 D    | j                   dk7  ry | j                          y r   )r   teardownrn   s    rB   r]   zContinuousScheduler.manual_stop*  s    >>X%rD   c                     t         rK   NotImplementedErrorrn   s    rB   r   z"ContinuousScheduler.ensure_running1      !!rD   c                     t         rK   r   rn   s    rB   r   zContinuousScheduler.teardown5  r   rD   c                     t         rK   r   rn   s    rB   pausezContinuousScheduler.pause9  r   rD   c                 n    t        | j                  | j                  t        | j                        | _        y rK   )ProfileBufferr;   r<   PROFILE_BUFFER_SECONDSr=   r   rn   s    rB   reset_bufferz ContinuousScheduler.reset_buffer=  s&    #LL$--)?ARAR
rD   c                 H    | j                   y | j                   j                  S rK   )r   rc   rn   s    rB   rc   zContinuousScheduler.profiler_idC  s!     ;;{{&&&rD   c                      t        j                         t        d       j                  dk(  r	 fd}|S  fd}|S )N   )max_sizer   c            
         j                   sj                  syt        j                         }t               }	 t	        j
                         j                         D cg c]  \  }}t        |      t        |
      f }}}t        |      D ]5  }j                  j                  j                   j                                7 g }j                  D ]   }	|	j                  r|j!                  |	       " |D ]  }	j                  j#                  |	        j$                  j$                  j'                  ||       yc c}}w # t        $ r  t        t	        j                                Y yw xY w)
                Take a sample of the stack on all the threads in the process.
                This should be called at a regular interval to collect samples.
                TF)r   r   lenr   sys_current_framesitemsstrr
   AttributeErrorr   exc_inforangeaddpopleftrl   r   remover   write)argskwargsr   tstidframesample_inactive_profilesr   cachecwdro   s             rB   _sample_stackz7ContinuousScheduler.make_sampler.<locals>._sample_stackR  s]    ((1E1E  #4#4#45U	! +.*=*=*?*E*E*G&C S=s#CDF ( |, JA((,,T->->-F-F-HIJ$&!#33 :G">>
 *009:  1 9G((//89 ;;*KK%%b&1M & ! /s||~> 	!s#   %E
  "EE
 E
 
&E32E3c            
      d   t               }	 t        j                         j                         D cg c]  \  }}t	        |      t        |      f }}}j                  j                  j                  ||       yc c}}w # t        $ r  t        t        j                                Y yw xY w)r   F)r   r   r   r   r   r
   r   r   r   r   r   )	r   r   r   r   r   r   r   r   ro   s	         rB   r   z7ContinuousScheduler.make_sampler.<locals>._sample_stack  s     U	! +.*=*=*?*E*E*G&C S=s#CDF  ;;*KK%%b&1 & ! /s||~> 	!s"   %B "B B  B &B/.B/)r   getcwdr   r   )ro   r   r   r   s   ` @@rB   r   z ContinuousScheduler.make_samplerJ  s?    iik#&>>W$>v 32 rD   c                    t        j                         }| j                  r| j                         | _        t        j                         |z
  }|| j
                  k  rt        | j
                  |z
         | j                  rd| _        n!t        j                         }| j                  r| j                  "| j                  j                          d | _        y y rF   )	r*   perf_counterr   r   r   r~   thread_sleepr   flush)ro   lastelapseds      rB   runzContinuousScheduler.run  s      "ll!%D
 '')D0G&T]]W45 !!$  $$&D+ ll. ;;"KKDK #rD   N)rq   rr   rs   r0   r   rL   rP   rM   r]   r   r   r   r   propertyrc   r   r   rH   rD   rB   rw   rw      sU    D%.B"""""
 ' 'cJrD   rw   c                   6     e Zd ZdZdZdZ fdZd Zd Z xZ	S )r5   zr
    This scheduler is based on running a daemon thread that will call
    the sampler at a regular interval.
    threadz)sentry.profiler.ThreadContinuousSchedulerc                 j    t         |   ||||       d | _        t        j                         | _        y rK   )superr   r   	threadingLocklockro   rA   r;   r<   r=   	__class__s        rB   r   z"ThreadContinuousScheduler.__init__  s+    GX|DNN$	rD   c                    d| _         t        j                         }| j                  r| j                  |k(  ry | j
                  5  | j                  r| j                  |k(  r
	 d d d        y || _        d| _        | j                          t        j                  | j                  | j                  d      | _        	 | j                  j                          d d d        y # t        $ r d| _        d | _        Y "w xY w# 1 sw Y   y xY w)NFT)nametargetdaemon)r   r   r   r   r   r   r   r   Threadr   r   r   startRuntimeErrorro   r   s     rB   r   z(ThreadContinuousScheduler.ensure_running  s     #iik <<DHHOYY 	# ||C	# 	# DHDL 
 $**		$((SWXDK#!!#)	# 	#*   #  %"	#+	# 	#s1   C:*AC::CC74C:6C77C::Dc                     | j                   rd| _         | j                  !| j                  j                          d | _        d | _        y rF   r   r   joinr   rn   s    rB   r   z"ThreadContinuousScheduler.teardown  8    << DL;;"KKDKrD   )
rq   rr   rs   __doc__r0   r   r   r   r   __classcell__r   s   @rB   r5   r5     s$    
 D6D%$#L	rD   r5   c                   2     e Zd ZdZdZ fdZd Zd Z xZS )r4   as  
    This scheduler is based on the thread scheduler but adapted to work with
    gevent. When using gevent, it may monkey patch the threading modules
    (`threading` and `_thread`). This results in the use of greenlets instead
    of native threads.

    This is an issue because the sampler CANNOT run in a greenlet because
    1. Other greenlets doing sync work will prevent the sampler from running
    2. The greenlet runs in the same thread as other greenlets so when taking
       a sample, other greenlets will have been evicted from the thread. This
       results in a sample containing only the sampler's code.
    geventc                     t         $t        dj                  | j                              t        |   ||||       d | _        t        j                         | _	        y )Nz"Profiler mode: {} is not available)
r)   r7   r8   r0   r   r   r   r   r   r   r   s        rB   r   z"GeventContinuousScheduler.__init__!  sN     AHHSTTGX|DNN$	rD   c                    d| _         t        j                         }| j                  r| j                  |k(  ry | j
                  5  | j                  r| j                  |k(  r
	 d d d        y || _        d| _        | j                          t        d      | _        	 | j                  j                  | j                         d d d        y # t        $ r d| _        d | _        Y "w xY w# 1 sw Y   y xY w)NFT   )r   r   r   r   r   r   r   r)   r   spawnr   r   r   s     rB   r   z(GeventContinuousScheduler.ensure_running,  s     #iik <<DHHOYY 	# ||C	# 	# DHDL $Q-DK#!!$((+!	# 	#"   #  %"	##	# 	#s0   C$*.C$%CC!C$ C!!C$$C-c                     | j                   rd| _         | j                  !| j                  j                          d | _        d | _        y rF   r   rn   s    rB   r   z"GeventContinuousScheduler.teardownN  r   rD   )	rq   rr   rs   r   r0   r   r   r   r   r   s   @rB   r4   r4     s     D	% #D	rD   r4   <   c                   $    e Zd Zd Zd Zd Zd Zy)r   c                 D   || _         || _        || _        || _        t	        j
                         j                  | _        t               | _	        t               | _        t        j                  t        j                        j                         | j                  z
  | _        y rK   )r;   r<   buffer_sizer=   uuiduuid4hexrc   ProfileChunkchunkr   start_monotonic_timer   r   utcr%   start_timestamp)ro   r;   r<   r   r=   s        rB   r   zProfileBuffer.__init__^  sw     &(::<++!^
 %(E! LL&002T5N5NN 	rD   c                     | j                  |      r.| j                          t               | _        t	               | _        | j                  j                  | j                  |z   |       y rK   )should_flushr   r   r   r   r   r   r   )ro   monotonic_timer   s      rB   r   zProfileBuffer.writeu  sK    ^,JJL%DJ(+D%

-->GrD   c                 :    || j                   z
  | j                  k\  S rK   )r   r   )ro   r   s     rB   r   zProfileBuffer.should_flush~  s     
  9 99T=M=MMMrD   c                     | j                   j                  | j                  | j                  | j                        }t               }|j                  |       | j                  |       y rK   )r   to_jsonrc   r;   r<   r   add_profile_chunkr=   )ro   r   envelopes      rB   r   zProfileBuffer.flush  sK    

""4#3#3T\\4==Q:""5)(#rD   N)rq   rr   rs   r   r   r   r   rH   rD   rB   r   r   ]  s    
.HN$rD   r   c                       e Zd Zd Zd Zd Zy)r   c                     t        j                         j                  | _        i | _        i | _        g | _        g | _        g | _        y rK   )	r   r   r   chunk_idindexed_framesindexed_stacksframesstackssamplesrn   s    rB   r   zProfileChunk.__init__  s:    

((  rD   c                 p   |D ]  \  }\  }}}	 || j                   vrt        |      D ]T  \  }}|| j                  vst        | j                        | j                  |<   | j                  j                  ||          V t        | j                         | j                   |<   | j                  j                  |D cg c]  }| j                  |    c}       | j                  j                  ||| j                   |   d        y c c}w # t        $ r! t        t        j                                Y 1w xY w)Nr$   )r  	enumerater   r   r  r   r  r  r   r   r   r   )	ro   r   r   r   r'   	frame_idsr  iframe_ids	            rB   r   zProfileChunk.write  s%   28 	;.C.(Iv; 4#6#66'0'; :8#4+>+>><?@S@S<TD//9 KK..vay9:
 588K8K4LD''1KK&&GPQ8,,X6Q ##%'%($($7$7$A	; R " ; +3<<>:;s)   -DA=D9D
3DD&D54D5c                    | j                   | j                  | j                  t        j                         D ci c]-  }t        |j                        dt        |j                        i/ c}d}t        |d   |d   |d   |d          | j                  |d   t        dd||d	d
}dD ]'  }||   	t        ||         j                         ||<   ) |S c c}w )Nr   )r  r  r  thread_metadatar  in_app_excludein_app_includeproject_root)r   versionpython2)r   
client_sdkplatformr   rc   r  )releaseenvironmentdist)r  r  r  r   r  r   identr   r   r   r   strip)ro   rc   r;   r<   r   r   payloadkeys           rB   r   zProfileChunk.to_json  s     kkkk||
 (113	   FLL!C,$  	

 	H$%$%N#		
  (" !&

 4 	9Cs|'"73<0668	9 ? s   2C
N)rq   rr   rs   r   r   r   rH   rD   rB   r   r     s    ;:%rD   r   )Or9   r   rf   r   r   r*   r   rX   collectionsr   r   r   sentry_sdk.constsr   sentry_sdk.enveloper   sentry_sdk._lru_cacher   sentry_sdk.profiler.utilsr	   r
   sentry_sdk.utilsr   r   r   r   r   typingr   r   r   r   r   r   r   r   r   r   typing_extensionsr   sentry_sdk._typesr   r   r   r   r   r    r!   r"   rg   intr#   gevent.monkeyr(   gevent.threadpoolr)   _ThreadPoolr   ImportErrorr+   r1   rC   rI   rN   rQ   rS   r[   r^   r`   r3   rd   ri   rk   rw   r5   r4   r   r   r   rH   rD   rB   <module>r)     sZ    	  
      ' % ( *  !+A   !	
O*;J0L 
3l#"0 h hV? 3 ?DF 3 FR  -$ -$`M MW  ::LJs   D< <EE