-
Notifications
You must be signed in to change notification settings - Fork 873
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
convert jboss-logmanager-mdc groovy test to java (#7703)
the `getMdcCopy` test is omitted as it is only available in jboss-logmanager 1.3+ and the test depends on jboss-logmanager 1.1 --------- Co-authored-by: opentelemetrybot <[email protected]>
- Loading branch information
1 parent
8d74cf1
commit a5143d6
Showing
2 changed files
with
133 additions
and
113 deletions.
There are no files selected for viewing
113 changes: 0 additions & 113 deletions
113
...gmanager/jboss-logmanager-mdc-1.1/javaagent/src/test/groovy/JbossLogmanagerMdcTest.groovy
This file was deleted.
Oops, something went wrong.
133 changes: 133 additions & 0 deletions
133
...ntelemetry/javaagent/instrumentation/jbosslogmanager/mdc/v1_1/JbossLogmanagerMdcTest.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,133 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.jbosslogmanager.mdc.v1_1; | ||
|
||
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; | ||
|
||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification; | ||
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; | ||
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.logging.Handler; | ||
import java.util.logging.LogRecord; | ||
import org.jboss.logmanager.ExtLogRecord; | ||
import org.jboss.logmanager.Level; | ||
import org.jboss.logmanager.LogContext; | ||
import org.jboss.logmanager.Logger; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
public class JbossLogmanagerMdcTest extends AgentInstrumentationSpecification { | ||
|
||
@RegisterExtension | ||
static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); | ||
|
||
static class LogHandler extends Handler { | ||
public List<ExtLogRecord> logRecords; | ||
|
||
LogHandler(LinkedList<ExtLogRecord> logRecords) { | ||
this.logRecords = logRecords; | ||
} | ||
|
||
@Override | ||
public void publish(LogRecord record) { | ||
logRecords.add((ExtLogRecord) record); | ||
} | ||
|
||
@Override | ||
public void flush() {} | ||
|
||
@Override | ||
public void close() {} | ||
} | ||
|
||
@Test | ||
void noIdsGeneratedWhenNoSpanProvided() { | ||
Logger logger = LogContext.getLogContext().getLogger("TestLogger"); | ||
LinkedList<ExtLogRecord> logRecords = new LinkedList<>(); | ||
|
||
logger.setLevel(Level.INFO); | ||
logger.addHandler(new LogHandler(logRecords)); | ||
|
||
logger.info("log message 1"); | ||
|
||
assertThat(logRecords.size()).isEqualTo(1); | ||
assertThat(logRecords.get(0).getMessage()).isEqualTo("log message 1"); | ||
assertThat(logRecords.get(0).getMdc("trace_id")).isNull(); | ||
assertThat(logRecords.get(0).getMdc("span_id")).isNull(); | ||
assertThat(logRecords.get(0).getMdc("trace_flags")).isNull(); | ||
} | ||
|
||
@Test | ||
void idsGeneratedWhenSpanProvided() throws InvocationTargetException, IllegalAccessException { | ||
Logger logger = LogContext.getLogContext().getLogger("TestLogger"); | ||
logger.setLevel(Level.DEBUG); | ||
LinkedList<ExtLogRecord> logRecords = new LinkedList<>(); | ||
logger.addHandler(new LogHandler(logRecords)); | ||
|
||
Span span1 = | ||
testing.runWithSpan( | ||
"test 1", | ||
() -> { | ||
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(); | ||
}); | ||
|
||
assertThat(logRecords.size()).isEqualTo(3); | ||
|
||
Method getMdcCopy = null; | ||
try { | ||
getMdcCopy = logRecords.get(0).getClass().getMethod("getMdcCopy"); | ||
} catch (NoSuchMethodException ignored) { | ||
} | ||
|
||
assertThat(logRecords.get(0).getMessage()).isEqualTo("log message 1"); | ||
assertThat(logRecords.get(0).getMdc("trace_id")).isEqualTo(span1.getSpanContext().getTraceId()); | ||
assertThat(logRecords.get(0).getMdc("span_id")).isEqualTo(span1.getSpanContext().getSpanId()); | ||
assertThat(logRecords.get(0).getMdc("trace_flags")).isEqualTo("01"); | ||
|
||
if (getMdcCopy != null) { | ||
@SuppressWarnings("unchecked") | ||
Map<String, String> copiedMdc = (Map<String, String>) getMdcCopy.invoke(logRecords.get(0)); | ||
assertThat(copiedMdc.get("trace_id")).isEqualTo(span1.getSpanContext().getTraceId()); | ||
assertThat(copiedMdc.get("span_id")).isEqualTo(span1.getSpanContext().getSpanId()); | ||
assertThat(copiedMdc.get("trace_flags")).isEqualTo("01"); | ||
} | ||
|
||
assertThat(logRecords.get(1).getMessage()).isEqualTo("log message 2"); | ||
assertThat(logRecords.get(1).getMdc("trace_id")).isNull(); | ||
assertThat(logRecords.get(1).getMdc("span_id")).isNull(); | ||
assertThat(logRecords.get(1).getMdc("trace_flags")).isNull(); | ||
|
||
assertThat(logRecords.get(2).getMessage()).isEqualTo("log message 3"); | ||
assertThat(logRecords.get(2).getMdc("trace_id")).isEqualTo(span2.getSpanContext().getTraceId()); | ||
assertThat(logRecords.get(2).getMdc("span_id")).isEqualTo(span2.getSpanContext().getSpanId()); | ||
assertThat(logRecords.get(2).getMdc("trace_flags")).isEqualTo("01"); | ||
|
||
if (getMdcCopy != null) { | ||
@SuppressWarnings("unchecked") | ||
Map<String, String> copiedMdc = (Map<String, String>) getMdcCopy.invoke(logRecords.get(2)); | ||
assertThat(copiedMdc.get("trace_id")).isEqualTo(span2.getSpanContext().getTraceId()); | ||
assertThat(copiedMdc.get("span_id")).isEqualTo(span2.getSpanContext().getSpanId()); | ||
assertThat(copiedMdc.get("trace_flags")).isEqualTo("01"); | ||
} | ||
} | ||
} |