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

Commit c5c0f6d

Browse files
authored
Merge pull request #47 from honeycombio/tredman.context-manager
2.4.2 - fixes issue in tracing context manager and some minor issues
2 parents 35acade + 7cdb5b8 commit c5c0f6d

File tree

7 files changed

+40
-41
lines changed

7 files changed

+40
-41
lines changed

beeline/__init__.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,7 @@ def _run_hooks_and_send(self, ev):
189189
self.log("executing presend hook on event ev = %s", ev.fields())
190190
self.presend_hook(ev.fields())
191191

192-
if hasattr(ev, 'traced_event'):
193-
self.log("enqueuing traced event ev = %s", ev.fields())
194-
self.tracer_impl.send_traced_event(ev, presampled=presampled)
195-
elif presampled:
192+
if presampled:
196193
self.log("enqueuing presampled event ev = %s", ev.fields())
197194
ev.send_presampled()
198195
else:

beeline/internal.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22

33
# these are mostly convenience methods for safely calling beeline methods
44
# even if the beeline hasn't been initialized
5-
def new_event(data=None, trace_name='', top_level=False):
6-
bl = beeline.get_beeline()
7-
if bl:
8-
bl.log("creating new event with data = %s", data)
9-
return bl.new_event(data, trace_name, top_level)
10-
115
def send_event():
126
bl = beeline.get_beeline()
137
if bl:

beeline/test_beeline.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,13 @@ def test_send_all(self):
5151
def test_run_hooks_and_send_no_hooks(self):
5252
''' ensure send works when no hooks defined '''
5353
ev = Mock()
54-
delattr(ev, 'traced_event')
5554
_beeline = beeline.Beeline()
5655
_beeline.tracer_impl = Mock()
5756
_beeline._run_hooks_and_send(ev)
5857

5958
# no hooks, not a traced event - call send
6059
ev.send.assert_called_once_with()
6160
ev.send_presampled.assert_not_called()
62-
_beeline.tracer_impl.send_traced_event.assert_not_called()
63-
64-
# traced_event is an implicit attribute on Mock, so send_traced_event
65-
# should be called
66-
traced_ev = Mock()
67-
_beeline._run_hooks_and_send(traced_ev)
68-
_beeline.tracer_impl.send_traced_event.assert_called_once_with(traced_ev, presampled=False)
6961

7062
def test_run_hooks_and_send_sampler(self):
7163
''' ensure send works with a sampler hook defined '''
@@ -76,8 +68,6 @@ def _sampler_drop_all(fields):
7668
_beeline = beeline.Beeline(sampler_hook=m_sampler_hook)
7769
_beeline.tracer_impl = Mock()
7870
ev = Mock()
79-
# non-traced event
80-
delattr(ev, 'traced_event')
8171

8272
_beeline._run_hooks_and_send(ev)
8373
m_sampler_hook.assert_called_once_with(ev.fields())
@@ -88,8 +78,6 @@ def _sampler_drop_none(fields):
8878
return True, 100
8979

9080
ev = Mock()
91-
# non-traced event
92-
delattr(ev, 'traced_event')
9381
m_sampler_hook.reset_mock()
9482

9583
m_sampler_hook.side_effect = _sampler_drop_none
@@ -100,17 +88,6 @@ def _sampler_drop_none(fields):
10088
ev.send_presampled.assert_called_once_with()
10189
ev.send.assert_not_called()
10290

103-
# traced event
104-
ev = Mock()
105-
m_sampler_hook.reset_mock()
106-
107-
_beeline._run_hooks_and_send(ev)
108-
m_sampler_hook.assert_called_once_with(ev.fields())
109-
self.assertEqual(ev.sample_rate, 100)
110-
_beeline.tracer_impl.send_traced_event.assert_called_once_with(ev, presampled=True)
111-
ev.send_presampled.assert_not_called()
112-
ev.send.assert_not_called()
113-
11491
def test_run_hooks_and_send_presend_hook(self):
11592
''' ensure send works when presend hook is defined '''
11693
def _presend_hook(fields):
@@ -122,14 +99,12 @@ def _presend_hook(fields):
12299
_beeline.tracer_impl = Mock()
123100

124101
ev = Mock()
125-
delattr(ev, 'traced_event')
126102
ev.fields.return_value = {
127103
"thing i don't want": "get it out of here",
128104
"happy data": "so happy",
129105
}
130106

131107
_beeline._run_hooks_and_send(ev)
132-
_beeline.tracer_impl.send_traced_event.assert_not_called()
133108
ev.send_presampled.assert_not_called()
134109
ev.send.assert_called_once_with()
135110
self.assertDictEqual(

beeline/test_trace.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def test_probability(self):
4444

4545
class TestSynchronousTracer(unittest.TestCase):
4646
def test_trace_context_manager_exception(self):
47-
''' ensure that send_traced_event is called even if an exception is
47+
''' ensure that span is sent even if an exception is
4848
raised inside the context manager '''
4949
m_client = Mock()
5050
tracer = SynchronousTracer(m_client)
@@ -63,6 +63,8 @@ def test_trace_context_manager_starts_span_if_trace_active(self):
6363
m_client = Mock()
6464
tracer = SynchronousTracer(m_client)
6565
tracer.start_span = Mock()
66+
tracer.start_trace = Mock()
67+
tracer.get_active_trace_id = Mock(return_value='asdf')
6668
mock_span = Mock()
6769
tracer.start_span.return_value = mock_span
6870
tracer.finish_span = Mock()
@@ -71,6 +73,37 @@ def test_trace_context_manager_starts_span_if_trace_active(self):
7173
pass
7274

7375
tracer.start_span.assert_called_once_with(context={'name': 'foo'}, parent_id=None)
76+
tracer.start_trace.assert_not_called()
77+
tracer.finish_span.assert_called_once_with(mock_span)
78+
79+
def test_trace_context_manager_passes_parent_id_if_supplied(self):
80+
''' ensure parent_id gets passed to start_span if supplied '''
81+
m_client = Mock()
82+
tracer = SynchronousTracer(m_client)
83+
tracer.start_span = Mock()
84+
mock_span = Mock()
85+
tracer.start_span.return_value = mock_span
86+
tracer.finish_span = Mock()
87+
88+
with tracer('foo', parent_id='zyxw'):
89+
pass
90+
91+
tracer.start_span.assert_called_once_with(context={'name': 'foo'}, parent_id='zyxw')
92+
tracer.finish_span.assert_called_once_with(mock_span)
93+
94+
def test_trace_context_manager_starts_trace_if_trace_id_supplied(self):
95+
''' ensure trace_id and parent_id get passed to start_trace if supplied '''
96+
m_client = Mock()
97+
tracer = SynchronousTracer(m_client)
98+
tracer.start_trace = Mock()
99+
mock_span = Mock()
100+
tracer.start_trace.return_value = mock_span
101+
tracer.finish_span = Mock()
102+
103+
with tracer('foo', trace_id='asdf', parent_id='zyxw'):
104+
pass
105+
106+
tracer.start_trace.assert_called_once_with(context={'name': 'foo'}, trace_id='asdf', parent_span_id='zyxw')
74107
tracer.finish_span.assert_called_once_with(mock_span)
75108

76109
def test_start_trace(self):

beeline/trace.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ def __init__(self, client):
4141
def __call__(self, name, trace_id=None, parent_id=None):
4242
try:
4343
span = None
44-
if self.get_active_trace_id():
45-
span = self.start_span(context={'name': name})
44+
if self.get_active_trace_id() and trace_id is None:
45+
span = self.start_span(context={'name': name}, parent_id=parent_id)
4646
if span:
4747
log('tracer context manager started new span, id = %s',
4848
span.id)
4949
else:
50-
span = self.start_trace(context={'name': name})
50+
span = self.start_trace(context={'name': name}, trace_id=trace_id, parent_span_id=parent_id)
5151
if span:
5252
log('tracer context manager started new trace, id = %s',
5353
span.trace_id)

beeline/version.py

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

setup.py

Lines changed: 1 addition & 1 deletion
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.1',
7+
version='2.4.2',
88
description='Honeycomb library for easy instrumentation',
99
url='https://github.com/honeycombio/beeline-python',
1010
author='Honeycomb.io',

0 commit comments

Comments
 (0)