Skip to content

Add opt-in text log pattern injection for Logback and Log4j - #21261

Open
lrwh wants to merge 7 commits into
open-telemetry:mainfrom
lrwh:feat/log-pattern-injection
Open

lrwh wants to merge 7 commits into
open-telemetry:mainfrom
lrwh:feat/log-pattern-injection

Conversation

@lrwh

@lrwh lrwh commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Add opt-in correlation fields to existing Logback and Log4j text patterns so console and file logs can be correlated without editing each application's logging configuration.

otel.instrumentation.logging.pattern-injection.enabled=true

Disabled by default. Supports Logback classic 1.0+, Log4j 1.2, and Log4j2 2.7+. Reuses existing MDC converters and configured trace/span key names, preserves timestamp options and repeated layout initialization, and exposes the resource's service.name outside a span. Application-provided correlation values take precedence. JSON layouts and Log4j2 PatternSelector branches are excluded.

Closes #21260.

Validated the parser and baseline/latest-dependency check tasks for all five logging modules on JDK 25, including focused parser and Logback checks for native default-key semantics. On Java 8, 102 installed-agent tests pass; six cases requiring newer Logback color converters are skipped. Regression coverage includes whole-line wrappers, literal braces in Logback composites, version-specific MDC defaults, native escaped keys, special correlation keys, key lists and wrapper aliases, repeated initialization, and original-MDC export with pattern injection enabled.

@lrwh
lrwh requested a review from a team as a code owner November 9, 2026 03:09
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Waiting on the author · refreshed 2026-11-10 05:05 UTC

Respond to 2 review items (e.g. link a commit, explain why not, ask a follow-up):

  • Inline threads: 1, 2
Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Should this be with reviewers? Comment /dashboard route:reviewers to route it to them.
  • Anything wrong — including the routing? Report it with what you expected; it helps us improve the dashboard.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Log-pattern handling can place Log4j2 correlation fields on the wrong line, omit Logback trace IDs for comma-containing keys, and duplicate correlation fields inside Log4j2 color wrappers.

3 open issues
What changed in this PR

Adds opt-in text log correlation to the Java agent’s existing Logback and Log4j instrumentation, addressing #21260 without requiring application logging-pattern changes.

Changes:

  • Adds a shared pattern rewriter and framework-specific layout instrumentation, disabled by default.
  • Exposes service.name outside spans and preserves application-provided correlation values.
  • Adds parser and installed-agent tests, configuration metadata, and usage documentation.
File Description
javaagent-extension-api/​src/​test/​java/​io/​opentelemetry/​javaagent/​bootstrap/​internal/​LogPatternInjectionTest.java Tests pattern parsing and repeated enhancement.
javaagent-extension-api/​src/​main/​java/​io/​opentelemetry/​javaagent/​bootstrap/​internal/​LogPatternInjection.java Adds shared configuration and pattern rewriting.
javaagent-extension-api/​src/​main/​java/​io/​opentelemetry/​javaagent/​bootstrap/​internal/​ConfiguredResourceAttributesHolder.java Exposes service name when injection is enabled.
instrumentation/​logback/​logback-mdc-1.0/​metadata.yaml References the shared setting.
instrumentation/​logback/​logback-mdc-1.0/​javaagent/​src/​test/​java/​io/​opentelemetry/​javaagent/​instrumentation/​logback/​v1_0/​LogPatternTest.java Tests Logback rendering and application overrides.
instrumentation/​logback/​logback-mdc-1.0/​javaagent/​src/​main/​java/​io/​opentelemetry/​javaagent/​instrumentation/​logback/​mdc/​v1_0/​PatternLayoutInstrumentation.java Enhances Logback patterns during startup.
instrumentation/​logback/​logback-mdc-1.0/​javaagent/​src/​main/​java/​io/​opentelemetry/​javaagent/​instrumentation/​logback/​mdc/​v1_0/​LoggingEventInstrumentation.java Preserves existing MDC values during enrichment.
instrumentation/​logback/​logback-mdc-1.0/​javaagent/​src/​main/​java/​io/​opentelemetry/​javaagent/​instrumentation/​logback/​mdc/​v1_0/​LogbackMdcInstrumentationModule.java Registers layout instrumentation.
instrumentation/​logback/​logback-mdc-1.0/​javaagent/​README.md Documents the opt-in setting.
instrumentation/​logback/​logback-mdc-1.0/​javaagent/​build.gradle.kts Adds injection and custom-key test variants.
instrumentation/​log4j/​log4j-mdc-1.2/​metadata.yaml References the shared setting.
instrumentation/​log4j/​log4j-mdc-1.2/​javaagent/​src/​test/​java/​io/​opentelemetry/​javaagent/​instrumentation/​log4j/​mdc/​v1_2/​LogPatternTest.java Tests Log4j 1 rendering and repeated initialization.
instrumentation/​log4j/​log4j-mdc-1.2/​javaagent/​src/​main/​java/​io/​opentelemetry/​javaagent/​instrumentation/​log4j/​mdc/​v1_2/​PatternLayoutInstrumentation.java Enhances constructor and setter patterns.
instrumentation/​log4j/​log4j-mdc-1.2/​javaagent/​src/​main/​java/​io/​opentelemetry/​javaagent/​instrumentation/​log4j/​mdc/​v1_2/​LoggingEventInstrumentation.java Enriches whole-MDC properties when enabled.
instrumentation/​log4j/​log4j-mdc-1.2/​javaagent/​src/​main/​java/​io/​opentelemetry/​javaagent/​instrumentation/​log4j/​mdc/​v1_2/​Log4j1PatternInjection.java Detects native whole-MDC converter support.
instrumentation/​log4j/​log4j-mdc-1.2/​javaagent/​src/​main/​java/​io/​opentelemetry/​javaagent/​instrumentation/​log4j/​mdc/​v1_2/​Log4j1InstrumentationModule.java Registers layout instrumentation.
instrumentation/​log4j/​log4j-mdc-1.2/​javaagent/​README.md Documents the opt-in setting.
instrumentation/​log4j/​log4j-mdc-1.2/​javaagent/​build.gradle.kts Adds injection and custom-key test variants.
instrumentation/​log4j/​log4j-context-data/​log4j-context-data-common/​testing/​src/​main/​java/​io/​opentelemetry/​instrumentation/​log4j/​contextdata/​LogPatternTest.java Shares Log4j2 rendering and selector-exclusion tests.
instrumentation/​log4j/​log4j-context-data/​log4j-context-data-common/​testing/​src/​main/​java/​io/​opentelemetry/​instrumentation/​log4j/​contextdata/​Log4j2Test.java Tests application-value precedence.
instrumentation/​log4j/​log4j-context-data/​log4j-context-data-2.7/​metadata.yaml References the shared setting.
instrumentation/​log4j/​log4j-context-data/​log4j-context-data-2.7/​javaagent/​src/​test/​java/​io/​opentelemetry/​javaagent/​instrumentation/​log4j/​contextdata/​v2_7/​AutoLogPatternTest.java Runs shared tests with the installed agent.
instrumentation/​log4j/​log4j-context-data/​log4j-context-data-2.7/​javaagent/​src/​main/​java/​io/​opentelemetry/​javaagent/​instrumentation/​log4j/​contextdata/​v2_7/​SpanDecoratingContextDataInjector.java Preserves application context-data values.
instrumentation/​log4j/​log4j-context-data/​log4j-context-data-2.7/​javaagent/​src/​main/​java/​io/​opentelemetry/​javaagent/​instrumentation/​log4j/​contextdata/​v2_7/​PatternLayoutInstrumentation.java Enhances builder patterns without selectors.
instrumentation/​log4j/​log4j-context-data/​log4j-context-data-2.7/​javaagent/​src/​main/​java/​io/​opentelemetry/​javaagent/​instrumentation/​log4j/​contextdata/​v2_7/​Log4j27InstrumentationModule.java Registers layout instrumentation.
instrumentation/​log4j/​log4j-context-data/​log4j-context-data-2.7/​javaagent/​build.gradle.kts Adds injection and custom-key test variants.
instrumentation/​log4j/​log4j-context-data/​log4j-context-data-2.17/​metadata.yaml References the shared setting.
instrumentation/​log4j/​log4j-context-data/​log4j-context-data-2.17/​library-autoconfigure/​src/​main/​java/​io/​opentelemetry/​instrumentation/​log4j/​contextdata/​v2_17/​OpenTelemetryContextDataProvider.java Preserves application ThreadContext values.
instrumentation/​log4j/​log4j-context-data/​log4j-context-data-2.17/​javaagent/​src/​test/​java/​io/​opentelemetry/​javaagent/​instrumentation/​log4j/​contextdata/​v2_17/​AutoLogPatternTest.java Runs shared tests with the installed agent.
instrumentation/​log4j/​log4j-context-data/​log4j-context-data-2.17/​javaagent/​src/​main/​java/​io/​opentelemetry/​javaagent/​instrumentation/​log4j/​contextdata/​v2_17/​PatternLayoutInstrumentation.java Enhances builder patterns without selectors.
instrumentation/​log4j/​log4j-context-data/​log4j-context-data-2.17/​javaagent/​src/​main/​java/​io/​opentelemetry/​javaagent/​instrumentation/​log4j/​contextdata/​v2_17/​Log4j2InstrumentationModule.java Registers layout instrumentation.
instrumentation/​log4j/​log4j-context-data/​log4j-context-data-2.17/​javaagent/​README.md Documents the opt-in setting.
instrumentation/​log4j/​log4j-context-data/​log4j-context-data-2.17/​javaagent/​build.gradle.kts Adds variants while retaining thread-local coverage.
instrumentation-docs/​src/​main/​resources/​shared-config-definitions.yaml Defines shared configuration metadata.
docs/​logging-pattern-injection.md Explains setup, supported layouts, and limitations.

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Logback MDC keys containing commas and both quote characters are quoted without escaping embedded delimiters, preventing correlation-value lookup and duplicating fields on repeated initialization.

1 open issue
3 resolved since last review

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Configured trace/span keys are inserted verbatim into Log4j labels and MDC options, allowing percent signs to split log records and commas to cause trace IDs to disappear.

No open issues 🎉

2 previously missed issues

In code that hasn't changed since last review

Medium severity Handle commas in custom Log4j2 MDC keys

javaagent-extension-api/​src/​main/​java/​io/​opentelemetry/​javaagent/​bootstrap/​internal/​LogPatternInjection.java:72

[Javaagent] Passing the configured key verbatim is unsafe for Log4j2 MDC options. A trace key such as custom,trace becomes %X{custom,trace}, which selects two MDC entries rather than the single custom,trace entry populated by the instrumentation; the trace ID therefore disappears from the injected output. Add a safe option/key-resolution mechanism for Log4j2, analogous to the Logback alias handling, and exercise a comma-containing key in the installed-agent rendering tests.

Medium severity Escape percent signs in Log4j labels for custom trace keys

javaagent-extension-api/​src/​main/​java/​io/​opentelemetry/​javaagent/​bootstrap/​internal/​LogPatternInjection.java:77

[Javaagent] A configured trace/span key containing %, such as trace%n, is copied verbatim into the Log4j label, where %n becomes an extra newline and splits the log record. Escape literal percent signs as %% in Log4j labels while retaining the original key inside the MDC option. Add a rendering regression test for these custom keys.

1 resolved since last review

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed because framework-specific parsing, escaping, and wrapper handling can omit correlation fields or break log-record boundaries.

No open issues 🎉

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Pattern parsing can place correlation fields after newlines and misidentify Logback MDC keys, producing malformed log output and missing or duplicate trace IDs.

4 open issues

Review effort: Balanced

Comment on lines +120 to +122
return pattern.substring(0, end)
+ " "
+ fields
Comment on lines +205 to +207
if ((option.equals(key) && (framework != Framework.LOG4J2 || option.indexOf(',') <= 0))
|| (framework == Framework.LOGBACK && option.startsWith(key + ":-"))
|| (framework == Framework.LOGBACK && option.isEmpty())) {
if (options.charAt(i) == '\\') {
i++;
} else if (options.charAt(i) == quote) {
return options.substring(1, i);
Comment on lines +348 to +349
} else if (framework == Framework.LOGBACK && open == '(' && c == '{') {
i = logbackOptionEnd(pattern, i) - 1;

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add opt-in correlation fields to text log patterns

2 participants