U
    d                     @   s   d dl Z d dlmZmZmZ ddlmZmZmZ ddl	m
Z
mZmZ zd dlZe ZW n ek
rp   dZY nX G dd de
Ze ZdS )	    N)CallableIterableUnion   )CounterMetricFamilyGaugeMetricFamilyMetric)	CollectorCollectorRegistryREGISTRYi   c                   @   s\   e Zd ZdZddd defeeg eeef f ee	dddZ
d	d
 Zee dddZdS )ProcessCollectorz6Collector for Standard Exports such as cpu and memory. c                   C   s   dS )Nself r   r   r   G/tmp/pip-unpacked-wheel-2bcd7zt2/prometheus_client/process_collector.py<lambda>       zProcessCollector.<lambda>z/proc)	namespacepidprocregistryc                 C   s   || _ || _|| _|r"|d | _nd| _d| _ztd| _W n ttt	t
fk
rZ   Y nX t| _d| _z|  | _W n t
k
r   Y nX |r||  d S )NZ	_process_Zprocess_g      Y@
SC_CLK_TCKr   )
_namespace_pid_proc_prefix_ticksossysconf
ValueError	TypeErrorAttributeErrorOSError	_PAGESIZE	_pagesize_btime
_boot_timeregister)r   r   r   r   r   r   r   r   __init__   s&    zProcessCollector.__init__c              
   C   sZ   t tj| jdd:}|D ].}|drt| d   W  5 Q R  S qW 5 Q R X d S )Nstatrbs   btime r   )openr   pathjoinr   
startswithfloatsplit)r   r)   liner   r   r   r&   0   s    
zProcessCollector._boot_time)returnc              
   C   s  | j s
g S tj| jt|   }g }zttj|dd}|	 
dd 
 }W 5 Q R X t| jd dt|d d}t| jd	 d
t|d | j d}t|d | j }t| jd d|| j  d}t|d | j }	t|d | j }
t| jd d|	|
 d}|||||g W n tk
r2   Y nX zttj|ddD}|D ]8}|drPt| jd dt|
 d d} qqPW 5 Q R X t| jd dtttj|d}|||g W n tk
r   Y nX |S )Nr)   r*      )Zvirtual_memory_byteszVirtual memory size in bytes.   )valueZresident_memory_byteszResident memory size in bytes.      Zstart_time_secondsz6Start time of the process since unix epoch in seconds.      Zcpu_seconds_totalz0Total user and system CPU time spent in seconds.limitss   Max open filemax_fdsz(Maximum number of open file descriptors.   open_fdsz Number of open file descriptors.fd)r%   r   r,   r-   r   strr   stripr+   readr0   r   r   r/   r$   r   r   extendr"   r.   lenlistdir)r   r   resultr)   partsZvmemZrssZstart_time_secs
start_timeutimeZstimecpur;   r1   r<   r>   r   r   r   collect6   sZ     
 




zProcessCollector.collectN)__name__
__module____qualname____doc__r   r@   r   r   intr
   r(   r&   r   r   rK   r   r   r   r   r      s   r   )r   typingr   r   r   Zmetrics_corer   r   r   r   r	   r
   r   resourceZgetpagesizer#   ImportErrorr   ZPROCESS_COLLECTORr   r   r   r   <module>   s   
T