Exemplo n.º 1
0
def test_batch_sender_flush_calls_transport_handler_with_correct_params(
    empty_annotations_dict,
    empty_binary_annotations_dict,
    fake_endpoint,
):
    # Tests that the transport handler is called with the value returned
    # by encoder.encode_queue.
    transport_handler = mock.Mock()
    transport_handler.get_max_payload_bytes = lambda: None
    encoder = MockEncoder(encoded_queue='foobar')
    sender = logging_helper.ZipkinBatchSender(
        transport_handler=transport_handler,
        max_portion_size=None,
        encoder=encoder,
    )
    with sender:
        sender.add_span(
            SpanBuilder(
                trace_id='000000000000000f',
                name='span',
                parent_id='0000000000000001',
                span_id='0000000000000002',
                timestamp=26.0,
                duration=4.0,
                annotations=empty_annotations_dict,
                tags=empty_binary_annotations_dict,
                kind=Kind.CLIENT,
                local_endpoint=fake_endpoint,
                service_name='test_service',
                report_timestamp=False,
            ))
    transport_handler.assert_called_once_with('foobar')
def test_batch_sender_add_span_too_big(fake_endpoint):
    # This time we set max_payload_bytes to 1000, so we have to send more batches.
    # Each encoded span is 175 bytes, so we can fit 5 of those in 1000 bytes.
    mock_transport_handler = mock.Mock(spec=MockTransportHandler)
    mock_transport_handler.get_max_payload_bytes = lambda: 1000
    sender = logging_helper.ZipkinBatchSender(
        mock_transport_handler,
        100,
        get_encoder(Encoding.V1_THRIFT),
    )
    with sender:
        for _ in range(201):
            sender.add_span(
                Span(
                    trace_id='000000000000000f',
                    name='span',
                    parent_id='0000000000000001',
                    span_id='0000000000000002',
                    kind=Kind.CLIENT,
                    timestamp=26.0,
                    duration=4.0,
                    local_endpoint=fake_endpoint,
                    annotations={},
                    tags={},
                ))

    # 5 spans per batch, means we need 201 / 4 = 41 batches to send them all.
    assert mock_transport_handler.call_count == 41
    for i in range(40):
        # The first 40 batches have 5 spans of 197 bytes + 5 bytes of
        # list headers = 990 bytes
        assert len(mock_transport_handler.call_args_list[i][0][0]) == 990
    # The last batch has a single remaining span of 197 bytes + 5 bytes of
    # list headers = 202 bytes
    assert len(mock_transport_handler.call_args_list[40][0][0]) == 202
Exemplo n.º 3
0
def test_batch_sender_add_span(
    empty_annotations_dict,
    empty_binary_annotations_dict,
    fake_endpoint,
):
    # This test verifies it's possible to add 1 span without throwing errors.
    # It also checks that exiting the ZipkinBatchSender context manager
    # triggers a flush of all the already added spans.
    encoder = MockEncoder(encoded_queue='foobar')
    sender = logging_helper.ZipkinBatchSender(
        transport_handler=MockTransportHandler(),
        max_portion_size=None,
        encoder=encoder,
    )
    with sender:
        sender.add_span(
            SpanBuilder(
                trace_id='000000000000000f',
                name='span',
                parent_id='0000000000000001',
                span_id='0000000000000002',
                timestamp=26.0,
                duration=4.0,
                annotations=empty_annotations_dict,
                tags=empty_binary_annotations_dict,
                kind=Kind.CLIENT,
                local_endpoint=fake_endpoint,
                service_name='test_service',
            ))
    assert encoder.encode_queue.call_count == 1
Exemplo n.º 4
0
def test_batch_sender_flush_calls_transport_handler_with_correct_params(
    mock_encode_bytes_list,
    empty_annotations_dict,
    empty_binary_annotations_dict,
    fake_endpoint,
):
    # Tests that the transport handler is called with the value returned
    # by thrift.encode_bytes_list.
    transport_handler = mock.Mock()
    transport_handler.get_max_payload_bytes = lambda: None
    sender = logging_helper.ZipkinBatchSender(transport_handler)
    with sender:
        sender.add_span(
            span_id='0000000000000002',
            parent_span_id='0000000000000001',
            trace_id='000000000000000f',
            span_name='span',
            annotations=empty_annotations_dict,
            binary_annotations=empty_binary_annotations_dict,
            timestamp_s=None,
            duration_s=None,
            endpoint=fake_endpoint,
            sa_endpoint=None,
        )
    transport_handler.assert_called_once_with(
        mock_encode_bytes_list.return_value)
Exemplo n.º 5
0
def test_batch_sender_add_span(fake_endpoint):
    # This test verifies it's possible to add 1 span without throwing errors.
    # It also checks that exiting the ZipkinBatchSender context manager
    # triggers a flush of all the already added spans.
    encoder = MockEncoder(encoded_queue="foobar")
    sender = logging_helper.ZipkinBatchSender(
        transport_handler=MockTransportHandler(),
        max_portion_size=None,
        encoder=encoder,
    )
    with sender:
        sender.add_span(
            Span(
                trace_id="000000000000000f",
                name="span",
                parent_id="0000000000000001",
                span_id="0000000000000002",
                kind=Kind.CLIENT,
                timestamp=26.0,
                duration=4.0,
                local_endpoint=fake_endpoint,
                annotations={},
                tags={},
            )
        )
    assert encoder.encode_queue.call_count == 1
Exemplo n.º 6
0
def test_batch_sender_defensive_about_transport_handler(
    mock_encode_bytes_list,
    create_sp,
    empty_annotations_dict,
    empty_binary_annotations_dict,
    fake_endpoint,
):
    """Make sure log_span doesn't try to call the transport handler if it's
    None."""
    sender = logging_helper.ZipkinBatchSender(transport_handler=None)
    with sender:
        sender.add_span(
            span_id='0000000000000002',
            parent_span_id='0000000000000001',
            trace_id='00000000000000015',
            span_name='span',
            annotations=empty_annotations_dict,
            binary_annotations=empty_binary_annotations_dict,
            timestamp_s=None,
            duration_s=None,
            endpoint=fake_endpoint,
            sa_endpoint=None,
        )
    assert create_sp.call_count == 1
    assert mock_encode_bytes_list.call_count == 0
Exemplo n.º 7
0
def test_batch_sender_add_span_too_big(
    empty_annotations_dict,
    empty_binary_annotations_dict,
    fake_endpoint,
):
    # This time we set max_payload_bytes to 1000, so we have to send more batches.
    # Each encoded span is 65 bytes, so we can fit 15 of those in 1000 bytes.
    mock_transport_handler = mock.Mock(spec=MockTransportHandler)
    mock_transport_handler.get_max_payload_bytes = lambda: 1000
    sender = logging_helper.ZipkinBatchSender(mock_transport_handler, 100)
    with sender:
        for _ in range(201):
            sender.add_span(
                span_id='0000000000000002',
                parent_span_id='0000000000000001',
                trace_id='000000000000000f',
                span_name='span',
                annotations=empty_annotations_dict,
                binary_annotations=empty_binary_annotations_dict,
                timestamp_s=None,
                duration_s=None,
                endpoint=fake_endpoint,
                sa_endpoint=None,
            )
    # 15 spans per batch, means we need 201 / 15 = 14 batches to send them all.
    assert mock_transport_handler.call_count == 14
    for i in range(13):
        # The first 13 batches have 15 spans of 65 bytes + 5 bytes of
        # list headers = 980 bytes
        assert len(mock_transport_handler.call_args_list[i][0][0]) == 980
    # The last batch has the 6 remaining spans of 65 bytes + 5 bytes of
    # list headers = 395 bytes
    assert len(mock_transport_handler.call_args_list[13][0][0]) == 395
Exemplo n.º 8
0
def test_batch_sender_add_span_many_times(
    mock_encode_bytes_list,
    empty_annotations_dict,
    empty_binary_annotations_dict,
    fake_endpoint,
):
    # We create MAX_PORTION_SIZE * 2 + 1 spans, so we should trigger flush 3
    # times, once every MAX_PORTION_SIZE spans.
    sender = logging_helper.ZipkinBatchSender(MockTransportHandler())
    max_portion_size = logging_helper.ZipkinBatchSender.MAX_PORTION_SIZE
    with sender:
        for _ in range(max_portion_size * 2 + 1):
            sender.add_span(
                span_id='0000000000000002',
                parent_span_id='0000000000000001',
                trace_id='000000000000000f',
                span_name='span',
                annotations=empty_annotations_dict,
                binary_annotations=empty_binary_annotations_dict,
                timestamp_s=None,
                duration_s=None,
                endpoint=fake_endpoint,
                sa_endpoint=None,
            )
    assert mock_encode_bytes_list.call_count == 3
    assert len(
        mock_encode_bytes_list.call_args_list[0][0][0]) == max_portion_size
    assert len(
        mock_encode_bytes_list.call_args_list[1][0][0]) == max_portion_size
    assert len(mock_encode_bytes_list.call_args_list[2][0][0]) == 1
Exemplo n.º 9
0
def test_batch_sender_with_error_on_exit():
    sender = logging_helper.ZipkinBatchSender(
        MockTransportHandler(), None, MockEncoder(),
    )
    with pytest.raises(ZipkinError):
        with sender:
            raise Exception("Error!")
Exemplo n.º 10
0
def test_batch_sender_defensive_about_transport_handler(
    empty_annotations_dict,
    empty_binary_annotations_dict,
    fake_endpoint,
):
    """Make sure log_span doesn't try to call the transport handler if it's
    None."""
    encoder = MockEncoder()
    sender = logging_helper.ZipkinBatchSender(
        transport_handler=None,
        max_portion_size=None,
        encoder=encoder,
    )
    with sender:
        sender.add_span(
            SpanBuilder(
                trace_id='000000000000000f',
                name='span',
                parent_id='0000000000000001',
                span_id='0000000000000002',
                timestamp=26.0,
                duration=4.0,
                annotations=empty_annotations_dict,
                tags=empty_binary_annotations_dict,
                kind=Kind.CLIENT,
                local_endpoint=fake_endpoint,
                service_name='test_service',
                report_timestamp=False,
            ))
    assert encoder.encode_span.call_count == 1
    assert encoder.encode_queue.call_count == 0
def test_batch_sender_add_span_many_times(fake_endpoint):
    # We create MAX_PORTION_SIZE * 2 + 1 spans, so we should trigger flush 3
    # times, once every MAX_PORTION_SIZE spans.
    encoder = MockEncoder()
    sender = logging_helper.ZipkinBatchSender(
        transport_handler=MockTransportHandler(),
        max_portion_size=None,
        encoder=encoder,
    )
    max_portion_size = logging_helper.ZipkinBatchSender.MAX_PORTION_SIZE
    with sender:
        for _ in range(max_portion_size * 2 + 1):
            sender.add_span(
                Span(
                    trace_id='000000000000000f',
                    name='span',
                    parent_id='0000000000000001',
                    span_id='0000000000000002',
                    kind=Kind.CLIENT,
                    timestamp=26.0,
                    duration=4.0,
                    local_endpoint=fake_endpoint,
                    annotations={},
                    tags={},
                ))

    assert encoder.encode_queue.call_count == 3
    assert len(
        encoder.encode_queue.call_args_list[0][0][0]) == max_portion_size
    assert len(
        encoder.encode_queue.call_args_list[1][0][0]) == max_portion_size
    assert len(encoder.encode_queue.call_args_list[2][0][0]) == 1
Exemplo n.º 12
0
def test_batch_sender_add_span(thrift_objs):
    # Not much logic here, so this is basically a smoke test
    sender = logging_helper.ZipkinBatchSender(mock_transport_handler)
    with sender:
        sender.add_span(
            span_id='0000000000000002',
            parent_span_id='0000000000000001',
            trace_id='000000000000000f',
            span_name='span',
            annotations='ann',
            binary_annotations='binary_ann',
            timestamp_s=None,
            duration_s=None,
        )
    assert thrift_objs.call_count == 1
Exemplo n.º 13
0
def test_batch_sender_flush_calls_transport_handler_with_correct_params(
        thrift_objs, create_sp):
    transport_handler = mock.Mock()
    sender = logging_helper.ZipkinBatchSender(transport_handler)
    with sender:
        sender.add_span(
            span_id='0000000000000002',
            parent_span_id='0000000000000001',
            trace_id='00000000000000015',
            span_name='span',
            annotations='ann',
            binary_annotations='binary_ann',
            timestamp_s=None,
            duration_s=None,
        )
    transport_handler.assert_called_once_with(thrift_objs.return_value)
Exemplo n.º 14
0
def test_batch_sender_defensive_about_transport_handler(thrift_obj, create_sp):
    """Make sure log_span doesn't try to call the transport handler if it's
    None."""
    sender = logging_helper.ZipkinBatchSender(transport_handler=None)
    with sender:
        sender.add_span(
            span_id='0000000000000002',
            parent_span_id='0000000000000001',
            trace_id='00000000000000015',
            span_name='span',
            annotations='ann',
            binary_annotations='binary_ann',
            timestamp_s=None,
            duration_s=None,
        )
    assert create_sp.call_count == 1
    assert thrift_obj.call_count == 0
Exemplo n.º 15
0
def test_batch_sender_add_span_many_times(thrift_obj):
    sender = logging_helper.ZipkinBatchSender(mock_transport_handler)
    max_portion_size = logging_helper.ZipkinBatchSender.MAX_PORTION_SIZE
    with sender:
        for _ in range(max_portion_size * 2 + 1):
            sender.add_span(
                span_id='0000000000000002',
                parent_span_id='0000000000000001',
                trace_id='000000000000000f',
                span_name='span',
                annotations='ann',
                binary_annotations='binary_ann',
                timestamp_s=None,
                duration_s=None,
            )
    assert thrift_obj.call_count == 3
    assert len(thrift_obj.call_args_list[0][0][0]) == max_portion_size
    assert len(thrift_obj.call_args_list[1][0][0]) == max_portion_size
    assert len(thrift_obj.call_args_list[2][0][0]) == 1
Exemplo n.º 16
0
def test_batch_sender_add_span(
    mock_encode_bytes_list,
    empty_annotations_dict,
    empty_binary_annotations_dict,
    fake_endpoint,
):
    # This test verifies it's possible to add 1 span without throwing errors.
    # It also checks that exiting the ZipkinBatchSender context manager
    # triggers a flush of all the already added spans.
    sender = logging_helper.ZipkinBatchSender(MockTransportHandler())
    with sender:
        sender.add_span(
            span_id='0000000000000002',
            parent_span_id='0000000000000001',
            trace_id='000000000000000f',
            span_name='span',
            annotations=empty_annotations_dict,
            binary_annotations=empty_binary_annotations_dict,
            timestamp_s=None,
            duration_s=None,
            endpoint=fake_endpoint,
            sa_endpoint=None,
        )
    assert mock_encode_bytes_list.call_count == 1
Exemplo n.º 17
0
def test_batch_sender_defensive_about_transport_handler(fake_endpoint):
    """Make sure log_span doesn't try to call the transport handler if it's
    None."""
    encoder = MockEncoder()
    sender = logging_helper.ZipkinBatchSender(
        transport_handler=None, max_portion_size=None, encoder=encoder,
    )
    with sender:
        sender.add_span(
            Span(
                trace_id="000000000000000f",
                name="span",
                parent_id="0000000000000001",
                span_id="0000000000000002",
                kind=Kind.CLIENT,
                timestamp=26.0,
                duration=4.0,
                local_endpoint=fake_endpoint,
                annotations={},
                tags={},
            )
        )
    assert encoder.encode_span.call_count == 1
    assert encoder.encode_queue.call_count == 0
Exemplo n.º 18
0
def test_batch_sender_with_error_on_exit():
    sender = logging_helper.ZipkinBatchSender(mock_transport_handler)
    with pytest.raises(ZipkinError):
        with sender:
            raise Exception('Error!')