Skip to content
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

convert Log4j mdc test from groovy to java #8170

Merged
merged 3 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.log4j.mdc.v1_2;

import java.util.ArrayList;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;

public class ListAppender extends AppenderSkeleton {

private static ArrayList<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 ArrayList<LoggingEvent> getEvents() {
return events;
}

public static void setEvents(ArrayList<LoggingEvent> events) {
ListAppender.events = events;
}
niteshs7 marked this conversation as resolved.
Show resolved Hide resolved
}
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.ArrayList;
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
public void cleanup() {
niteshs7 marked this conversation as resolved.
Show resolved Hide resolved
ListAppender.clearEvents();
}

@Test
public void noIdsWhenNoSpan() {
logger.info("log message 1");
logger.info("log message 2");

ArrayList<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
public 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();
});

ArrayList<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