-
Notifications
You must be signed in to change notification settings - Fork 699
Initial implementation of context #57
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
Conversation
Some questions about the context. These might have easy answers, I just don't know them: Do we need a context API instead of using Do we expect implementations to override |
IMHO the point of adding type annotations to API code is to make implementers' lives easy and give them the option to do type checking. The annotations here don't do much to describe the behavior of the class, add a lot of boilerplate code, and (at least in my experience) add significant friction while writing the code. |
slot = self._slots[name] | ||
slot.set(value) | ||
|
||
def with_current_context( |
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.
would there be value in exposing a contextmanager like this as well?
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.
I don't know at this moment. I'm open to add it if we find it helpful.
Yes for now, although our direction is to use contextvars in the future.
There will be cases like this, for example https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-gevent. |
|
||
# user can propagate context explicitly | ||
thread = Thread( | ||
target=Context.with_current_context(work), |
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.
this helps clear up the value of with_current_context. Nice!
I feel like the need to add with_current_context will probably be a gotcha in many cases. It's too bad there isn't a way to make this something that is implicitly shared.
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.
When the day comes that contextvars or some built-in Python lib support such scenario, we can say goodbye to this Context
class and never need to reinvent wheels again :)
|
||
from opentelemetry.context import Context | ||
|
||
class Span(object): |
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.
@c24t this should unblock your work on the actual span creation and tracer context update.
self.parent, | ||
)) | ||
|
||
async def __aenter__(self): |
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.
This example only works on Python3.7+ due to the new async/await
syntax.
We can explore more in #62.
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.
LGTM!
def call_with_current_context( | ||
*args: 'object', | ||
**kwargs: 'object', | ||
) -> 'object': |
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.
Since we don't expect users to bring their own context impl (as we do with trace and metrics) I think it's fine to lose the type annotations in this module, especially when they're this useless.
|
||
if __name__ == '__main__': | ||
asyncio.run(main()) | ||
""" |
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.
How about adding this module to the sphinx docs?
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.
Sure, I'll send a follow up PR to add test cases and docs.
* Add TraceState * Fix review comments * Minor Fix, add TODOs * Use Map to preserve ordering * Add underscore for private methods & rename variable - Give more descriptive name than `s` to variable. - Private properties prefixed with an underscore for JavaScript users. - Private methods prefixed with an underscore for JavaScript users. * Remove TEST_ONLY methods * Add comment about TraceState class * Add unset method
This is based on the work in
opencensus-context
.