def test_stop_non_root_ts_duration_overridden(self): tracer = MockTracer() tracer.set_transport_configured(configured=True) tracer.get_context().push(zipkin.create_attrs_for_span()) ts = time.time() context = tracer.zipkin_span( service_name='test_service', span_name='test_span', timestamp=ts, duration=25, ) context.start() context.stop() assert len(tracer.get_spans()) == 1 endpoint = create_endpoint(service_name='test_service') assert tracer.get_spans()[0] == Span( trace_id=context.zipkin_attrs.trace_id, name='test_span', parent_id=context.zipkin_attrs.parent_span_id, span_id=context.zipkin_attrs.span_id, kind=Kind.LOCAL, timestamp=ts, duration=25, annotations={}, local_endpoint=endpoint, remote_endpoint=None, tags={}, ) assert tracer.is_transport_configured() is True
def test_stop_non_root(self, mock_time): tracer = MockTracer() tracer.set_transport_configured(configured=True) tracer.get_context().push(zipkin.create_attrs_for_span()) context = tracer.zipkin_span( service_name="test_service", span_name="test_span", ) context.start() context.stop() assert len(tracer.get_spans()) == 1 endpoint = create_endpoint(service_name="test_service") assert tracer.get_spans()[0] == Span( trace_id=context.zipkin_attrs.trace_id, name="test_span", parent_id=context.zipkin_attrs.parent_span_id, span_id=context.zipkin_attrs.span_id, kind=Kind.LOCAL, timestamp=123, duration=0, annotations={}, local_endpoint=endpoint, remote_endpoint=None, tags={}, ) assert tracer.is_transport_configured() is True
def test_init(self, mock_generate_kind): # Test that all arguments are correctly saved zipkin_attrs = ZipkinAttrs(None, None, None, None, None) transport = MockTransportHandler() firehose = MockTransportHandler() stack = Stack([]) span_storage = SpanStorage() tracer = MockTracer() context = tracer.zipkin_span( service_name="test_service", span_name="test_span", zipkin_attrs=zipkin_attrs, transport_handler=transport, max_span_batch_size=10, annotations={"test_annotation": 1}, binary_annotations={"status": "200"}, port=80, sample_rate=100.0, include=("cs", "cr"), add_logging_annotation=True, report_root_timestamp=True, use_128bit_trace_id=True, host="127.0.0.255", context_stack=stack, span_storage=span_storage, firehose_handler=firehose, kind=Kind.CLIENT, timestamp=1234, duration=10, encoding=Encoding.V2_JSON, ) assert context.service_name == "test_service" assert context.span_name == "test_span" assert context.zipkin_attrs_override == zipkin_attrs assert context.transport_handler == transport assert context.max_span_batch_size == 10 assert context.annotations == {"test_annotation": 1} assert context.binary_annotations == {"status": "200"} assert context.port == 80 assert context.sample_rate == 100.0 assert context.add_logging_annotation is True assert context.report_root_timestamp_override is True assert context.use_128bit_trace_id is True assert context.host == "127.0.0.255" assert context._context_stack == stack assert context._span_storage == span_storage assert context.firehose_handler == firehose assert mock_generate_kind.call_count == 1 assert mock_generate_kind.call_args == mock.call( context, Kind.CLIENT, ("cs", "cr"), ) assert context.timestamp == 1234 assert context.duration == 10 assert context.encoding == Encoding.V2_JSON assert context._tracer == tracer # Check for backward compatibility assert tracer.get_spans() == span_storage assert tracer.get_context() == stack
def test_service_exits_on_erroneous_span(log_mock): """Tests that for invalid zipkin_attrs exceptions are not thrown Services may not be handling them. Instead log an error """ mock_transport_handler, mock_logs = mock_logger() tracer = MockTracer() try: zipkin_attrs = ZipkinAttrs( trace_id='0', span_id='1, 1', # invalid span id parent_span_id='2', flags='0', is_sampled=True, ) with tracer.zipkin_span( service_name='test_service_name', span_name='service_span', zipkin_attrs=zipkin_attrs, transport_handler=mock_transport_handler, encoding=Encoding.V1_THRIFT, ): pass except Exception: pytest.fail('Exception not expected to be thrown!') finally: assert log_mock.call_count == 1 assert len(mock_logs) == 0 assert len(tracer.get_spans()) == 0
def test_stop_root(self): transport = MockTransportHandler() tracer = MockTracer() context = tracer.zipkin_span( service_name='test_service', span_name='test_span', transport_handler=transport, sample_rate=100.0, ) context.start() with mock.patch.object(context, 'logging_context') as mock_log_ctx: context.stop() assert mock_log_ctx.stop.call_count == 1 # Test that we reset everything after calling stop() assert context.logging_context is None assert tracer.is_transport_configured() is False assert len(tracer.get_spans()) == 0
def test_error_stopping_log_context(self): '''Tests if exception is raised while emitting traces that 1. tracer is cleared 2. excpetion is not passed up ''' transport = MockTransportHandler() tracer = MockTracer() context = tracer.zipkin_span( service_name='test_service', span_name='test_span', transport_handler=transport, sample_rate=100.0, ) context.start() with mock.patch.object(context, 'logging_context') as mock_log_ctx: mock_log_ctx.stop.side_effect = Exception try: context.stop() except Exception: pytest.fail('Exception not expected to be thrown!') assert mock_log_ctx.stop.call_count == 1 assert len(tracer.get_spans()) == 0
def test_zipkin_logging_server_context_emit_spans( add_span_mock, flush_mock, time_mock, fake_endpoint ): # This lengthy function tests that the logging context properly # logs both client and server spans. trace_id = "000000000000000f" parent_span_id = "0000000000000001" server_span_id = "0000000000000002" client_span_id = "0000000000000003" client_span_name = "breadcrumbs" attr = ZipkinAttrs( trace_id=trace_id, span_id=server_span_id, parent_span_id=parent_span_id, flags=None, is_sampled=True, ) tracer = MockTracer() client_span = Span( trace_id=trace_id, name=client_span_name, parent_id=server_span_id, span_id=client_span_id, kind=Kind.CLIENT, timestamp=26.0, duration=4.0, local_endpoint=create_endpoint(service_name="test_server"), annotations={"ann2": 2, "cs": 26, "cr": 30}, tags={"bann2": "yiss"}, ) tracer.get_spans().append(client_span) transport_handler = mock.Mock() context = logging_helper.ZipkinLoggingContext( zipkin_attrs=attr, endpoint=fake_endpoint, span_name="GET /foo", transport_handler=transport_handler, report_root_timestamp=True, get_tracer=lambda: tracer, service_name="test_server", encoding=Encoding.V1_JSON, ) context.start_timestamp = 24 context.response_status_code = 200 context.tags = {"k": "v"} time_mock.return_value = 42 context.emit_spans() client_log_call, server_log_call = add_span_mock.call_args_list assert ( server_log_call[0][1].build_v1_span() == Span( trace_id=trace_id, name="GET /foo", parent_id=parent_span_id, span_id=server_span_id, kind=Kind.SERVER, timestamp=24.0, duration=18.0, local_endpoint=fake_endpoint, annotations={"sr": 24, "ss": 42}, tags={"k": "v"}, ).build_v1_span() ) assert client_log_call[0][1] == client_span assert flush_mock.call_count == 1
def test_zipkin_logging_server_context_emit_spans_with_firehose( add_span_mock, flush_mock, time_mock, fake_endpoint): # This lengthy function tests that the logging context properly # logs both client and server spans. trace_id = '000000000000000f' parent_span_id = '0000000000000001' server_span_id = '0000000000000002' client_span_id = '0000000000000003' client_span_name = 'breadcrumbs' client_svc_name = 'svc' attr = ZipkinAttrs( trace_id=trace_id, span_id=server_span_id, parent_span_id=parent_span_id, flags=None, is_sampled=True, ) tracer = MockTracer() client_span = Span( trace_id=trace_id, name=client_span_name, parent_id=server_span_id, span_id=client_span_id, kind=Kind.CLIENT, timestamp=26.0, duration=4.0, local_endpoint=create_endpoint(service_name=client_svc_name), annotations={ 'ann2': 2, 'cs': 26, 'cr': 30 }, tags={'bann2': 'yiss'}, ) tracer.get_spans().append(client_span) transport_handler = mock.Mock() firehose_handler = mock.Mock() context = logging_helper.ZipkinLoggingContext( zipkin_attrs=attr, endpoint=fake_endpoint, span_name='GET /foo', transport_handler=transport_handler, report_root_timestamp=True, get_tracer=lambda: tracer, firehose_handler=firehose_handler, service_name='test_server', encoding=Encoding.V1_JSON, ) context.start_timestamp = 24 context.response_status_code = 200 context.tags = {'k': 'v'} time_mock.return_value = 42 context.emit_spans() call_args = add_span_mock.call_args_list firehose_client_log_call, client_log_call = call_args[0], call_args[2] firehose_server_log_call, server_log_call = call_args[1], call_args[3] assert server_log_call[0][1].build_v1_span() == \ firehose_server_log_call[0][1].build_v1_span() assert server_log_call[0][1].build_v1_span() == Span( trace_id=trace_id, name='GET /foo', parent_id=parent_span_id, span_id=server_span_id, kind=Kind.SERVER, timestamp=24.0, duration=18.0, local_endpoint=fake_endpoint, annotations={ 'sr': 24, 'ss': 42 }, tags={ 'k': 'v' }, ).build_v1_span() assert client_log_call[0][1] == firehose_client_log_call[0][ 1] == client_span assert flush_mock.call_count == 2