Skip to content

Commit b32365b

Browse files
authored
Allow to start_as_current_span with end_on_exit=False (open-telemetry#1519)
1 parent 4083cac commit b32365b

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v0.17b0...HEAD)
88

9+
### Added
10+
- Added `end_on_exit` argument to `start_as_current_span`
11+
([#1519](https://github.com/open-telemetry/opentelemetry-python/pull/1519)])
12+
913
## [0.17b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v0.17b0) - 2021-01-20
1014

1115
### Added

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ def start_as_current_span(
290290
start_time: typing.Optional[int] = None,
291291
record_exception: bool = True,
292292
set_status_on_exception: bool = True,
293+
end_on_exit: bool = True,
293294
) -> typing.Iterator["Span"]:
294295
"""Context manager for creating a new span and set it
295296
as the current span in this tracer's context.
@@ -396,6 +397,7 @@ def start_as_current_span(
396397
start_time: typing.Optional[int] = None,
397398
record_exception: bool = True,
398399
set_status_on_exception: bool = True,
400+
end_on_exit: bool = True,
399401
) -> typing.Iterator["Span"]:
400402
# pylint: disable=unused-argument,no-self-use
401403
yield INVALID_SPAN

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,7 @@ def start_as_current_span(
759759
start_time: Optional[int] = None,
760760
record_exception: bool = True,
761761
set_status_on_exception: bool = True,
762+
end_on_exit: bool = True,
762763
) -> Iterator[trace_api.Span]:
763764
span = self.start_span(
764765
name=name,
@@ -770,7 +771,7 @@ def start_as_current_span(
770771
record_exception=record_exception,
771772
set_status_on_exception=set_status_on_exception,
772773
)
773-
with self.use_span(span, end_on_exit=True) as span_context:
774+
with self.use_span(span, end_on_exit=end_on_exit) as span_context:
774775
yield span_context
775776

776777
def start_span( # pylint: disable=too-many-locals

opentelemetry-sdk/tests/trace/test_trace.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,14 @@ def func():
485485
self.assertIsNotNone(root2.end_time)
486486
self.assertIsNot(root1, root2)
487487

488+
def test_start_as_current_span_no_end_on_exit(self):
489+
tracer = new_tracer()
490+
491+
with tracer.start_as_current_span("root", end_on_exit=False) as root:
492+
self.assertIsNone(root.end_time)
493+
494+
self.assertIsNone(root.end_time)
495+
488496
def test_explicit_span_resource(self):
489497
resource = resources.Resource.create({})
490498
tracer_provider = trace.TracerProvider(resource=resource)

0 commit comments

Comments
 (0)