def test_error_handling(self): class TestException(Exception): pass def error_parser(propagation_context): raise TestException() # implicitly tests finish_span m_client = Mock() # these values are used before sending m_client.new_event.return_value.start_time = datetime.datetime.now() m_client.new_event.return_value.sample_rate = 99 tracer = SynchronousTracer(m_client) tracer.register_hooks(http_trace_parser=error_parser) header_value = '1;trace_id=bloop,parent_id=scoop,context=e30K' req = DictRequest({ # case shouldn't matter 'X-HoNEyComb-TrACE': header_value, }) span = tracer.propagate_and_start_trace( context={'big': 'important_stuff'}, request=req) # Verify that our new span is at the top of the stack self.assertEqual(tracer._trace.stack[0], span) # Verify that we got the event in the mock. self.assertEqual(span.event.sample_rate, 99)
def test_propagate_and_start_trace(self): # FIXME: Test basics, including error handling and custom hooks # implicitly tests finish_span m_client = Mock() # these values are used before sending m_client.new_event.return_value.start_time = datetime.datetime.now() m_client.new_event.return_value.sample_rate = 1 tracer = SynchronousTracer(m_client) header_value = '1;dataset=flibble,trace_id=bloop,parent_id=scoop,context=e30K' req = DictRequest({ # case shouldn't matter 'X-HoNEyComb-TrACE': header_value, }) span = tracer.propagate_and_start_trace( context={'big': 'important_stuff'}, request=req) self.assertEqual(tracer._trace.stack[0], span) self.assertEqual(span.trace_id, "bloop") self.assertEqual(span.parent_id, "scoop") tracer.finish_trace(span) self.assertEqual(span.event.dataset, 'flibble') # ensure the event is sent span.event.send_presampled.assert_called_once_with() # ensure that there is no current trace self.assertIsNone(tracer._trace)
def test_propagate_and_start_trace_uses_honeycomb_header_when_w3c_also_present( self): # implicitly tests finish_span m_client = Mock() # these values are used before sending m_client.new_event.return_value.start_time = datetime.datetime.now() m_client.new_event.return_value.sample_rate = 1 tracer = SynchronousTracer(m_client) w3c_value = '00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-00' honeycomb_value = '1;dataset=flibble,trace_id=bloop,parent_id=scoop,context=e30K' req = DictRequest({ 'traceparent': w3c_value, 'x-honeycomb-trace': honeycomb_value }) span = tracer.propagate_and_start_trace( context={'big': 'important_stuff'}, request=req) self.assertEqual(tracer._trace.stack[0], span) self.assertEqual(span.trace_id, "bloop") self.assertEqual(span.parent_id, "scoop") tracer.finish_trace(span) self.assertEqual(span.event.dataset, 'flibble') # ensure the event is sent span.event.send_presampled.assert_called_once_with() # ensure that there is no current trace self.assertIsNone(tracer._trace)
def test_propagate_and_start_trace_uses_w3c_header_as_fallback(self): # implicitly tests finish_span m_client = Mock() # these values are used before sending m_client.new_event.return_value.start_time = datetime.datetime.now() m_client.new_event.return_value.sample_rate = 1 tracer = SynchronousTracer(m_client) header_value = '00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-00' req = DictRequest({ 'traceparent': header_value, }) span = tracer.propagate_and_start_trace( context={'big': 'important_stuff'}, request=req) self.assertEqual(tracer._trace.stack[0], span) self.assertEqual(span.trace_id, "0af7651916cd43dd8448eb211c80319c") self.assertEqual(span.parent_id, "b7ad6b7169203331") tracer.finish_trace(span) # ensure the event is sent span.event.send_presampled.assert_called_once_with() # ensure that there is no current trace self.assertIsNone(tracer._trace)
def test_has_header(self): '''Test that the hook properly parses honeycomb trace headers''' req = DictRequest({ # case shouldn't matter 'X-HoNEyComb-TrACE': header_value, }) pc = hc.http_trace_parser_hook(req) self.assertEqual(pc.trace_id, "bloop") self.assertEqual(pc.parent_id, "scoop")
def test_has_header(self): '''Test that the hook properly parses W3C trace headers''' req = DictRequest(_TEST_HEADERS) pc = w3c.http_trace_parser_hook(req) self.assertEquals(pc.trace_id, _TEST_TRACE_ID) self.assertEquals(pc.parent_id, _TEST_PARENT_ID) self.assertEquals(pc.trace_fields, { "tracestate": _TEST_TRACESTATE, "traceflags": _TEST_TRACE_FLAGS })
def test_no_header(self): req = DictRequest({}) pc = hc.http_trace_parser_hook(req) self.assertIsNone(pc)