Examples of aiozipkin usage

Below is a list of examples from aiozipkin/examples

Every example is a correct tiny python program.

Basic Usage

import asyncio

import aiozipkin as az


async def run() -> None:
    # setup zipkin client
    zipkin_address = "http://127.0.0.1:9411/api/v2/spans"
    endpoint = az.create_endpoint("simple_service", ipv4="127.0.0.1", port=8080)

    # creates tracer object that traces all calls, if you want sample
    # only 50% just set sample_rate=0.5
    tracer = await az.create(zipkin_address, endpoint, sample_rate=1.0)

    # create and setup new trace
    with tracer.new_trace(sampled=True) as span:
        span.name("root_span")
        span.tag("span_type", "root")
        span.kind(az.CLIENT)
        span.annotate("SELECT * FROM")
        # imitate long SQL query
        await asyncio.sleep(0.1)
        span.annotate("start end sql")

        # create child span
        with tracer.new_child(span.context) as nested_span:
            nested_span.name("nested_span_1")
            nested_span.kind(az.CLIENT)
            nested_span.tag("span_type", "inner1")
            nested_span.remote_endpoint("remote_service_1")
            await asyncio.sleep(0.01)

        # create other child span
        with tracer.new_child(span.context) as nested_span:
            nested_span.name("nested_span_2")
            nested_span.kind(az.CLIENT)
            nested_span.remote_endpoint("remote_service_2")
            nested_span.tag("span_type", "inner2")
            await asyncio.sleep(0.01)

    await tracer.close()
    print("-" * 100)
    print("Check zipkin UI for produced traces: http://localhost:9411/zipkin")


if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(run())

aiohttp Example

Full featured example with aiohttp application:

import asyncio

from aiohttp import web

import aiozipkin as az


async def handle(request: web.Request) -> web.StreamResponse:
    tracer = az.get_tracer(request.app)
    span = az.request_span(request)

    with tracer.new_child(span.context) as child_span:
        child_span.name("mysql:select")
        # call to external service like https://python.org
        # or database query
        await asyncio.sleep(0.01)

    text = """
    <html lang="en">
    <head>
        <title>aiohttp simple example</title>
    </head>
    <body>
        <h3>This page was traced by aiozipkin</h3>
        <p><a href="http://127.0.0.1:9001/status">Go to not traced page</a></p>
    </body>
    </html>
    """
    return web.Response(text=text, content_type="text/html")


async def not_traced_handle(request: web.Request) -> web.StreamResponse:
    text = """
    <html lang="en">
    <head>
        <title>aiohttp simple example</title>
    </head>
    <body>
        <h3>This page was NOT traced by aiozipkin></h3>
        <p><a href="http://127.0.0.1:9001">Go to traced page</a></p>
    </body>
    </html>
    """
    return web.Response(text=text, content_type="text/html")


async def make_app(host: str, port: int) -> web.Application:
    app = web.Application()
    app.router.add_get("/", handle)
    # here we aquire reference to route, so later we can command
    # aiozipkin not to trace it
    skip_route = app.router.add_get("/status", not_traced_handle)

    endpoint = az.create_endpoint("aiohttp_server", ipv4=host, port=port)

    zipkin_address = "http://127.0.0.1:9411/api/v2/spans"
    tracer = await az.create(zipkin_address, endpoint, sample_rate=1.0)
    az.setup(app, tracer, skip_routes=[skip_route])
    return app


def run() -> None:
    host = "127.0.0.1"
    port = 9001
    loop = asyncio.get_event_loop()
    app = loop.run_until_complete(make_app(host, port))
    web.run_app(app, host=host, port=port)


if __name__ == "__main__":
    run()

Microservices Demo

There is a larger micro services example, using aiohttp. This demo consists of five simple services that call each other, as result you can study client server communication and zipkin integration for large projects. For more information see:

https://github.com/aio-libs/aiozipkin/tree/master/examples