-
Notifications
You must be signed in to change notification settings - Fork 657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove thread lock by loading RuntimeContext explicitly. #3763
Conversation
thanks @WqyJh is this PR fixing an existing issue? If not please open one and mark this PR as fixing it ✌️ |
Hi @WqyJh, thanks for the change -- I'm very much in favor of this idea. Would you be able to add test coverage? |
Sure, I'd like to. However, I didn't see the coverage report, therefore not sure where the uncovered parts are. AFAIK, all functions related to this modification including |
That's good. What do you think about adding at least one test for |
Added an unitest However, I found that It's clear that this problem is not introduced by this PR because I didn't modify these code lines. If you also think it's an abnormal behavior, I think we should merge this PR first and open a new issue for this problem. default_context = "contextvars_context"
configured_context = environ.get(
OTEL_PYTHON_CONTEXT, default_context
) # type: str
try:
return next( # type: ignore
iter( # type: ignore
entry_points( # type: ignore
group="opentelemetry_context",
name=configured_context,
)
)
).load()()
except Exception: # pylint: disable=broad-except
logger.exception(
"Failed to load context: %s", configured_context
) |
Hi @pmcollins, this PR needs review. Would you please help review it and let's finish this PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes LGTM. Thanks for doing this.
Should this be mentioned in the changelog?
Yes, I think it should. |
Please feel free to add the changelog entry, then that check will pass. |
All works seems done, can it be merged now? |
69370ea
to
cb919f6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
@WqyJh can you fix the mypy failure with the following diff? diff --git a/opentelemetry-api/tests/context/test_context.py b/opentelemetry-api/tests/context/test_context.py
index af5b38ef8..18f6f68a5 100644
--- a/opentelemetry-api/tests/context/test_context.py
+++ b/opentelemetry-api/tests/context/test_context.py
@@ -85,11 +85,11 @@ class TestInitContext(unittest.TestCase):
self.assertIsInstance(ctx, ContextVarsRuntimeContext)
@patch.dict("os.environ", {OTEL_PYTHON_CONTEXT: "contextvars_context"})
- def test_load_runtime_context(self):
+ def test_load_runtime_context(self): # type: ignore[misc]
ctx = context._load_runtime_context() # pylint: disable=W0212
self.assertIsInstance(ctx, ContextVarsRuntimeContext)
@patch.dict("os.environ", {OTEL_PYTHON_CONTEXT: "foo"})
- def test_load_runtime_context_fallback(self):
+ def test_load_runtime_context_fallback(self): # type: ignore[misc]
ctx = context._load_runtime_context() # pylint: disable=W0212
self.assertIsInstance(ctx, ContextVarsRuntimeContext) |
Fixes #3663, #3381
The old
_load_runtime_context
is an function wrapper, which acquires lock and check value of_RUNTIME_CONTEXT
every time the wrapped function (get_current
/attach
/deatch
) called. Since it should be initialized after the first_load_runtime_context
call, there's no need to check repeatedly.The solution is, initialize it on module import, which should be exactly once and thread safe, and use it directly without redundant check afterwards.