Skip to content

Commit e56fd19

Browse files
committed
capture code attributes
1 parent 2994158 commit e56fd19

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

instrumentation/log4j/log4j-appender-1.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/log4j/appender/v1_2/Log4jAppenderInstrumentation.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public static class ForcedLogAdvice {
4848
@Advice.OnMethodEnter(suppress = Throwable.class)
4949
public static void methodEnter(
5050
@Advice.This Category logger,
51+
@Advice.Argument(0) String fqcn,
5152
@Advice.Argument(1) Priority level,
5253
@Advice.Argument(2) Object message,
5354
@Advice.Argument(3) Throwable t,
@@ -56,7 +57,7 @@ public static void methodEnter(
5657
// framework delegates to another
5758
callDepth = CallDepth.forClass(LoggerProvider.class);
5859
if (callDepth.getAndIncrement() == 0) {
59-
LogEventMapper.INSTANCE.capture(logger, level, message, t);
60+
LogEventMapper.INSTANCE.capture(fqcn, logger, level, message, t);
6061
}
6162
}
6263

instrumentation/log4j/log4j-appender-1.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/log4j/appender/v1_2/LogEventMapper.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,19 @@
2828
import org.apache.log4j.Category;
2929
import org.apache.log4j.MDC;
3030
import org.apache.log4j.Priority;
31+
import org.apache.log4j.spi.LocationInfo;
3132

3233
public final class LogEventMapper {
3334

3435
private static final Cache<String, AttributeKey<String>> mdcAttributeKeys = Cache.bounded(100);
3536

3637
public static final LogEventMapper INSTANCE = new LogEventMapper();
3738

39+
private static final AttributeKey<String> CODE_FILEPATH = AttributeKey.stringKey("code.filepath");
40+
private static final AttributeKey<String> CODE_FUNCTION = AttributeKey.stringKey("code.function");
41+
private static final AttributeKey<String> CODE_LINENO = AttributeKey.stringKey("code.lineno");
42+
private static final AttributeKey<String> CODE_NAMESPACE =
43+
AttributeKey.stringKey("code.namespace");
3844
// copied from org.apache.log4j.Level because it was only introduced in 1.2.12
3945
private static final int TRACE_INT = 5000;
4046

@@ -60,7 +66,13 @@ private LogEventMapper() {
6066
captureMdcAttributes.size() == 1 && captureMdcAttributes.get(0).equals("*");
6167
}
6268

63-
public void capture(Category logger, Priority level, Object message, Throwable throwable) {
69+
boolean captureCodeAttributes =
70+
AgentInstrumentationConfig.get()
71+
.getBoolean(
72+
"otel.instrumentation.log4j-appender.experimental.capture-code-attributes", false);
73+
74+
public void capture(
75+
String fqcn, Category logger, Priority level, Object message, Throwable throwable) {
6476
String instrumentationName = logger.getName();
6577
if (instrumentationName == null || instrumentationName.isEmpty()) {
6678
instrumentationName = "ROOT";
@@ -104,6 +116,20 @@ public void capture(Category logger, Priority level, Object message, Throwable t
104116
attributes.put(ThreadIncubatingAttributes.THREAD_ID, currentThread.getId());
105117
}
106118

119+
if (captureCodeAttributes) {
120+
LocationInfo locationInfo = new LocationInfo(new Throwable(), fqcn);
121+
String fileName = locationInfo.getFileName();
122+
if (fileName != null) {
123+
attributes.put(CODE_FILEPATH, fileName);
124+
}
125+
attributes.put(CODE_NAMESPACE, locationInfo.getClassName());
126+
attributes.put(CODE_FUNCTION, locationInfo.getMethodName());
127+
String lineNumber = locationInfo.getLineNumber();
128+
if (lineNumber != null) {
129+
attributes.put(CODE_LINENO, lineNumber);
130+
}
131+
}
132+
107133
builder.setAllAttributes(attributes.build());
108134

109135
// span context

0 commit comments

Comments
 (0)