OpenTelemetry Python Extensions

1.1.0 · active · verified Thu Apr 16

OpenTelemetry Extensions for Python is a collection of helper classes, functions, and decorators designed to simplify the use of the OpenTelemetry Python API and SDK packages. It currently supports Python >= 3.8 and is actively maintained, with version 1.1.0 released in October 2024.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the OpenTelemetry tracer provider using `otel-extensions` and create a basic traced operation. It shows how to configure common options, which can also be provided via environment variables.

import os
from otel_extensions import init_telemetry_provider, TelemetryOptions
from opentelemetry import trace

# Configure telemetry options, can also be set via environment variables
options = TelemetryOptions(
    OTEL_EXPORTER_OTLP_ENDPOINT=os.environ.get('OTEL_EXPORTER_OTLP_ENDPOINT', 'http://localhost:4317/'),
    OTEL_EXPORTER_OTLP_PROTOCOL=os.environ.get('OTEL_EXPORTER_OTLP_PROTOCOL', 'grpc'),
    OTEL_SERVICE_NAME=os.environ.get('OTEL_SERVICE_NAME', 'my-service'),
    OTEL_PROCESSOR_TYPE=os.environ.get('OTEL_PROCESSOR_TYPE', 'batch')
)

# Initialize the global tracer provider
init_telemetry_provider(options)

# Get a tracer and create a span
tracer = trace.get_tracer(__name__)

with tracer.start_as_current_span("example-operation") as span:
    span.set_attribute("custom.attribute", "example_value")
    print("Performing an example operation with tracing...")

print("Telemetry provider initialized and example span created.")

view raw JSON →