Skip to content

agent does not export trace_id and span_id mdc values as attributes to collector #14897

@maxxedev

Description

@maxxedev

Describe the bug

Javaagent can be configured to include trace_id and span_id in app's MDC. However, these MDC values are not exported to collector.

Example:
app might log something like:

app-1        | 2025-10-06 04:57:24.022Z [http-nio-8080-exec-1] INFO i.o.example.javagent.Controller - \
trace_id=f0167d362d66c6c76a88ce08b4f4312c, trace_flags=01, span_id=fd55bf7512bc0a69, foo=bar - \ 
A sample log message! 

where trace_id, span_id and foo are MDC names.

foo MDC key-value is exported to collector as attributes but not trace_id/span_id:

collector-1  | Body: Str(A sample log message!)
collector-1  | Attributes:
collector-1  |      -> foo: Str(bar)
                          (trace_id, span_id??)
collector-1  | Trace ID: f0167d362d66c6c76a88ce08b4f4312c
collector-1  | Span ID: fd55bf7512bc0a69
collector-1  | Flags: 1

Steps to reproduce

+++ b/javaagent/docker-compose.yml
@@ -9,6 +9,8 @@ services:
       OTEL_EXPORTER_OTLP_LOGS_ENDPOINT: "http://collector:4318/v1/logs"
       # Logs are disabled by default
       OTEL_LOGS_EXPORTER: "otlp"
+      OTEL_INSTRUMENTATION_LOGBACK_APPENDER_EXPERIMENTAL_CAPTURE_MDC_ATTRIBUTES: "*"
+      LOGGING_PATTERN_CONSOLE: "%d{yyyy-MM-dd HH:mm:ss.SSS'Z',GMT} [%thread] %level %logger{36} - %mdc - %msg %kvp%n"


b/javaagent/src/main/java/io/opentelemetry/example/javagent/Controller.java
@@ -44,8 +45,10 @@ public class Controller {
     Span span = tracer.spanBuilder("doWork").startSpan();
     try (Scope ignored = span.makeCurrent()) {
       Thread.sleep(sleepTime);
+      MDC.put("foo", "bar");
       LOGGER.info("A sample log message!");
     } finally {
+      MDC.remove("foo");
       span.end();
     }
   }

Expected behavior

trace_id, trace_flags and span_id should be exported as attributes

Actual behavior

trace_id, trace_flags and span_id are not exported as attributes

Javaagent or library instrumentation version

2.20.1

Environment

JDK:
OS:

Additional context

No response

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingneeds triageNew issue that requires triage

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions