o
    Zh                     @   sh   d dl Z d dlZd dlmZmZ d dlZd dlmZ d dlm	Z	 e	r(d dl
mZ dZG dd deZdS )	    N)ThreadLock)logger)TYPE_CHECKING)Optional
   c                   @   sb   e Zd ZdZdZd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 )Monitorz
    Performs health checks in a separate thread once every interval seconds
    and updates the internal state. Other parts of the SDK only read this state
    and act accordingly.
    zsentry.monitorr   c                 C   s6   || _ || _d| _d| _d | _t | _d | _d| _d S )NTr   )		transportinterval_healthy_downsample_factor_threadr   _thread_lock_thread_for_pid_running)selfr	   r
    r   I/var/www/html/lang_env/lib/python3.10/site-packages/sentry_sdk/monitor.py__init__   s   
zMonitor.__init__c              	      s    j t kr jdurdS  jP  j t kr' jdur'	 W d   dS  fdd}t j|d}d|_z|  W n t	yP   d _
Y W d   dS w | _t  _ W d   dS 1 sdw   Y  dS )a-  
        Check that the monitor has an active thread to run in, or create one if not.

        Note that this might fail (e.g. in Python 3.12 it's not possible to
        spawn new threads at interpreter shutdown). In that case self._running
        will be False after running this function.
        Nc                      s.    j rt j  j r    j sd S d S N)r   timesleepr
   runr   r   r   r   r   6   s
   z(Monitor._ensure_running.<locals>._thread)nametargetTF)r   osgetpidr   r   r   r   daemonstartRuntimeErrorr   )r   r   threadr   r   r   _ensure_running&   s,   	
zMonitor._ensure_runningc                 C   s   |    |   d S r   )check_healthset_downsample_factorr   r   r   r   r   L   s   zMonitor.runc                 C   sN   | j r| jdkrtd d| _d S | jtk r|  jd7  _td| j d S )Nr   z=[Monitor] health check positive, reverting to normal sampling   zA[Monitor] health check negative, downsampling with a factor of %d)r   r   r   debugdownsample_factorMAX_DOWNSAMPLE_FACTORr   r   r   r   r$   Q   s   


zMonitor.set_downsample_factorc                 C   s   | j  | _dS )z
        Perform the actual health checks,
        currently only checks if the transport is rate-limited.
        TODO: augment in the future with more checks.
        N)r	   
is_healthyr   r   r   r   r   r#   a   s   zMonitor.check_healthc                 C      |    | jS r   )r"   r   r   r   r   r   r)   j   s   zMonitor.is_healthyc                 C   r*   r   )r"   r   r   r   r   r   r'   o   s   zMonitor.downsample_factorc                 C   s
   d| _ d S )NF)r   r   r   r   r   killu   s   
zMonitor.killc                 C   s   |    d S r   )r+   r   r   r   r   __del__y   s   zMonitor.__del__N)r   )__name__
__module____qualname____doc__r   r   r"   r   r$   r#   r)   propertyr'   r+   r,   r   r   r   r   r      s    
&	
r   )r   r   	threadingr   r   Z
sentry_sdkZsentry_sdk.utilsr   Zsentry_sdk._typesr   typingr   r(   objectr   r   r   r   r   <module>   s    