Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Merged
merged 13 commits into from
Aug 15, 2019

Conversation

alban
Copy link
Contributor

@alban alban commented Aug 12, 2019

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)

@@ -65,6 +65,8 @@
import typing

from opentelemetry import loader
from opentelemetry import trace as trace_api
Copy link
Member

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'! 😄

Copy link
Contributor Author

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...

@carlosalberto
Copy link
Contributor

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')
Copy link
Member

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?

Copy link
Contributor Author

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
        )

Copy link
Member

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)

Copy link
Contributor

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

Copy link
Member

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.

Copy link
Member

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.

Copy link
Member

@reyang reyang left a 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.

@alban
Copy link
Contributor Author

alban commented Aug 13, 2019

Also, the tickets were originally created to track the API + the SDK changes, so that's remaining ;) (Can be done in future PRs)

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).
@carlosalberto
Copy link
Contributor

@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)

Copy link
Member

@c24t c24t left a 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.
Copy link
Member

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.

Copy link
Member

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.

Copy link
Contributor

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
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Adds a single Event with the name and, optionally, attributes passed
Adds a single `Event` with the name and, optionally, attributes passed

Copy link
Member

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.

Copy link
Contributor Author

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.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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.

Copy link
Contributor Author

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))

alban and others added 6 commits August 14, 2019 12:23
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
```
@alban
Copy link
Contributor Author

alban commented Aug 14, 2019

I updated the PR:

  • tests: use a dict in add_link and add_event instead of a string by mistake
  • make argument optional in add_link and add_event. I reused Span.empty_attributes in the implementation but didn't expose Link/Event in the API
  • API docstring: changes in spelling "span" following Chris' suggestion. But I didn't take Attribute and Link to avoid CI error
  • tests: we don't have getters but I added tests accessing the span members directly

name: str,
attributes: 'types.Attributes',
) -> None:
"""Adds an Event.
Copy link
Member

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
Copy link
Member

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
Copy link
Member

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.

Copy link
Contributor Author

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.

alban and others added 6 commits August 14, 2019 15:51
Symptoms:
```
opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py:249:4: W0221: Parameters differ from overridden 'add_link' method (arguments-differ)
```
@alban
Copy link
Contributor Author

alban commented Aug 14, 2019

Thanks for the additional review. I updated the PR again:

  • use your phrasing in the docstring
  • rename argument to link_target_context
  • avoid types in quotes when possible (types.Attributes)
  • update ASK to follow changes in API

@reyang reyang merged commit b7b38a5 into open-telemetry:master Aug 15, 2019
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.

7 participants