o
    á)i  ã                   @   s|   U 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	 e	e
ƒZdaeej ed< dd„ Zdd	„ Zd
d„ Zdd„ ZdS )é    N)ÚOptional)ÚREGISTRYÚCollectorRegistryÚmultiprocess)Úinit_loggerÚ_prometheus_multiproc_dirc                   C   s>   dt jvrt ¡ atjt jd< t dtj¡ dS t d¡ dS )zPSet up prometheus multiprocessing directory if not already configured.
    
    ÚPROMETHEUS_MULTIPROC_DIRz&Created PROMETHEUS_MULTIPROC_DIR at %sz½Found PROMETHEUS_MULTIPROC_DIR was set by user. This directory must be wiped between vLLM runs or you will find inaccurate metrics. Unset the variable and vLLM will properly handle cleanup.N)	ÚosÚenvironÚtempfileÚTemporaryDirectoryr   ÚnameÚloggerÚdebugÚwarning© r   r   úf/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/vllm/v1/metrics/prometheus.pyÚsetup_multiprocess_prometheus   s   
ÿr   c                  C   s0   t  d¡durt d¡ tƒ } t | ¡ | S tS )z“Get the appropriate prometheus registry based on multiprocessing 
    configuration.
    
    Returns:
        Registry: A prometheus registry
    r   Nz2Using multiprocess registry for prometheus metrics)r	   Úgetenvr   r   r   r   ZMultiProcessCollectorr   )Úregistryr   r   r   Úget_prometheus_registry'   s   

r   c                  C   s6   t } t| jƒD ]}t|dƒrd|jv r|  |¡ qdS )a&  Unregister any existing vLLM collectors from the prometheus registry.
    
    This is useful for testing and CI/CD where metrics may be registered
    multiple times across test runs.
    
    Also, in case of multiprocess, we need to unregister the metrics from the 
    global registry.
    Ú_nameZvllmN)r   ÚlistZ_collector_to_namesÚhasattrr   Ú
unregister)r   Ú	collectorr   r   r   Úunregister_vllm_metrics7   s   	
€þr   c               
   C   sn   t } | du rdS zt ¡ }t || ¡ t d|¡ W dS  ty6 } zt dt	|ƒ¡ W Y d}~dS d}~ww )zShutdown prometheus metrics.Nz0Marked Prometheus metrics for process %d as deadz Error during metrics cleanup: %s)
r   r	   Úgetpidr   Zmark_process_deadr   r   Ú	ExceptionÚerrorÚstr)ÚpathÚpidÚer   r   r   Úshutdown_prometheusG   s   €ÿr$   )r	   r   Útypingr   Zprometheus_clientr   r   r   Zvllm.loggerr   Ú__name__r   r   r   Ú__annotations__r   r   r   r$   r   r   r   r   Ú<module>   s   
