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

Logging: don't include injected trace context attributes in emitted records

Open Beginner friendly
#5,071 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

What problem do you want to solve?

When setting up opentelemetry-instrumentation-logging and using either the set_logging_format or inject_trace_context arguments, the instrumentor sets up a log record factory such that new logging.LogRecord objects have otelSpanID, otelTraceID, and otelTraceSampled attributes. The log handler set up by logging.basicConfig() or by a user’s on custom logging can then use those attributes when formatting messages. That’s useful, although the names are a little weird for Python (see also #4643).

However, when the opentelemetry.instrumentation.logging.handler.LoggingHandler handler emits an OTel LogRecord, it winds up including these as attributes alongside the standardized trace_id, span_id, and flags fields, which is both duplicative and confusing! When I tried replacing the deprecated log handling from opentelemetry.sdk._logs with this instrumentor, I started seeing these attributes in the monitoring service I send these logs to and thought something was broken because of their names. Eventually I realized they were just duplicative, extra information.

For example, the log data I get when exporting from this instrumentor look like this in the protobuf (using protoc to unpack the bytes sent to the collector):

resource_logs {
  resource {
    // ...a bunch of attributes describing my app...
  }
  scope_logs {
    scope {
      name: "django.request"
    }
    log_records {
      time_unix_nano: 1789677011375527168
      severity_number: SEVERITY_NUMBER_ERROR
      severity_text: "ERROR"
      flags: 3
      trace_id: "q\267\352\003\2269\326\326a\271\214\364/\325\014?"
      span_id: "\010:\336L\177\363Ix"
      observed_time_unix_nano: 1789677011378342341
      body {
        string_value: "Internal Server Error: /.test-error/"
      }
      attributes {
        key: "otelSpanID"
        value {
          string_value: "083ade4c7ff34978"                 // <-- Duplicative of span_id
        }
      }
      attributes {
        key: "otelTraceID"
        value {
          string_value: "71b7ea039639d6d661b98cf42fd50c3f" // <-- Duplicative of trace_id
        }
      }
      attributes {
        key: "otelTraceSampled"
        value {
          bool_value: true                                 // <-- Duplicative of flags (I think?)
        }
      }
      // ...other attributes my app sets...
    }
  }
}
Describe the solution you'd like

It would be nice if these extra, duplicative attributes were not included in the opentelemetry._logs.LogRecord emitted by opentelemetry.instrumentation.logging.handler.LoggingHandler.

Describe alternatives you've considered

You can avoid this by setting set_logging_format=False and inject_trace_context=False, but then this information is not available for other log handlers (e.g. the stderr logger set up by logging.basicConfig()).

Additional Context

No response

Would you like to implement a fix?

None

Tip

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it. Learn more here.

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 instrumentation/opentelemetry-instrumentation-logging/src/opentelemetry/instrumentation/logging/handler.py at the LogRecord emission code linked in the issue. Check how otelSpanID, otelTraceID, and otelTraceSampled become attributes, then verify that emitted records omit those duplicates while retaining standardized trace fields and other application attributes.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
observability-sre
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
75/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.