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 extensionB
). - 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.

Features & API Focus¶
OpenTracingManager
— The trace manager¶
-
class
cdumay_opentracing.
OpenTracingManager
¶ This class manages the opentracing context.
- It allows to recover using the
opentracing
library the initialized tracer. - It manage spans stack
-
classmethod
register
(clazz, driver)¶ Register a driver for the given class
Parameters: - clazz (Any) – Class managed by the driver.
- driver (cdumay_opentracing.OpenTracingDriver) – Driver to register.
-
classmethod
get_driver
(obj)¶ Find registered driver for the given object
Parameters: obj (Any) – object to manipulate Returns: registered driver for this class Return type: cdumay_opentracing.OpenTracingDriver
Extract tags from carrier object using a registered driver.
Parameters: obj (Any) – object to manipulate Returns: Tags to add on span Return type: dict
-
classmethod
get_current_span
()¶ Get the current span
Returns: The current span Return type: opentracing.span.Span or None
-
classmethod
create_span
(obj, name, tags)¶ Create a new span
Parameters: - obj (Any) – Any object
- name (str) – Span name
- tags (dict) – Additional tags
Returns: Span
Return type: opentracing.span.Span
-
classmethod
finish_span
(span)¶ Terminate the given span
Parameters: span (opentracing.span.Span) – Span to finish
-
classmethod
log_kv
(span, obj, event, **kwargs)¶ Adds a log record to the Span.
Parameters: - span (opentracing.span.Span) – the Span instance to use.
- obj (Any) – the carrier object.
- event (str) – Span event name.
- kwargs (dict) – A dict of string keys and values of any type to log
Returns: Returns the Span itself, for call chaining.
Return type: opentracing.span.Span
- It allows to recover using the
OpenTracingDriver
- Classes driver¶
-
class
cdumay_opentracing.
OpenTracingDriver
¶ -
FORMAT
¶ opentracing.Format
used to load and store span context
-
classmethod
extract
(data)¶ - Extract span context from a carrier object
Parameters: data (Any) – the carrier object. Returns: a SpanContext instance extracted from carrier or None if no such span context could be found.
-
classmethod
inject
(span, data)¶ - Injects the span context into a carrier object.
Parameters: - span (opentracing.span.SpanContext) – the SpanContext instance to inject
- data (Any) – the carrier object.
- Extract tags from carrier object.
Parameters: data (Any) – the carrier object. Returns: Tags to add on span Return type: dict
-
classmethod
log_kv
(span, data, event, **kwargs)¶ - Adds a log record to the Span.
Parameters: - span (opentracing.span.Span) – the Span instance to use.
- data (Any) – the carrier object.
- event (str) – Span event name.
- kwargs (dict) – A dict of string keys and values of any type to log
Returns: Returns the Span itself, for call chaining.
Return type: opentracing.span.Span
-