Skip to content

Commit

Permalink
convert jboss-logmanager-mdc groovy test to java (#7703)
Browse files Browse the repository at this point in the history
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
cuichenli and opentelemetrybot authored Feb 3, 2023
1 parent 8d74cf1 commit a5143d6
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 113 deletions.

This file was deleted.

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");
}
}
}

0 comments on commit a5143d6

Please sign in to comment.