convert jboss-logmanager-mdc groovy test to java
the `getMdcCopy` test is omitted as it is only available in
jboss-logmanager 1.3+ and the test depends on jboss-logmanager 1.1
DELETE
instrumentation/jboss-logmanager/jboss-logmanager-mdc-1.1/javaagent/src/test/groovy/JbossLogmanagerMdcTest.groovy
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-import io.opentelemetry.api.trace.Span
-import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification
-import org.jboss.logmanager.ExtLogRecord
-import org.jboss.logmanager.Level
-import org.jboss.logmanager.LogContext
-
-import java.util.logging.Handler
-import java.util.logging.LogRecord
-
-class JbossLogmanagerMdcTest extends AgentInstrumentationSpecification {
- class LogHandler extends Handler {
- public List<ExtLogRecord> logRecords
-
- LogHandler(LinkedList<ExtLogRecord> logRecords) {
- this.logRecords = logRecords
- }
- @Override
- void publish(LogRecord record) {
- logRecords.push(record as ExtLogRecord)
- }
-
- @Override
- void flush() {
-
- }
-
- @Override
- void close() throws SecurityException {
- }
- }
- def "no ids when no span"() {
- given:
- LinkedList<ExtLogRecord> logRecords = []
- def logger = LogContext.getLogContext().getLogger('TestLogger')
- logger.setLevel(Level.INFO)
- logger.addHandler(new LogHandler(logRecords))
-
- when:
- logger.info("log message 1")
-
- then:
- logRecords.size() == 1
- logRecords.get(0).message == "log message 1"
- logRecords.get(0).getMdc("trace_id") == null
- logRecords.get(0).getMdc("span_id") == null
- logRecords.get(0).getMdc("trace_flags") == null
-
- cleanup:
- logRecords.clear()
- }
-
- def "ids when span"() {
- given:
- def logger = LogContext.getLogContext().getLogger('TestLogger')
- logger.setLevel(Level.DEBUG)
- LinkedList<ExtLogRecord> logRecords = []
- logger.addHandler(new LogHandler(logRecords))
- when:
- def span1 = runWithSpan("test") {
- logger.info("log message 1")
- Span.current()
- }
-
- logger.info("log message 2")
-
- def span2 = runWithSpan("test 2") {
- logger.info("log message 3")
- Span.current()
- }
-
- then:
-
- logRecords.size() == 3
-
- // The method getMdcCopy exists only in jboss-logmanager 1.3+
- def hasGetMdcCopy = logRecords.get(0).metaClass.getMetaMethod("getMdcCopy") != null
-
- logRecords.get(2).message == "log message 1"
- logRecords.get(2).getMdc("trace_id") == span1.spanContext.traceId
- logRecords.get(2).getMdc("span_id") == span1.spanContext.spanId
- logRecords.get(2).getMdc("trace_flags") == "01"
-
- if (hasGetMdcCopy) {
- assert logRecords.get(2).getMdcCopy().get("trace_id") == span1.spanContext.traceId
- assert logRecords.get(2).getMdcCopy().get("span_id") == span1.spanContext.spanId
- assert logRecords.get(2).getMdcCopy().get("trace_flags") == "01"
- }
-
- logRecords.get(1).message == "log message 2"
- logRecords.get(1).getMdc("trace_id") == null
- logRecords.get(1).getMdc("span_id") == null
- logRecords.get(1).getMdc("trace_flags") == null
-
- logRecords.get(0).message == "log message 3"
- logRecords.get(0).getMdc("trace_id") == span2.spanContext.traceId
- logRecords.get(0).getMdc("span_id") == span2.spanContext.spanId
- logRecords.get(0).getMdc("trace_flags") == "01"
-
- if (hasGetMdcCopy) {
- assert logRecords.get(0).getMdcCopy().get("trace_id") == span2.spanContext.traceId
- assert logRecords.get(0).getMdcCopy().get("span_id") == span2.spanContext.spanId
- assert logRecords.get(0).getMdcCopy().get("trace_flags") == "01"
- }
-
- cleanup:
- logRecords.clear()
- }
-}

ADD
instrumentation/jboss-logmanager/jboss-logmanager-mdc-1.1/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/jbosslogmanager/mdc/v1_1/JbossLogmanagerMdcTest.java
+/*
+ * 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");
+ }
+ }
+}

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 <107717825+opentelemetrybot@users.noreply.github.com>