From ca19dc275b6faf9d8ca5e13cc6b82cecdcf25e0b Mon Sep 17 00:00:00 2001 From: WqyJh <781345688@qq.com> Date: Mon, 22 Apr 2024 19:31:33 +0800 Subject: [PATCH] Resolve conversation --- .../src/opentelemetry/context/__init__.py | 15 +++++++++++++-- opentelemetry-api/tests/context/test_context.py | 8 ++++---- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/context/__init__.py b/opentelemetry-api/src/opentelemetry/context/__init__.py index 887d6930c5..da92d1f51b 100644 --- a/opentelemetry-api/src/opentelemetry/context/__init__.py +++ b/opentelemetry-api/src/opentelemetry/context/__init__.py @@ -51,8 +51,19 @@ def _load_runtime_context() -> typing.Optional[_RuntimeContext]: ) ).load()() except Exception: # pylint: disable=broad-except - logger.exception("Failed to load context: %s", configured_context) - return None + logger.exception( + "Failed to load context: %s, fallback to %s", + configured_context, + OTEL_PYTHON_CONTEXT, + ) + return next( # type: ignore + iter( # type: ignore + entry_points( # type: ignore + group="opentelemetry_context", + name=OTEL_PYTHON_CONTEXT, + ) + ) + ).load()() _RUNTIME_CONTEXT = _load_runtime_context() diff --git a/opentelemetry-api/tests/context/test_context.py b/opentelemetry-api/tests/context/test_context.py index 6291e8e50d..4a1e854e83 100644 --- a/opentelemetry-api/tests/context/test_context.py +++ b/opentelemetry-api/tests/context/test_context.py @@ -13,7 +13,7 @@ # limitations under the License. import unittest -from os import environ +from unittest.mock import patch from opentelemetry import context from opentelemetry.context.context import Context @@ -80,11 +80,11 @@ def test_set_current(self): class TestInitContext(unittest.TestCase): - def test_load_runtime_context(self): - environ[OTEL_PYTHON_CONTEXT] = "contextvars_context" + def test_load_runtime_context_default(self): ctx = context._load_runtime_context() # pylint: disable=W0212 self.assertIsInstance(ctx, ContextVarsRuntimeContext) - environ.pop(OTEL_PYTHON_CONTEXT) + @patch.dict("os.environ", {OTEL_PYTHON_CONTEXT: "contextvars_context"}) + def test_load_runtime_context(self): ctx = context._load_runtime_context() # pylint: disable=W0212 self.assertIsInstance(ctx, ContextVarsRuntimeContext)