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

Allow to start_as_current_span with end_on_exit=False #1519

Merged
merged 5 commits into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#1285](https://github.com/open-telemetry/opentelemetry-python/pull/1285))
- Added `__repr__` for `DefaultSpan`, added `trace_flags` to `__repr__` of
`SpanContext` ([#1485](https://github.com/open-telemetry/opentelemetry-python/pull/1485)])
- Added `end_on_exit` argument to `start_as_current_span`
([#1519](https://github.com/open-telemetry/opentelemetry-python/pull/1519)])

### Changed
- `opentelemetry-exporter-zipkin` Updated zipkin exporter status code and error tag
Expand Down
2 changes: 2 additions & 0 deletions opentelemetry-api/src/opentelemetry/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ def start_as_current_span(
start_time: typing.Optional[int] = None,
record_exception: bool = True,
set_status_on_exception: bool = True,
end_on_exit: bool = True,
) -> typing.Iterator["Span"]:
"""Context manager for creating a new span and set it
as the current span in this tracer's context.
Expand Down Expand Up @@ -396,6 +397,7 @@ def start_as_current_span(
start_time: typing.Optional[int] = None,
record_exception: bool = True,
set_status_on_exception: bool = True,
end_on_exit: bool = True,
) -> typing.Iterator["Span"]:
# pylint: disable=unused-argument,no-self-use
yield INVALID_SPAN
Expand Down
3 changes: 2 additions & 1 deletion opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,7 @@ def start_as_current_span(
start_time: Optional[int] = None,
record_exception: bool = True,
set_status_on_exception: bool = True,
end_on_exit: bool = True,
) -> Iterator[trace_api.Span]:
span = self.start_span(
name=name,
Expand All @@ -768,7 +769,7 @@ def start_as_current_span(
record_exception=record_exception,
set_status_on_exception=set_status_on_exception,
)
with self.use_span(span, end_on_exit=True) as span_context:
with self.use_span(span, end_on_exit=end_on_exit) as span_context:
yield span_context

def start_span( # pylint: disable=too-many-locals
Expand Down
8 changes: 8 additions & 0 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,14 @@ def func():
self.assertIsNotNone(root2.end_time)
self.assertIsNot(root1, root2)

def test_start_as_current_span_no_end_on_exit(self):
tracer = new_tracer()

with tracer.start_as_current_span("root", end_on_exit=False) as root:
self.assertIsNone(root.end_time)

self.assertIsNone(root.end_time)

def test_explicit_span_resource(self):
resource = resources.Resource.create({})
tracer_provider = trace.TracerProvider(resource=resource)
Expand Down