-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathtest_system_metrics.py
152 lines (134 loc) · 5.05 KB
/
test_system_metrics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from __future__ import annotations
import pytest
from inline_snapshot import snapshot
from opentelemetry.instrumentation.system_metrics import SystemMetricsInstrumentor
from opentelemetry.sdk.metrics.export import InMemoryMetricReader
import logfire
import logfire._internal.metrics
from logfire._internal.integrations.system_metrics import get_base_config
from tests.test_metrics import get_collected_metrics
def get_collected_metric_names(metrics_reader: InMemoryMetricReader) -> list[str]:
try:
return sorted(
{
metric['name']
for metric in get_collected_metrics(metrics_reader)
if metric['name'] != 'system.network.connections'
}
)
finally:
SystemMetricsInstrumentor().uninstrument()
def test_default_system_metrics_collection(metrics_reader: InMemoryMetricReader) -> None:
logfire.instrument_system_metrics()
assert get_collected_metric_names(metrics_reader) == snapshot(
[
'process.runtime.cpython.cpu.utilization',
'system.cpu.simple_utilization',
'system.memory.utilization',
'system.swap.utilization',
]
)
def test_all_system_metrics_collection(metrics_reader: InMemoryMetricReader) -> None:
logfire.instrument_system_metrics(base='full')
assert get_collected_metric_names(metrics_reader) == snapshot(
[
'process.open_file_descriptor.count',
'process.runtime.cpython.context_switches',
'process.runtime.cpython.cpu.utilization',
'process.runtime.cpython.cpu_time',
'process.runtime.cpython.gc_count',
'process.runtime.cpython.memory',
'process.runtime.cpython.thread_count',
'system.cpu.simple_utilization',
'system.cpu.time',
'system.cpu.utilization',
'system.disk.io',
'system.disk.operations',
'system.disk.time',
'system.memory.usage',
'system.memory.utilization',
'system.network.dropped_packets',
'system.network.errors',
'system.network.io',
'system.network.packets',
'system.swap.usage',
'system.swap.utilization',
'system.thread_count',
]
)
def test_custom_system_metrics_collection(metrics_reader: InMemoryMetricReader) -> None:
logfire.instrument_system_metrics({'system.memory.utilization': ['available']}, base=None)
assert get_collected_metric_names(metrics_reader) == ['system.memory.utilization']
def test_basic_base():
assert get_base_config('basic') == {
'process.runtime.cpu.utilization': None,
'system.cpu.simple_utilization': None,
'system.memory.utilization': ['available'],
'system.swap.utilization': ['used'],
}, 'Docs need to be updated if this test fails'
def test_full_base():
config = get_base_config('full')
config.pop('system.network.connections', None)
assert config == {
'system.cpu.simple_utilization': None,
'system.cpu.time': ['idle', 'user', 'system', 'irq', 'softirq', 'nice', 'iowait', 'steal', 'interrupt', 'dpc'],
'system.cpu.utilization': [
'idle',
'user',
'system',
'irq',
'softirq',
'nice',
'iowait',
'steal',
'interrupt',
'dpc',
],
'system.memory.usage': [
'available',
'used',
'free',
'active',
'inactive',
'buffers',
'cached',
'shared',
'wired',
'slab',
'total',
],
'system.memory.utilization': [
'available',
'used',
'free',
'active',
'inactive',
'buffers',
'cached',
'shared',
'wired',
'slab',
],
'system.swap.usage': ['used', 'free'],
'system.swap.utilization': ['used'],
'system.disk.io': ['read', 'write'],
'system.disk.operations': ['read', 'write'],
'system.disk.time': ['read', 'write'],
'system.network.dropped.packets': ['transmit', 'receive'],
'system.network.packets': ['transmit', 'receive'],
'system.network.errors': ['transmit', 'receive'],
'system.network.io': ['transmit', 'receive'],
'system.thread_count': None,
'process.runtime.memory': ['rss', 'vms'],
'process.runtime.cpu.time': ['user', 'system'],
'process.runtime.gc_count': None,
'process.runtime.thread_count': None,
'process.runtime.cpu.utilization': None,
'process.runtime.context_switches': ['involuntary', 'voluntary'],
'process.open_file_descriptor.count': None,
}, 'Docs and the MetricName type need to be updated if this test fails'
def test_empty_base():
assert get_base_config(None) == {}
def test_invalid_base():
with pytest.raises(ValueError):
get_base_config('invalid') # type: ignore