-
Notifications
You must be signed in to change notification settings - Fork 337
Migrate dd-trace-ot tests to JUnit 5 #11483
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
Open
amarziali
wants to merge
5
commits into
master
Choose a base branch
from
andrea.marziali/migrate-junit-ddtrace-ot
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
74 changes: 0 additions & 74 deletions
74
dd-trace-ot/correlation-id-injection/src/test/groovy/CorrelationIdInjectorTest.groovy
This file was deleted.
Oops, something went wrong.
64 changes: 0 additions & 64 deletions
64
dd-trace-ot/correlation-id-injection/src/test/groovy/Log4j2CorrelationIdInjectorTest.groovy
This file was deleted.
Oops, something went wrong.
63 changes: 0 additions & 63 deletions
63
dd-trace-ot/correlation-id-injection/src/test/groovy/Slf4jCorrelationIdInjectorTest.groovy
This file was deleted.
Oops, something went wrong.
80 changes: 80 additions & 0 deletions
80
dd-trace-ot/correlation-id-injection/src/test/java/CorrelationIdInjectorTest.java
This file contains hidden or 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,80 @@ | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import datadog.opentracing.DDTracer; | ||
| import datadog.trace.api.CorrelationIdentifier; | ||
| import datadog.trace.api.GlobalTracer; | ||
| import datadog.trace.test.util.DDJavaSpecification; | ||
| import io.opentracing.Scope; | ||
| import io.opentracing.Span; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| abstract class CorrelationIdInjectorTest extends DDJavaSpecification { | ||
|
|
||
| protected String logPattern = | ||
| "TRACE_ID=%X{" | ||
| + CorrelationIdentifier.getTraceIdKey() | ||
| + "} SPAN_ID=%X{" | ||
| + CorrelationIdentifier.getSpanIdKey() | ||
| + "} %m"; | ||
|
|
||
| @Test | ||
| void testCorrelationIdInjection() throws Exception { | ||
| DDTracer tracer = buildTracer(); | ||
| LogJournal journal = buildJournal(); | ||
| TestLogger logger = buildLogger(); | ||
|
|
||
| logger.log("Event without context"); | ||
|
|
||
| assertEquals("TRACE_ID= SPAN_ID= Event without context", journal.nextLog()); | ||
|
|
||
| Span rootSpan = tracer.buildSpan("operation1").start(); | ||
| Scope rootScope = tracer.activateSpan(rootSpan); | ||
| logger.log("Event with root span context"); | ||
|
|
||
| assertEquals(expectedLog("Event with root span context"), journal.nextLog()); | ||
|
|
||
| Span childSpan = tracer.buildSpan("operation1").asChildOf(rootSpan).start(); | ||
| Scope childScope = tracer.activateSpan(childSpan); | ||
| logger.log("Event with child span context"); | ||
|
|
||
| assertEquals(expectedLog("Event with child span context"), journal.nextLog()); | ||
|
|
||
| childScope.close(); | ||
| childSpan.finish(); | ||
| logger.log("Event with root span context"); | ||
|
|
||
| assertEquals(expectedLog("Event with root span context"), journal.nextLog()); | ||
|
|
||
| rootScope.close(); | ||
| rootSpan.finish(); | ||
| logger.log("Event without context"); | ||
|
|
||
| assertEquals("TRACE_ID= SPAN_ID= Event without context", journal.nextLog()); | ||
|
|
||
| tracer.close(); | ||
| } | ||
|
|
||
| private static String expectedLog(String message) { | ||
| return String.format( | ||
| "TRACE_ID=%s SPAN_ID=%s %s", | ||
| CorrelationIdentifier.getTraceId(), CorrelationIdentifier.getSpanId(), message); | ||
| } | ||
|
|
||
| DDTracer buildTracer() { | ||
| DDTracer tracer = new DDTracer.DDTracerBuilder().build(); | ||
| GlobalTracer.registerIfAbsent(tracer); | ||
| return tracer; | ||
| } | ||
|
|
||
| abstract LogJournal buildJournal(); | ||
|
|
||
| abstract TestLogger buildLogger(); | ||
|
|
||
| interface LogJournal { | ||
| String nextLog(); | ||
| } | ||
|
|
||
| interface TestLogger { | ||
| void log(String message); | ||
| } | ||
| } | ||
70 changes: 70 additions & 0 deletions
70
dd-trace-ot/correlation-id-injection/src/test/java/Log4j2CorrelationIdInjectorTest.java
This file contains hidden or 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,70 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import org.apache.logging.log4j.Level; | ||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.apache.logging.log4j.core.Appender; | ||
| import org.apache.logging.log4j.core.Filter; | ||
| import org.apache.logging.log4j.core.LogEvent; | ||
| import org.apache.logging.log4j.core.LoggerContext; | ||
| import org.apache.logging.log4j.core.appender.AbstractAppender; | ||
| import org.apache.logging.log4j.core.config.Configuration; | ||
| import org.apache.logging.log4j.core.config.LoggerConfig; | ||
| import org.apache.logging.log4j.core.layout.PatternLayout; | ||
|
|
||
| class Log4j2CorrelationIdInjectorTest extends CorrelationIdInjectorTest { | ||
|
|
||
| @Override | ||
| LogJournal buildJournal() { | ||
| LoggerContext context = LoggerContext.getContext(false); | ||
| Configuration config = context.getConfiguration(); | ||
|
|
||
| TestAppender appender = | ||
| new TestAppender(PatternLayout.newBuilder().withPattern(logPattern).build()); | ||
| appender.start(); | ||
| config.addAppender(appender); | ||
| updateLoggers(appender, config); | ||
| return appender; | ||
| } | ||
|
|
||
| @Override | ||
| TestLogger buildLogger() { | ||
| Logger logger = LogManager.getLogger("TestLogger"); | ||
| return message -> logger.error(message); | ||
| } | ||
|
|
||
| private static void updateLoggers(Appender appender, Configuration config) { | ||
| Level level = null; | ||
| Filter filter = null; | ||
| for (LoggerConfig loggerConfig : config.getLoggers().values()) { | ||
| loggerConfig.addAppender(appender, level, filter); | ||
| } | ||
| config.getRootLogger().addAppender(appender, level, filter); | ||
| } | ||
|
|
||
| static class TestAppender extends AbstractAppender | ||
| implements CorrelationIdInjectorTest.LogJournal { | ||
| List<String> events; | ||
| int read; | ||
|
|
||
| protected TestAppender(PatternLayout patternLayout) { | ||
| super("TestAppender", null, patternLayout, false, null); | ||
| events = new ArrayList<>(); | ||
| read = 0; | ||
| } | ||
|
|
||
| @Override | ||
| public void append(LogEvent event) { | ||
| String log = ((PatternLayout) getLayout()).toSerializable(event); | ||
| events.add(log); | ||
| } | ||
|
|
||
| @Override | ||
| public String nextLog() { | ||
| if (events.size() <= read) { | ||
| return null; | ||
| } | ||
| return events.get(read++); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I think it is a const now?
if yes, it should be
protected static final String LOG_PATTERN = ....