Skip to content

Commit 2185bf4

Browse files
committed
Fix NPE in getMdcCopy of LoggingEventInstrumentation
1 parent b999d0f commit 2185bf4

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

dd-java-agent/instrumentation/log4j1/src/main/java/datadog/trace/instrumentation/log4j1/LoggingEventInstrumentation.java

+4
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ public static void onExit(
134134
if (!injectionRequired) {
135135
return;
136136
}
137+
if (mdc == null) {
138+
// this.mdcCopy can be null when MDC.getContext() returns null
139+
return;
140+
}
137141
// at this point the mdc has been shallow copied. No need to replace with a new hashtable.
138142
// Just add our info
139143
String serviceName = Config.get().getServiceName();

dd-java-agent/instrumentation/log4j1/src/test/groovy/MdcTest.groovy

+11-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,16 @@ class MdcTest extends AgentTestRunner {
1616
then:
1717
assert event.getMDC("data") == "dog"
1818
where:
19-
injectionEnabled | _
20-
"true" | _
21-
"false" | _
19+
injectionEnabled << ["true", "false"]
20+
}
21+
22+
def "should prevent NPE when MDC context doesn't exist and getMDCCopy"() {
23+
setup:
24+
injectSysConfig("logs.injection", "true")
25+
def event = new LoggingEvent("test", Category.getRoot(), Priority.INFO, "hello world", null)
26+
when:
27+
event.getMDCCopy()
28+
then:
29+
noExceptionThrown()
2230
}
2331
}

0 commit comments

Comments
 (0)