OpenCensus Runtime Context

0.1.3 · deprecated · verified Sun Mar 29

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]

Warnings

Install

Imports

Quickstart

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]

from threading import Thread
from opencensus.common.runtime_context import RuntimeContext

def work(name):
    # The context is automatically propagated to new threads started via with_current_context
    current_context = RuntimeContext.current_context()
    print(f"Worker '{name}' in context: {current_context}")

# Create a context on the main thread
RuntimeContext.current_context()['request_id'] = 'main_req_123'

# Propagate context explicitly to a new thread
thread = Thread(
    target=RuntimeContext.with_current_context(work),
    args=('child_thread_A',),
)
thread.start()
thread.join()

# Another example with a simple function call in the current context
def another_work(name):
    current_context = RuntimeContext.current_context()
    print(f"Another worker '{name}' in context: {current_context}")

another_work('direct_call_B')

view raw JSON →