def test_zipkin_trace_zero_sampling():
    run_times = 100000
    hook = ZipkinTraceHook(sample_rate=0)

    request = Request()
    request.tracing.traceflags = True
    for _ in range(run_times):
        hook.before_send_request(request)

    assert not request.tracing.annotations
Beispiel #2
0
def test_zipkin_trace_zero_sampling():
    run_times = 100000
    hook = ZipkinTraceHook(sample_rate=0)

    request = Request()
    request.tracing.traceflags = True
    for _ in range(run_times):
        hook.before_send_request(request)

    assert not request.tracing.annotations
Beispiel #3
0
def test_zipkin_trace_sampling():
    run_times = 100000
    sample_rate = 1.0 - random.random()
    hook = ZipkinTraceHook(sample_rate=sample_rate)
    count = 0
    for _ in range(run_times):
        if hook._lucky(_uniq_id()):
            count += 1

    assert 0.9 * run_times * sample_rate <= count
    assert count <= run_times * sample_rate * 1.1
def test_zipkin_trace_sampling():
    run_times = 100000
    sample_rate = 1.0 - random.random()
    hook = ZipkinTraceHook(sample_rate=sample_rate)
    count = 0
    for _ in range(run_times):
        if hook._lucky(_uniq_id()):
            count += 1

    assert 0.9 * run_times * sample_rate <= count
    assert count <= run_times * sample_rate * 1.1
def test_zipkin_trace(trace_server):
    endpoint = b'endpoint1'
    zipkin_tracer = ZipkinTraceHook(dst=trace_buf)
    tchannel = TChannel(name='test')
    tchannel.hooks.register(zipkin_tracer)

    hostport = 'localhost:%d' % trace_server.port

    response = yield tchannel.request(hostport).send(InMemStream(endpoint),
                                                     InMemStream(hostport),
                                                     InMemStream(),
                                                     traceflag=True)
    header = yield response.get_header()
    body = yield response.get_body()
    assert header == "from handler1"
    assert body == "from handler2"
    traces = []
    for trace in trace_buf.getvalue().split("\n"):
        if trace:
            traces.append(json.loads(trace))

    trace_id = traces[0][0][u'trace_id']
    for trace in traces:
        assert trace_id == trace[0][u'trace_id']
        if trace[0][u'name'] == u'endpoint2':
            parent_span_id = trace[0][u'parent_span_id']
        else:
            span_id = trace[0][u'span_id']

    assert parent_span_id == span_id
Beispiel #6
0
def test_no_infinite_trace_submit():
    """Zipkin submissions must not trace themselves."""

    def submit(request):
        return TResponse(True)

    zipkin_server = TChannel('zipkin')
    zipkin_server.thrift.register(TCollector, handler=submit)
    zipkin_server.listen()

    class TestTraceHook(EventHook):
        def __init__(self):
            self.tracings = []

        def before_send_request(self, request):
            # if request.service == 'tcollector':
            self.tracings.append(request.tracing)

    server = TChannel('server', known_peers=[zipkin_server.hostport])
    server.hooks.register(ZipkinTraceHook(tchannel=server, sample_rate=1))
    test_trace_hook = TestTraceHook()
    server.hooks.register(test_trace_hook)
    server.thrift.register(TCollector, handler=submit)

    @server.raw.register
    @tornado.gen.coroutine
    def hello(request):
        if request.body == 'body':
            yield server.raw(
                service='server',
                endpoint='hello',
                body='boy',
                hostport=server.hostport,
                trace=True,
            )
        raise tornado.gen.Return('hello')

    server.listen()

    client = TChannel('client')
    client.thrift.register(TCollector, handler=submit)

    yield client.raw(
        service='server',
        endpoint='hello',
        hostport=server.hostport,
        body='body',
        trace=True,
    )

    # Continue yielding to the IO loop to allow our zipkin traces to be
    # handled.
    for _ in xrange(100):
        yield tornado.gen.moment

    # One trace for 'hello' and then 3 submissions to tcollector (1 time as
    # client, 2 times as server)
    assert len(test_trace_hook.tracings) == 4, test_trace_hook.tracings
Beispiel #7
0
def trace_server():
    with MockServer() as server:
        register(server.tchannel)
        server.tchannel.hooks.register(
            ZipkinTraceHook(
                dst=trace_buf,
                sample_rate=1,
            ), )
        yield server
Beispiel #8
0
def test_zipkin_trace(trace_server):
    endpoint = b'endpoint1'
    zipkin_tracer = ZipkinTraceHook(dst=trace_buf)
    tchannel = TChannel(name='test')
    tchannel.hooks.register(zipkin_tracer)

    hostport = 'localhost:%d' % trace_server.port

    response = yield tchannel.raw(
        service='test-client',
        hostport=hostport,
        endpoint=endpoint,
        headers=hostport,
        trace=True,
    )

    header = response.headers
    body = response.body
    assert header == "from handler1"
    assert body == "from handler2"
    traces = []
    for trace in trace_buf.getvalue().split("\n"):
        if trace:
            traces.append(json.loads(trace))

    parent_span_id = object()
    trace_id = traces[0][0][u'trace_id']

    assert traces

    for trace in traces:
        assert trace_id == trace[0][u'trace_id']
        if trace[0][u'name'] == u'endpoint2':
            parent_span_id = trace[0][u'parent_span_id']
        else:
            span_id = trace[0][u'span_id']

    assert parent_span_id == span_id