Skip to content

Commit e6aa3ef

Browse files
committed
code review
1 parent 8cc0987 commit e6aa3ef

File tree

2 files changed

+11
-14
lines changed
  • instrumentation/log4j/log4j-appender-1.2/javaagent/src

2 files changed

+11
-14
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,15 @@ public void capture(
141141
}
142142

143143
String lineNumber = locationInfo.getLineNumber();
144-
int codeLineNo = 0;
144+
int codeLineNo = -1;
145145
if (!lineNumber.equals("?")) {
146146
try {
147147
codeLineNo = Integer.parseInt(lineNumber);
148148
} catch (NumberFormatException e) {
149149
// ignore
150150
}
151151
}
152-
if (codeLineNo > 0) {
152+
if (codeLineNo >= 0) {
153153
if (SemconvStability.isEmitStableCodeSemconv()) {
154154
attributes.put(CodeAttributes.CODE_LINE_NUMBER, codeLineNo);
155155
}

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
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;
1814
import static io.opentelemetry.semconv.incubating.ThreadIncubatingAttributes.THREAD_ID;
1915
import static io.opentelemetry.semconv.incubating.ThreadIncubatingAttributes.THREAD_NAME;
2016
import static java.util.concurrent.TimeUnit.MILLISECONDS;
@@ -24,6 +20,7 @@
2420
import io.opentelemetry.api.trace.SpanContext;
2521
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
2622
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
23+
import io.opentelemetry.instrumentation.testing.junit.code.SemconvCodeStabilityUtil;
2724
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
2825
import io.opentelemetry.sdk.logs.data.LogRecordData;
2926
import io.opentelemetry.sdk.testing.assertj.AttributeAssertion;
@@ -37,7 +34,6 @@
3734
import org.apache.log4j.Logger;
3835
import org.apache.log4j.MDC;
3936
import org.apache.log4j.helpers.Loader;
40-
import org.assertj.core.api.AbstractLongAssert;
4137
import org.assertj.core.api.AssertAccess;
4238
import org.junit.jupiter.api.Test;
4339
import org.junit.jupiter.api.extension.RegisterExtension;
@@ -78,20 +74,21 @@ private static Stream<Arguments> provideParameters() {
7874
@Test
7975
public void testCodeAttributes() {
8076
logger.info("this is test message");
77+
List<AttributeAssertion> assertions =
78+
SemconvCodeStabilityUtil.codeFileAndLineAssertions("Log4j1Test.java");
79+
assertions.addAll(
80+
SemconvCodeStabilityUtil.codeFunctionAssertions(Log4j1Test.class, "testCodeAttributes"));
81+
assertions.add(equalTo(THREAD_NAME, Thread.currentThread().getName()));
82+
assertions.add(equalTo(THREAD_ID, Thread.currentThread().getId()));
83+
8184
testing.waitAndAssertLogRecords(
8285
logRecord ->
8386
logRecord
8487
.hasBody("this is test message")
8588
.hasInstrumentationScope(InstrumentationScopeInfo.builder("abc").build())
8689
.hasSeverity(Severity.INFO)
8790
.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")));
91+
.hasAttributesSatisfyingExactly(assertions));
9592
}
9693

9794
@ParameterizedTest

0 commit comments

Comments
 (0)