|
7 | 7 |
|
8 | 8 | import ch.qos.logback.classic.spi.ILoggingEvent;
|
9 | 9 | import ch.qos.logback.classic.spi.LoggerContextVO;
|
10 |
| -import ch.qos.logback.classic.spi.LoggingEventVO; |
| 10 | +import ch.qos.logback.classic.spi.LoggingEvent; |
11 | 11 | import ch.qos.logback.core.Appender;
|
12 | 12 | import ch.qos.logback.core.UnsynchronizedAppenderBase;
|
13 | 13 | import ch.qos.logback.core.spi.AppenderAttachable;
|
|
18 | 18 | import io.opentelemetry.context.Context;
|
19 | 19 | import io.opentelemetry.instrumentation.api.incubator.log.LoggingContextConstants;
|
20 | 20 | import io.opentelemetry.instrumentation.logback.mdc.v1_0.internal.UnionMap;
|
21 |
| -import java.lang.reflect.InvocationTargetException; |
22 |
| -import java.lang.reflect.Proxy; |
| 21 | +import java.lang.reflect.Field; |
23 | 22 | import java.util.HashMap;
|
24 | 23 | import java.util.Iterator;
|
25 | 24 | import java.util.Map;
|
26 | 25 |
|
27 | 26 | public class OpenTelemetryAppender extends UnsynchronizedAppenderBase<ILoggingEvent>
|
28 | 27 | implements AppenderAttachable<ILoggingEvent> {
|
| 28 | + private static final Field MDC_MAP_FIELD; |
| 29 | + |
| 30 | + static { |
| 31 | + Field field; |
| 32 | + try { |
| 33 | + field = LoggingEvent.class.getDeclaredField("mdcPropertyMap"); |
| 34 | + field.setAccessible(true); |
| 35 | + } catch (Exception exception) { |
| 36 | + field = null; |
| 37 | + } |
| 38 | + MDC_MAP_FIELD = field; |
| 39 | + } |
| 40 | + |
29 | 41 | private boolean addBaggage;
|
30 | 42 | private String traceIdKey = LoggingContextConstants.TRACE_ID;
|
31 | 43 | private String spanIdKey = LoggingContextConstants.SPAN_ID;
|
@@ -59,11 +71,15 @@ public void setTraceFlagsKey(String traceFlagsKey) {
|
59 | 71 | this.traceFlagsKey = traceFlagsKey;
|
60 | 72 | }
|
61 | 73 |
|
62 |
| - public ILoggingEvent wrapEvent(ILoggingEvent event) { |
| 74 | + private void processEvent(ILoggingEvent event) { |
| 75 | + if (MDC_MAP_FIELD == null || event.getClass() != LoggingEvent.class) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
63 | 79 | Map<String, String> eventContext = event.getMDCPropertyMap();
|
64 | 80 | if (eventContext != null && eventContext.containsKey(traceIdKey)) {
|
65 | 81 | // Assume already instrumented event if traceId is present.
|
66 |
| - return event; |
| 82 | + return; |
67 | 83 | }
|
68 | 84 |
|
69 | 85 | Map<String, String> contextData = new HashMap<>();
|
@@ -98,32 +114,18 @@ public ILoggingEvent wrapEvent(ILoggingEvent event) {
|
98 | 114 | ? new LoggerContextVO(oldVo.getName(), eventContextMap, oldVo.getBirthTime())
|
99 | 115 | : null;
|
100 | 116 |
|
101 |
| - ILoggingEvent wrappedEvent = |
102 |
| - (ILoggingEvent) |
103 |
| - Proxy.newProxyInstance( |
104 |
| - ILoggingEvent.class.getClassLoader(), |
105 |
| - new Class<?>[] {ILoggingEvent.class}, |
106 |
| - (proxy, method, args) -> { |
107 |
| - if ("getMDCPropertyMap".equals(method.getName())) { |
108 |
| - return eventContextMap; |
109 |
| - } else if ("getLoggerContextVO".equals(method.getName())) { |
110 |
| - return vo; |
111 |
| - } |
112 |
| - try { |
113 |
| - return method.invoke(event, args); |
114 |
| - } catch (InvocationTargetException exception) { |
115 |
| - throw exception.getCause(); |
116 |
| - } |
117 |
| - }); |
118 |
| - // https://github.com/qos-ch/logback/blob/9e833ec858953a2296afdc3292f8542fc08f2a45/logback-classic/src/main/java/ch/qos/logback/classic/net/LoggingEventPreSerializationTransformer.java#L29 |
119 |
| - // LoggingEventPreSerializationTransformer accepts only subclasses of LoggingEvent and |
120 |
| - // LoggingEventVO, here we transform our wrapped event into a LoggingEventVO |
121 |
| - return LoggingEventVO.build(wrappedEvent); |
| 117 | + try { |
| 118 | + MDC_MAP_FIELD.set(event, eventContextMap); |
| 119 | + } catch (IllegalAccessException ignored) { |
| 120 | + // setAccessible(true) was called on the field |
| 121 | + } |
| 122 | + ((LoggingEvent) event).setLoggerContextRemoteView(vo); |
122 | 123 | }
|
123 | 124 |
|
124 | 125 | @Override
|
125 | 126 | protected void append(ILoggingEvent event) {
|
126 |
| - aai.appendLoopOnAppenders(wrapEvent(event)); |
| 127 | + processEvent(event); |
| 128 | + aai.appendLoopOnAppenders(event); |
127 | 129 | }
|
128 | 130 |
|
129 | 131 | @Override
|
|
0 commit comments