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

Make use_span more flexible (closes #147). #154

Merged
merged 4 commits into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions opentelemetry-api/src/opentelemetry/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,8 @@ def start_span(
is equivalent to::

span = tracer.create_span(name)
with tracer.use_span(span):
span.start()
with tracer.use_span(span, end_on_exit=True):
do_work()

Args:
Expand Down Expand Up @@ -472,17 +473,21 @@ def create_span(
return INVALID_SPAN

@contextmanager # type: ignore
def use_span(self, span: "Span") -> typing.Iterator[None]:
def use_span(
self, span: "Span", end_on_exit: bool = False
) -> typing.Iterator[None]:
"""Context manager for controlling a span's lifetime.

Start the given span and set it as the current span in this tracer's
context.
Set the given span as the current span in this tracer's context.

On exiting the context manager stop the span and set its parent as the
current span.
On exiting the context manager set the span that was previously active
as the current span (this is usually but not necessarily the parent of
the given span). If ``end_on_exit`` is ``True``, then the span is also
ended when exiting the context manager.

Args:
span: The span to start and make current.
end_on_exit: Whether to end the span automatically when leaving the context manager.
Oberon00 marked this conversation as resolved.
Show resolved Hide resolved
"""
# pylint: disable=unused-argument,no-self-use
yield
Expand Down
23 changes: 15 additions & 8 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,10 @@ def start_span(
kind: trace_api.SpanKind = trace_api.SpanKind.INTERNAL,
) -> typing.Iterator["Span"]:
"""See `opentelemetry.trace.Tracer.start_span`."""
with self.use_span(self.create_span(name, parent, kind)) as span:

span = self.create_span(name, parent, kind)
Copy link
Member

Choose a reason for hiding this comment

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

This approach seems weird, not sure if other Python libraries have similar concept of using a ContextManager with an optional flag to avoid clean up (e.g. using open(filename, 'rb', close_on_exit=False) as f:).
Is it possible to use the following approach?

span = self.create_span(name, parent, kind)
span.start()
self.use_span(span)
do_something()
span.end()

Copy link
Member Author

Choose a reason for hiding this comment

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

Only example I could find was closefd in the built-in open function (https://docs.python.org/3/library/functions.html#open). But I know in .NET there are similar examples where you choose whether some TextReader should close the underlying file.

See also #97 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

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

Is it possible to use the following approach?

The problem with that is that after span.end(), the span is still active (which may be what you want if you have async children, but not in the general case).

span.start()
with self.use_span(span, end_on_exit=True):
yield span

def create_span(
Expand Down Expand Up @@ -460,16 +463,20 @@ def create_span(
)

@contextmanager
def use_span(self, span: "Span") -> typing.Iterator["Span"]:
def use_span(
self, span: Span, end_on_exit: bool = False
) -> typing.Iterator[Span]:
"""See `opentelemetry.trace.Tracer.use_span`."""
span.start()
span_snapshot = self._current_span_slot.get()
self._current_span_slot.set(span)
try:
yield span
span_snapshot = self._current_span_slot.get()
self._current_span_slot.set(span)
try:
yield span
finally:
self._current_span_slot.set(span_snapshot)
finally:
self._current_span_slot.set(span_snapshot)
span.end()
if end_on_exit:
span.end()

def add_span_processor(self, span_processor: SpanProcessor) -> None:
"""Registers a new :class:`SpanProcessor` for this `Tracer`.
Expand Down