o
    i%                  
   @  s  U d dl mZ d dlZd dlmZ d dlmZ d dlmZm	Z	m
Z
mZ d dlmZmZ er9d dlmZ d dlmZ zd dlZd d	lmZmZ W n eyX Z zed
edZ[ww e	d Zded< eee
ee  f Zd Z ded< d Z!ded< i eeeddde e e!dg e!dgdZ"ded< ejdkre"#dd dD ]Z$e"#e$d qdddgdgdZ%ded< e	d Z&d3d"d#Z'd4d5d)d*Z(d6d+d,Z)d6d-d.Z*d6d/d0Z+d6d1d2Z,dS )7    )annotationsN)Iterable)python_implementation)TYPE_CHECKINGLiteralOptionalcast)CallbackOptionsObservation)LiteralString)Logfire)_DEFAULT_CONFIGSystemMetricsInstrumentorz`logfire.instrument_system_metrics()` requires the `opentelemetry-instrumentation-system-metrics` package.
You can install this with:
    pip install 'logfire[system-metrics]')system.cpu.simple_utilizationsystem.cpu.timesystem.cpu.utilizationsystem.memory.usagesystem.memory.utilizationzsystem.swap.usagesystem.swap.utilizationzsystem.disk.iozsystem.disk.operationszsystem.disk.timezsystem.network.dropped.packetszsystem.network.packetszsystem.network.errorszsystem.network.iosystem.network.connectionszsystem.thread_countz"process.open_file_descriptor.countzprocess.context_switcheszprocess.cpu.timeprocess.cpu.utilizationprocess.cpu.core_utilizationzprocess.memory.usagezprocess.memory.virtualzprocess.thread.countzprocess.runtime.gc_countzcpython.gc.collected_objectszcpython.gc.collectionsz cpython.gc.uncollectable_objectsa  type[Literal['system.cpu.simple_utilization', 'system.cpu.time', 'system.cpu.utilization', 'system.memory.usage', 'system.memory.utilization', 'system.swap.usage', 'system.swap.utilization', 'system.disk.io', 'system.disk.operations', 'system.disk.time', 'system.network.dropped.packets', 'system.network.packets', 'system.network.errors', 'system.network.io', 'system.network.connections', 'system.thread_count', 'process.open_file_descriptor.count', 'process.context_switches', 'process.cpu.time', 'process.cpu.utilization', 'process.cpu.core_utilization', 'process.memory.usage', 'process.memory.virtual', 'process.thread.count', 'process.runtime.gc_count', 'cpython.gc.collected_objects', 'cpython.gc.collections', 'cpython.gc.uncollectable_objects']]
MetricNamez<idle user system irq softirq nice iowait steal interrupt dpczlist[LiteralString]
CPU_FIELDSzDavailable used free active inactive buffers cached shared wired slabMEMORY_FIELDStotalused)r   r   r   r   r   r   r   r   ConfigFULL_CONFIGdarwinr   )zprocess.runtime.memoryzprocess.runtime.cpu.timezprocess.runtime.thread_countprocess.runtime.cpu.utilizationz process.runtime.context_switches	available)r   r   r   r   BASIC_CONFIG)basicfullNbaseBasereturnc                 C  s2   | dkrt S | dkrtS | d u ri S td|  )Nr#   r$   zInvalid base: )r"   r   
ValueError)r%    r)   s/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/logfire/_internal/integrations/system_metrics.pyget_base_config   s   r+   r#   logfire_instancer   configConfig | Nonec                 C  s   i t ||pi }d|v rt|  d|v rt|  d|v r%t|  |d= d|v r0t|  |d= t|d}|j| j d d S )Nr   r   r    r   )r-   )meter_provider)	r+   measure_simple_cpu_utilization$measure_process_cpu_core_utilization'measure_process_runtime_cpu_utilizationmeasure_process_cpu_utilizationr   
instrumentr-   get_meter_provider)r,   r-   r%   instrumentorr)   r)   r*   instrument_system_metrics   s   
r7   c                 C  s"   ddd}| j d|gdd	d
 d S )N_optionsr	   r'   Iterable[Observation]c                 s  s    t t d V  d S Nd   )r
   psutilcpu_percentr8   r)   r)   r*   callback   s   z0measure_simple_cpu_utilization.<locals>.callbackr   zBAverage CPU usage across all cores, as a fraction between 0 and 1.1descriptionunitr8   r	   r'   r9   )metric_gauge_callbackr,   r?   r)   r)   r*   r0      s   

r0   c                   sD   t      d fdd}| jdt   d|gd	d
d d S )Nr8   r	   r'   r9   c                 3      t   d V  d S r:   r
   r=   r>   processr)   r*   r?      s   z9measure_process_runtime_cpu_utilization.<locals>.callbackzprocess.runtime.z.cpu.utilizationRuntime CPU utilizationr@   rA   rD   )r<   Processr=   rE   r   lowerrF   r)   rI   r*   r2      s   
r2   c                   sD   t    t  pd d fdd}| jd|gd	d
d d S )N   r8   r	   r'   r9   c                 3  s    t  d   V  d S r:   rH   r>   num_cpusrJ   r)   r*   r?      s   z1measure_process_cpu_utilization.<locals>.callbackr   rK   r@   rA   rD   )r<   rL   r=   	cpu_countrE   rF   r)   rO   r*   r3      s   
r3   c                   s6   t      d fdd}| jd|gdd	d
 dS )zRSame as process.cpu.utilization, but not divided by the number of available cores.r8   r	   r'   r9   c                 3  rG   r:   rH   r>   rI   r)   r*   r?      s   z6measure_process_cpu_core_utilization.<locals>.callbackr   zFRuntime CPU utilization, not divided by the number of available cores.corerA   NrD   )r<   rL   r=   rE   rF   r)   rI   r*   r1      s   
r1   )r%   r&   r'   r   )Nr#   )r,   r   r-   r.   r%   r&   )r,   r   )-
__future__r   syscollections.abcr   platformr   typingr   r   r   r   opentelemetry.metricsr	   r
   typing_extensionsr   logfirer   r<   ,opentelemetry.instrumentation.system_metricsr   r   ImportErroreRuntimeErrorr   __annotations__dictstrr   splitr   r   r   pop_deprecatedr"   r&   r+   r7   r0   r2   r3   r1   r)   r)   r)   r*   <module>   sn    '
>




