Skip to content
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

@traced_function now works with AsyncIO's coroutines. #84

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

@traced_function now works with AsyncIO's coroutines. #84

wants to merge 2 commits into from

Conversation

nicholasamorim
Copy link
Contributor

When using Tornado 5 it's common to start migrating to async/await syntaxes.

However, @traced_function only understands Tornado's Futures but not necessarily AsyncIO coroutines.

This PR adds supports to it without breaking compatibility with older versions of Python.

@coveralls
Copy link

coveralls commented May 23, 2019

Coverage Status

Coverage increased (+2.3%) to 79.286% when pulling 6bbcf2c on nicholasamorim:traced_function_asyncio into 4d560d5 on uber-common:master.

is_tornado = tornado.concurrent.is_future(res)
is_asyncio = is_asyncio_coroutine(res)
if is_asyncio:
task = asyncio.create_task(res)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about span propagation? I'm afraid that it doesn't work properly here, because we add task to asyncio loop and next time asyncio may execute the task in another scope. See https://github.com/opentracing/opentracing-python/blob/master/opentracing/scope_managers/asyncio.py#L37

Copy link
Contributor Author

@nicholasamorim nicholasamorim May 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, wow, I hadn't seen that. I'm still new to all of this, so.. makes sense.

How can that be fixed? I'm guessing this should be fixed at the ScopeManager?

What happens on Tornado 6 (no more StackContext) - just use the AsyncIO scope manager?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion the best way to solve the problem is make auto span propagation in asyncio code (including scheduling tasks) with scope manager. It can work something like this PR opentracing/opentracing-python#118
Current code of traced_function isn't ready for it now as I can see.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @nicholasamorim and @condorcet,
Any updates here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these changes definitely should be proved, so we have to add some tests first.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jamim @condorcet

I might be way over my head in this but these changes seems to work fine with the changes @condorcet merged on opentracing/opentracing-python#118

I've been using it with the ContextVarsScopeManager and propagation seems to be working fine

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicholasamorim Behavior of the decorator is different for AsyncioScopeManager and ContextVarsScopeManager and it's not obvious now. Should we aware users about it?
Also we need some unit tests :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Behavior of the decorator is different for AsyncioScopeManager and ContextVarsScopeManager and it's not obvious now.

How different would be? I don't understand the internals enough to see that. Looking at the decorator I'd expect it to be scopemanager-agnostic.

Can we "fix" what's different?

I can add some unit tests, sure, just trying to understand it all before.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for delay!
The problem with AsyncioScopeManager still the same: it doesn't support auto context propagation, so if you setup this scope manager and use the decorator then wrapped coroutine won't take proper parent context. We have to provide and activate parent span explicitly as mentioned in docs https://github.com/opentracing/opentracing-python/blob/master/opentracing/scope_managers/asyncio.py#L37
I believe it's possible to write a magic wrapper that do something for us. But in my opinion it makes code more complex (at least for debugging) and the best way just to mention that in asyncio application traced_function should be used with ContextVarsScopeManager.
Ask me If you need additional explanation!

Also notice this part of code changed in master.

@yurishkuro yurishkuro mentioned this pull request Jun 14, 2019
opentracing_instrumentation/local_span.py Outdated Show resolved Hide resolved
opentracing_instrumentation/local_span.py Outdated Show resolved Hide resolved
@nicholasamorim
Copy link
Contributor Author

Thanks for you suggestions @Jamim.

I will look into this PR that was suggested by @condorcet opentracing/opentracing-python#118 and see how I can improve this. Accepting any pointers, though.

@CLAassistant
Copy link

CLAassistant commented Oct 4, 2020

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


Nicholas Amorim seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants