Skip to content

Commit

Permalink
[core] Guard against when there is no current call context
Browse files Browse the repository at this point in the history
Fixes #851
  • Loading branch information
brettlangdon committed Mar 19, 2019
1 parent 906b798 commit 166662f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
2 changes: 1 addition & 1 deletion ddtrace/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ def get_correlation_ids(tracer=None):
return None, None

span = tracer.current_span()
if span is None:
if not span:
return None, None
return span.trace_id, span.span_id
10 changes: 8 additions & 2 deletions ddtrace/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,20 @@ def current_root_span(self):
# set the host just once on the root span
root_span.set_tag('host', '127.0.0.1')
"""
return self.get_call_context().get_current_root_span()
ctx = self.get_call_context()
if ctx:
return ctx.get_current_root_span()
return None

def current_span(self):
"""
Return the active span for the current call context or ``None``
if no spans are available.
"""
return self.get_call_context().get_current_span()
ctx = self.get_call_context()
if ctx:
return ctx.get_current_span()
return None

def record(self, context):
"""
Expand Down
26 changes: 18 additions & 8 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from ddtrace import helpers
import mock

from nose.tools import eq_, ok_
from ddtrace import helpers

from .base import BaseTracerTestCase
from .util import override_global_tracer
Expand All @@ -16,16 +16,16 @@ def test_correlation_identifiers(self):
active_trace_id, active_span_id = span.trace_id, span.span_id
trace_id, span_id = helpers.get_correlation_ids()

eq_(trace_id, active_trace_id)
eq_(span_id, active_span_id)
self.assertEqual(trace_id, active_trace_id)
self.assertEqual(span_id, active_span_id)

def test_correlation_identifiers_without_trace(self):
# ensures `None` is returned if no Traces are active
with override_global_tracer(self.tracer):
trace_id, span_id = helpers.get_correlation_ids()

ok_(trace_id is None)
ok_(span_id is None)
self.assertIsNone(trace_id)
self.assertIsNone(span_id)

def test_correlation_identifiers_with_disabled_trace(self):
# ensures `None` is returned if tracer is disabled
Expand All @@ -34,5 +34,15 @@ def test_correlation_identifiers_with_disabled_trace(self):
self.tracer.trace('MockSpan')
trace_id, span_id = helpers.get_correlation_ids()

ok_(trace_id is None)
ok_(span_id is None)
self.assertIsNone(trace_id)
self.assertIsNone(span_id)

def test_correlation_identifiers_missing_context(self):
# ensures we return `None` if there is no current context
self.tracer.get_call_context = mock.MagicMock(return_value=None)

with override_global_tracer(self.tracer):
trace_id, span_id = helpers.get_correlation_ids()

self.assertIsNone(trace_id)
self.assertIsNone(span_id)
6 changes: 6 additions & 0 deletions tests/test_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,12 @@ def test_tracer_current_span(self):
span = self.trace('fake_span')
self.assertEqual(self.tracer.current_span(), span)

def test_tracer_current_span_missing_context(self):
self.assertIsNone(self.tracer.current_span())

def test_tracer_current_root_span_missing_context(self):
self.assertIsNone(self.tracer.current_root_span())

def test_default_provider_get(self):
# Tracer Context Provider must return a Context object
# even if empty
Expand Down

1 comment on commit 166662f

@ian-axelrod
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dude. ufast.

Please sign in to comment.