|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 |
| -# pylint: disable=protected-access |
| 15 | +# pylint: disable=protected-access,too-many-lines |
16 | 16 |
|
17 | 17 | import sys
|
18 | 18 | from collections import namedtuple
|
@@ -235,14 +235,28 @@ def _assert_metrics(self, observer_name, reader, expected):
|
235 | 235 | assertions += 1
|
236 | 236 | self.assertEqual(len(expected), assertions)
|
237 | 237 |
|
238 |
| - def _test_metrics(self, observer_name, expected): |
| 238 | + @staticmethod |
| 239 | + def _setup_instrumentor() -> InMemoryMetricReader: |
239 | 240 | reader = InMemoryMetricReader()
|
240 | 241 | meter_provider = MeterProvider(metric_readers=[reader])
|
241 | 242 |
|
242 | 243 | system_metrics = SystemMetricsInstrumentor()
|
243 | 244 | system_metrics.instrument(meter_provider=meter_provider)
|
| 245 | + return reader |
| 246 | + |
| 247 | + def _test_metrics(self, observer_name, expected): |
| 248 | + reader = self._setup_instrumentor() |
244 | 249 | self._assert_metrics(observer_name, reader, expected)
|
245 | 250 |
|
| 251 | + def _assert_metrics_not_found(self, observer_name): |
| 252 | + reader = self._setup_instrumentor() |
| 253 | + seen_metrics = set() |
| 254 | + for resource_metrics in reader.get_metrics_data().resource_metrics: |
| 255 | + for scope_metrics in resource_metrics.scope_metrics: |
| 256 | + for metric in scope_metrics.metrics: |
| 257 | + seen_metrics.add(metric.name) |
| 258 | + self.assertNotIn(observer_name, seen_metrics) |
| 259 | + |
246 | 260 | # This patch is added here to stop psutil from raising an exception
|
247 | 261 | # because we're patching cpu_times
|
248 | 262 | # pylint: disable=unused-argument
|
@@ -855,6 +869,14 @@ def test_context_switches(self, mock_process_num_ctx_switches):
|
855 | 869 | ]
|
856 | 870 | self._test_metrics("process.context_switches", expected)
|
857 | 871 |
|
| 872 | + @mock.patch("psutil.Process.num_ctx_switches") |
| 873 | + def test_context_switches_not_implemented_error( |
| 874 | + self, mock_process_num_ctx_switches |
| 875 | + ): |
| 876 | + mock_process_num_ctx_switches.side_effect = NotImplementedError |
| 877 | + |
| 878 | + self._assert_metrics_not_found("process.context_switches") |
| 879 | + |
858 | 880 | @mock.patch("psutil.Process.num_threads")
|
859 | 881 | def test_thread_count(self, mock_process_thread_num):
|
860 | 882 | mock_process_thread_num.configure_mock(**{"return_value": 42})
|
@@ -947,6 +969,16 @@ def test_runtime_context_switches(self, mock_process_num_ctx_switches):
|
947 | 969 | f"process.runtime.{self.implementation}.context_switches", expected
|
948 | 970 | )
|
949 | 971 |
|
| 972 | + @mock.patch("psutil.Process.num_ctx_switches") |
| 973 | + def test_runtime_context_switches_not_implemented_error( |
| 974 | + self, mock_process_num_ctx_switches |
| 975 | + ): |
| 976 | + mock_process_num_ctx_switches.side_effect = NotImplementedError |
| 977 | + |
| 978 | + self._assert_metrics_not_found( |
| 979 | + f"process.runtime.{self.implementation}.context_switches", |
| 980 | + ) |
| 981 | + |
950 | 982 | @mock.patch("psutil.Process.num_threads")
|
951 | 983 | def test_runtime_thread_count(self, mock_process_thread_num):
|
952 | 984 | mock_process_thread_num.configure_mock(**{"return_value": 42})
|
|
0 commit comments