OpenCensus Requests Integration
OpenCensus-ext-requests provides integration for the OpenCensus Python library to automatically trace HTTP requests made with the popular 'requests' package. OpenCensus is a set of libraries for collecting application metrics and distributed traces. The current version is 0.8.0. The OpenCensus project is largely in maintenance mode, having merged with OpenTracing to form OpenTelemetry, and users are encouraged to migrate.
Common errors
-
HTTP requests made with 'requests' are not appearing in traces.
cause The `requests` integration has not been enabled or configured correctly.fixEnsure `opencensus.trace.config_integration.trace_integrations(['requests'])` is called before making `requests` calls and after initializing the tracer. -
Application hangs or freezes when performing HTTP requests using the 'requests' library.
cause This often occurs when the `opencensus-ext-requests` integration is used in conjunction with the `threading` integration and a synchronous exporter transport (`SyncTransport`).fixSwitch your exporter to use an asynchronous transport (e.g., `AsyncTransport`) or review your threading/requests integration strategy if `SyncTransport` is unavoidable. -
Seeing duplicate log entries when using `opencensus-ext-azure` and a logger configured with `AzureLogHandler`.
cause The `AzureLogHandler` is likely being added multiple times to the same logger instance, a common issue with older or deprecated libraries.fixEnsure that `AzureLogHandler` is added to your logger only once during application initialization. Check for multiple instantiations or reconfigurations of the logger.
Warnings
- breaking The OpenCensus project is deprecated and has merged into OpenTelemetry. All OpenCensus GitHub repositories (except for the core `opencensus-python` repo) were archived on July 31, 2023. Users are strongly encouraged to migrate to OpenTelemetry for ongoing support, new features, and security patches.
- gotcha When combining `opencensus-ext-requests` and the `threading` integration with a synchronous exporter transport (e.g., `SyncTransport`), the application may freeze or hang indefinitely.
- gotcha When using `opencensus-ext-requests` with Azure Application Insights, the requests integration might create two spans, leading to broken dependency lineage in the Application Map.
Install
-
pip install opencensus-ext-requests
Imports
- config_integration
from opencensus.trace import config_integration
- Tracer
from opencensus.trace.tracer import Tracer
- ProbabilitySampler
from opencensus.trace.samplers import ProbabilitySampler
- requests
import requests
Quickstart
import os
import requests
from opencensus.trace import config_integration
from opencensus.trace.tracer import Tracer
from opencensus.trace.samplers import ProbabilitySampler
from opencensus.trace.exporters import PrintExporter
# Enable the requests integration
config_integration.trace_integrations(['requests'])
# Configure a tracer with a sampler and an exporter
tracer = Tracer(
sampler=ProbabilitySampler(rate=1.0),
exporter=PrintExporter()
)
print("Making an HTTP request which will be traced...")
with tracer.span(name='parent_span'):
# The requests.get call below will be automatically traced
# and appear as a child span of 'parent_span'.
# Using a non-existent URL for demonstration, a real URL would work similarly.
try:
response = requests.get('http://non-existent-example.com/api/data')
print(f"Request completed with status: {response.status_code}")
except requests.exceptions.ConnectionError as e:
print(f"Connection error: {e}. This is expected for non-existent-example.com")
print("Trace data should have been printed to stdout by PrintExporter.")