|
10 | 10 | import static io.opentelemetry.semconv.CodeAttributes.CODE_FILE_PATH; |
11 | 11 | import static io.opentelemetry.semconv.CodeAttributes.CODE_FUNCTION_NAME; |
12 | 12 | import static io.opentelemetry.semconv.CodeAttributes.CODE_LINE_NUMBER; |
| 13 | +import static io.opentelemetry.semconv.incubating.OtelIncubatingAttributes.OTEL_EVENT_NAME; |
13 | 14 | import static io.opentelemetry.semconv.incubating.ThreadIncubatingAttributes.THREAD_ID; |
14 | 15 | import static io.opentelemetry.semconv.incubating.ThreadIncubatingAttributes.THREAD_NAME; |
15 | 16 | import static java.util.Collections.emptyList; |
|
26 | 27 | import java.util.Hashtable; |
27 | 28 | import java.util.List; |
28 | 29 | import java.util.Map; |
| 30 | +import java.util.logging.Logger; |
29 | 31 | import org.apache.log4j.Category; |
30 | 32 | import org.apache.log4j.MDC; |
31 | 33 | import org.apache.log4j.Priority; |
32 | 34 | import org.apache.log4j.spi.LocationInfo; |
33 | 35 |
|
34 | 36 | public final class LogEventMapper { |
35 | 37 |
|
| 38 | + private static final Logger logger = Logger.getLogger(LogEventMapper.class.getName()); |
| 39 | + |
36 | 40 | private static final Cache<String, AttributeKey<String>> mdcAttributeKeys = Cache.bounded(100); |
37 | 41 |
|
38 | 42 | public static final LogEventMapper INSTANCE = new LogEventMapper(); |
@@ -69,6 +73,11 @@ private LogEventMapper() { |
69 | 73 | .collect(toMap(attr -> attr, LogEventMapper::getMdcAttributeKey)); |
70 | 74 | this.captureAllMdcAttributes = |
71 | 75 | captureMdcAttributes.size() == 1 && captureMdcAttributes.get(0).equals("*"); |
| 76 | + if (captureEventName) { |
| 77 | + logger.warning( |
| 78 | + "The otel.instrumentation.log4j-appender.experimental.capture-event-name setting is" |
| 79 | + + " deprecated and will be removed in a future version."); |
| 80 | + } |
72 | 81 | } |
73 | 82 |
|
74 | 83 | boolean captureCodeAttributes = |
@@ -162,38 +171,51 @@ public void capture( |
162 | 171 | private void captureMdcAttributes(LogRecordBuilder builder) { |
163 | 172 |
|
164 | 173 | Hashtable<?, ?> context = MDC.getContext(); |
| 174 | + if (context == null) { |
| 175 | + return; |
| 176 | + } |
| 177 | + |
| 178 | + // otel.event.name takes priority over event.name |
| 179 | + Object otelEventName = context.get(OTEL_EVENT_NAME.getKey()); |
| 180 | + if (otelEventName instanceof String) { |
| 181 | + builder.setEventName((String) otelEventName); |
| 182 | + } else if (captureEventName) { |
| 183 | + Object eventName = context.get(EVENT_NAME.getKey()); |
| 184 | + if (eventName != null) { |
| 185 | + builder.setEventName(eventName.toString()); |
| 186 | + } |
| 187 | + } |
165 | 188 |
|
166 | 189 | if (captureAllMdcAttributes) { |
167 | | - if (context != null) { |
168 | | - for (Map.Entry<?, ?> entry : context.entrySet()) { |
169 | | - setAttributeOrEventName( |
170 | | - builder, getMdcAttributeKey(String.valueOf(entry.getKey())), entry.getValue()); |
| 190 | + for (Map.Entry<?, ?> entry : context.entrySet()) { |
| 191 | + String key = String.valueOf(entry.getKey()); |
| 192 | + if (!OTEL_EVENT_NAME.getKey().equals(key) |
| 193 | + && !(captureEventName && EVENT_NAME.getKey().equals(key))) { |
| 194 | + Object value = entry.getValue(); |
| 195 | + if (value != null) { |
| 196 | + builder.setAttribute(getMdcAttributeKey(key), value.toString()); |
| 197 | + } |
171 | 198 | } |
172 | 199 | } |
173 | 200 | return; |
174 | 201 | } |
175 | 202 |
|
176 | 203 | for (Map.Entry<String, AttributeKey<String>> entry : captureMdcAttributes.entrySet()) { |
177 | | - Object value = context.get(entry.getKey()); |
178 | | - setAttributeOrEventName(builder, entry.getValue(), value); |
| 204 | + String key = entry.getKey(); |
| 205 | + if (!OTEL_EVENT_NAME.getKey().equals(key) |
| 206 | + && !(captureEventName && EVENT_NAME.getKey().equals(key))) { |
| 207 | + Object value = context.get(key); |
| 208 | + if (value != null) { |
| 209 | + builder.setAttribute(entry.getValue(), value.toString()); |
| 210 | + } |
| 211 | + } |
179 | 212 | } |
180 | 213 | } |
181 | 214 |
|
182 | 215 | private static AttributeKey<String> getMdcAttributeKey(String key) { |
183 | 216 | return mdcAttributeKeys.computeIfAbsent(key, AttributeKey::stringKey); |
184 | 217 | } |
185 | 218 |
|
186 | | - private void setAttributeOrEventName( |
187 | | - LogRecordBuilder builder, AttributeKey<String> key, Object value) { |
188 | | - if (value != null) { |
189 | | - if (captureEventName && key.equals(EVENT_NAME)) { |
190 | | - builder.setEventName(value.toString()); |
191 | | - } else { |
192 | | - builder.setAttribute(key, value.toString()); |
193 | | - } |
194 | | - } |
195 | | - } |
196 | | - |
197 | 219 | private static Severity levelToSeverity(Priority level) { |
198 | 220 | int lev = level.toInt(); |
199 | 221 | if (lev <= TRACE_INT) { |
|
0 commit comments