Skip to content
This repository was archived by the owner on Aug 13, 2025. It is now read-only.

Commit 35acade

Browse files
authored
Merge pull request #45 from honeycombio/tredman.hotfix
Fix crash in contextmanager if span is None
2 parents e2c760e + 8c7dbfb commit 35acade

File tree

4 files changed

+36
-18
lines changed

4 files changed

+36
-18
lines changed

beeline/test_trace.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,18 @@ def test_run_hooks_and_send_adds_trace_fields(self):
332332
# ensure we only added fields b and c and did not try to overwrite a
333333
self.assertDictContainsSubset({'app.a': 1, 'app.b': 2, 'app.c': 3}, m_span.event.fields())
334334

335+
def test_trace_context_manager_does_not_crash_if_span_is_none(self):
336+
m_client = Mock()
337+
tracer = SynchronousTracer(m_client)
338+
tracer.start_span = Mock()
339+
tracer.start_span.return_value = None
340+
tracer.finish_span = Mock()
341+
342+
with tracer('foo'):
343+
pass
344+
345+
tracer.start_span.assert_called_once_with(context={'name': 'foo'}, parent_id=None)
346+
335347
class TestTraceContext(unittest.TestCase):
336348
def test_marshal_trace_context(self):
337349
trace_id = "123456"

beeline/trace.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,34 @@ def __call__(self, name, trace_id=None, parent_id=None):
4343
span = None
4444
if self.get_active_trace_id():
4545
span = self.start_span(context={'name': name})
46-
log('tracer context manager started new span, id = %s',
47-
span.id)
46+
if span:
47+
log('tracer context manager started new span, id = %s',
48+
span.id)
4849
else:
4950
span = self.start_trace(context={'name': name})
50-
log('tracer context manager started new trace, id = %s',
51-
span.trace_id)
51+
if span:
52+
log('tracer context manager started new trace, id = %s',
53+
span.trace_id)
5254
yield
5355
except Exception as e:
54-
span.add_context({
55-
"app.exception_type": str(type(e)),
56-
"app.exception_string": str(e),
57-
})
56+
if span:
57+
span.add_context({
58+
"app.exception_type": str(type(e)),
59+
"app.exception_string": str(e),
60+
})
5861
raise
5962
finally:
60-
if span.is_root():
61-
log('tracer context manager ending trace, id = %s',
62-
span.trace_id)
63-
self.finish_trace(span)
63+
if span:
64+
if span.is_root():
65+
log('tracer context manager ending trace, id = %s',
66+
span.trace_id)
67+
self.finish_trace(span)
68+
else:
69+
log('tracer context manager ending span, id = %s',
70+
span.id)
71+
self.finish_span(span)
6472
else:
65-
log('tracer context manager ending span, id = %s',
66-
span.id)
67-
self.finish_span(span)
73+
log('tracer context manager span for %s was unexpectedly None', name)
6874

6975
@init_state
7076
def start_trace(self, context=None, trace_id=None, parent_span_id=None):

beeline/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION = '2.4.0'
1+
VERSION = '2.4.1'

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
setup(
55
python_requires='>=2.7',
66
name='honeycomb-beeline',
7-
version='2.4.0',
7+
version='2.4.1',
88
description='Honeycomb library for easy instrumentation',
99
url='https://github.com/honeycombio/beeline-python',
1010
author='Honeycomb.io',
@@ -18,7 +18,7 @@
1818
tests_require=[
1919
'mock',
2020
'flask',
21-
'tornado',
21+
'tornado==5',
2222
'django<2; python_version == "2.7"',
2323
'django>=2; python_version >= "3.0"',
2424
],

0 commit comments

Comments
 (0)