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

Logback: don't make MDCPropertyMap of logging event immutable #12718

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.util.VirtualField;
import io.opentelemetry.instrumentation.logback.mdc.v1_0.internal.UnionMap;
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
import io.opentelemetry.javaagent.bootstrap.internal.AgentCommonConfig;
import io.opentelemetry.javaagent.bootstrap.internal.ConfiguredResourceAttributesHolder;
Expand Down Expand Up @@ -74,6 +73,9 @@ public static void onExit(
}

Map<String, String> spanContextData = new HashMap<>();
if (contextData != null) {
spanContextData.putAll(contextData);
}

SpanContext spanContext = Java8BytecodeBridge.spanFromContext(context).getSpanContext();

Expand All @@ -96,11 +98,7 @@ public static void onExit(
}
}

if (contextData == null) {
contextData = spanContextData;
} else {
contextData = new UnionMap<>(contextData, spanContextData);
}
contextData = spanContextData;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.incubator.log.LoggingContextConstants;
import io.opentelemetry.instrumentation.logback.mdc.v1_0.internal.UnionMap;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -83,6 +82,9 @@ private void processEvent(ILoggingEvent event) {
}

Map<String, String> contextData = new HashMap<>();
if (eventContext != null) {
contextData.putAll(eventContext);
}
Context context = Context.current();
Span currentSpan = Span.fromContext(context);

Expand All @@ -102,20 +104,14 @@ private void processEvent(ILoggingEvent event) {
"baggage." + key, value.getValue()));
}

if (eventContext == null) {
eventContext = contextData;
} else {
eventContext = new UnionMap<>(eventContext, contextData);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was there some benefit in retaining both individual maps with the use of UnionMap that we lose now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It avoided copying the original map.

}
Map<String, String> eventContextMap = eventContext;
LoggerContextVO oldVo = event.getLoggerContextVO();
LoggerContextVO vo =
oldVo != null
? new LoggerContextVO(oldVo.getName(), eventContextMap, oldVo.getBirthTime())
? new LoggerContextVO(oldVo.getName(), contextData, oldVo.getBirthTime())
: null;

try {
MDC_MAP_FIELD.set(event, eventContextMap);
MDC_MAP_FIELD.set(event, contextData);
} catch (IllegalAccessException ignored) {
// setAccessible(true) was called on the field
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@

package io.opentelemetry.instrumentation.logback.mdc.v1_0;

import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

class LogbackTest extends AbstractLogbackTest {
Expand All @@ -18,4 +22,15 @@ class LogbackTest extends AbstractLogbackTest {
public InstrumentationExtension getInstrumentationExtension() {
return testing;
}

@Test
void testMdcMutable() {
TestAppender testAppender = TestAppender.instance;
runWithSpanAndBaggage("test", baggage, () -> logger.info("log message"));

assertThat(testAppender.lastEvent.getMessage()).isEqualTo("log message");
Map<String, String> map = testAppender.lastEvent.getMDCPropertyMap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two instrumentation points:


Is there any reason to miss getMdc test here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a library instrumentation tests. It only serves as a regression test to ensure that we don't accidentally introduce something similar to the union map in the future.

// verify that mdc map associated with the event is mutable
map.put("test", "test");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.logback.mdc.v1_0;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import ch.qos.logback.core.UnsynchronizedAppenderBase;
import ch.qos.logback.core.spi.AppenderAttachable;
import ch.qos.logback.core.spi.AppenderAttachableImpl;
import java.util.Iterator;

public class TestAppender extends UnsynchronizedAppenderBase<ILoggingEvent>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public class TestAppender extends UnsynchronizedAppenderBase<ILoggingEvent>
class TestAppender extends UnsynchronizedAppenderBase<ILoggingEvent>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this class must be public

implements AppenderAttachable<ILoggingEvent> {

static TestAppender instance;
private final AppenderAttachableImpl<ILoggingEvent> aai = new AppenderAttachableImpl<>();
ILoggingEvent lastEvent;

public TestAppender() {
instance = this;
}

private void processEvent(ILoggingEvent event) {
lastEvent = event;
}

@Override
protected void append(ILoggingEvent event) {
processEvent(event);
aai.appendLoopOnAppenders(event);
}

@Override
public void addAppender(Appender<ILoggingEvent> appender) {
aai.addAppender(appender);
}

@Override
public Iterator<Appender<ILoggingEvent>> iteratorForAppenders() {
return aai.iteratorForAppenders();
}

@Override
public Appender<ILoggingEvent> getAppender(String name) {
return aai.getAppender(name);
}

@Override
public boolean isAttached(Appender<ILoggingEvent> appender) {
return aai.isAttached(appender);
}

@Override
public void detachAndStopAllAppenders() {
aai.detachAndStopAllAppenders();
}

@Override
public boolean detachAppender(Appender<ILoggingEvent> appender) {
return aai.detachAppender(appender);
}

@Override
public boolean detachAppender(String name) {
return aai.detachAppender(name);
}
}
Loading
Loading