OpenCensus Runtime Context is a Python library that provides in-process context propagation, using `contextvars` for Python 3.6+ (with asyncio support) and thread-local storage for older Python versions (2.7, 3.4, 3.5). While the `opencensus-python` repository still exists, the broader OpenCensus project has merged with OpenTracing to form OpenTelemetry, which is now the recommended successor for observability. This library's last release was in August 2022, version 0.1.3. [1, 2, 3, 9]
pip install opencensus-contextVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to explicitly propagate the runtime context to a new thread using `RuntimeContext.with_current_context`. By default, context propagation happens automatically within a process, following control flow for threads and asynchronous coroutines. The context is a dictionary-like object. [1, 2]
Plan a migration to OpenTelemetry. Official bridge libraries are available for Python to help with incremental migration. Be aware of potential breaking changes due to semantic conventions, data model differences, and API feature differences. [3, 8, 9, 11]
For Azure Monitor users, migrate to the Azure Monitor OpenTelemetry Distro or the Azure Monitor OpenTelemetry exporters. Note that Microsoft does not recommend or support the OpenTelemetry OpenCensus shim for Azure Monitor users. [14, 16]
Ensure that your target Python version's context propagation mechanism is understood, especially when dealing with concurrent or asynchronous code. For Python < 3.6, explicit context propagation might be more frequently required in complex scenarios.
Upgrade your Python environment to 3.7 or later before attempting to migrate from OpenCensus to OpenTelemetry. If an upgrade is not feasible, OpenTelemetry solutions will not be officially supported.
Use `RuntimeContext.set_opencensus_context('key', value)` and `RuntimeContext.get_opencensus_context('key')` to manage context variables.Ensure the library is installed using pip: `pip install opencensus-context` or `pip install opencensus` (as `opencensus-context` is usually installed with `opencensus`).
Install the main OpenCensus library: `pip install opencensus`.
Downgrade conflicting dependencies, for example, a common fix is to downgrade 'protobuf' to a version compatible with your 'opencensus' and related 'opencensus-ext' libraries, e.g., `pip install protobuf==3.20.3` or `pip install google-api-core==2.9.0`.
Try installing without `--no-binary` if it's being used. Ensure `setuptools` is up-to-date (`pip install --upgrade setuptools`). If packaging for a specific distribution, check for available source distributions or pre-built wheels.
Correct the import statement to `from opencensus.common.runtime_context import RuntimeContext`.