Python extensions for OpenTelemetry
Slowing down. Last release over 2 years ago, little activity since. Fine for existing code; think twice for a new project.
pip install otel-extensionsotel-extensions-python: OpenTelemetry Extensions for Python
OpenTelemetry Extensions for Python is a collection of helper classes, functions, and decorators to facilitate the use of the OpenTelemetry Python API & SDK packages
Version Support
Python >= 3.6
Installation
pip install
You can install through pip using:
pip install otel-extensions
(you may need to run pip with root permission: sudo pip install otel-extensions)
Setuptools
Install via Setuptools.
python setup.py install --user
(or sudo python setup.py install to install the package for all users)
Features
Tracer Provider Initialization
from otel_extensions import init_telemetry_provider, TelemetryOptions
# Provide options for telemetry provider
# Alternatively, any of the following options can be specified through
# environment variables with the equivalent name
options = TelemetryOptions(
# OTLP receiver endpoint
OTEL_EXPORTER_OTLP_ENDPOINT="http://localhost:4317/",
# CA bundle for TLS verification of endpoint (if endpoint scheme is https)
OTEL_EXPORTER_OTLP_CERTIFICATE="/path/to/ca/bundle",
# protocol for OTLP receiver (supported: gprc | http/protobuf | custom)
OTEL_EXPORTER_OTLP_PROTOCOL="grpc",
# Custom span exporter class name (needed if protocol set to 'custom')
OTEL_EXPORTER_CUSTOM_SPAN_EXPORTER_TYPE="pkg.ClassName",
# Name of service
OTEL_SERVICE_NAME="My Service",
# Processor type
# batch: use BatchSpanProcessor
# simple: use SimpleSpanProcessor
OTEL_PROCESSOR_TYPE="batch",
# Optional parent span id. Will be injected into current context
TRACEPARENT="001233454656...."
)
# Initialize the global tracer provider
init_telemetry_provider(options)
Instrumentation Decorator
You can use the @instrumented decorator to automatically wrap a span around a function or method.
(As of version 0.2.0, the decorator can support coroutine functions defined as async def as well as normal functions)
from otel_extensions import init_telemetry_provider, instrumented
import asyncio
async def main():
foo()
await async_foo()
@instrumented
def foo():
"""Creates a span named 'foo'"""
bar()
@instrumented(span_name="custom span name")
def bar():
"""Creates a span named 'custom span name'"""
print("Hello World")
@instrumented(span_attributes={"attr1": "val1", "attr2": "val2"})
def fn_with_attrs():
"""Creates a span named 'fn_with_attrs' and sets key/value pairs
from `span_attributes` as span attributes"""
print("Hello World")
@instrumented
async def async_foo():
"""Creates a span named 'async_foo'"""
await async_bar()
@instrumented(span_name="custom span name")
async def async_bar():
"""Creates a span named 'custom span name'"""
print("Hello World")
@instrumented(span_name="custom span name")
async def async_bar():
"""Creates a span named 'custom span name'"""
print("Hello World")
@instrumented(span_attributes={"attr1": "val1", "attr2": "val2"})
async def async_fn_with_attrs():
"""Creates a span named 'async_fn_with_attrs' and sets key/value pairs
from `span_attributes` as span attributes"""
print("Hello World")
if __name__ == '__main__':
# init telemetry provider (using options from environment variables)
init_telemetry_provider()
asyncio.run(main())