From 71db261c7b19b6547f4d91a7f2270aa07a638f78 Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Wed, 18 Sep 2024 22:12:37 +0200 Subject: [PATCH] opentelemetry-test-utils: don't crash in TestBase.get_sorted_metrics without metrics (#4194) --- .../src/opentelemetry/test/test_base.py | 3 ++- .../tests/test_base.py | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 tests/opentelemetry-test-utils/tests/test_base.py diff --git a/tests/opentelemetry-test-utils/src/opentelemetry/test/test_base.py b/tests/opentelemetry-test-utils/src/opentelemetry/test/test_base.py index f9ac2dfc193..456bd1413f3 100644 --- a/tests/opentelemetry-test-utils/src/opentelemetry/test/test_base.py +++ b/tests/opentelemetry-test-utils/src/opentelemetry/test/test_base.py @@ -144,8 +144,9 @@ def disable_logging(highest_level=logging.CRITICAL): logging.disable(logging.NOTSET) def get_sorted_metrics(self): + metrics_data = self.memory_metrics_reader.get_metrics_data() resource_metrics = ( - self.memory_metrics_reader.get_metrics_data().resource_metrics + metrics_data.resource_metrics if metrics_data else [] ) all_metrics = [] diff --git a/tests/opentelemetry-test-utils/tests/test_base.py b/tests/opentelemetry-test-utils/tests/test_base.py new file mode 100644 index 00000000000..92b83b9b34c --- /dev/null +++ b/tests/opentelemetry-test-utils/tests/test_base.py @@ -0,0 +1,21 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from opentelemetry.test.test_base import TestBase + + +class TestBaseTestCase(TestBase): + def test_get_sorted_metrics_works_without_metrics(self): + metrics = self.get_sorted_metrics() + self.assertEqual(metrics, [])