Skip to content

Commit 9cd067a

Browse files
committed
fix logback
1 parent 3d33231 commit 9cd067a

File tree

4 files changed

+46
-27
lines changed

4 files changed

+46
-27
lines changed

instrumentation/logback/logback-appender-1.0/library/src/main/java/io/opentelemetry/instrumentation/logback/appender/v1_0/internal/LoggingEventMapper.java

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
package io.opentelemetry.instrumentation.logback.appender.v1_0.internal;
77

8+
import static io.opentelemetry.semconv.CodeAttributes.CODE_FILE_PATH;
9+
import static io.opentelemetry.semconv.CodeAttributes.CODE_FUNCTION_NAME;
10+
import static io.opentelemetry.semconv.CodeAttributes.CODE_LINE_NUMBER;
811
import static java.util.Collections.emptyList;
912

1013
import ch.qos.logback.classic.Level;
@@ -18,6 +21,7 @@
1821
import io.opentelemetry.api.logs.LoggerProvider;
1922
import io.opentelemetry.api.logs.Severity;
2023
import io.opentelemetry.context.Context;
24+
import io.opentelemetry.instrumentation.api.internal.SemconvStability;
2125
import io.opentelemetry.instrumentation.api.internal.cache.Cache;
2226
import io.opentelemetry.javaagent.tooling.muzzle.NoMuzzle;
2327
import io.opentelemetry.semconv.ExceptionAttributes;
@@ -47,13 +51,12 @@
4751
*/
4852
public final class LoggingEventMapper {
4953
// copied from CodeIncubatingAttributes
50-
private static final AttributeKey<String> CODE_FILE_PATH =
51-
AttributeKey.stringKey("code.file.path");
52-
private static final AttributeKey<String> CODE_FUNCTION_NAME =
53-
AttributeKey.stringKey("code.function.name");
54-
private static final AttributeKey<Long> CODE_LINE_NUMBER =
55-
AttributeKey.longKey("code.line.number");
56-
// copied from
54+
private static final AttributeKey<String> CODE_FILEPATH = AttributeKey.stringKey("code.filepath");
55+
private static final AttributeKey<String> CODE_NAMESPACE =
56+
AttributeKey.stringKey("code.namespace");
57+
private static final AttributeKey<String> CODE_FUNCTION = AttributeKey.stringKey("code.function");
58+
private static final AttributeKey<Long> CODE_LINENO = AttributeKey.longKey("code.lineno");
59+
// copied from ThreadIncubatingAttributes
5760
private static final AttributeKey<Long> THREAD_ID = AttributeKey.longKey("thread.id");
5861
private static final AttributeKey<String> THREAD_NAME = AttributeKey.stringKey("thread.name");
5962

@@ -161,15 +164,28 @@ private void mapLoggingEvent(
161164
if (callerData != null && callerData.length > 0) {
162165
StackTraceElement firstStackElement = callerData[0];
163166
String fileName = firstStackElement.getFileName();
164-
if (fileName != null) {
165-
attributes.put(CODE_FILE_PATH, fileName);
166-
}
167-
attributes.put(
168-
CODE_FUNCTION_NAME,
169-
firstStackElement.getClassName() + "." + firstStackElement.getMethodName());
170167
int lineNumber = firstStackElement.getLineNumber();
171-
if (lineNumber > 0) {
172-
attributes.put(CODE_LINE_NUMBER, lineNumber);
168+
169+
if (SemconvStability.isEmitOldCodeSemconv()) {
170+
if (fileName != null) {
171+
attributes.put(CODE_FILEPATH, fileName);
172+
}
173+
attributes.put(CODE_NAMESPACE, firstStackElement.getClassName());
174+
attributes.put(CODE_FUNCTION, firstStackElement.getMethodName());
175+
if (lineNumber > 0) {
176+
attributes.put(CODE_LINENO, lineNumber);
177+
}
178+
}
179+
if (SemconvStability.isEmitStableCodeSemconv()) {
180+
if (fileName != null) {
181+
attributes.put(CODE_FILE_PATH, fileName);
182+
}
183+
attributes.put(
184+
CODE_FUNCTION_NAME,
185+
firstStackElement.getClassName() + "." + firstStackElement.getMethodName());
186+
if (lineNumber > 0) {
187+
attributes.put(CODE_LINE_NUMBER, lineNumber);
188+
}
173189
}
174190
}
175191
}

instrumentation/logback/logback-appender-1.0/library/src/slf4j2ApiTest/java/io/opentelemetry/instrumentation/logback/appender/v1_0/Slf4j2Test.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.opentelemetry.instrumentation.logback.appender.v1_0;
77

8+
import static io.opentelemetry.instrumentation.testing.junit.code.SemconvCodeStabilityUtil.codeAttributesLogCount;
89
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
910

1011
import io.opentelemetry.api.common.AttributeKey;
@@ -61,7 +62,7 @@ void keyValue() {
6162
.hasResource(resource)
6263
.hasInstrumentationScope(instrumentationScopeInfo)
6364
.hasBody("log message 1")
64-
.hasTotalAttributeCount(11) // 3 code attributes + 8 key value pairs
65+
.hasTotalAttributeCount(codeAttributesLogCount() + 8) // 8 key value pairs
6566
.hasAttributesSatisfying(
6667
equalTo(AttributeKey.stringKey("string key"), "string value"),
6768
equalTo(AttributeKey.booleanKey("boolean key"), true),
@@ -90,7 +91,7 @@ void multipleMarkers() {
9091
.hasResource(resource)
9192
.hasInstrumentationScope(instrumentationScopeInfo)
9293
.hasBody("log message 1")
93-
.hasTotalAttributeCount(4) // 3 code attributes + 1 marker
94+
.hasTotalAttributeCount(codeAttributesLogCount() + 1) // 1 marker
9495
.hasAttributesSatisfying(
9596
equalTo(
9697
AttributeKey.stringArrayKey("logback.marker"),
@@ -115,7 +116,7 @@ void arguments() {
115116
.hasInstrumentationScope(instrumentationScopeInfo)
116117
.hasBody(
117118
"log message 'world' and 3.141592653589793, bool true, long 9223372036854775807")
118-
.hasTotalAttributeCount(5)
119+
.hasTotalAttributeCount(codeAttributesLogCount() + 2)
119120
.hasAttributesSatisfying(
120121
equalTo(
121122
AttributeKey.stringArrayKey("log.body.parameters"),
@@ -148,7 +149,7 @@ void logstash() {
148149
.hasResource(resource)
149150
.hasInstrumentationScope(instrumentationScopeInfo)
150151
.hasBody("log message 1")
151-
.hasTotalAttributeCount(6) // 3 code attributes + 3 markers
152+
.hasTotalAttributeCount(codeAttributesLogCount() + 3) // 3 markers
152153
.hasAttributesSatisfying(
153154
equalTo(AttributeKey.stringKey("field1"), "value1"),
154155
equalTo(AttributeKey.longKey("field2"), 2L),
@@ -186,7 +187,8 @@ void logstashVariousValues() {
186187
.hasResource(resource)
187188
.hasInstrumentationScope(instrumentationScopeInfo)
188189
.hasBody("log message 1")
189-
.hasTotalAttributeCount(17) // 3 code attributes + 14 fields (including map keys)
190+
// 14 fields (including map keys)
191+
.hasTotalAttributeCount(codeAttributesLogCount() + 14)
190192
.hasAttributesSatisfying(
191193
equalTo(AttributeKey.longKey("field1"), 1L),
192194
equalTo(AttributeKey.doubleKey("field2"), 2.0),
@@ -231,7 +233,6 @@ void logstashEmptyAndNullValues() {
231233
.hasResource(resource)
232234
.hasInstrumentationScope(instrumentationScopeInfo)
233235
.hasBody("log message 1")
234-
.hasTotalAttributeCount(3) // 3 code attributes
235-
);
236+
.hasTotalAttributeCount(codeAttributesLogCount()));
236237
}
237238
}

instrumentation/logback/logback-appender-1.0/library/src/test/java/io/opentelemetry/instrumentation/logback/appender/v1_0/AbstractOpenTelemetryAppenderTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.opentelemetry.instrumentation.logback.appender.v1_0;
77

8+
import static io.opentelemetry.instrumentation.testing.junit.code.SemconvCodeStabilityUtil.codeAttributesLogCount;
89
import static io.opentelemetry.instrumentation.testing.junit.code.SemconvCodeStabilityUtil.codeFileAndLineAssertions;
910
import static io.opentelemetry.instrumentation.testing.junit.code.SemconvCodeStabilityUtil.codeFunctionAssertions;
1011
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
@@ -18,7 +19,6 @@
1819
import ch.qos.logback.core.ContextBase;
1920
import io.opentelemetry.api.common.AttributeKey;
2021
import io.opentelemetry.api.logs.Severity;
21-
import io.opentelemetry.instrumentation.api.internal.SemconvStability;
2222
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
2323
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
2424
import io.opentelemetry.sdk.logs.data.LogRecordData;
@@ -93,7 +93,7 @@ void logNoSpan() {
9393
.hasResource(resource)
9494
.hasInstrumentationScope(instrumentationScopeInfo)
9595
.hasBody("log message 1")
96-
.hasTotalAttributeCount(SemconvStability.isEmitStableCodeSemconv() ? 2 : 3));
96+
.hasTotalAttributeCount(codeAttributesLogCount()));
9797
}
9898

9999
@Test
@@ -157,7 +157,7 @@ void logContextData() {
157157
.hasResource(resource)
158158
.hasInstrumentationScope(instrumentationScopeInfo)
159159
.hasBody("log message 1")
160-
.hasTotalAttributeCount(2 + 3) // 3 code attributes
160+
.hasTotalAttributeCount(2 + codeAttributesLogCount()) // code attributes
161161
.hasAttributesSatisfying(
162162
equalTo(AttributeKey.stringKey("key1"), "val1"),
163163
equalTo(AttributeKey.stringKey("key2"), "val2")));
@@ -181,7 +181,7 @@ void logLoggerContext() {
181181
.hasResource(resource)
182182
.hasInstrumentationScope(instrumentationScopeInfo)
183183
.hasBody("log message 1")
184-
.hasTotalAttributeCount(1 + 3) // 3 code attributes
184+
.hasTotalAttributeCount(codeAttributesLogCount() + 1)
185185
.hasAttributesSatisfying(
186186
equalTo(AttributeKey.stringKey("test-property"), "test-value")));
187187
}

instrumentation/logback/logback-appender-1.0/library/src/test/java/io/opentelemetry/instrumentation/logback/appender/v1_0/LogReplayOpenTelemetryAppenderTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
package io.opentelemetry.instrumentation.logback.appender.v1_0;
77

8+
import static io.opentelemetry.instrumentation.testing.junit.code.SemconvCodeStabilityUtil.codeAttributesLogCount;
9+
810
import ch.qos.logback.classic.LoggerContext;
911
import ch.qos.logback.classic.util.ContextInitializer;
1012
import ch.qos.logback.core.spi.ContextAware;
@@ -74,6 +76,6 @@ void twoLogs() {
7476
.hasResource(resource)
7577
.hasInstrumentationScope(instrumentationScopeInfo)
7678
.hasBody("log message 1")
77-
.hasTotalAttributeCount(3));
79+
.hasTotalAttributeCount(codeAttributesLogCount()));
7880
}
7981
}

0 commit comments

Comments
 (0)