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

Commit 25696fd

Browse files
authored
Merge pull request #39 from honeycombio/tredman.propagate_custom_context
[tracing] propagate trace fields to earlier spans
2 parents aa1d8ee + 613727d commit 25696fd

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

beeline/test_trace.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,26 @@ def _presend_hook(fields):
311311
},
312312
)
313313

314+
def test_run_hooks_and_send_adds_trace_fields(self):
315+
''' ensure trace fields are propagated backwards '''
316+
m_client = Mock()
317+
tracer = SynchronousTracer(m_client)
318+
m_span = Mock()
319+
m_span.event = Event()
320+
m_span.event.start_time = datetime.datetime.now()
321+
# set an existing trace field
322+
m_span.event.add_field('app.a', 1)
323+
324+
with patch('beeline.trace._should_sample') as m_sample_fn:
325+
m_sample_fn.return_value = True
326+
# add some trace fields
327+
tracer.add_trace_field('a', 0)
328+
tracer.add_trace_field('b', 2)
329+
tracer.add_trace_field('c', 3)
330+
tracer.finish_span(m_span)
331+
332+
# ensure we only added fields b and c and did not try to overwrite a
333+
self.assertDictContainsSubset({'app.a': 1, 'app.b': 2, 'app.c': 3}, m_span.event.fields())
314334

315335
class TestTraceContext(unittest.TestCase):
316336
def test_marshal_trace_context(self):

beeline/trace.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ def finish_span(self, span):
115115
# send the span's event. Even if the stack is in an unhealthy state,
116116
# it's probably better to send event data than not
117117
if span.event:
118+
# propagate trace fields that may have been added in later spans
119+
for k, v in self._state.trace_fields.items():
120+
# don't overwrite existing values because they may be different
121+
if k not in span.event.fields():
122+
span.event.add_field(k, v)
123+
118124
duration = datetime.datetime.now() - span.event.start_time
119125
duration_ms = duration.total_seconds() * 1000.0
120126
span.event.add_field('duration_ms', duration_ms)

beeline/version.py

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

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.2.0',
7+
version='2.3.0',
88
description='Honeycomb library for easy instrumentation',
99
url='https://github.com/honeycombio/beeline-python',
1010
author='Honeycomb.io',

0 commit comments

Comments
 (0)