Skip to content

Fix NPE in getMdcCopy of LoggingEventInstrumentation #8599

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ public static void onExit(
if (!injectionRequired) {
return;
}
if (mdc == null) {
// this.mdcCopy can be null when MDC.getContext() returns null
return;
}
// at this point the mdc has been shallow copied. No need to replace with a new hashtable.
// Just add our info
String serviceName = Config.get().getServiceName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@ class MdcTest extends AgentTestRunner {
then:
assert event.getMDC("data") == "dog"
where:
injectionEnabled | _
"true" | _
"false" | _
injectionEnabled << ["true", "false"]
}

def "should prevent NPE when MDC context doesn't exist and getMDCCopy"() {
setup:
injectSysConfig("logs.injection", "true")
def event = new LoggingEvent("test", Category.getRoot(), Priority.INFO, "hello world", null)
when:
event.getMDCCopy()
then:
noExceptionThrown()
}
}
Loading