-
Notifications
You must be signed in to change notification settings - Fork 711
Span: add attribute, event, link setter to the API #83
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
@@ -65,6 +65,8 @@ | |||
import typing | |||
|
|||
from opentelemetry import loader | |||
from opentelemetry import trace as trace_api |
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 import can be removed and the trace_api.SpanContext
below can be replaced with 'SpanContext'
! 😄
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! I'm fixing this...
It all looks good, in addition to what was commented by @a-feld mentioned Also, the tickets were originally created to track the API + the SDK changes, so that's remaining ;) (Can be done in future PRs) |
|
||
root.add_event('event1', 'event1-detail') | ||
|
||
root.add_link(other_neighbor, 'neighbor') |
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.
add_link
requires a SpanContext
, do we intend to revisit this later?
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 am not sure to follow: on this line, the other_neighbor
argument is a SpanContext
, created above:
other_neighbor = trace_api.SpanContext(
trace_id=0x0000000000000000000000000000cafe,
span_id=0x00000000cafe1234
)
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.
Sorry I thought it was a string. You're right.
I guess the correct invocation should be something like this? root.add_link(other_neighbor, {'name': 'neighbor'})
I also wonder if the API should treat attributes
as optional? root.add_link(my_span_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.
I also wonder if the API should treat attributes as optional?
Oh actually it should (same with add_event()
)
Related question: should we add Link
/Event
classes or keep with this minimalistic approach? cc @c24t
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 PR seems like a great place to add Link
/Event
.
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.
@reyang points out that we could use a singleton default empty attribute map here (like @carlosalberto's CURRENT_SPAN
to avoid creating a bunch of empty attributes.
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, I've put one question in the comments.
Should I remove the "Fixes #XXX" in the PR description then? |
Basic tests that exercise the API are added as well. However it does not actually test that the attributes, events and links are written properly because there is no getter. Those tests can be added later when the attributes are actually used. The spec mentions additional APIs for lazy initialization (AddLazyEvent, AddLazyLink). Those are not added in this patch because OT-Python does not expose the Event type (currently defined in the SDK only).
@alban Yes, please ;) A simple mention to them should be fine (we can close later on the issues when the actual SDK portions are done) |
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.
Minor comments, but this looks great. Thanks!
name: str, | ||
attributes: 'types.Attributes', | ||
) -> None: | ||
"""Adds an Event. |
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 open question is whether the caller ever needs access to the events or links they create (in which case we need the classes in the API) or if we should treat them as part of the implementation of these methods.
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 it's best to start out with the API that exposes as few details as possible.
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.
There are two places where I've seen Event
/Link
and related members being publicly used/exposed : 1) SpanData
(but as that is probable going to the SDK at the very least) and 2) MessageEvent
(which extends Event
, at least in Java: https://github.com/open-telemetry/opentelemetry-java/blob/master/contrib/trace_utils/src/main/java/io/opentelemetry/contrib/trace/MessageEvent.java).
Makes me feel we should include these Event
, etc classes here.
) -> None: | ||
"""Adds an Event. | ||
|
||
Adds a single Event with the name and, optionally, attributes passed |
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.
Adds a single Event with the name and, optionally, attributes passed | |
Adds a single `Event` with the name and, optionally, attributes passed |
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.
Do we have an explicit Event
class? If not, then that suggestion would break the doc build.
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.
At the moment, we don't. We have the following
Event = namedtuple('Event', ('name', 'attributes'))
But that's in the SDK, not exposed in the API.
) -> None: | ||
"""Adds a Link to another Span. | ||
|
||
Adds a single Link to the Span via the SpanContext passed as argument. |
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.
Adds a single Link to the Span via the SpanContext passed as argument. | |
Adds a single `Link` to the `Span` via the `SpanContext` passed as argument. |
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 Span" should be "the span" (following your rationale #83 (comment))
Co-Authored-By: Chris Kleinknecht <[email protected]>
Co-Authored-By: Chris Kleinknecht <[email protected]>
Even though the public API does not expose getters, we can access the span members directly in Python.
Symptoms: ``` /home/travis/build/kinvolk/opentelemetry-python/opentelemetry-api/src/opentelemetry/trace/__init__.py:docstring of opentelemetry.trace.Span.set_attribute:3: WARNING: 'any' reference target not found: Attribute 298/home/travis/build/kinvolk/opentelemetry-python/opentelemetry-api/src/opentelemetry/trace/__init__.py:docstring of opentelemetry.trace.Span.add_link:3: WARNING: 'any' reference target not found: Link 299 ```
I updated the PR:
|
name: str, | ||
attributes: 'types.Attributes', | ||
) -> None: | ||
"""Adds an Event. |
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 it's best to start out with the API that exposes as few details as possible.
) -> None: | ||
"""Adds an Event. | ||
|
||
Adds a single Event with the name and, optionally, attributes passed |
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.
Do we have an explicit Event
class? If not, then that suggestion would break the doc build.
) -> None: | ||
if self.events is Span.empty_events: | ||
self.events = BoundedList(MAX_NUM_EVENTS) | ||
if attributes is None: | ||
attributes = Span.empty_attributes |
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.
It somewhat irks me that we can't use Span.empty_attributes
as default and leave out this check, because the API already specifies a default argument of None
. But I know of no better way to write this than the one you used here.
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 initially tried to use {}
as default value but the CI complained with:
Dangerous default value {} as argument
and I read recommendations to use None
in that case.
Co-Authored-By: Christian Neumüller <[email protected]>
Co-Authored-By: Christian Neumüller <[email protected]>
Co-Authored-By: Christian Neumüller <[email protected]>
Co-Authored-By: Christian Neumüller <[email protected]>
Symptoms: ``` opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py:249:4: W0221: Parameters differ from overridden 'add_link' method (arguments-differ) ```
Thanks for the additional review. I updated the PR again:
|
Basic tests that exercise the API are added as well. However it does not
actually test that the attributes, events and links are written properly
because there is no getter. Those tests can be added later when the
attributes are actually used.
The spec mentions additional APIs for lazy initialization (AddLazyEvent,
AddLazyLink). Those are not added in this patch because OT-Python does
not expose the Event type (currently defined in the SDK only).
#75 (attribute)
#76 (event)
#77 (link)