fix(threading): attribute error when run is called w/o start#4246
fix(threading): attribute error when run is called w/o start#4246xrmx merged 5 commits intoopen-telemetry:mainfrom
Conversation
|
It might be better to just move the context initialization into |
@herin049 I don't think that's the intended behaviour. It's common to initialize threads in one place in code and start them elsewhere. The intent is for the spawned thread to inherit the context of the place where it is Consider this pseudocode example worker_threads = []
for i in range(10):
t = threading.Thread(target=lambda: notify_downstream())
worker_threads.append(t)
# further initialization
# ...
for t in worker_threads:
# we want to capture this context, not the one
# above at initialization
with tracer.start_as_current_span(name=f"process_{i}"):
t.start() # or t.run()Happy to discuss further, of course. |
Yep, you're right here, not sure what I was thinking with my original suggestion. That being said, one potential issue that we might encounter with the current fix is if with tracer.start_as_current_span(name="run_1"):
t.run()
with tracer.start_as_current_span(name="run_2"):
t.run()With the current approach, the second call to token = None
try:
if hasattr(instance, "_otel_context"):
token = context.attach(instance._otel_context)
return call_wrapped(*args, **kwargs)
finally:
if token is not None:
context.detach(token)Furthermore, I also don't think it really makes sense to initialize |
I think it still make sense to attach in |
...ntelemetry-instrumentation-threading/src/opentelemetry/instrumentation/threading/__init__.py
Outdated
Show resolved
Hide resolved
instrumentation/opentelemetry-instrumentation-threading/tests/test_threading.py
Outdated
Show resolved
Hide resolved
|
LGTM! |
|
@dinmukhamedm Please fix the conflicts, thanks |
5c5fce8 to
390407b
Compare
done |
|
@dinmukhamedm please pull main. Opening PRs from company repositories where we cannot push on our own is painful since for merging we require the branch to include latest main. |
390407b to
0c91f20
Compare
|
@xrmx updated and invited you as an outside collaborator to the fork, just in case |
Description
Fixes an issue where a thread run without being started would result in an attribute error.
Options considered:
run, notstartThread.runis designed to be overriden by custom child classes ofThread, in which case instrumentation may not workrunif not captured atstartThread.runis designed to be overriden by custom runners, in which case instrumentation may not workI am still open to any of the above options, and would appreciate review / further discussion.
Fixes #4245
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration
Does This PR Require a Core Repo Change?
Checklist:
See contributing.md for styleguide, changelog guidelines, and more.