Skip to content

Commit

Permalink
Recreate span on every run of a decorated function
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-ryzhov committed Dec 19, 2020
1 parent 753ec6c commit c855c14
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ 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)])
### Changed
- Recreate span on every run of a `start_as_current_span`-decorated function
([#1451](https://github.com/open-telemetry/opentelemetry-python/pull/1451))

## [0.16b1](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v0.16b1) - 2020-11-26
### Added
Expand Down
4 changes: 3 additions & 1 deletion opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,7 @@ def __init__(
self.ids_generator = ids_generator
self.instrumentation_info = instrumentation_info

@contextmanager
def start_as_current_span(
self,
name: str,
Expand All @@ -763,7 +764,8 @@ def start_as_current_span(
record_exception=record_exception,
set_status_on_exception=set_status_on_exception,
)
return self.use_span(span, end_on_exit=True)
with self.use_span(span, end_on_exit=True) as span_context:
yield span_context

def start_span( # pylint: disable=too-many-locals
self,
Expand Down
31 changes: 31 additions & 0 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,37 @@ def test_start_as_current_span_explicit(self):
self.assertIs(trace_api.get_current_span(), root)
self.assertIsNotNone(child.end_time)

def test_start_as_current_span_decorator(self):
tracer = new_tracer()

self.assertEqual(trace_api.get_current_span(), trace_api.INVALID_SPAN)

@tracer.start_as_current_span("root")
def func():
root = trace_api.get_current_span()

with tracer.start_as_current_span("child") as child:
self.assertIs(trace_api.get_current_span(), child)
self.assertIs(child.parent, root.get_span_context())

# After exiting the child's scope the parent should become the
# current span again.
self.assertIs(trace_api.get_current_span(), root)
self.assertIsNotNone(child.end_time)

return root

root1 = func()

self.assertEqual(trace_api.get_current_span(), trace_api.INVALID_SPAN)
self.assertIsNotNone(root1.end_time)

# Second call must create a new span
root2 = func()
self.assertEqual(trace_api.get_current_span(), trace_api.INVALID_SPAN)
self.assertIsNotNone(root2.end_time)
self.assertIsNot(root1, root2)

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

0 comments on commit c855c14

Please sign in to comment.