Skip to content

Commit

Permalink
convert Log4j mdc test from groovy to java (#8170)
Browse files Browse the repository at this point in the history
Related to #7195
  • Loading branch information
niteshs7 authored Mar 30, 2023
1 parent 1adc5d3 commit 2d8ba00
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 106 deletions.

This file was deleted.

This file was deleted.

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

0 comments on commit 2d8ba00

Please sign in to comment.