{"id":7468,"library":"opencensus-ext-requests","title":"OpenCensus Requests Integration","description":"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.","status":"maintenance","version":"0.8.0","language":"en","source_language":"en","source_url":"https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-requests","tags":["tracing","monitoring","requests","http","observability","deprecated"],"install":[{"cmd":"pip install opencensus-ext-requests","lang":"bash","label":"Install package"}],"dependencies":[{"reason":"This extension instruments the 'requests' library.","package":"requests","optional":false},{"reason":"Core OpenCensus library required for tracing functionality.","package":"opencensus","optional":false}],"imports":[{"note":"Used to enable the Requests integration.","symbol":"config_integration","correct":"from opencensus.trace import config_integration"},{"note":"Essential for creating spans and managing the trace context.","symbol":"Tracer","correct":"from opencensus.trace.tracer import Tracer"},{"note":"Commonly used sampler for controlling trace collection rate.","symbol":"ProbabilitySampler","correct":"from opencensus.trace.samplers import ProbabilitySampler"},{"note":"The library being instrumented.","symbol":"requests","correct":"import requests"}],"quickstart":{"code":"import os\nimport requests\nfrom opencensus.trace import config_integration\nfrom opencensus.trace.tracer import Tracer\nfrom opencensus.trace.samplers import ProbabilitySampler\nfrom opencensus.trace.exporters import PrintExporter\n\n# Enable the requests integration\nconfig_integration.trace_integrations(['requests'])\n\n# Configure a tracer with a sampler and an exporter\ntracer = Tracer(\n    sampler=ProbabilitySampler(rate=1.0),\n    exporter=PrintExporter()\n)\n\nprint(\"Making an HTTP request which will be traced...\")\nwith tracer.span(name='parent_span'):\n    # The requests.get call below will be automatically traced\n    # and appear as a child span of 'parent_span'.\n    # Using a non-existent URL for demonstration, a real URL would work similarly.\n    try:\n        response = requests.get('http://non-existent-example.com/api/data')\n        print(f\"Request completed with status: {response.status_code}\")\n    except requests.exceptions.ConnectionError as e:\n        print(f\"Connection error: {e}. This is expected for non-existent-example.com\")\n\nprint(\"Trace data should have been printed to stdout by PrintExporter.\")","lang":"python","description":"This quickstart demonstrates how to enable the `requests` integration, initialize a basic OpenCensus tracer, and perform an HTTP request. The `PrintExporter` is used to output trace data directly to the console for easy verification. The `config_integration.trace_integrations(['requests'])` call is crucial for automatically instrumenting `requests` calls."},"warnings":[{"fix":"Migrate your instrumentation to OpenTelemetry. Official bridge libraries are available to facilitate a gradual migration.","message":"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.","severity":"breaking","affected_versions":"All versions"},{"fix":"Use an asynchronous exporter transport like `AsyncTransport` or avoid enabling both `threading` and `requests` integrations simultaneously if `SyncTransport` is necessary.","message":"When combining `opencensus-ext-requests` and the `threading` integration with a synchronous exporter transport (e.g., `SyncTransport`), the application may freeze or hang indefinitely.","severity":"gotcha","affected_versions":"<=0.8.0"},{"fix":"Implement a telemetry processor to override `envelope.data.baseData.type` to 'HTTP' for relevant spans, as detailed in various community discussions.","message":"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.","severity":"gotcha","affected_versions":"<=0.8.0"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure `opencensus.trace.config_integration.trace_integrations(['requests'])` is called before making `requests` calls and after initializing the tracer.","cause":"The `requests` integration has not been enabled or configured correctly.","error":"HTTP requests made with 'requests' are not appearing in traces."},{"fix":"Switch your exporter to use an asynchronous transport (e.g., `AsyncTransport`) or review your threading/requests integration strategy if `SyncTransport` is unavoidable.","cause":"This often occurs when the `opencensus-ext-requests` integration is used in conjunction with the `threading` integration and a synchronous exporter transport (`SyncTransport`).","error":"Application hangs or freezes when performing HTTP requests using the 'requests' library."},{"fix":"Ensure that `AzureLogHandler` is added to your logger only once during application initialization. Check for multiple instantiations or reconfigurations of the logger.","cause":"The `AzureLogHandler` is likely being added multiple times to the same logger instance, a common issue with older or deprecated libraries.","error":"Seeing duplicate log entries when using `opencensus-ext-azure` and a logger configured with `AzureLogHandler`."}]}