Skip to content

Add literal Context Data (MDC) filtering to JSON Template Layout - #4186

Open
ramanathan1504 wants to merge 12 commits into
2.xfrom
fix/issue-4166
Open

Add literal Context Data (MDC) filtering to JSON Template Layout#4186
ramanathan1504 wants to merge 12 commits into
2.xfrom
fix/issue-4166

Conversation

@ramanathan1504

@ramanathan1504 ramanathan1504 commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Add allowed/disallowed literal key filters to JSON Template Layout's (Context Data) MDC and Map resolvers.

Update EcsLayout.json to prevent MDC entries from overwriting structural ECS fields.

Fixes #4166.

@ppkarwasz ppkarwasz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hi @ramanathan1504,

Looks good to me, with two small comments:

@github-project-automation github-project-automation Bot moved this to Changes requested in Log4j pull request tracker Jul 22, 2026
@ramanathan1504

Copy link
Copy Markdown
Contributor Author

@vy done — keyExcludes is gone, it's literal with allowed and disallowed now, and the grammar block in the ReadOnlyStringMapResolver javadoc is updated to match the one u pasted.

Two things your example didn't pin down: disallowed currently wins over allowed, and when pattern is also set a key has to satisfy both — is that what u meant, or should allowed widen the pattern instead?

@ramanathan1504

Copy link
Copy Markdown
Contributor Author

@ppkarwasz reworked to the literal grammar @vy proposed on the issue, both your comments are still in — the sets are Collections.emptySet() when absent with @NonNull on the fields, and the benchmark compares two full layouts: 4.562 vs 1.833 ops/us.

A mis-shaped literal now throws the way flatten does, but a typo'd sub-key (disalowed) still parses and silently filters nothing — worth rejecting unknown keys under literal, or leave it as is?

Comment thread src/changelog/.2.x.x/4166_add_mdc_literal_key_filter.xml Outdated
@vy vy changed the title add keyExcludes to MDC resolvers to prevent overwriting of structural… Add literal Context Data (MDC) filtering to JSON Template Layout Sep 2, 2026
@vy
vy enabled auto-merge (squash) September 2, 2026 10:21
Comment on lines +30 to +41
"literal": {
"disallowed": [
"@timestamp",
"message",
"log.logger",
"log.level",
"event.dataset",
"process.thread.name",
"process.thread.id",
"ecs.version"
]
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Code looks good, but the naming for the configuration option seems odd. What about filter or keyFilter? The word literal doesn't tell me much, what this could do.

{
  "filter": {
    "includes": [
      "@timestamp",
      ...
    ]
  }
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd not use the word filter, it doesn't match what we already have: key and pattern, which both "filter".

@ramanathan1504, we had an internal conversation with @ppkarwasz, and agreed to overload key instead. That is, if key is of type string, use the existing behavior, if it is of type object, use the new semantics with allowed/disallowed. Would you mind updating the PR in that direction, please?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@vy de4101c literal is gone, key takes a string (unchanged) or an object with allowed/disallowed. The object form combines with pattern/flatten and disallowed wins over allowed. tell me if u meant allowed to widen the pattern.

@github-project-automation github-project-automation Bot moved this from Changes requested to Approved in Log4j pull request tracker Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

JsonTemplateLayout: add MDC key filtering to ThreadContextMapResolver and harden EcsLayout.json

3 participants