コード例 #1
0
def main():
    # Setup BasicTracer to log to console
    logger = logging.getLogger('span_logging')

    logger.setLevel(logging.DEBUG)

    hndlr = logging.StreamHandler(sys.stdout)
    hndlr.setFormatter(
        logging.Formatter("[%(levelname)-8s] %(name)s: %(message)s"))

    logger.addHandler(hndlr)

    recorder = LogSpanRecorder(logger)

    tracer = BasicTracer(recorder=recorder)
    tracer.register_required_propagators()

    # Use tracer to create spans
    span = tracer.start_span(operation_name='main')

    for i in range(10):
        child_span = tracer.start_span(operation_name='loop', child_of=span)
        child_span.set_tag('i', i)

        child_span.log_kv({'message': "Sleeping for 1 second"})

        time.sleep(1)

        child_span.finish()

    span.finish()
コード例 #2
0
def test_start_span():
    """ Test in process child span creation."""
    tracer = BasicTracer()
    tracer.register_required_propagators()
    sp = tracer.start_span(operation_name='test')
    sp.set_baggage_item('foo', 'bar')

    child = tracer.start_span(operation_name='child', child_of=sp.context)
    assert child.context.trace_id == sp.context.trace_id
    assert child.context.sampled == sp.context.sampled
    assert child.context.baggage == sp.context.baggage
    assert child.parent_id == sp.context.span_id
コード例 #3
0
def test_start_span():
    """ Test in process child span creation."""
    tracer = BasicTracer()
    tracer.register_required_propagators()
    sp = tracer.start_span(operation_name='test')
    sp.set_baggage_item('foo', 'bar')

    child = tracer.start_span(
        operation_name='child', child_of=sp.context)
    assert child.context.trace_id == sp.context.trace_id
    assert child.context.sampled == sp.context.sampled
    assert child.context.baggage == sp.context.baggage
    assert child.parent_id == sp.context.span_id
コード例 #4
0
def test_propagation():
    tracer = BasicTracer()
    tracer.register_required_propagators()
    sp = tracer.start_span(operation_name='test')
    sp.context.sampled = False
    sp.set_baggage_item('foo', 'bar')

    # Test invalid types
    with pytest.raises(UnsupportedFormatException):
        tracer.inject(sp.context, 'invalid', {})
    with pytest.raises(UnsupportedFormatException):
        tracer.extract('invalid', {})

    tests = [(Format.BINARY, bytearray()), (Format.TEXT_MAP, {})]
    for format, carrier in tests:
        tracer.inject(sp.context, format, carrier)
        extracted_ctx = tracer.extract(format, carrier)

        assert extracted_ctx.trace_id == sp.context.trace_id
        assert extracted_ctx.span_id == sp.context.span_id
        assert extracted_ctx.sampled == sp.context.sampled
        assert extracted_ctx.baggage == sp.context.baggage

    # Test string value of sampled field
    headers = {}
    tracer.inject(sp.context, Format.HTTP_HEADERS, headers)
    headers['ot-tracer-sampled'] = '0'
    span_ctx0 = tracer.extract(Format.HTTP_HEADERS, headers)
    assert not span_ctx0.sampled

    headers['ot-tracer-sampled'] = '1'
    span_ctx1 = tracer.extract(Format.HTTP_HEADERS, headers)
    assert span_ctx1.sampled
コード例 #5
0
def test_propagation():
    tracer = BasicTracer()
    tracer.register_required_propagators()
    sp = tracer.start_span(operation_name='test')
    sp.context.sampled = False
    sp.set_baggage_item('foo', 'bar')

    # Test invalid types
    with pytest.raises(UnsupportedFormatException):
        tracer.inject(sp.context, 'invalid', {})
    with pytest.raises(UnsupportedFormatException):
        tracer.extract('invalid', {})

    tests = [(Format.BINARY, bytearray()),
             (Format.TEXT_MAP, {})]
    for format, carrier in tests:
        tracer.inject(sp.context, format, carrier)
        extracted_ctx = tracer.extract(format, carrier)

        assert extracted_ctx.trace_id == sp.context.trace_id
        assert extracted_ctx.span_id == sp.context.span_id
        assert extracted_ctx.sampled == sp.context.sampled
        assert extracted_ctx.baggage == sp.context.baggage

    # Test string value of sampled field
    headers = {}
    tracer.inject(sp.context, Format.HTTP_HEADERS, headers)
    headers['ot-tracer-sampled'] = '0'
    span_ctx0 = tracer.extract(Format.HTTP_HEADERS, headers)
    assert not span_ctx0.sampled

    headers['ot-tracer-sampled'] = '1'
    span_ctx1 = tracer.extract(Format.HTTP_HEADERS, headers)
    assert span_ctx1.sampled
コード例 #6
0
 def start_span(self, *args, **kwargs):
     span = BasicTracer.start_span(self, *args, **kwargs)
     if span.parent_id is None:
         span.context.trace_id = '1-%x-%s' % (int(
             time.time()), binascii.b2a_hex(os.urandom(12)).decode('ascii'))
     span.context.span_id = binascii.b2a_hex(os.urandom(8)).decode('ascii')
     return span
コード例 #7
0
def test_propagation():
    tracer = BasicTracer()
    sp = tracer.start_span(operation_name="test")
    sp.set_baggage_item("foo", "bar")
    opname = 'op'

    tests = [(Format.BINARY, bytearray()), (Format.TEXT_MAP, {})]
    for format, carrier in tests:
        tracer.inject(sp, format, carrier)

        child = tracer.join(opname, format, carrier)

        assert child.raw.context.trace_id == sp.raw.context.trace_id
        assert child.raw.context.parent_id == sp.raw.context.span_id
        assert child.raw.context.sampled == sp.raw.context.sampled
        assert child.raw.baggage == sp.raw.baggage
コード例 #8
0
def test_span_log_kv():
    recorder = InMemoryRecorder()
    tracer = BasicTracer(recorder=recorder)

    span = tracer.start_span('x')
    span.log_kv({
        'foo': 'bar',
        'baz': 42,
        })
    span.finish()

    finished_spans = recorder.get_spans()
    assert len(finished_spans) == 1
    assert len(finished_spans[0].logs) == 1
    assert len(finished_spans[0].logs[0].key_values) == 2
    assert finished_spans[0].logs[0].key_values['foo'] == 'bar'
    assert finished_spans[0].logs[0].key_values['baz'] == 42
コード例 #9
0
def test_span_log_kv():
    recorder = InMemoryRecorder()
    tracer = BasicTracer(recorder=recorder)

    span = tracer.start_span('x')
    span.log_kv({
        'foo': 'bar',
        'baz': 42,
    })
    span.finish()

    finished_spans = recorder.get_spans()
    assert len(finished_spans) == 1
    assert len(finished_spans[0].logs) == 1
    assert len(finished_spans[0].logs[0].key_values) == 2
    assert finished_spans[0].logs[0].key_values['foo'] == 'bar'
    assert finished_spans[0].logs[0].key_values['baz'] == 42
コード例 #10
0
def test_propagation():
    tracer = BasicTracer()
    sp = tracer.start_span(operation_name="test")
    sp.set_baggage_item("foo", "bar")
    opname = 'op'

    tests = [(Format.BINARY, bytearray()),
             (Format.TEXT_MAP, {})]
    for format, carrier in tests:
        tracer.inject(sp, format, carrier)

        child = tracer.join(opname, format, carrier)

        assert child.raw.context.trace_id == sp.raw.context.trace_id
        assert child.raw.context.parent_id == sp.raw.context.span_id
        assert child.raw.context.sampled == sp.raw.context.sampled
        assert child.raw.baggage == sp.raw.baggage
コード例 #11
0
def test_span_sampling_priority():
    recorder = InMemoryRecorder()
    tracer = BasicTracer(recorder=recorder)

    span = tracer.start_span('x')
    assert span.context.sampled is True

    span.set_tag(tags.SAMPLING_PRIORITY, 0)
    assert span.context.sampled is False

    span.finish()

    assert len(recorder.get_spans()) == 1

    def get_sampled_spans():
        return [span for span in recorder.get_spans() if span.context.sampled]

    assert len(get_sampled_spans()) == 0
コード例 #12
0
def test_span_sampling_priority():
    recorder = InMemoryRecorder()
    tracer = BasicTracer(recorder=recorder)

    span = tracer.start_span('x')
    assert span.context.sampled is True

    span.set_tag(tags.SAMPLING_PRIORITY, 0)
    assert span.context.sampled is False

    span.finish()

    assert len(recorder.get_spans()) == 1

    def get_sampled_spans():
        return [span for span in recorder.get_spans() if span.context.sampled]

    assert len(get_sampled_spans()) == 0
コード例 #13
0
def test_propagation():
    tracer = BasicTracer()
    tracer.register_required_propagators()
    sp = tracer.start_span(operation_name='test')
    sp.context.sampled = False
    sp.set_baggage_item('foo', 'bar')

    # Test invalid types
    with pytest.raises(UnsupportedFormatException):
        tracer.inject(sp.context, 'invalid', {})
    with pytest.raises(UnsupportedFormatException):
        tracer.extract('invalid', {})

    tests = [(Format.BINARY, bytearray()),
             (Format.TEXT_MAP, {})]
    for format, carrier in tests:
        tracer.inject(sp.context, format, carrier)
        extracted_ctx = tracer.extract(format, carrier)

        assert extracted_ctx.trace_id == sp.context.trace_id
        assert extracted_ctx.span_id == sp.context.span_id
        assert extracted_ctx.sampled == sp.context.sampled
        assert extracted_ctx.baggage == sp.context.baggage