diff --git a/instrumentation/logback/logback-mdc-1.0/library/src/main/java/io/opentelemetry/instrumentation/logback/mdc/v1_0/OpenTelemetryAppender.java b/instrumentation/logback/logback-mdc-1.0/library/src/main/java/io/opentelemetry/instrumentation/logback/mdc/v1_0/OpenTelemetryAppender.java index 4b3a3a3bcbee..f3c148b9647c 100644 --- a/instrumentation/logback/logback-mdc-1.0/library/src/main/java/io/opentelemetry/instrumentation/logback/mdc/v1_0/OpenTelemetryAppender.java +++ b/instrumentation/logback/logback-mdc-1.0/library/src/main/java/io/opentelemetry/instrumentation/logback/mdc/v1_0/OpenTelemetryAppender.java @@ -11,6 +11,7 @@ import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.classic.spi.LoggerContextVO; +import ch.qos.logback.classic.spi.LoggingEventVO; import ch.qos.logback.core.Appender; import ch.qos.logback.core.UnsynchronizedAppenderBase; import ch.qos.logback.core.spi.AppenderAttachable; @@ -82,22 +83,27 @@ public ILoggingEvent wrapEvent(ILoggingEvent event) { ? new LoggerContextVO(oldVo.getName(), eventContextMap, oldVo.getBirthTime()) : null; - return (ILoggingEvent) - Proxy.newProxyInstance( - ILoggingEvent.class.getClassLoader(), - new Class[] {ILoggingEvent.class}, - (proxy, method, args) -> { - if ("getMDCPropertyMap".equals(method.getName())) { - return eventContextMap; - } else if ("getLoggerContextVO".equals(method.getName())) { - return vo; - } - try { - return method.invoke(event, args); - } catch (InvocationTargetException exception) { - throw exception.getCause(); - } - }); + ILoggingEvent wrappedEvent = + (ILoggingEvent) + Proxy.newProxyInstance( + ILoggingEvent.class.getClassLoader(), + new Class[] {ILoggingEvent.class}, + (proxy, method, args) -> { + if ("getMDCPropertyMap".equals(method.getName())) { + return eventContextMap; + } else if ("getLoggerContextVO".equals(method.getName())) { + return vo; + } + try { + return method.invoke(event, args); + } catch (InvocationTargetException exception) { + throw exception.getCause(); + } + }); + // https://github.com/qos-ch/logback/blob/9e833ec858953a2296afdc3292f8542fc08f2a45/logback-classic/src/main/java/ch/qos/logback/classic/net/LoggingEventPreSerializationTransformer.java#L29 + // LoggingEventPreSerializationTransformer accepts only subclasses of LoggingEvent and + // LoggingEventVO, here we transform our wrapped event into a LoggingEventVO + return LoggingEventVO.build(wrappedEvent); } @Override diff --git a/instrumentation/logback/logback-mdc-1.0/library/src/main/java/io/opentelemetry/instrumentation/logback/mdc/v1_0/internal/UnionMap.java b/instrumentation/logback/logback-mdc-1.0/library/src/main/java/io/opentelemetry/instrumentation/logback/mdc/v1_0/internal/UnionMap.java index 5da8c72b1a46..e84b136d7d0d 100644 --- a/instrumentation/logback/logback-mdc-1.0/library/src/main/java/io/opentelemetry/instrumentation/logback/mdc/v1_0/internal/UnionMap.java +++ b/instrumentation/logback/logback-mdc-1.0/library/src/main/java/io/opentelemetry/instrumentation/logback/mdc/v1_0/internal/UnionMap.java @@ -5,10 +5,12 @@ package io.opentelemetry.instrumentation.logback.mdc.v1_0.internal; +import java.io.Serializable; import java.util.AbstractMap; import java.util.AbstractSet; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.Map; @@ -21,7 +23,8 @@ *

This class is internal and is hence not for public use. Its APIs are unstable and can change * at any time. */ -public final class UnionMap extends AbstractMap { +public final class UnionMap extends AbstractMap implements Serializable { + private static final long serialVersionUID = 1L; private final Map first; private final Map second; @@ -128,6 +131,11 @@ public Set> entrySet() { Collections.unmodifiableSet(new ConcatenatedSet<>(first.entrySet(), filteredSecond)); } + private Object writeReplace() { + // serialize this object as HashMap + return new HashMap<>(this); + } + // Member sets must be deduped by caller. static final class ConcatenatedSet extends AbstractSet {