OpenCensus Logging Integration
This library provides an integration for the OpenCensus tracing framework with Python's standard `logging` module. It enriches standard log records with `traceId`, `spanId`, and `traceSampled` attributes, enabling correlation of application logs with distributed traces. The current version is 0.1.1. The OpenCensus project is largely deprecated in favor of OpenTelemetry, with official support for some related exporters (e.g., Azure Monitor) ending by September 2024.
Warnings
- breaking The OpenCensus project, including `opencensus-ext-logging`, has been officially deprecated in favor of OpenTelemetry. OpenCensus will no longer receive new features or security updates, and users are strongly encouraged to migrate.
- deprecated OpenCensus Azure Monitor exporters, which commonly leverage `opencensus-ext-logging` for log correlation, are also deprecated and will be officially unsupported by September 2024.
- gotcha The `opencensus-ext-logging` integration only affects Python `logging` instances created *after* `config_integration.trace_integrations(['logging'])` has been called. Loggers initialized before this configuration will not be enriched.
- gotcha When using `opencensus-ext-logging` in conjunction with certain `logging.Handler` implementations (e.g., `AzureLogHandler` from `opencensus-ext-azure`), there have been reports of recursive logging issues, which can lead to excessive logging or application crashes.
Install
-
pip install opencensus-ext-logging
Imports
- config_integration
from opencensus.trace import config_integration
Quickstart
import logging
import os
from opencensus.trace import config_integration
from opencensus.trace import tracer as tracer_module
from opencensus.trace.exporters import PrintExporter
from opencensus.trace.samplers import AlwaysOnSampler
# 1. Configure the logging integration
# This must be done *before* loggers are created for existing loggers to be affected.
config_integration.trace_integrations(['logging'])
# 2. Set up a basic Python logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - TraceId: %(traceId)s - SpanId: %(spanId)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
# 3. Set up an OpenCensus tracer with an exporter (e.g., PrintExporter)
# This will activate trace context, making trace and span IDs available for logging.
exporter = PrintExporter()
sampler = AlwaysOnSampler()
tracer = tracer_module.Tracer(exporter=exporter, sampler=sampler)
logger.info("This log message will not have trace/span IDs yet as no span is active.")
# 4. Use the tracer to create a span to enable trace context propagation
with tracer.span(name='my_operation') as span:
logger.info("This log message should now have trace and span IDs.")
with tracer.span(name='sub_operation'):
logger.warning("Another log message within a sub-span.")
logger.info("This log message is outside the span and will not have trace/span IDs from the active span.")