o
    );c                     @   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y7   dZY nw G dd de
Ze ZdS )	    N)CallableIterableUnion   )CounterMetricFamilyGaugeMetricFamilyMetric)	CollectorCollectorRegistryREGISTRYi   c                	   @   sb   e Zd ZdZddd defdedeg eeef f ded	e	fd
dZ
dd Zdee fddZdS )ProcessCollectorz6Collector for Standard Exports such as cpu and memory. c                   C   s   dS )Nself r   r   r   n/Users/cmarchbanks/go/src/github.com/prometheus/client_python/build/lib/prometheus_client/process_collector.py<lambda>   s    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y,   Y nw t| _d| _z|  | _W n	 t
yC   Y nw |rM||  d S d S )NZ	_process_Zprocess_g      Y@
SC_CLK_TCKr   )Z
_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   sr   t tj| jdd$}|D ]}|dr&t| d   W  d    S qW d    d S 1 s2w   Y  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 d    n1 s6w   Y  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y   Y nw zSttj|dd$}|D ]}|drt| jd dt|
 d d} nqW d    n1 sw   Y  t| jd dtttj|d}|||g W |S  ty   Y |S w )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)   readr.   r   r   r-   r"   r   r   extendr    r,   lenlistdir)r   r   resultr'   partsZvmemZrssZstart_time_secsZ
start_timeutimeZstimeZcpur9   r/   r:   r<   r   r   r   collect6   sf   





zProcessCollector.collectN)__name__
__module____qualname____doc__r   r>   r   r   intr
   r&   r$   r   r   rG   r   r   r   r   r      s"    
r   )r   typingr   r   r   metrics_corer   r   r   r   r	   r
   r   ZresourceZgetpagesizer!   ImportErrorr   PROCESS_COLLECTORr   r   r   r   <module>   s    T