cdumay-opentracing

cdumay-opentracing is a python library to facilitate opentracing integration.

Example

The following example shows how to use it with Jaeger:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import opentracing
from cdumay_opentracing import OpenTracingDriver, OpenTracingSpan


class A(object):
    "A sample class"
    def __init__(self):
        self.a = 5
        self.b = "toto"
        self.trace = dict()


class B(A):
    """A class which inherit of A"""


class DriverA(OpenTracingDriver):
    @classmethod
    def extract(cls, data):
        return opentracing.tracer.extract(cls.FORMAT, data.trace)

    @classmethod
    def inject(cls, span, data):
        opentracing.tracer.inject(span, cls.FORMAT, data.trace)

    @classmethod
    def tags(cls, data):
        return dict(a=data.a, b=data.b)


def child():
    """A function that automatically recovers the current span"""
    with OpenTracingSpan(B(), "test-2") as span:
        span.log_kv(dict(event="done"))


if __name__ == '__main__':
    import os, time
    from jaeger_client import Config
    from cdumay_opentracing import OpenTracingManager

    tracer = Config(service_name="test", config=dict(
        sampler=dict(type='const', param=1), logging=True,
        local_agent=dict(reporting_host=os.getenv('JAEGER_HOST', 'localhost'))
    )).initialize_tracer()

    OpenTracingManager.register(A, DriverA)

    a = A()
    with OpenTracingSpan(a, "test") as span:
        child()
        OpenTracingManager.log_kv(span, a, "done")

    time.sleep(4)
    tracer.close()

Explanations

  • line 5-14: We create classes for our example.
  • line 17-28: We create a driver which allows to automate the manipulation of the class A.
  • line 38-45: We initialize tracing
  • line 47: We register the driver into the Tracing manager. It will allow to manipulate A instances (and by extension B).
  • line 49-50: We make a span using a.
  • line 51: We call a function from a span which will create a sub-span.
  • line 54-55: We sleep and close to make sure that the span is sent.
_images/example.png

Get It Now

Install the lib using pip:

pip install -U cdumay-opentracing