Skip to content

Commit 219b977

Browse files
committed
Optimize _load_runtime_context
1 parent 8ed71b1 commit 219b977

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

opentelemetry-api/src/opentelemetry/context/__init__.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,32 +44,32 @@ def wrapper( # type: ignore[misc]
4444
**kwargs: typing.Dict[typing.Any, typing.Any],
4545
) -> typing.Optional[typing.Any]:
4646
global _RUNTIME_CONTEXT # pylint: disable=global-statement
47-
48-
with _RUNTIME_CONTEXT_LOCK:
49-
if _RUNTIME_CONTEXT is None:
50-
# FIXME use a better implementation of a configuration manager
51-
# to avoid having to get configuration values straight from
52-
# environment variables
53-
default_context = "contextvars_context"
54-
55-
configured_context = environ.get(
56-
OTEL_PYTHON_CONTEXT, default_context
57-
) # type: str
58-
try:
59-
60-
_RUNTIME_CONTEXT = next( # type: ignore
61-
iter( # type: ignore
62-
entry_points( # type: ignore
63-
group="opentelemetry_context",
64-
name=configured_context,
47+
if _RUNTIME_CONTEXT is None:
48+
with _RUNTIME_CONTEXT_LOCK:
49+
if _RUNTIME_CONTEXT is None:
50+
# FIXME use a better implementation of a configuration manager
51+
# to avoid having to get configuration values straight from
52+
# environment variables
53+
default_context = "contextvars_context"
54+
55+
configured_context = environ.get(
56+
OTEL_PYTHON_CONTEXT, default_context
57+
) # type: str
58+
try:
59+
60+
_RUNTIME_CONTEXT = next( # type: ignore
61+
iter( # type: ignore
62+
entry_points( # type: ignore
63+
group="opentelemetry_context",
64+
name=configured_context,
65+
)
6566
)
66-
)
67-
).load()()
67+
).load()()
6868

69-
except Exception: # pylint: disable=broad-except
70-
logger.exception(
71-
"Failed to load context: %s", configured_context
72-
)
69+
except Exception: # pylint: disable=broad-except
70+
logger.exception(
71+
"Failed to load context: %s", configured_context
72+
)
7373
return func(*args, **kwargs) # type: ignore[misc]
7474

7575
return typing.cast(_F, wrapper) # type: ignore[misc]

0 commit comments

Comments
 (0)