OpenTracing Shim for OpenTelemetry¶
The OpenTelemetry OpenTracing shim is a library which allows an easy migration from OpenTracing to OpenTelemetry.
The shim consists of a set of classes which implement the OpenTracing Python API while using OpenTelemetry constructs behind the scenes. Its purpose is to allow applications which are already instrumented using OpenTracing to start using OpenTelemetry with a minimal effort, without having to rewrite large portions of the codebase.
To use the shim, a TracerShim instance is created and then used as if
it were an “ordinary” OpenTracing opentracing.Tracer, as in the
following example:
import time
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.ext.opentracing_shim import create_tracer
# Tell OpenTelemetry which Tracer implementation to use.
trace.set_tracer_provider(TracerProvider())
# Create an OpenTelemetry Tracer.
otel_tracer = trace.get_tracer(__name__)
# Create an OpenTracing shim.
shim = create_tracer(otel_tracer)
with shim.start_active_span("ProcessHTTPRequest"):
print("Processing HTTP request")
# Sleeping to mock real work.
time.sleep(0.1)
with shim.start_active_span("GetDataFromDB"):
print("Getting data from DB")
# Sleeping to mock real work.
time.sleep(0.2)
Note
While the OpenTracing Python API represents time values as the number of
seconds since the epoch expressed as float values, the
OpenTelemetry Python API represents time values as the number of
nanoseconds since the epoch expressed as int values. This fact
requires the OpenTracing shim to convert time values back and forth between
the two representations, which involves floating point arithmetic.
Due to the way computers represent floating point values in hardware, representation of decimal floating point values in binary-based hardware is imprecise by definition.
The above results in slight imprecisions in time values passed to the
shim via the OpenTracing API when comparing the value passed to the shim
and the value stored in the OpenTelemetry opentelemetry.trace.Span
object behind the scenes. This is not a bug in this library or in
Python. Rather, this is a generic problem which stems from the fact that
not every decimal floating point number can be correctly represented in
binary, and therefore affects other libraries and programming languages as
well. More information about this problem can be found in the
Floating Point Arithmetic: Issues and Limitations section of the
Python documentation.
While testing this library, the aforementioned imprecisions were observed to be of less than a microsecond.
-
opentelemetry.ext.opentracing_shim.create_tracer(otel_tracer_provider)[source]¶ Creates a
TracerShimobject from the provided OpenTelemetryopentelemetry.trace.TracerProvider.The returned
TracerShimis an implementation ofopentracing.Tracerusing OpenTelemetry under the hood.- Parameters
otel_tracer_provider – A
opentelemetry.trace.TracerProviderto be used for constructing theTracerShim. A tracer from this source will be used to perform the actual tracing when user code is instrumented using the OpenTracing API.- Returns
The created
TracerShim.
-
class
opentelemetry.ext.opentracing_shim.SpanContextShim(otel_context)[source]¶ Implements
opentracing.SpanContextby wrapping aopentelemetry.trace.SpanContextobject.- Parameters
otel_context – A
opentelemetry.trace.SpanContextto be used for constructing theSpanContextShim.
-
unwrap()[source]¶ Returns the wrapped
opentelemetry.trace.SpanContextobject.- Returns
The
opentelemetry.trace.SpanContextobject wrapped by thisSpanContextShim.
-
property
baggage¶ Implements the
baggageproperty from the base class.Warning
Not implemented yet.
-
class
opentelemetry.ext.opentracing_shim.SpanShim(tracer, context, span)[source]¶ Implements
opentracing.Spanby wrapping aopentelemetry.trace.Spanobject.- Parameters
tracer – The
opentracing.Tracerthat created thisSpanShim.context – A
SpanContextShimwhich contains the context for thisSpanShim.span – A
opentelemetry.trace.Spanto wrap.
-
unwrap()[source]¶ Returns the wrapped
opentelemetry.trace.Spanobject.- Returns
The
opentelemetry.trace.Spanobject wrapped by thisSpanShim.
-
set_operation_name(operation_name)[source]¶ Implements the
set_operation_name()method from the base class.Updates the name of the wrapped OpenTelemetry span.
- Returns
Returns this
SpanShiminstance to allow call chaining.
-
finish(finish_time=None)[source]¶ Implements the
finish()method from the base class.Ends the OpenTelemetry span wrapped by this
SpanShim.If finish_time is provided, the time value is converted to the OpenTelemetry time format (number of nanoseconds since the epoch, expressed as an integer) and passed on to the OpenTelemetry tracer when ending the OpenTelemetry span. If finish_time isn’t provided, it is up to the OpenTelemetry tracer implementation to generate a timestamp when ending the span.
- Parameters
finish_time (
float, optional) – An explicit finish time expressed as the number of seconds since the epoch as returned bytime.time(). Defaults toNone.
-
set_tag(key, value)[source]¶ Implements the
set_tag()method from the base class.Sets an OpenTelemetry attribute on the wrapped OpenTelemetry span.
-
log_kv(key_values, timestamp=None)[source]¶ Implements the
log_kv()method from the base class.Logs an
opentelemetry.trace.Eventfor the wrapped OpenTelemetry span.
-
class
opentelemetry.ext.opentracing_shim.ScopeShim(manager, span, span_cm=None)[source]¶ A
ScopeShimwraps the OpenTelemetry functionality related to span activation/deactivation while using OpenTracingopentracing.Scopeobjects for presentation.Unlike other classes in this package, the
ScopeShimclass doesn’t wrap an OpenTelemetry class because OpenTelemetry doesn’t have the notion of “scope” (though it does have similar functionality).There are two ways to construct a
ScopeShimobject: using the default initializer and using thefrom_context_manager()class method.It is necessary to have both ways for constructing
ScopeShimobjects because in some cases we need to create the object from an OpenTelemetryopentelemetry.trace.Spancontext manager (as returned byopentelemetry.trace.Tracer.use_span()), in which case our only way of retrieving aopentelemetry.trace.Spanobject is by calling the__enter__()method on the context manager, which makes the span active in the OpenTelemetry tracer; whereas in other cases we need to accept aSpanShimobject and wrap it in aScopeShim. The former is used mainly when the instrumentation code retrieves the currently-active span usingScopeManagerShim.active. The latter is mainly used when the instrumentation code activates a span usingScopeManagerShim.activate().- Parameters
manager – The
ScopeManagerShimthat created thisScopeShim.span_cm (
contextlib.AbstractContextManager, optional) – A Python context manager which yields an OpenTelemetryopentelemetry.trace.Spanfrom its__enter__()method. Used byfrom_context_manager()to store the context manager as an attribute so that it can later be closed by calling its__exit__()method. Defaults toNone.
- TODO: Is
contextlib.AbstractContextManagerthe correct type for span_cm?
-
classmethod
from_context_manager(manager, span_cm)[source]¶ Constructs a
ScopeShimfrom an OpenTelemetryopentelemetry.trace.Spancontext manager.The method extracts a
opentelemetry.trace.Spanobject from the context manager by calling the context manager’s__enter__()method. This causes the span to start in the OpenTelemetry tracer.Example usage:
span = otel_tracer.start_span("TestSpan") span_cm = otel_tracer.use_span(span) scope_shim = ScopeShim.from_context_manager( scope_manager_shim, span_cm=span_cm, )
- Parameters
manager – The
ScopeManagerShimthat created thisScopeShim.span_cm – A context manager as returned by
opentelemetry.trace.Tracer.use_span().
-
close()[source]¶ Implements the
close()method fromopentracing.Scope.Closes the
ScopeShim. If theScopeShimwas created from a context manager, calling this method sets the active span in the OpenTelemetry tracer back to the span which was active before thisScopeShimwas created. In addition, if the span represented by thisScopeShimwas activated with the finish_on_close argument set toTrue, calling this method will end the span.Warning
In the current state of the implementation it is possible to create a
ScopeShimdirectly from aSpanShim, that is - without usingfrom_context_manager(). For that reason we need to be able to end the span represented by theScopeShimin this case, too. Please note that closing aScopeShimcreated this way (for example as returned byScopeManagerShim.active()) always ends the associated span, regardless of the value passed in finish_on_close when activating the span.
-
class
opentelemetry.ext.opentracing_shim.ScopeManagerShim(tracer)[source]¶ Implements
opentracing.ScopeManagerby setting and getting the activeopentelemetry.trace.Spanin the OpenTelemetry tracer.This class keeps a reference to a
TracerShimas an attribute. This reference is used to communicate with the OpenTelemetry tracer. It is necessary to have a reference to theTracerShimrather than theopentelemetry.trace.Tracerwrapped by it because when constructing aSpanShimwe need to pass a reference to aopentracing.Tracer.- Parameters
tracer – A
TracerShimto use for setting and getting active span state.
-
activate(span, finish_on_close)[source]¶ Implements the
activate()method from the base class.Activates a
SpanShimand returns aScopeShimwhich represents the active span.
-
property
active¶ Implements the
activeproperty from the base class.Returns a
ScopeShimobject representing the currently-active span in the OpenTelemetry tracer.- Returns
A
ScopeShimrepresenting the active span in the OpenTelemetry tracer, orNoneif no span is currently active.
Warning
Calling
ScopeShim.close()on theScopeShimreturned by this property always ends the corresponding span, regardless of the finish_on_close value used when activating the span. This is a limitation of the current implementation of the OpenTracing shim and is likely to be handled in future versions.
-
property
tracer¶ Returns the
TracerShimreference used by thisScopeManagerShimfor setting and getting the active span from the OpenTelemetry tracer.- Returns
The
TracerShimused for setting and getting the active span.
Warning
This property is not a part of the OpenTracing API. It used internally by the current implementation of the OpenTracing shim and will likely be removed in future versions.
-
class
opentelemetry.ext.opentracing_shim.TracerShim(tracer)[source]¶ Implements
opentracing.Tracerby wrapping aopentelemetry.trace.Tracerobject.This wrapper class allows using an OpenTelemetry tracer as if it were an OpenTracing tracer. It exposes the same methods as an “ordinary” OpenTracing tracer, and uses OpenTelemetry transparently for performing the actual tracing.
This class depends on the OpenTelemetry API. Therefore, any implementation of a
opentelemetry.trace.Tracershould work with this class.- Parameters
tracer – A
opentelemetry.trace.Tracerto use for tracing. This tracer will be invoked by the shim to create actual spans.
-
unwrap()[source]¶ Returns the
opentelemetry.trace.Tracerobject that is wrapped by thisTracerShimand used for actual tracing.- Returns
The
opentelemetry.trace.Tracerused for actual tracing.
-
start_active_span(operation_name, child_of=None, references=None, tags=None, start_time=None, ignore_active_span=False, finish_on_close=True)[source]¶ Implements the
start_active_span()method from the base class.Starts and activates a span. In terms of functionality, this method behaves exactly like the same method on a “regular” OpenTracing tracer. See
opentracing.Tracer.start_active_span()for more details.- Parameters
operation_name (
str) – Name of the operation represented by the new span from the perspective of the current service.child_of (
SpanShimorSpanContextShim, optional) – ASpanShimorSpanContextShimrepresenting the parent in a “child of” reference. If specified, the references parameter must be omitted. Defaults toNone.references (
list, optional) – A list ofopentracing.Referenceobjects that identify one or more parents of typeSpanContextShim. Defaults toNone.tags (
dict, optional) – A dictionary of tags. The keys must be of typestr. The values may be one ofstr,bool,int,float. Defaults toNone.start_time (
float, optional) – An explicit start time expressed as the number of seconds since the epoch as returned bytime.time(). Defaults toNone.ignore_active_span (
bool, optional) – Ignore the currently-active span in the OpenTelemetry tracer and make the created span the root span of a new trace. Defaults toFalse.finish_on_close (
bool, optional) – Determines whether the created span should end automatically when closing the returnedScopeShim. Defaults toTrue.
- Returns
A
ScopeShimthat is already activated by theScopeManagerShim.
-
start_span(operation_name=None, child_of=None, references=None, tags=None, start_time=None, ignore_active_span=False)[source]¶ Implements the
start_span()method from the base class.Starts a span. In terms of functionality, this method behaves exactly like the same method on a “regular” OpenTracing tracer. See
opentracing.Tracer.start_span()for more details.- Parameters
operation_name (
str) – Name of the operation represented by the new span from the perspective of the current service.child_of (
SpanShimorSpanContextShim, optional) – ASpanShimorSpanContextShimrepresenting the parent in a “child of” reference. If specified, the references parameter must be omitted. Defaults toNone.references (
list, optional) – A list ofopentracing.Referenceobjects that identify one or more parents of typeSpanContextShim. Defaults toNone.tags (
dict, optional) – A dictionary of tags. The keys must be of typestr. The values may be one ofstr,bool,int,float. Defaults toNone.start_time (
float, optional) – An explicit start time expressed as the number of seconds since the epoch as returned bytime.time(). Defaults toNone.ignore_active_span (
bool, optional) – Ignore the currently-active span in the OpenTelemetry tracer and make the created span the root span of a new trace. Defaults toFalse.
- Returns
An already-started
SpanShiminstance.