Skip to content

Commit 83aad2f

Browse files
Oberon00reyangc24t
authored
Make use_span more flexible (closes #147). (#154)
Co-Authored-By: Reiley Yang <reyang@microsoft.com> Co-Authored-By: Chris Kleinknecht <libc@google.com>
1 parent 60fe160 commit 83aad2f

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

opentelemetry-api/src/opentelemetry/trace/__init__.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,8 @@ def start_span(
415415
is equivalent to::
416416
417417
span = tracer.create_span(name)
418-
with tracer.use_span(span):
418+
span.start()
419+
with tracer.use_span(span, end_on_exit=True):
419420
do_work()
420421
421422
Args:
@@ -472,17 +473,22 @@ def create_span(
472473
return INVALID_SPAN
473474

474475
@contextmanager # type: ignore
475-
def use_span(self, span: "Span") -> typing.Iterator[None]:
476+
def use_span(
477+
self, span: "Span", end_on_exit: bool = False
478+
) -> typing.Iterator[None]:
476479
"""Context manager for controlling a span's lifetime.
477480
478-
Start the given span and set it as the current span in this tracer's
479-
context.
481+
Set the given span as the current span in this tracer's context.
480482
481-
On exiting the context manager stop the span and set its parent as the
482-
current span.
483+
On exiting the context manager set the span that was previously active
484+
as the current span (this is usually but not necessarily the parent of
485+
the given span). If ``end_on_exit`` is ``True``, then the span is also
486+
ended when exiting the context manager.
483487
484488
Args:
485489
span: The span to start and make current.
490+
end_on_exit: Whether to end the span automatically when leaving the
491+
context manager.
486492
"""
487493
# pylint: disable=unused-argument,no-self-use
488494
yield

opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,10 @@ def start_span(
436436
kind: trace_api.SpanKind = trace_api.SpanKind.INTERNAL,
437437
) -> typing.Iterator["Span"]:
438438
"""See `opentelemetry.trace.Tracer.start_span`."""
439-
with self.use_span(self.create_span(name, parent, kind)) as span:
439+
440+
span = self.create_span(name, parent, kind)
441+
span.start()
442+
with self.use_span(span, end_on_exit=True):
440443
yield span
441444

442445
def create_span(
@@ -473,16 +476,20 @@ def create_span(
473476
)
474477

475478
@contextmanager
476-
def use_span(self, span: "Span") -> typing.Iterator["Span"]:
479+
def use_span(
480+
self, span: Span, end_on_exit: bool = False
481+
) -> typing.Iterator[Span]:
477482
"""See `opentelemetry.trace.Tracer.use_span`."""
478-
span.start()
479-
span_snapshot = self._current_span_slot.get()
480-
self._current_span_slot.set(span)
481483
try:
482-
yield span
484+
span_snapshot = self._current_span_slot.get()
485+
self._current_span_slot.set(span)
486+
try:
487+
yield span
488+
finally:
489+
self._current_span_slot.set(span_snapshot)
483490
finally:
484-
self._current_span_slot.set(span_snapshot)
485-
span.end()
491+
if end_on_exit:
492+
span.end()
486493

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

0 commit comments

Comments
 (0)