Skip to main content

Distributed tracing instrumentation for asyncio application with zipkin

Project description

aiozipkin

https://travis-ci.org/aio-libs/aiozipkin.svg?branch=master https://codecov.io/gh/aio-libs/aiozipkin/branch/master/graph/badge.svg https://img.shields.io/pypi/v/aiozipkin.svg Documentation Status Chat on Gitter

aiozipkin is Python 3.5+ module that adds distributed tracing capabilities from asyncio applications with zipkin (http://zipkin.io) server instrumentation.

zipkin is a distributed tracing system. It helps gather timing data needed to troubleshoot latency problems in microservice architectures. It manages both the collection and lookup of this data. Zipkin’s design is based on the Google Dapper paper.

zipkin ui animation

Simple example

import asyncio
import aiozipkin as az


async def run():
    # setup zipkin client
    zipkin_address = "http://127.0.0.1:9411"
    endpoint = az.create_endpoint(
        "simple_service", ipv4="127.0.0.1", port=8080)
    tracer = az.create(zipkin_address, endpoint, sample_rate=1.0)

    # create and setup new trace
    with tracer.new_trace(sampled=True) as span:
        # give a name for the span
        span.name("Slow SQL")
        # tag with relevant information
        span.tag("span_type", "root")
        # indicate that this is client span
        span.kind(az.CLIENT)
        # make timestamp and name it with START SQL query
        span.annotate("START SQL SELECT * FROM")
        # imitate long SQL query
        await asyncio.sleep(0.1)
        # make other timestamp and name it "END SQL"
        span.annotate("END SQL")

    await tracer.close()

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

aiohttp example

aiozipkin includes aiohttp server instrumentation, for this create web.Application() as usual and install aiozipkin plugin:

import aiozipkin as az

def init_app():
    host, port = "127.0.0.1", 8080
    app = web.Application()
    endpoint = az.create_endpoint("AIOHTTP_SERVER", ipv4=host, port=port)
    tracer = az.create(zipkin_address, endpoint, sample_rate=1.0)
    az.setup(app, tracer)

That is it, plugin adds middleware that tries to fetch context from headers, and create/join new trace. Optionally on client side you can add propagation headers in order to force tracing and to see network latency between client and server.

import aiozipkin as az

endpoint = az.create_endpoint("AIOHTTP_CLIENT")
tracer = az.create(zipkin_address, endpoint)

with tracer.new_trace() as span:
    span.kind(az.CLIENT)
    headers = span.context.make_headers()
    host = "http://127.0.0.1:8080/api/v1/posts/{}".format(i)
    resp = await session.get(host, headers=headers)
    await resp.text()

Installation

Installation process is simple, just:

$ pip install aiozipkin

Requirements

CHANGES

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiozipkin-0.0.1b2.tar.gz (12.6 kB view details)

Uploaded Source

File details

Details for the file aiozipkin-0.0.1b2.tar.gz.

File metadata

  • Download URL: aiozipkin-0.0.1b2.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for aiozipkin-0.0.1b2.tar.gz
Algorithm Hash digest
SHA256 0ac960d75a6df90f8ec22e0d0042a7857f1dd4e7cbfd33f2c1b2d65fce3adf96
MD5 2baf82698700617978dd0ad521575eb2
BLAKE2b-256 79d827c8970bcea2d907f9cfa374ca180849a458cc32cfaf1fd9995a91e30129

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page