open-telemetry / open-telemetry/opentelemetry-python-contrib

Python enable integration?

Open
#1,898 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
1.1k
Forks
1.1k
Avg merge
4d 15h
Merged PRs (30d)
16

Description

I'm using Python 3.9.5, collecting logs using the default logging library (and uvicorn) and sending logs to GCP.

I'm trying to connect OT's SpanID and TraceID data into our logging, but am struggling to make sense of these instructions without additional context:
"If you do not set OTEL_PYTHON_LOG_CORRELATION to true but instead set the logging format manually or through your framework, you must ensure that this integration is enabled before you set the logging format" This is important because unless the integration is enabled, the tracing context variables are not injected into the log record objects. This means any attempted log statements made after setting the logging format and before enabling this integration will result in KeyError exceptions. Such exceptions are automatically swallowed by the logging module and do not result in crashes but you may still lose out on important log messages.
[https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/logging/logging.html#envvar-OTEL_PYTHON_LOG_CORRELATION]

**A few questions:

  • Where in this below block of code should I insert "OTEL_PYTHON_LOG_CORRELATION": True, and how?
  • Alternatively, how should I enable the integration prior to setting a logging format please?**

This is my logging code at present:


`import logging
import logging.config

_logger = logging.getLogger(__name__)

def configure_logging(settings: AppSettings) -> None:
root = logging.getLogger()

if root.handlers:
    root.handlers.clear()

logging_config = {
    "version": 1,
    "disable_existing_loggers": True,
     
    "formatters": {
        "standard": {"%(asctime)s %(levelname)s [%(name)s] [%(filename)s:%(lineno)d] "
                    "[trace_id=%(otelTraceID)s span_id=%(otelSpanID)s "
                    "resource.service.name=%(otelServiceName)s "
                    "trace_sampled=%(otelTraceSampled)s] - %(message)s"
                    }
    },
    
    "handlers": {
        "default": {
            "level": settings.LOGGER_LEVEL,
            "formatter": "standard",
            "class": "google.cloud.logging_v2.handlers.StructuredLogHandler",
            #"OTEL_PYTHON_LOG_CORRELATION": True,
        },
    },
    "loggers": {
        "": {"handlers": ["default"], "level": settings.LOGGER_LEVEL, "propagate": False},  # root logger
        "example1": {"handlers": ["default"], "level": settings.LOGGER_LEVEL, "propagate": False},
        "example2": {"handlers": ["default"], "level": settings.LOGGER_LEVEL, "propagate": False},
        "example3": {"handlers": ["default"], "level": settings.LOGGER_LEVEL, "propagate": False},
    },
}

logging.config.dictConfig(logging_config)`

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the OpenTelemetry Python logging integration documentation linked in the issue, especially the OTEL_PYTHON_LOG_CORRELATION section. Review the configure_logging function and its logging.config.dictConfig block to determine what context a user needs before setting the format. Done means the documentation clearly explains both requested configuration approaches and their ordering.

Written by the indexing model from the issue text.

Assessment

Tech stack
gcp, python
Domain
documentation, observability
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.