OpenTelemetry Contrib Instrumentation Packages

0.62b0 · active · verified Thu Apr 16

The `opentelemetry-contrib-instrumentations` library is a metapackage that conveniently installs a collection of community-maintained OpenTelemetry Python instrumentation packages. These instrumentations automatically capture telemetry data (traces, metrics, logs) from various third-party frameworks, databases, and HTTP clients without requiring manual code changes to the instrumented libraries themselves. The project is actively developed and follows a frequent release cadence, often aligning with the core OpenTelemetry Python SDK, typically on a monthly or bi-monthly schedule, with `b` (beta) versions being common.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up the OpenTelemetry SDK with a console exporter and then enable automatic tracing for the `requests` library. It initializes a `TracerProvider`, configures a `SimpleSpanProcessor` with a `ConsoleSpanExporter` to print traces to the console, and then uses `RequestsInstrumentor().instrument()` to automatically trace HTTP requests made with the `requests` library. Run the script and observe the trace output in the console. For a more comprehensive automatic instrumentation, you can also use `opentelemetry-distro` which automatically instruments all compatible installed libraries.

import os
import requests
from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
from opentelemetry.instrumentation.requests import RequestsInstrumentor

# Configure OpenTelemetry SDK
resource = Resource.create({"service.name": os.environ.get('OTEL_SERVICE_NAME', 'my-python-app')})
tracer_provider = TracerProvider(resource=resource)
span_processor = SimpleSpanProcessor(ConsoleSpanExporter())
tracer_provider.add_span_processor(span_processor)
trace.set_tracer_provider(tracer_provider)

# Instrument the requests library
RequestsInstrumentor().instrument()

# Make an HTTP request that will be traced
def make_request():
    print("Making an HTTP request...")
    response = requests.get("https://www.example.com")
    print(f"Request completed with status: {response.status_code}")

if __name__ == "__main__":
    make_request()

# To run this, install:
# pip install opentelemetry-sdk opentelemetry-exporter-console opentelemetry-instrumentation-requests requests

view raw JSON →