Skip to content

Commit

Permalink
Fix using logback mdc instrumentation along with SocketAppender (#8498)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit authored May 16, 2023
1 parent f916243 commit d7d6296
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,7 +23,8 @@
* <p>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<K, V> extends AbstractMap<K, V> {
public final class UnionMap<K, V> extends AbstractMap<K, V> implements Serializable {
private static final long serialVersionUID = 1L;

private final Map<K, V> first;
private final Map<K, V> second;
Expand Down Expand Up @@ -128,6 +131,11 @@ public Set<Entry<K, V>> 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<T> extends AbstractSet<T> {

Expand Down

0 comments on commit d7d6296

Please sign in to comment.