Repository: open-telemetry/opentelemetry-python

Issue:
Check if ThreadLocalRuntimeContext can be removed since python3.4 support is dropped
https://github.com/open-telemetry/opentelemetry-python/blob/master/opentelemetry-api/src/opentelemetry/context/threadlocal_context.py#L21


Discussion:

Resolving patch:
diff --git a/opentelemetry-api/src/opentelemetry/context/threadlocal_context.py b/opentelemetry-api/src/opentelemetry/context/threadlocal_context.py
deleted file mode 100644
--- a/opentelemetry-api/src/opentelemetry/context/threadlocal_context.py
+++ /dev/null
@@ -1,60 +0,0 @@
-# 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.
-
-import threading
-
-from opentelemetry.context.context import Context, RuntimeContext
-
-
-class ThreadLocalRuntimeContext(RuntimeContext):
-    """An implementation of the RuntimeContext interface
-    which uses thread-local storage under the hood. This
-    implementation is available for usage with Python 3.4.
-    """
-
-    class Token:
-        def __init__(self, context: Context) -> None:
-            self._context = context
-
-    _CONTEXT_KEY = "current_context"
-
-    def __init__(self) -> None:
-        self._current_context = threading.local()
-
-    def attach(self, context: Context) -> object:
-        """See `opentelemetry.context.RuntimeContext.attach`."""
-        current = self.get_current()
-        setattr(self._current_context, self._CONTEXT_KEY, context)
-        return self.Token(current)
-
-    def get_current(self) -> Context:
-        """See `opentelemetry.context.RuntimeContext.get_current`."""
-        if not hasattr(self._current_context, self._CONTEXT_KEY):
-            setattr(
-                self._current_context, self._CONTEXT_KEY, Context(),
-            )
-        context = getattr(
-            self._current_context, self._CONTEXT_KEY
-        )  # type: Context
-        return context
-
-    def detach(self, token: object) -> None:
-        """See `opentelemetry.context.RuntimeContext.detach`."""
-        if not isinstance(token, self.Token):
-            raise ValueError("invalid token")
-        # pylint: disable=protected-access
-        setattr(self._current_context, self._CONTEXT_KEY, token._context)
-
-
-__all__ = ["ThreadLocalRuntimeContext"]


Test patch:
diff --git a/opentelemetry-api/tests/context/test_threadlocal_context.py b/opentelemetry-api/tests/context/test_threadlocal_context.py
deleted file mode 100644
--- a/opentelemetry-api/tests/context/test_threadlocal_context.py
+++ /dev/null
@@ -1,33 +0,0 @@
-# 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.mock import patch
-
-from opentelemetry import context
-from opentelemetry.context.threadlocal_context import ThreadLocalRuntimeContext
-
-from .base_context import ContextTestCases
-
-
-class TestThreadLocalContext(ContextTestCases.BaseTest):
-    def setUp(self) -> None:
-        super(TestThreadLocalContext, self).setUp()
-        self.mock_runtime = patch.object(
-            context, "_RUNTIME_CONTEXT", ThreadLocalRuntimeContext(),
-        )
-        self.mock_runtime.start()
-
-    def tearDown(self) -> None:
-        super(TestThreadLocalContext, self).tearDown()
-        self.mock_runtime.stop()