Skip to content

Commit 5dfce22

Browse files
authored
opentracer: implement active_span (#1395)
1 parent 769ba30 commit 5dfce22

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

ddtrace/opentracer/tracer.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,25 @@ def start_span(self, operation_name=None, child_of=None, references=None,
270270

271271
return otspan
272272

273+
@property
274+
def active_span(self):
275+
"""Retrieves the active span from the opentracing scope manager
276+
277+
Falls back to using the datadog active span if one is not found. This
278+
allows opentracing users to use datadog instrumentation.
279+
"""
280+
scope = self._scope_manager.active
281+
if scope:
282+
return scope.span
283+
else:
284+
dd_span = self._dd_tracer.current_span()
285+
if dd_span:
286+
ot_span = Span(self, None, dd_span.name)
287+
ot_span._associate_dd_span(dd_span)
288+
else:
289+
ot_span = None
290+
return ot_span
291+
273292
def inject(self, span_context, format, carrier): # noqa: A002
274293
"""Injects a span context into a carrier.
275294

tests/opentracer/test_tracer.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,11 @@ def test_start_active_span_trace(self, ot_tracer, writer):
416416
assert spans[2].parent_id is spans[0].span_id
417417
assert spans[3].parent_id is spans[2].span_id
418418

419+
def test_active_span(self, ot_tracer, writer):
420+
with ot_tracer._dd_tracer.trace("dd") as span:
421+
assert ot_tracer.active_span is not None
422+
assert ot_tracer.active_span._dd_span is span
423+
419424

420425
@pytest.fixture
421426
def nop_span_ctx():

0 commit comments

Comments
 (0)