o
    i'                     @   s   d dl 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 ddlmZ G dd deZG dd deZG dd deZdS )    N   )ValueCollector)CPU_PERCENT)CPU_TIME_SYS)CPU_TIME_USER)CTX_SWITCH_INVOLUNTARY)CTX_SWITCH_VOLUNTARY)GC_COUNT_GEN0)GC_COUNT_GEN1)GC_COUNT_GEN2)MEM_RSS)THREAD_COUNTc                   @   s   e Zd Zg ZdZdS )RuntimeMetricCollectorTN)__name__
__module____qualname__valueperiodic r   r   ^/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/internal/runtime/metric_collectors.pyr      s    r   c                   @   s   e Zd ZdZdgZdd ZdS )GCRuntimeMetricCollectorz|Collector for garbage collection generational counts

    More information at https://docs.python.org/3/library/gc.html
    gcc                 C   s:   | j d}| }t|d ft|d ft|d fg}|S )Nr   r   r      )modulesget	get_countr	   r
   r   )selfkeysr   countsmetricsr   r   r   
collect_fn   s   


z#GCRuntimeMetricCollector.collect_fnN)r   r   r   __doc__required_modulesr    r   r   r   r   r      s    r   c                	   @   sf   e Zd ZdZdgZedd edd edd edd iZ	e
dd ed	d ed
d iZdd Zdd ZdS )PSUtilRuntimeMetricCollectorzCollector for psutil metrics.

    Performs batched operations via proc.oneshot() to optimize the calls.
    See https://psutil.readthedocs.io/en/latest/#psutil.Process.oneshot
    for more information.
    ddtrace.vendor.psutilc                 C   
   |   jS N)	cpu_timessystempr   r   r   <lambda>4      
 z%PSUtilRuntimeMetricCollector.<lambda>c                 C   r%   r&   )r'   userr)   r   r   r   r+   5   r,   c                 C   r%   r&   )num_ctx_switches	voluntaryr)   r   r   r   r+   6   r,   c                 C   r%   r&   )r.   involuntaryr)   r   r   r   r+   7   r,   c                 C      |   S r&   )num_threadsr)   r   r   r   r+   :       c                 C   r%   r&   )memory_inforssr)   r   r   r   r+   ;   r,   c                 C   r1   r&   )cpu_percentr)   r   r   r   r+   <   r3   c                 C   s0   | j d t | _dd | j D | _d S )Nr$   c                 S   s   i | ]}|d qS )r   r   ).0keyr   r   r   
<dictcomp>A   s    zAPSUtilRuntimeMetricCollector._on_modules_load.<locals>.<dictcomp>)r   Processosgetpidproc
delta_funsr   stored_values)r   r   r   r   _on_modules_load?   s   z-PSUtilRuntimeMetricCollector._on_modules_loadc              
   C   s   | j  ` i }| j D ])\}}z|| j }W n ty#   d}Y nw || j|d }|| j|< |||< q| j D ]\}}z|| j }W n tyR   d}Y nw |||< q<t| W  d    S 1 shw   Y  d S )Nr   )	r=   oneshotr>   items	Exceptionr?   r   abs_funslist)r   r   r   metric	delta_funr   deltaabs_funr   r   r   r    C   s(   



$z'PSUtilRuntimeMetricCollector.collect_fnN)r   r   r   r!   r"   r   r   r   r   r>   r   r   r   rD   r@   r    r   r   r   r   r#   *   s    r#   )r;   	collectorr   	constantsr   r   r   r   r   r	   r
   r   r   r   r   r   r#   r   r   r   r   <module>   s    