def make_zipkin_spans(spans):
    zipkin_spans = []
    for span in spans:
        endpoint = make_endpoint(
            ipv4=span.tracer.ip_address,
            port=0,  # span.port,
            service_name=span.tracer.service_name)
        # TODO extend Zipkin Thrift and pass endpoint once only
        for event in span.logs:
            event.host = endpoint
        with span.update_lock:
            add_zipkin_annotations(span=span, endpoint=endpoint)
            hi, lo = extract_from_trace_id(span.trace_id)
            zipkin_span = zipkin_types.Span(
                trace_id=id_to_int(lo),
                trace_id_high=id_to_int(hi),
                name=span.operation_name,
                id=id_to_int(span.span_id),
                parent_id=id_to_int(span.parent_id) or None,
                annotations=span.logs,
                binary_annotations=span.tags,
                debug=span.is_debug(),
                timestamp=timestamp_micros(span.start_time),
                duration=timestamp_micros(span.end_time - span.start_time))
        zipkin_spans.append(zipkin_span)
    return zipkin_spans
Exemplo n.º 2
0
def test_child_span(tracer):
    span = tracer.start_span("test")
    child = tracer.start_span("child", references=child_of(span.context))
    child.set_tag(ext_tags.SPAN_KIND, ext_tags.SPAN_KIND_RPC_CLIENT)
    child.set_tag('bender', 'is great')
    child.log_event('kiss-my-shiny-metal-...')
    child.finish()
    span.finish()
    tracer.reporter.report_span.assert_called_once()
    assert len(span.logs) == 0, 'Parent span is Local, must not have events'
    assert len(child.logs) == 1, 'Child must have one events'
    add_zipkin_annotations(span=span, endpoint=None)
    add_zipkin_annotations(span=child, endpoint=None)
    assert len([t for t in span.tags if t.key == g.LOCAL_COMPONENT]) == 1
    assert len(child.logs) == 3, 'Child must have three events'
    assert log_exists(child, g.CLIENT_SEND), 'Must have cs event'
    assert log_exists(child, g.CLIENT_RECV), 'Must have cr event'

    tracer.sampler = ConstSampler(False)
    span = tracer.start_span("test")
    child = tracer.start_span("child", references=child_of(span.context))
    child.set_tag('bender', 'is great')
    child.log_event('kiss-my-shiny-metal-...')
    child.finish()
    span.finish()
    assert len(child.logs) == 0, "Must have no events, not sampled"
    assert len(child.tags) == 0, "Must have no attributes, not sampled"
    tracer.close()
Exemplo n.º 3
0
def test_start_trace(tracer):
    assert type(tracer) is Tracer
    with mock.patch.object(random.Random, 'getrandbits') as mock_random, \
            mock.patch('time.time') as mock_timestamp:
        mock_random.return_value = 12345
        mock_timestamp.return_value = 54321

        span = tracer.start_span('test')
        span.set_tag(ext_tags.SPAN_KIND, ext_tags.SPAN_KIND_RPC_SERVER)
        assert span, "Span must not be nil"
        assert span.tracer == tracer, "Tracer must be referenced from span"
        assert span.kind == ext_tags.SPAN_KIND_RPC_SERVER, \
            'Span must be server-side'
        assert span.trace_id == 12345, "Must match trace_id"
        assert span.is_sampled(), "Must be sampled"
        assert span.parent_id is None, "Must not have parent_id (root span)"
        assert span.start_time == 54321, "Must match timestamp"

        span.finish()
        assert span.end_time is not None, "Must have end_time defined"
        add_zipkin_annotations(span, None)
        assert len(span.logs) == 2, "Must have two events"
        assert log_exists(span, g.SERVER_RECV), 'Must have sr event'
        assert log_exists(span, g.SERVER_SEND), 'Must have ss event'
        tracer.reporter.assert_called_once()

    tracer.close()
Exemplo n.º 4
0
def test_child_span(tracer):
    span = tracer.start_span("test")
    child = tracer.start_span("child", parent=span)
    child.set_tag(ext_tags.SPAN_KIND, ext_tags.SPAN_KIND_RPC_CLIENT)
    child.set_tag('bender', 'is great')
    child.log_event('kiss-my-shiny-metal-...')
    child.finish()
    span.finish()
    tracer.reporter.report_span.assert_called_once()
    assert len(span.logs) == 0, 'Parent span is Local, must not have events'
    assert len(child.logs) == 1, 'Child must have one events'
    add_zipkin_annotations(span=span, endpoint=None)
    add_zipkin_annotations(span=child, endpoint=None)
    assert len([t for t in span.tags if t.key == g.LOCAL_COMPONENT]) == 1
    assert len(child.logs) == 3, 'Child must have three events'
    assert log_exists(child, g.CLIENT_SEND), 'Must have cs event'
    assert log_exists(child, g.CLIENT_RECV), 'Must have cr event'

    tracer.sampler = ConstSampler(False)
    span = tracer.start_span("test")
    child = tracer.start_span("child", parent=span)
    child.set_tag('bender', 'is great')
    child.log_event('kiss-my-shiny-metal-...')
    child.finish()
    span.finish()
    assert len(child.logs) == 0, "Must have no events, not sampled"
    assert len(child.tags) == 0, "Must have no attributes, not sampled"
    tracer.close()
Exemplo n.º 5
0
def test_start_trace(tracer):
    assert type(tracer) is Tracer
    with mock.patch.object(random.Random, 'getrandbits') as mock_random, \
            mock.patch('time.time') as mock_timestamp:
        mock_random.return_value = 12345L
        mock_timestamp.return_value = 54321L

        span = tracer.start_span('test')
        span.set_tag(ext_tags.SPAN_KIND, ext_tags.SPAN_KIND_RPC_SERVER)
        assert span, "Span must not be nil"
        assert span.tracer == tracer, "Tracer must be referenced from span"
        assert span.kind == ext_tags.SPAN_KIND_RPC_SERVER, \
            'Span must be server-side'
        assert span.trace_id == 12345L, "Must match trace_id"
        assert span.is_sampled(), "Must be sampled"
        assert span.parent_id is None, "Must not have parent_id (root span)"
        assert span.start_time == 54321L, "Must match timestamp"

        span.finish()
        assert span.end_time is not None, "Must have end_time defined"
        add_zipkin_annotations(span, None)
        assert len(span.logs) == 2, "Must have two events"
        assert log_exists(span, g.SERVER_RECV), 'Must have sr event'
        assert log_exists(span, g.SERVER_SEND), 'Must have ss event'
        tracer.reporter.assert_called_once()

        # TODO restore below once debug Trace Attribute is supported
        # tracer.sampler = ConstSampler(False)
        # span = tracer.start_span("test2", debug=True)
        # assert span.is_sampled(), \
        #     'Debug span must be sampled even if sampler said no'
    tracer.close()
def test_start_trace(tracer):
    assert type(tracer) is Tracer
    with mock.patch('os.urandom') as mock_urandom, mock.patch('time.time') as mock_timestamp, \
            mock.patch('uuid.uuid1') as mock_uuid1:
        mock_urandom.return_value = int.to_bytes(12345, 8)
        mock_timestamp.return_value = 54321
        mock_uuid1.return_value = uuid.UUID(int=987654321)

        span = tracer.start_span('test')
        span.set_tag(ext_tags.SPAN_KIND, ext_tags.SPAN_KIND_RPC_SERVER)
        assert span, "Span must not be nil"
        assert span.tracer == tracer, "Tracer must be referenced from span"
        assert span.kind == ext_tags.SPAN_KIND_RPC_SERVER, \
            'Span must be server-side'
        assert span.trace_id == 987654321, "Must match trace_id"
        assert span.span_id == 12345, "Must match span_id"
        assert span.is_sampled(), "Must be sampled"
        assert span.parent_id is None, "Must not have parent_id (root span)"
        assert span.start_time == 54321, "Must match timestamp"

        span.finish()
        assert span.end_time is not None, "Must have end_time defined"
        add_zipkin_annotations(span, None)
        assert len(span.logs) == 2, "Must have two events"
        assert log_exists(span, g.SERVER_RECV), 'Must have sr event'
        assert log_exists(span, g.SERVER_SEND), 'Must have ss event'
        tracer.reporter.assert_called_once()
Exemplo n.º 7
0
def test_start_child(tracer):
    root = tracer.start_span("test")
    span = tracer.start_span("test", parent=root)
    span.set_tag(ext_tags.SPAN_KIND, ext_tags.SPAN_KIND_RPC_SERVER)
    assert span.is_sampled(), "Must be sampled"
    assert span.trace_id == root.trace_id, "Must have the same trace id"
    assert span.parent_id == root.span_id, "Must inherit parent id"
    span.finish()
    assert span.end_time is not None, "Must have end_time set"
    add_zipkin_annotations(span, None)
    assert len(span.logs) == 2, "Must have two events"
    assert log_exists(span, g.SERVER_SEND), 'Must have ss event'
    assert log_exists(span, g.SERVER_RECV), 'Must have sr event'
    tracer.reporter.assert_called_once()
    tracer.close()
Exemplo n.º 8
0
def test_span_peer_tags(tracer):
    for test in [[1, 2, 3], [2, 1, 3], [3, 2, 1]]:
        span = tracer.start_span(operation_name='x')
        span.set_tag(ext_tags.SPAN_KIND, ext_tags.SPAN_KIND_RPC_SERVER)
        for t in test:
            # either of peer tags can initialize span.peer dictionary, so
            # we try permutations such that each gets a change to be first.
            if t == 1:
                span.set_tag(ext_tags.PEER_SERVICE, 'downstream')
            elif t == 2:
                span.set_tag(ext_tags.PEER_HOST_IPV4, 127 << 24 | 1)
            elif t == 3:
                span.set_tag(ext_tags.PEER_PORT, 12345)
        span.finish()
        add_zipkin_annotations(span, None)
        ca = [e for e in span.tags if e.key == 'ca'][0]
        assert ca.host.service_name == 'downstream'
        assert ca.host.ipv4 == 127 << 24 | 1
        assert ca.host.port == 12345
Exemplo n.º 9
0
def test_start_child(tracer, mode):
    root = tracer.start_span("test")
    if mode == 'arg':
        span = tracer.start_span("test", child_of=root.context)
    elif mode == 'ref':
        span = tracer.start_span("test", references=child_of(root.context))
    else:
        raise ValueError('bad mode')
    span.set_tag(ext_tags.SPAN_KIND, ext_tags.SPAN_KIND_RPC_SERVER)
    assert span.is_sampled(), "Must be sampled"
    assert span.trace_id == root.trace_id, "Must have the same trace id"
    assert span.parent_id == root.span_id, "Must inherit parent id"
    span.finish()
    assert span.end_time is not None, "Must have end_time set"
    add_zipkin_annotations(span, None)
    assert len(span.logs) == 2, "Must have two events"
    assert log_exists(span, g.SERVER_SEND), 'Must have ss event'
    assert log_exists(span, g.SERVER_RECV), 'Must have sr event'
    tracer.reporter.assert_called_once()
    tracer.close()