Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Tracer's active_span shorthand. #69

Merged
merged 1 commit into from
Mar 26, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions opentracing/harness/api_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ def is_parent(self, parent, span):
"""
return False

def test_active_span(self):
tracer = self.tracer()
span = tracer.start_span('Fry')

if self.check_scope_manager():
assert tracer.active_span is None
with tracer.scope_manager.activate(span, True):
assert tracer.active_span is span

def test_start_active_span(self):
# the first usage returns a `Scope` that wraps a root `Span`
tracer = self.tracer()
Expand Down
11 changes: 11 additions & 0 deletions opentracing/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ def scope_manager(self):
"""ScopeManager accessor"""
return self._scope_manager

@property
def active_span(self):
"""Provides access to the the active Span. This is a shorthand for
Tracer.scope_manager.active.span, and None will be returned if
Scope.span is None.

:return: returns the active Span.
"""
scope = self._scope_manager.active
return None if scope is None else scope.span

def start_active_span(self,
operation_name,
finish_on_close,
Expand Down
5 changes: 5 additions & 0 deletions tests/test_noop_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,8 @@ def test_tracer():
child = tracer.start_span(operation_name='child',
references=child_of(span))
assert span == child


def test_tracer_active_span():
tracer = Tracer()
assert tracer.active_span is tracer.scope_manager.active.span