Skip to content

Commit

Permalink
Removing get_current_span from Tracer and TracerProvider
Browse files Browse the repository at this point in the history
After discussion in the SIG, we decided to remove the
legacy get_current_span APIs from Tracer and TracerProvider
to reduce long-term confusion of how to idiomatically retrieve
the span.
  • Loading branch information
toumorokoshi committed May 15, 2020
1 parent 088931d commit 95b3663
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 48 deletions.
24 changes: 4 additions & 20 deletions opentelemetry-api/src/opentelemetry/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,18 +272,6 @@ class Tracer(abc.ABC):
# This is the default behavior when creating spans.
CURRENT_SPAN = DefaultSpan(INVALID_SPAN_CONTEXT)

@abc.abstractmethod
def get_current_span(self) -> "Span": # noqa
"""Gets the currently active span from the context.
If there is no current span, return a placeholder span with an invalid
context.
Returns:
The currently active :class:`.Span`, or a placeholder span with an
invalid :class:`.SpanContext`.
"""

@abc.abstractmethod
def start_span(
self,
Expand Down Expand Up @@ -355,11 +343,11 @@ def start_as_current_span(
with tracer.start_as_current_span("one") as parent:
parent.add_event("parent's event")
with tracer.start_as_current_span("two") as child:
with trace.start_as_current_span("two") as child:
child.add_event("child's event")
tracer.get_current_span() # returns child
tracer.get_current_span() # returns parent
tracer.get_current_span() # returns previously active span
trace.get_current_span() # returns child
trace.get_current_span() # returns parent
trace.get_current_span() # returns previously active span
This is a convenience method for creating spans attached to the
tracer's context. Applications that need more control over the span
Expand Down Expand Up @@ -413,10 +401,6 @@ class DefaultTracer(Tracer):
All operations are no-op.
"""

def get_current_span(self) -> "Span": # noqa
# pylint: disable=no-self-use
return INVALID_SPAN

def start_span(
self,
name: str,
Expand Down
4 changes: 0 additions & 4 deletions opentelemetry-api/tests/trace/test_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ class TestTracer(unittest.TestCase):
def setUp(self):
self.tracer = trace.DefaultTracer()

def test_get_current_span(self):
span = self.tracer.get_current_span()
self.assertIsInstance(span, trace.Span)

def test_start_span(self):
with self.tracer.start_span("") as span:
self.assertIsInstance(span, trace.Span)
Expand Down
5 changes: 1 addition & 4 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,9 +593,6 @@ def __init__(
self.source = source
self.instrumentation_info = instrumentation_info

def get_current_span(self): # pylint: disable
return self.source.get_current_span()

def start_as_current_span(
self,
name: str,
Expand All @@ -618,7 +615,7 @@ def start_span( # pylint: disable=too-many-locals
set_status_on_exception: bool = True,
) -> trace_api.Span:
if parent is Tracer.CURRENT_SPAN:
parent = self.get_current_span()
parent = trace_api.get_current_span()

parent_context = parent
if isinstance(parent_context, trace_api.Span):
Expand Down
22 changes: 2 additions & 20 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,24 +211,6 @@ def test_span_processor_for_source(self):
span2.span_processor, tracer_provider._active_span_processor
)

def test_get_current_span_multiple_tracers(self):
"""In the case where there are multiple tracers,
get_current_span will return the same active span
for both tracers.
"""
tracer_1 = new_tracer()
tracer_2 = new_tracer()
root = tracer_1.start_span("root")
with tracer_1.use_span(root, True):
self.assertIs(tracer_1.get_current_span(), root)
self.assertIs(tracer_2.get_current_span(), root)
self.assertIs(trace_api.get_current_span(), root)

# outside of the loop, both should not reference a span.
self.assertIs(tracer_1.get_current_span(), None)
self.assertIs(tracer_2.get_current_span(), None)
self.assertIs(trace_api.get_current_span(), None)

def test_start_span_implicit(self):
tracer = new_tracer()

Expand Down Expand Up @@ -271,7 +253,7 @@ def test_start_span_implicit(self):

self.assertIsNotNone(child.end_time)

self.assertIsNone(tracer.get_current_span())
self.assertIsNone(trace_api.get_current_span())
self.assertIsNotNone(root.end_time)

def test_start_span_explicit(self):
Expand Down Expand Up @@ -599,7 +581,7 @@ def event_formatter():
self.assertEqual(root.events[4].timestamp, now)

def test_invalid_event_attributes(self):
self.assertIsNone(self.tracer.get_current_span())
self.assertIsNone(trace_api.get_current_span())

with self.tracer.start_as_current_span("root") as root:
root.add_event("event0", {"attr1": True, "attr2": ["hi", False]})
Expand Down

0 comments on commit 95b3663

Please sign in to comment.