o
    hZ                     @   sz  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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" 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Z3zd dl4m5Z5 d dl6m7Z8 e8Z7e5ddZ9W n e:y   ej;Z9dZ7Y nw da<dd Z=dd Z>d d! Z?d"d# Z@d$d% ZAd&d' ZBd(d) ZCd*d+ ZDd,d- ZEd.d/ ZFd0d1 ZGG d2d3 d3ZHG d4d5 d5ZIG d6d7 d7eIZJG d8d9 d9eIZKd:ZLG d;d< d<ZMG d=d> d>ZNdS )?    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                 C   s   t d u}|rtd t  t rtj}ntj}| dd ur$| d }n| di }|dp0|}t	}|tjkr@t|| ||a n|tjkrMt|| ||a nt
d|tdjt jd |sett dS )	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   GeventContinuousSchedulerr.   ThreadContinuousSchedulergetr   
ValueErrorformatatexitregister)optionssdk_infocapture_funcalready_initializeddefault_profiler_moder+   experiments	frequency r@   i/var/www/html/pro-man-master/venv/lib/python3.10/site-packages/sentry_sdk/profiler/continuous_profiler.pysetup_continuous_profilerJ   s<   




rB   c                   C   s   t d u rdS t jS NF)r/   sampledr@   r@   r@   rA   is_profile_session_sampled      rE   c                   C   s$   t d u rd S t  sd S t   d S N)r/   is_auto_start_enabledmanual_startr@   r@   r@   rA   !try_autostart_continuous_profiler   s
   rJ   c                   C   s   t d u rd S t  S rG   )r/   
auto_startr@   r@   r@   rA   !try_profile_lifecycle_trace_start   s   rL   c                   C      t d u rd S t   d S rG   )r/   rI   r@   r@   r@   rA   start_profiler      rN   c                   C      t jdtdd t  d S )NzWThe `start_profile_session` function is deprecated. Please use `start_profile` instead.   
stacklevel)warningswarnDeprecationWarningrN   r@   r@   r@   rA   start_profile_session      
rW   c                   C   rM   rG   )r/   manual_stopr@   r@   r@   rA   stop_profiler   rO   rZ   c                   C   rP   )NzUThe `stop_profile_session` function is deprecated. Please use `stop_profile` instead.rQ   rR   )rT   rU   rV   rZ   r@   r@   r@   rA   stop_profile_session   rX   r[   c                   C   s   t   d ad S rG   )rZ   r/   r@   r@   r@   rA   r1      s   r1   c                   C   s   t d u rd S t jS rG   )r/   profiler_idr@   r@   r@   rA   get_profiler_id   rF   r]   c                 C   s   | sdS t   t| k S rC   )randomfloat)sample_rater@   r@   rA   +determine_profile_session_sampling_decision   s   ra   c                   @   s"   e Zd ZU dZeed< dd ZdS )ContinuousProfileTactivec                 C   s
   d| _ d S rC   )rc   selfr@   r@   rA   stop   s   
zContinuousProfile.stopN)__name__
__module____qualname__rc   bool__annotations__rf   r@   r@   r@   rA   rb      s   
 rb   c                   @   st   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zedd Zdd Zdd ZdS )ContinuousSchedulerunknownc                 C   sz   d| | _ || _|| _|| _| jd| _| jd}t|| _|  | _	d | _
d | _d| _d| _tdd| _t | _d S )Ng      ?profile_lifecycleprofile_session_sample_rateF   )maxlen)intervalr9   r:   r;   r4   	lifecyclera   rD   make_samplersamplerbufferpidrunningsoft_shutdownr   new_profilessetactive_profiles)re   r?   r9   r:   r;   ro   r@   r@   rA   __init__   s    

zContinuousScheduler.__init__c                 C   s0   | j t kr	dS | jd}|sdS |dS )NFr,   continuous_profiling_auto_start)rw   osgetpidr9   r4   )re   r>   r@   r@   rA   rH      s   
z)ContinuousScheduler.is_auto_start_enabledc                 C   s@   | j sd S | jdkrd S td t }| j| |   |S )Ntracez"[Profiling] Auto starting profiler)rD   rs   r   r0   rb   rz   appendensure_running)re   profiler@   r@   rA   rK     s   

zContinuousScheduler.auto_startc                 C   s$   | j sd S | jdkrd S |   d S Nmanual)rD   rs   r   rd   r@   r@   rA   rI      s
   
z ContinuousScheduler.manual_startc                 C   s   | j dkrd S |   d S r   )rs   teardownrd   r@   r@   rA   rY   *  s   
zContinuousScheduler.manual_stopc                 C      t rG   NotImplementedErrorrd   r@   r@   rA   r   1     z"ContinuousScheduler.ensure_runningc                 C   r   rG   r   rd   r@   r@   rA   r   5  r   zContinuousScheduler.teardownc                 C   r   rG   r   rd   r@   r@   rA   pause9  r   zContinuousScheduler.pausec                 C   s   t | j| jt| j| _d S rG   )ProfileBufferr9   r:   PROFILE_BUFFER_SECONDSr;   rv   rd   r@   r@   rA   reset_buffer=  s   
z ContinuousScheduler.reset_bufferc                 C   s   | j d u rd S | j jS rG   )rv   r\   rd   r@   r@   rA   r\   C  s   
zContinuousScheduler.profiler_idc                    sD   t  tdd jdkr fdd}|S  fdd}|S )N   )max_sizer   c                     s   j sjsdS tj }t }z fddt  D }W n ty0   tt	  Y dS w t
|D ]}jj   q5g }jD ]
}|jsP|| qF|D ]}j| qSjdurhj|| dS )
                Take a sample of the stack on all the threads in the process.
                This should be called at a regular interval to collect samples.
                Tc                    $   g | ]\}}t |t| fqS r@   strr	   .0tidframecachecwdr@   rA   
<listcomp>j      KContinuousScheduler.make_sampler.<locals>._sample_stack.<locals>.<listcomp>FN)rz   r|   lenr   sys_current_framesitemsAttributeErrorr
   exc_inforangeaddpopleftrc   r   removerv   write)argskwargsrz   tssample_inactive_profilesr   r   r   re   r@   rA   _sample_stackR  s0   






z7ContinuousScheduler.make_sampler.<locals>._sample_stackc                     sd   t  }z fddt  D }W n ty#   tt  Y dS w jdur0j|| dS )r   c                    r   r@   r   r   r   r@   rA   r     r   r   FN)	r   r   r   r   r   r
   r   rv   r   )r   r   r   r   r   r@   rA   r     s   


)r   getcwdr   rs   )re   r   r@   r   rA   rt   J  s   

[z ContinuousScheduler.make_samplerc                 C   sz   t  }| jr,|  | _t  | }|| jk rt| j|  | jr%d| _nt  }| js| jd ur;| j  d | _d S d S rC   )	r)   perf_counterrx   ru   ry   rr   thread_sleeprv   flush)re   lastelapsedr@   r@   rA   run  s   




zContinuousScheduler.runN)rg   rh   ri   r.   r}   rH   rK   rI   rY   r   r   r   r   propertyr\   rt   r   r@   r@   r@   rA   rl      s    

erl   c                       s8   e Zd ZdZdZdZ fddZdd Zdd	 Z  Z	S )
r3   zr
    This scheduler is based on running a daemon thread that will call
    the sampler at a regular interval.
    threadz)sentry.profiler.ThreadContinuousSchedulerc                    s&   t  |||| d | _t | _d S rG   )superr}   r   	threadingLocklockre   r?   r9   r:   r;   	__class__r@   rA   r}     s   z"ThreadContinuousScheduler.__init__c              	   C   s   d| _ t }| jr| j|krd S | jN | jr&| j|kr&	 W d    d S || _d| _|   tj| j	| j
dd| _z| j  W n tyQ   d| _d | _Y n	w W d    d S W d    d S 1 sew   Y  d S )NFT)nametargetdaemon)ry   r   r   rx   rw   r   r   r   Threadr   r   r   startRuntimeErrorre   rw   r@   r@   rA   r     s,   
"z(ThreadContinuousScheduler.ensure_runningc                 C   0   | j rd| _ | jd ur| j  d | _d | _d S rC   rx   r   joinrv   rd   r@   r@   rA   r        


z"ThreadContinuousScheduler.teardown)
rg   rh   ri   __doc__r.   r   r}   r   r   __classcell__r@   r@   r   rA   r3     s    &r3   c                       s4   e Zd ZdZdZ fddZdd Zdd Z  ZS )	r2   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                    s>   t d u rtd| jt |||| d | _t | _	d S )Nz"Profiler mode: {} is not available)
r(   r5   r6   r.   r   r}   r   r   r   r   r   r   r@   rA   r}   !  s
   z"GeventContinuousScheduler.__init__c              	   C   s   d| _ t }| jr| j|krd S | jJ | jr&| j|kr&	 W d    d S || _d| _|   td| _z	| j	| j
 W n tyM   d| _d | _Y n	w W d    d S W d    d S 1 saw   Y  d S )NFT   )ry   r   r   rx   rw   r   r   r(   r   spawnr   r   r   r@   r@   rA   r   ,  s,   

"z(GeventContinuousScheduler.ensure_runningc                 C   r   rC   r   rd   r@   r@   rA   r   N  r   z"GeventContinuousScheduler.teardown)	rg   rh   ri   r   r.   r}   r   r   r   r@   r@   r   rA   r2     s    "r2   <   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
r   c                 C   sP   || _ || _|| _|| _t j| _t | _	t
 | _t
tj | j | _d S rG   )r9   r:   buffer_sizer;   uuiduuid4hexr\   ProfileChunkchunkr   start_monotonic_timer   r   utcr$   start_timestamp)re   r9   r:   r   r;   r@   r@   rA   r}   ^  s   zProfileBuffer.__init__c                 C   s:   |  |r|   t | _t | _| j| j| | d S rG   )should_flushr   r   r   r   r   r   r   )re   monotonic_timer   r@   r@   rA   r   u  s
   
zProfileBuffer.writec                 C   s   || j  | jkS rG   )r   r   )re   r   r@   r@   rA   r   ~  s   zProfileBuffer.should_flushc                 C   s4   | j | j| j| j}t }|| | | d S rG   )r   to_jsonr\   r9   r:   r   add_profile_chunkr;   )re   r   enveloper@   r@   rA   r     s   
zProfileBuffer.flushN)rg   rh   ri   r}   r   r   r   r@   r@   r@   rA   r   ]  s
    	r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )r   c                 C   s.   t  j| _i | _i | _g | _g | _g | _d S rG   )	r   r   r   chunk_idindexed_framesindexed_stacksframesstackssamplesrd   r@   r@   rA   r}     s   
zProfileChunk.__init__c           	   	      s   |D ]]\}\}}}zG| j vrBt|D ]\}}| jvr,t j j|<  j||  qt j  j |<  j fdd|D   j|| j | d W q ty_   t	t
  Y qw d S )Nc                    s   g | ]} j | qS r@   )r   )r   frame_idrd   r@   rA   r     s    z&ProfileChunk.write.<locals>.<listcomp>r#   )r   	enumerater   r   r   r   r   r   r   r
   r   r   )	re   r   r   r   r&   	frame_idsr   ir   r@   rd   rA   r     s,   

zProfileChunk.writec                 C   s   | j | j| jdd t D d}t|d |d |d |d  | j|d td	d
||dd}dD ]}|| d urCt|| 	 ||< q1|S )Nc                 S   s"   i | ]}t |jd t |jiqS )r   )r   identr   )r   r   r@   r@   rA   
<dictcomp>  s    
z(ProfileChunk.to_json.<locals>.<dictcomp>)r   r   r   thread_metadatar   in_app_excludein_app_includeproject_rootr   )r   versionpython2)r   
client_sdkplatformr   r\   r   )releaseenvironmentdist)
r   r   r   r   r   r   r   r   r   strip)re   r\   r9   r:   r   payloadkeyr@   r@   rA   r     s6   zProfileChunk.to_jsonN)rg   rh   ri   r}   r   r   r@   r@   r@   rA   r     s    
r   )Or7   r   r^   r   r   r)   r   rT   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!   r_   intr"   gevent.monkeyr'   gevent.threadpoolr(   _ThreadPoolr   ImportErrorr*   r/   rB   rE   rJ   rL   rN   rW   rZ   r[   r1   r]   ra   rb   rl   r3   r2   r   r   r   r@   r@   r@   rA   <module>   s     	
6
 lBI0