diff --git a/beeline/test_trace.py b/beeline/test_trace.py index 59fd2fd..14b9f68 100644 --- a/beeline/test_trace.py +++ b/beeline/test_trace.py @@ -311,6 +311,26 @@ def _presend_hook(fields): }, ) + def test_run_hooks_and_send_adds_trace_fields(self): + ''' ensure trace fields are propagated backwards ''' + m_client = Mock() + tracer = SynchronousTracer(m_client) + m_span = Mock() + m_span.event = Event() + m_span.event.start_time = datetime.datetime.now() + # set an existing trace field + m_span.event.add_field('app.a', 1) + + with patch('beeline.trace._should_sample') as m_sample_fn: + m_sample_fn.return_value = True + # add some trace fields + tracer.add_trace_field('a', 0) + tracer.add_trace_field('b', 2) + tracer.add_trace_field('c', 3) + tracer.finish_span(m_span) + + # ensure we only added fields b and c and did not try to overwrite a + self.assertDictContainsSubset({'app.a': 1, 'app.b': 2, 'app.c': 3}, m_span.event.fields()) class TestTraceContext(unittest.TestCase): def test_marshal_trace_context(self): diff --git a/beeline/trace.py b/beeline/trace.py index a712986..650c636 100644 --- a/beeline/trace.py +++ b/beeline/trace.py @@ -115,6 +115,12 @@ def finish_span(self, span): # send the span's event. Even if the stack is in an unhealthy state, # it's probably better to send event data than not if span.event: + # propagate trace fields that may have been added in later spans + for k, v in self._state.trace_fields.items(): + # don't overwrite existing values because they may be different + if k not in span.event.fields(): + span.event.add_field(k, v) + duration = datetime.datetime.now() - span.event.start_time duration_ms = duration.total_seconds() * 1000.0 span.event.add_field('duration_ms', duration_ms) diff --git a/beeline/version.py b/beeline/version.py index 8418bbc..0f572f9 100644 --- a/beeline/version.py +++ b/beeline/version.py @@ -1 +1 @@ -VERSION = '2.2.0' +VERSION = '2.3.0' diff --git a/setup.py b/setup.py index eda2730..642d616 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ setup( python_requires='>=2.7', name='honeycomb-beeline', - version='2.2.0', + version='2.3.0', description='Honeycomb library for easy instrumentation', url='https://github.com/honeycombio/beeline-python', author='Honeycomb.io',