-
Notifications
You must be signed in to change notification settings - Fork 652
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
Initial support for explicit Span parent support. #33
Initial support for explicit Span parent support. #33
Conversation
"""Constant used to represent the current span being used as a parent. | ||
This is the default behavior when creating spans. | ||
""" | ||
CURRENT_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.
Consider _CURRENT_SPAN
if this is meant to be used inside this file.
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 think exposing this constant is fine, but maybe the name may be a bit misleading in a broader context. Maybe name it CURRENT_SPAN_AS_PARENT
?
Actually we need to expose it, as API implementations will need to compare against it.
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.
One more point: This will not typecheck (CURRENT_SPAN does not have type Span
). We should use typing.NewType
to create a dummy type for this object and then accept that type in the functions that support CURRENT_SPAN (in a typing.Union
).
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.
Actually we need to expose it, as API implementations will need to compare against it.
Correct. My initial thought had been to hide it but we have to expose it ;)
And yes, we can use typing.NewType
for this. But I'm actually thinking we could simply try to use the Java approach here, by having a specific default/no-op Span
that will effectively act as such dummy. Will update the 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.
Using a sentinel for the span kwarg is a great solution, and does make for a nice API.
It took me a few passes to understand that CURRENT_SPAN
wasn't meant to be the actual span, if other people find this confusing we might want to call it something else.
@@ -73,6 +73,11 @@ class Tracer: | |||
and controlling spans' lifecycles. | |||
""" | |||
|
|||
"""Constant used to represent the current span being used as a parent. | |||
This is the default behavior when creating spans. | |||
""" |
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.
""" | |
# Constant used to represent the current span being used as a parent. This | |
# is the default behavior when creating spans. | |
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.
The Sphinx syntax for documentation comments uses #:
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.
Aha! Got it.
@@ -73,6 +73,11 @@ class Tracer: | |||
and controlling spans' lifecycles. | |||
""" | |||
|
|||
"""Constant used to represent the current span being used as a parent. | |||
This is the default behavior when creating spans. | |||
""" |
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.
The Sphinx syntax for documentation comments uses #:
"""Constant used to represent the current span being used as a parent. | ||
This is the default behavior when creating spans. | ||
""" | ||
CURRENT_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.
I think exposing this constant is fine, but maybe the name may be a bit misleading in a broader context. Maybe name it CURRENT_SPAN_AS_PARENT
?
Actually we need to expose it, as API implementations will need to compare against it.
def start_span(self, name: str, parent: 'Span') -> Iterator['Span']: | ||
def start_span(self, | ||
name: str, | ||
parent: 'Span' = CURRENT_SPAN) -> Iterator['Span']: |
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.
Nit: Normally you don't put spaces around the =
in a default argument, but I don't know how the situation is with type annotations.
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.
Same, was not sure about how things work with annotations (had barely used them before ;) )
Hope we can get a linter enabled soon :)
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 is right! And pylint actually does report on this if you remove the spaces:
opentelemetry/trace/__init__.py:95:33: C0326: Exactly one space required around keyword argument assignment
parent: 'Span'=CURRENT_SPAN) -> Iterator['Span']:
^ (bad-whitespace)
created span will be a root span. | ||
|
||
Applications that need to create spans detached from the tracer's | ||
context should use this method. | ||
|
||
with tracer.start_span(name) as span: |
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 think this sample code is a copy & paste error. It should probably not be here (although it made more sense when the paragraph talking about use_span
was directly before it).
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.
Was wondering the same.
"""Constant used to represent the current span being used as a parent. | ||
This is the default behavior when creating spans. | ||
""" | ||
CURRENT_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.
One more point: This will not typecheck (CURRENT_SPAN does not have type Span
). We should use typing.NewType
to create a dummy type for this object and then accept that type in the functions that support CURRENT_SPAN (in a typing.Union
).
Agreed. |
Hey hey - I've updated the PR, but the part regarding My initial idea was to move |
I vote for keeping them in the same module and moving Some other benefits:
We had the opposite experience in OC. One of the original sins of OC-python was that some features were transliterated straight from java, including a bunch of classes that (as in java) each got their own file. As the implementations drifted apart to better suit each language, the fact that the two projects had similar structure became more confusing than helpful since assumptions from one project didn't always carry over to the other. I may be biased in seeing single-class modules as "java-like" and multiple-class modules as pythonic, but in my experience modules -- like namespaces -- are one of the nice features of the language, and make for a more easily comprehensible project. |
Lets use this one for now then. If/when we feel we need a different way, we can go for it.
Interesting point. In any case, lets then keep what we have now ;) |
Co-Authored-By: Chris Kleinknecht <libc@google.com>
Co-Authored-By: Chris Kleinknecht <libc@google.com>
Specifically for CURRENT_SPAN usage.
Hey, I've updated the code so now Two items remain, and they should do as separated PRs in my opinion:
Let me know. If all is fine, I will go ahead and merge 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.
LGTM 👍
Addresses #23
It uses a new public constant
CURRENT_SPAN
to signal that the default parent is the currentSpan
.I applied the same logic to
create_span
, to have uniformity. OC Java, for example, allows users to start aSpan
as either the current instance or not, and both implicitly used the previous currentSpan
as parent. We also had this in OT Python and it worked fine.Opinions on this last change?