OpenTelemetry Requests Instrumentation

raw JSON →
0.61b0 verified Tue May 12 auth: no python install: stale quickstart: stale

Provides tracing capabilities for HTTP requests made using the 'requests' library. Current version: 0.61b0. Release cadence: beta releases with incremental improvements.

pip install opentelemetry-instrumentation-requests
error ModuleNotFoundError: No module named 'opentelemetry'
cause The core OpenTelemetry SDK or the `opentelemetry-instrumentation-requests` package itself is not installed in the Python environment where the application is running, or there's an issue with Python's namespace package resolution.
fix
Ensure that opentelemetry-api, opentelemetry-sdk, and opentelemetry-instrumentation-requests are installed in your active Python environment using pip: pip install opentelemetry-api opentelemetry-sdk opentelemetry-instrumentation-requests.
error opentelemetry-instrumentation-requests no traces
cause HTTP requests made by the `requests` library are not being traced because the `RequestsInstrumentor` has not been properly initialized or the OpenTelemetry SDK (TracerProvider, SpanProcessor, Exporter) is not configured correctly before the requests are made.
fix
Before making any requests calls, initialize the OpenTelemetry SDK with a TracerProvider, a SpanProcessor, and an Exporter. Then, call RequestsInstrumentor().instrument() to enable tracing:
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
from opentelemetry.instrumentation.requests import RequestsInstrumentor
import requests

# Configure the SDK
provider = TracerProvider()
processor = SimpleSpanProcessor(ConsoleSpanExporter()) # Or a different exporter like OTLPSpanExporter
provider.add_span_processor(processor)
trace.set_tracer_provider(provider)

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

# Now, requests will be traced
response = requests.get("http://example.com")
error RuntimeError: Requested component 'otlp_proto_grpc' not found
cause This error typically occurs when using the `opentelemetry-instrument` CLI for auto-instrumentation, and the OpenTelemetry OTLP exporter package for gRPC (or HTTP) is not installed with its required dependencies.
fix
Install the opentelemetry-exporter-otlp package with the necessary gRPC or HTTP dependencies. For gRPC, use: pip install 'opentelemetry-exporter-otlp[grpc]'. For HTTP, use: pip install 'opentelemetry-exporter-otlp[http]'. Also, ensure that relevant environment variables like OTEL_TRACES_EXPORTER and OTEL_METRICS_EXPORTER are correctly set (e.g., OTEL_TRACES_EXPORTER=otlp, and OTEL_METRICS_EXPORTER=none if not collecting metrics).
breaking Instrumenting after importing 'requests' may result in incomplete tracing due to module caching.
fix Ensure 'RequestsInstrumentor().instrument()' is called before importing 'requests'.
gotcha Setting 'OTEL_PYTHON_REQUESTS_EXCLUDED_URLS' to exclude certain URLs may not work if the environment variable is not set correctly.
fix Verify the environment variable is set with the correct regex patterns to exclude desired URLs.
breaking The 'requests' package is a direct dependency of opentelemetry-instrumentation-requests and must be installed.
fix Install the 'requests' package using pip: 'pip install requests'.
breaking The 'requests' library is a required dependency for 'opentelemetry-instrumentation-requests' and must be installed. The absence of 'requests' will cause a `ModuleNotFoundError`.
fix Install the 'requests' library using `pip install requests`.
python os / libc status wheel install import disk
3.10 alpine (musl) - - - -
3.10 slim (glibc) - - - -
3.11 alpine (musl) - - - -
3.11 slim (glibc) - - - -
3.12 alpine (musl) - - - -
3.12 slim (glibc) - - - -
3.13 alpine (musl) - - - -
3.13 slim (glibc) - - - -
3.9 alpine (musl) - - - -
3.9 slim (glibc) - - - -

This script sets up OpenTelemetry tracing, instruments the 'requests' library, and makes an HTTP GET request to 'https://api.example.com'.

import os
from opentelemetry.instrumentation.requests import RequestsInstrumentor
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.resources import Resource, SERVICE_NAME

# Set up the OpenTelemetry SDK
resource = Resource.create({SERVICE_NAME: 'my-service'})
provider = TracerProvider(resource=resource)
provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))
provider.register()

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

# Make an HTTP request
import requests
response = requests.get('https://api.example.com')
print(response.status_code)