18
18
from platform import python_implementation
19
19
from unittest import mock
20
20
21
- from opentelemetry .instrumentation .system_metrics import (
22
- SystemMetricsInstrumentor ,
23
- )
24
21
from opentelemetry .sdk .metrics import MeterProvider
25
22
from opentelemetry .sdk .metrics .export import InMemoryMetricReader
26
23
from opentelemetry .test .test_base import TestBase
27
24
25
+ from opentelemetry .instrumentation .system_metrics import (
26
+ SystemMetricsInstrumentor ,
27
+ )
28
+
28
29
29
30
def _mock_netconnection ():
30
31
NetConnection = namedtuple (
@@ -96,7 +97,7 @@ def test_system_metrics_instrument(self):
96
97
for scope_metrics in resource_metrics .scope_metrics :
97
98
for metric in scope_metrics .metrics :
98
99
metric_names .append (metric .name )
99
- self .assertEqual (len (metric_names ), 18 )
100
+ self .assertEqual (len (metric_names ), 21 )
100
101
101
102
observer_names = [
102
103
"system.cpu.time" ,
@@ -117,6 +118,9 @@ def test_system_metrics_instrument(self):
117
118
f"process.runtime.{ self .implementation } .memory" ,
118
119
f"process.runtime.{ self .implementation } .cpu_time" ,
119
120
f"process.runtime.{ self .implementation } .gc_count" ,
121
+ f"process.runtime.{ self .implementation } .thread_count" ,
122
+ f"process.runtime.{ self .implementation } .context_switches" ,
123
+ f"process.runtime.{ self .implementation } .cpu.utilization" ,
120
124
]
121
125
122
126
for observer in metric_names :
@@ -128,6 +132,9 @@ def test_runtime_metrics_instrument(self):
128
132
"process.runtime.memory" : ["rss" , "vms" ],
129
133
"process.runtime.cpu.time" : ["user" , "system" ],
130
134
"process.runtime.gc_count" : None ,
135
+ "process.runtime.thread_count" : None ,
136
+ "process.runtime.cpu.utilization" : None ,
137
+ "process.runtime.context_switches" : ["involuntary" , "voluntary" ],
131
138
}
132
139
133
140
reader = InMemoryMetricReader ()
@@ -140,12 +147,15 @@ def test_runtime_metrics_instrument(self):
140
147
for scope_metrics in resource_metrics .scope_metrics :
141
148
for metric in scope_metrics .metrics :
142
149
metric_names .append (metric .name )
143
- self .assertEqual (len (metric_names ), 3 )
150
+ self .assertEqual (len (metric_names ), 6 )
144
151
145
152
observer_names = [
146
153
f"process.runtime.{ self .implementation } .memory" ,
147
154
f"process.runtime.{ self .implementation } .cpu_time" ,
148
155
f"process.runtime.{ self .implementation } .gc_count" ,
156
+ f"process.runtime.{ self .implementation } .thread_count" ,
157
+ f"process.runtime.{ self .implementation } .context_switches" ,
158
+ f"process.runtime.{ self .implementation } .cpu.utilization" ,
149
159
]
150
160
151
161
for observer in metric_names :
@@ -161,9 +171,9 @@ def _assert_metrics(self, observer_name, reader, expected):
161
171
for data_point in metric .data .data_points :
162
172
for expect in expected :
163
173
if (
164
- dict (data_point .attributes )
165
- == expect .attributes
166
- and metric .name == observer_name
174
+ dict (data_point .attributes )
175
+ == expect .attributes
176
+ and metric .name == observer_name
167
177
):
168
178
self .assertEqual (
169
179
data_point .value ,
@@ -782,3 +792,37 @@ def test_runtime_get_count(self, mock_gc_get_count):
782
792
self ._test_metrics (
783
793
f"process.runtime.{ self .implementation } .gc_count" , expected
784
794
)
795
+
796
+ @mock .patch ("psutil.Process.num_ctx_switches" )
797
+ def test_runtime_context_switches (self , mock_process_num_ctx_switches ):
798
+ PCtxSwitches = namedtuple ("PCtxSwitches" , ["voluntary" , "involuntary" ])
799
+
800
+ mock_process_num_ctx_switches .configure_mock (
801
+ ** {"return_value" : PCtxSwitches (voluntary = 1 , involuntary = 2 )}
802
+ )
803
+
804
+ expected = [
805
+ _SystemMetricsResult ({"type" : "voluntary" }, 1 ),
806
+ _SystemMetricsResult ({"type" : "involuntary" }, 2 ),
807
+ ]
808
+ self ._test_metrics (
809
+ f"process.runtime.{ self .implementation } .context_switches" , expected
810
+ )
811
+
812
+ @mock .patch ("psutil.Process.num_threads" )
813
+ def test_runtime_thread_num (self , mock_process_thread_num ):
814
+ mock_process_thread_num .configure_mock (** {"return_value" : 42 })
815
+
816
+ expected = [_SystemMetricsResult ({}, 42 )]
817
+ self ._test_metrics (
818
+ f"process.runtime.{ self .implementation } .thread_count" , expected
819
+ )
820
+
821
+ @mock .patch ("psutil.Process.cpu_percent" )
822
+ def test_runtime_cpu_percent (self , mock_process_cpu_percent ):
823
+ mock_process_cpu_percent .configure_mock (** {"return_value" : 42 })
824
+
825
+ expected = [_SystemMetricsResult ({}, 42 )]
826
+ self ._test_metrics (
827
+ f"process.runtime.{ self .implementation } .cpu.utilization" , expected
828
+ )
0 commit comments