OpenTelemetry Requests Instrumentation
Provides tracing capabilities for HTTP requests made using the 'requests' library. Current version: 0.61b0. Release cadence: beta releases with incremental improvements.
Warnings
- breaking Instrumenting after importing 'requests' may result in incomplete tracing due to module caching.
- gotcha Setting 'OTEL_PYTHON_REQUESTS_EXCLUDED_URLS' to exclude certain URLs may not work if the environment variable is not set correctly.
Install
-
pip install opentelemetry-instrumentation-requests
Imports
- RequestsInstrumentor
from opentelemetry.instrumentation.requests import RequestsInstrumentor
Quickstart
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)