def test_encoding(encoding, validate_fn): zipkin_attrs = ZipkinAttrs( trace_id=generate_random_64bit_string(), span_id=generate_random_64bit_string(), parent_span_id=generate_random_64bit_string(), is_sampled=True, flags=None, ) inner_span_id = generate_random_64bit_string() mock_transport_handler = MockTransportHandler(10000) # Let's hardcode the timestamp rather than call time.time() every time. # The issue with time.time() is that the convertion to int of the # returned float value * 1000000 is not precise and in the same test # sometimes returns N and sometimes N+1. This ts value doesn't have that # issue afaict, probably since it ends in zeros. ts = 1538544126.115900 with mock.patch('time.time', autospec=True) as mock_time: # zipkin.py start, logging_helper.start, 3 x logging_helper.stop # I don't understand why logging_helper.stop would run 3 times, but # that's what I'm seeing in the test mock_time.side_effect = iter([ts, ts, ts + 10, ts + 10, ts + 10]) with zipkin.zipkin_span( service_name='test_service_name', span_name='test_span_name', transport_handler=mock_transport_handler, binary_annotations={'some_key': 'some_value'}, encoding=encoding, zipkin_attrs=zipkin_attrs, host='10.0.0.0', port=8080, kind=Kind.CLIENT, ) as span: with mock.patch.object( zipkin, 'generate_random_64bit_string', return_value=inner_span_id, ): with zipkin.zipkin_span( service_name='test_service_name', span_name='inner_span', timestamp=ts, duration=5, annotations={'ws': ts}, ): span.add_sa_binary_annotation( 8888, 'sa_service', '2001:0db8:85a3:0000:0000:8a2e:0370:7334', ) output = mock_transport_handler.get_payloads()[0] validate_fn(output, zipkin_attrs, inner_span_id, ts)
def test_override_span_name(): transport = MockTransportHandler() with zipkin.zipkin_span( service_name='my_service', span_name='span_name', transport_handler=transport, kind=Kind.CLIENT, sample_rate=100.0, encoding=Encoding.V1_JSON, ) as context: context.override_span_name('new_span_name') with zipkin.zipkin_span( service_name='my_service', span_name='nested_span', ) as nested_context: nested_context.override_span_name('new_nested_span') spans = json.loads(transport.get_payloads()[0]) assert len(spans) == 2 assert spans[0]['name'] == 'new_nested_span' assert spans[1]['name'] == 'new_span_name'