Skip to content

Commit 1b9194f

Browse files
committed
add unit test
1 parent 41f3d84 commit 1b9194f

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

instrumentation/log4j/log4j-appender-1.2/javaagent/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ configurations {
3434
tasks.withType<Test>().configureEach {
3535
// TODO run tests both with and without experimental log attributes
3636
jvmArgs("-Dotel.instrumentation.log4j-appender.experimental.capture-mdc-attributes=*")
37+
jvmArgs("-Dotel.instrumentation.log4j-appender.experimental.capture-code-attributes=true")
3738
jvmArgs("-Dotel.instrumentation.log4j-appender.experimental-log-attributes=true")
3839
jvmArgs("-Dotel.instrumentation.log4j-appender.experimental-log-attributes=true")
3940
jvmArgs("-Dotel.instrumentation.common.experimental.controller-telemetry.enabled=true")

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public final class LogEventMapper {
3838

3939
private static final AttributeKey<String> CODE_FILEPATH = AttributeKey.stringKey("code.filepath");
4040
private static final AttributeKey<String> CODE_FUNCTION = AttributeKey.stringKey("code.function");
41-
private static final AttributeKey<String> CODE_LINENO = AttributeKey.stringKey("code.lineno");
41+
private static final AttributeKey<Long> CODE_LINENO = AttributeKey.longKey("code.lineno");
4242
private static final AttributeKey<String> CODE_NAMESPACE =
4343
AttributeKey.stringKey("code.namespace");
4444
// copied from org.apache.log4j.Level because it was only introduced in 1.2.12
@@ -125,9 +125,15 @@ public void capture(
125125
attributes.put(CODE_NAMESPACE, locationInfo.getClassName());
126126
attributes.put(CODE_FUNCTION, locationInfo.getMethodName());
127127
String lineNumber = locationInfo.getLineNumber();
128-
if (lineNumber != null) {
129-
attributes.put(CODE_LINENO, lineNumber);
128+
int codeLineNo = 0;
129+
if (!lineNumber.equals("?")) {
130+
try {
131+
codeLineNo = Integer.parseInt(lineNumber);
132+
}catch (NumberFormatException e) {
133+
// ignore
134+
}
130135
}
136+
attributes.put(CODE_LINENO, codeLineNo);
131137
}
132138

133139
builder.setAllAttributes(attributes.build());

instrumentation/log4j/log4j-appender-1.2/javaagent/src/test/java/io/opentelemetry/instrumentation/log4j/appender/v1_2/Log4j1Test.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
import static io.opentelemetry.semconv.ExceptionAttributes.EXCEPTION_MESSAGE;
1212
import static io.opentelemetry.semconv.ExceptionAttributes.EXCEPTION_STACKTRACE;
1313
import static io.opentelemetry.semconv.ExceptionAttributes.EXCEPTION_TYPE;
14+
import static io.opentelemetry.semconv.incubating.CodeIncubatingAttributes.CODE_FILEPATH;
15+
import static io.opentelemetry.semconv.incubating.CodeIncubatingAttributes.CODE_FUNCTION;
16+
import static io.opentelemetry.semconv.incubating.CodeIncubatingAttributes.CODE_LINENO;
17+
import static io.opentelemetry.semconv.incubating.CodeIncubatingAttributes.CODE_NAMESPACE;
18+
import static io.opentelemetry.semconv.incubating.ThreadIncubatingAttributes.THREAD_ID;
19+
import static io.opentelemetry.semconv.incubating.ThreadIncubatingAttributes.THREAD_NAME;
1420
import static java.util.concurrent.TimeUnit.MILLISECONDS;
1521

1622
import io.opentelemetry.api.common.AttributeKey;
@@ -31,13 +37,15 @@
3137
import org.apache.log4j.Logger;
3238
import org.apache.log4j.MDC;
3339
import org.apache.log4j.helpers.Loader;
40+
import org.assertj.core.api.AbstractLongAssert;
3441
import org.assertj.core.api.AssertAccess;
3542
import org.junit.jupiter.api.Test;
3643
import org.junit.jupiter.api.extension.RegisterExtension;
3744
import org.junit.jupiter.params.ParameterizedTest;
3845
import org.junit.jupiter.params.provider.Arguments;
3946
import org.junit.jupiter.params.provider.MethodSource;
4047

48+
@SuppressWarnings("deprecation") // using deprecated semconv
4149
class Log4j1Test {
4250

4351
static {
@@ -67,6 +75,25 @@ private static Stream<Arguments> provideParameters() {
6775
Arguments.of(true, true));
6876
}
6977

78+
@Test
79+
public void testCodeAttributes() {
80+
logger.info("this is test message");
81+
testing.waitAndAssertLogRecords(
82+
logRecord ->
83+
logRecord
84+
.hasBody("this is test message")
85+
.hasInstrumentationScope(InstrumentationScopeInfo.builder("abc").build())
86+
.hasSeverity(Severity.INFO)
87+
.hasSeverityText("INFO")
88+
.hasAttributesSatisfyingExactly(
89+
equalTo(THREAD_NAME, Thread.currentThread().getName()),
90+
equalTo(THREAD_ID, Thread.currentThread().getId()),
91+
equalTo(CODE_NAMESPACE, Log4j1Test.class.getName()),
92+
equalTo(CODE_FUNCTION, "testCodeAttributes"),
93+
satisfies(CODE_LINENO, AbstractLongAssert::isPositive),
94+
equalTo(CODE_FILEPATH, "Log4j1Test.java")));
95+
}
96+
7097
@ParameterizedTest
7198
@MethodSource("provideParameters")
7299
public void test(boolean logException, boolean withParent) throws InterruptedException {
@@ -79,7 +106,6 @@ public void test(boolean logException, boolean withParent) throws InterruptedExc
79106
test(Logger::error, Logger::error, logException, withParent, "abc", Severity.ERROR, "ERROR");
80107
testing.clearData();
81108
}
82-
83109
private static void test(
84110
LoggerMethod loggerMethod,
85111
ExceptionLoggerMethod exceptionLoggerMethod,

0 commit comments

Comments
 (0)