Skip to content

Commit

Permalink
Merge pull request #39 from honeycombio/tredman.propagate_custom_context
Browse files Browse the repository at this point in the history
[tracing] propagate trace fields to earlier spans
  • Loading branch information
tredman authored Oct 22, 2018
2 parents aa1d8ee + 613727d commit 25696fd
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
20 changes: 20 additions & 0 deletions beeline/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 6 additions & 0 deletions beeline/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion beeline/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = '2.2.0'
VERSION = '2.3.0'
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 25696fd

Please sign in to comment.