diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/exponential_histogram/mapping/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/exponential_histogram/mapping/__init__.py index 27d88f66bff..df617a134a0 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/exponential_histogram/mapping/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/exponential_histogram/mapping/__init__.py @@ -13,6 +13,7 @@ # limitations under the License. from abc import ABC, abstractmethod +from threading import Lock class Mapping(ABC): @@ -20,6 +21,12 @@ class Mapping(ABC): # pylint: disable=no-member def __new__(cls, scale: int): + if not hasattr(cls, "_mappings"): + cls._mappings = {} + + if not hasattr(cls, "_mappings_lock"): + cls._mappings_lock = Lock() + with cls._mappings_lock: if scale not in cls._mappings: cls._mappings[scale] = super().__new__(cls) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/exponential_histogram/mapping/exponent_mapping.py b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/exponential_histogram/mapping/exponent_mapping.py index a7aa2e2650a..8e975bf857b 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/exponential_histogram/mapping/exponent_mapping.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/exponential_histogram/mapping/exponent_mapping.py @@ -34,9 +34,6 @@ class ExponentMapping(Mapping): - _mappings = {} - _mappings_lock = Lock() - _min_scale = -10 _max_scale = 0 diff --git a/opentelemetry-sdk/tests/metrics/exponential_histogram/test_mapping.py b/opentelemetry-sdk/tests/metrics/exponential_histogram/test_mapping.py new file mode 100644 index 00000000000..d4542965d10 --- /dev/null +++ b/opentelemetry-sdk/tests/metrics/exponential_histogram/test_mapping.py @@ -0,0 +1,57 @@ +# 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 unittest import TestCase +from math import inf + +from opentelemetry.sdk.metrics._internal.exponential_histogram.mapping import ( + Mapping, +) + + +class TestMapping(TestCase): + + def test_lock(self): + + class Child0(Mapping): + def _get_max_scale(self) -> int: + return inf + + def _get_min_scale(self) -> int: + return -inf + + def map_to_index(self, value: float) -> int: + pass + + def get_lower_boundary(self, index: int) -> float: + pass + + class Child1(Mapping): + def _get_max_scale(self) -> int: + return inf + + def _get_min_scale(self) -> int: + return -inf + + def map_to_index(self, value: float) -> int: + pass + + def get_lower_boundary(self, index: int) -> float: + pass + + child_0 = Child0(0) + child_1 = Child1(1) + + self.assertIsNot(child_0._mappings, child_1._mappings) + self.assertIsNot(child_0._mappings_lock, child_1._mappings_lock)