-
Notifications
You must be signed in to change notification settings - Fork 881
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
convert Log4j mdc test from groovy to java (#8170)
Related to #7195
- Loading branch information
Showing
5 changed files
with
131 additions
and
106 deletions.
There are no files selected for viewing
29 changes: 0 additions & 29 deletions
29
instrumentation/log4j/log4j-mdc-1.2/javaagent/src/test/groovy/ListAppender.groovy
This file was deleted.
Oops, something went wrong.
76 changes: 0 additions & 76 deletions
76
instrumentation/log4j/log4j-mdc-1.2/javaagent/src/test/groovy/Log4j1MdcTest.groovy
This file was deleted.
Oops, something went wrong.
37 changes: 37 additions & 0 deletions
37
...src/test/java/io/opentelemetry/javaagent/instrumentation/log4j/mdc/v1_2/ListAppender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.log4j.mdc.v1_2; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.apache.log4j.AppenderSkeleton; | ||
import org.apache.log4j.spi.LoggingEvent; | ||
|
||
public class ListAppender extends AppenderSkeleton { | ||
|
||
private static final List<LoggingEvent> events = new ArrayList<LoggingEvent>(); | ||
|
||
@Override | ||
protected void append(LoggingEvent loggingEvent) { | ||
events.add(loggingEvent); | ||
} | ||
|
||
@Override | ||
public boolean requiresLayout() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void close() {} | ||
|
||
public static void clearEvents() { | ||
events.clear(); | ||
} | ||
|
||
public static List<LoggingEvent> getEvents() { | ||
return events; | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
...rc/test/java/io/opentelemetry/javaagent/instrumentation/log4j/mdc/v1_2/Log4j1MdcTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.log4j.mdc.v1_2; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; | ||
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; | ||
import java.util.List; | ||
import org.apache.log4j.LogManager; | ||
import org.apache.log4j.Logger; | ||
import org.apache.log4j.spi.LoggingEvent; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
public class Log4j1MdcTest { | ||
|
||
@RegisterExtension | ||
private static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); | ||
|
||
private static final Logger logger = LogManager.getLogger("TestLogger"); | ||
|
||
@BeforeEach | ||
void cleanup() { | ||
ListAppender.clearEvents(); | ||
} | ||
|
||
@Test | ||
void noIdsWhenNoSpan() { | ||
logger.info("log message 1"); | ||
logger.info("log message 2"); | ||
|
||
List<LoggingEvent> events = ListAppender.getEvents(); | ||
|
||
assertEquals(2, events.size()); | ||
assertEquals("log message 1", events.get(0).getMessage()); | ||
assertNull(events.get(0).getMDC("trace_id")); | ||
assertNull(events.get(0).getMDC("span_id")); | ||
assertNull(events.get(0).getMDC("trace_flags")); | ||
|
||
assertEquals("log message 2", events.get(1).getMessage()); | ||
assertNull(events.get(1).getMDC("trace_id")); | ||
assertNull(events.get(1).getMDC("span_id")); | ||
assertNull(events.get(1).getMDC("trace_flags")); | ||
} | ||
|
||
@Test | ||
void idsWhenSpan() { | ||
Span span1 = | ||
testing.runWithSpan( | ||
"test", | ||
() -> { | ||
logger.info("log message 1"); | ||
return Span.current(); | ||
}); | ||
|
||
logger.info("log message 2"); | ||
|
||
Span span2 = | ||
testing.runWithSpan( | ||
"test 2", | ||
() -> { | ||
logger.info("log message 3"); | ||
return Span.current(); | ||
}); | ||
|
||
List<LoggingEvent> events = ListAppender.getEvents(); | ||
|
||
assertEquals(3, events.size()); | ||
assertEquals("log message 1", events.get(0).getMessage()); | ||
assertEquals(events.get(0).getMDC("trace_id"), span1.getSpanContext().getTraceId()); | ||
assertEquals(events.get(0).getMDC("span_id"), span1.getSpanContext().getSpanId()); | ||
assertEquals(events.get(0).getMDC("trace_flags"), "01"); | ||
|
||
assertEquals("log message 2", events.get(1).getMessage()); | ||
assertNull(events.get(1).getMDC("trace_id")); | ||
assertNull(events.get(1).getMDC("span_id")); | ||
assertNull(events.get(1).getMDC("trace_flags")); | ||
|
||
assertEquals("log message 3", events.get(2).getMessage()); | ||
// this explicit getMDCCopy() call here is to make sure that whole instrumentation is tested | ||
events.get(2).getMDCCopy(); | ||
assertEquals(events.get(2).getMDC("trace_id"), span2.getSpanContext().getTraceId()); | ||
assertEquals(events.get(2).getMDC("span_id"), span2.getSpanContext().getSpanId()); | ||
assertEquals(events.get(2).getMDC("trace_flags"), "01"); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
instrumentation/log4j/log4j-mdc-1.2/javaagent/src/test/resources/log4j.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
log4j.rootLogger=INFO, LIST | ||
log4j.appender.LIST=ListAppender | ||
log4j.appender.LIST=io.opentelemetry.javaagent.instrumentation.log4j.mdc.v1_2.ListAppender |