Semi-automatic
Python (logging)

Python Semi-Automatic Instrumentation

The OpenTelemetry auto-instrumentation (opens in a new tab) for Python enables traces with minimal effort, but logging and custom metrics require additional configuration.

Enabling Auto-Instrumentation for Logs

If you are using our recommended Instrumentaton, you should not need to do anything to enable auto-instrumentation for logs. Ironically, once you have enabled the auto-instrumentation, you will need to explicitly configure logging to write logs to stderr to ensure that logs continue to be displayed on the console.

Setting Up stderr Logging in Python

By default, Python installs a handler that writes logs to stderr if there are no handlers configured. However, as the OpenTelemetry Operator will inject a handler to send logs to the collector, you must explicitly configure a handler to write logs to stderr.

import logging
import sys
 
# By default, Python logs only at WARN level and higher.  If
# you want to capture INFO level logs, you must set the root
# logger's level to INFO.
logger = logging.getLogger()
logger.setLevel(logging.INFO)
 
# Create and configure a StreamHandler for stderr.  You can also
# adjust the level and format of the logs if you wish.
console_handler = logging.StreamHandler(sys.stderr)
console_handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
 
# Use the logger as usual
 

Environment Variables

When using auto-instrumentation via the OpenTelemetry Operator (opens in a new tab), you should not have to set any environment variables as the operator will automatically inject the necessary environment variables into your application's containers.

Setting some of the OTEL_* environment variables manually can cause conflicts with the auto-instrumentation. For example, changing the endpoint or protocol for the OTLP exporter can cause the auto-instrumentation to fail.