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

Fix using logback mdc instrumentation along with SocketAppender #8498

Merged
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 @@ -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