-
Notifications
You must be signed in to change notification settings - Fork 872
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add code attributes for Logback #6591
Changes from all commits
7528865
1ebe9f4
d9bbf85
52f05e8
8d1cf4d
df6d85a
6d9d706
5db9ded
887fe3a
e363a1f
dba1c75
8037182
33b65c5
4dd5c6b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,13 @@ class LogbackTest extends AgentInstrumentationSpecification { | |
waitForTraces(1) | ||
} | ||
|
||
String jvmVersion = System.getProperty("java.vm.specification.version") | ||
int codeAttributes = 3 | ||
boolean jvmVersionGreaterThanOrEqualTo18 = !jvmVersion.startsWith("1.8") && Integer.parseInt(jvmVersion) >= 18 | ||
if (jvmVersionGreaterThanOrEqualTo18) { | ||
codeAttributes = 4 // Java 18 specificity on line number (lineNumber > 0 check) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if this is because of groovy, I would be surprised to see different behavior for a regular Java class, I may try to convert this test to Java after merging it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line number is negative for Java <= 17 and this Groovy test, everything is fine for the library test based on JUnit 5 |
||
} | ||
|
||
if (severity != null) { | ||
await() | ||
.untilAsserted( | ||
|
@@ -56,12 +63,12 @@ class LogbackTest extends AgentInstrumentationSpecification { | |
assertThat(log.getSeverity()).isEqualTo(severity) | ||
assertThat(log.getSeverityText()).isEqualTo(severityText) | ||
if (exception) { | ||
assertThat(log.getAttributes().size()).isEqualTo(5) | ||
assertThat(log.getAttributes().size()).isEqualTo(5 + codeAttributes) | ||
assertThat(log.getAttributes().get(SemanticAttributes.EXCEPTION_TYPE)).isEqualTo(IllegalStateException.getName()) | ||
assertThat(log.getAttributes().get(SemanticAttributes.EXCEPTION_MESSAGE)).isEqualTo("hello") | ||
assertThat(log.getAttributes().get(SemanticAttributes.EXCEPTION_STACKTRACE)).contains(LogbackTest.name) | ||
} else { | ||
assertThat(log.getAttributes().size()).isEqualTo(2) | ||
assertThat(log.getAttributes().size()).isEqualTo(2 + codeAttributes) | ||
assertThat(log.getAttributes().get(SemanticAttributes.EXCEPTION_TYPE)).isNull() | ||
assertThat(log.getAttributes().get(SemanticAttributes.EXCEPTION_MESSAGE)).isNull() | ||
assertThat(log.getAttributes().get(SemanticAttributes.EXCEPTION_STACKTRACE)).isNull() | ||
|
@@ -123,7 +130,7 @@ class LogbackTest extends AgentInstrumentationSpecification { | |
assertThat(log.getInstrumentationScopeInfo().getName()).isEqualTo("abc") | ||
assertThat(log.getSeverity()).isEqualTo(Severity.INFO) | ||
assertThat(log.getSeverityText()).isEqualTo("INFO") | ||
assertThat(log.getAttributes().size()).isEqualTo(4) | ||
assertThat(log.getAttributes().size()).isEqualTo(3 + 3) // 3 code attributes | ||
assertThat(log.getAttributes().get(AttributeKey.stringKey("logback.mdc.key1"))).isEqualTo("val1") | ||
assertThat(log.getAttributes().get(AttributeKey.stringKey("logback.mdc.key2"))).isEqualTo("val2") | ||
assertThat(log.getAttributes().get(SemanticAttributes.THREAD_NAME)).isEqualTo(Thread.currentThread().getName()) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ public class OpenTelemetryAppender extends UnsynchronizedAppenderBase<ILoggingEv | |
new LogEmitterProviderHolder(); | ||
|
||
private volatile boolean captureExperimentalAttributes = false; | ||
private volatile boolean captureCodeAttributes = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted first that the |
||
private volatile List<String> captureMdcAttributes = emptyList(); | ||
|
||
private volatile LoggingEventMapper mapper; | ||
|
@@ -33,7 +34,9 @@ public OpenTelemetryAppender() {} | |
|
||
@Override | ||
public void start() { | ||
mapper = new LoggingEventMapper(captureExperimentalAttributes, captureMdcAttributes); | ||
mapper = | ||
new LoggingEventMapper( | ||
captureExperimentalAttributes, captureMdcAttributes, captureCodeAttributes); | ||
super.start(); | ||
} | ||
|
||
|
@@ -62,6 +65,18 @@ public void setCaptureExperimentalAttributes(boolean captureExperimentalAttribut | |
this.captureExperimentalAttributes = captureExperimentalAttributes; | ||
} | ||
|
||
/** | ||
* Sets whether the code attributes (file name, class name, method name and line number) should be | ||
* set to logs. Enabling these attributes can potentially impact performance (see | ||
* https://logback.qos.ch/manual/layouts.html). | ||
* | ||
* @param captureCodeAttributes To enable or disable the code attributes (file name, class name, | ||
* method name and line number) | ||
*/ | ||
public void setCaptureCodeAttributes(boolean captureCodeAttributes) { | ||
this.captureCodeAttributes = captureCodeAttributes; | ||
} | ||
|
||
/** Configures the {@link MDC} attributes that will be copied to logs. */ | ||
public void setCaptureMdcAttributes(String attributes) { | ||
if (attributes != null) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,6 @@ | |
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; | ||
|
||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.api.trace.SpanContext; | ||
import io.opentelemetry.context.Scope; | ||
|
@@ -71,7 +70,7 @@ void logNoSpan() { | |
assertThat(logData.getResource()).isEqualTo(resource); | ||
assertThat(logData.getInstrumentationScopeInfo()).isEqualTo(instrumentationScopeInfo); | ||
assertThat(logData.getBody().asString()).isEqualTo("log message 1"); | ||
assertThat(logData.getAttributes()).isEqualTo(Attributes.empty()); | ||
assertThat(logData.getAttributes().size()).isEqualTo(4); // 4 code attributes | ||
} | ||
|
||
@Test | ||
|
@@ -115,13 +114,27 @@ void logWithExtras() { | |
.isLessThan(TimeUnit.MILLISECONDS.toNanos(Instant.now().toEpochMilli())); | ||
assertThat(logData.getSeverity()).isEqualTo(Severity.INFO); | ||
assertThat(logData.getSeverityText()).isEqualTo("INFO"); | ||
assertThat(logData.getAttributes().size()).isEqualTo(3); | ||
assertThat(logData.getAttributes().size()).isEqualTo(3 + 4); // 4 code attributes | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WDYT about refactoring this to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was not aware about these classes. Thanks! I have a first look at the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, that sounds fine 👍 |
||
assertThat(logData.getAttributes().get(SemanticAttributes.EXCEPTION_TYPE)) | ||
.isEqualTo(IllegalStateException.class.getName()); | ||
assertThat(logData.getAttributes().get(SemanticAttributes.EXCEPTION_MESSAGE)) | ||
.isEqualTo("Error!"); | ||
assertThat(logData.getAttributes().get(SemanticAttributes.EXCEPTION_STACKTRACE)) | ||
.contains("logWithExtras"); | ||
|
||
String file = logData.getAttributes().get(SemanticAttributes.CODE_FILEPATH); | ||
assertThat(file).isEqualTo("OpenTelemetryAppenderConfigTest.java"); | ||
|
||
String codeClass = logData.getAttributes().get(SemanticAttributes.CODE_NAMESPACE); | ||
assertThat(codeClass) | ||
.isEqualTo( | ||
"io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppenderConfigTest"); | ||
|
||
String method = logData.getAttributes().get(SemanticAttributes.CODE_FUNCTION); | ||
assertThat(method).isEqualTo("logWithExtras"); | ||
|
||
Long lineNumber = logData.getAttributes().get(SemanticAttributes.CODE_LINENO); | ||
assertThat(lineNumber).isGreaterThan(1); | ||
} | ||
|
||
@Test | ||
|
@@ -140,7 +153,7 @@ void logContextData() { | |
assertThat(logData.getResource()).isEqualTo(resource); | ||
assertThat(logData.getInstrumentationScopeInfo()).isEqualTo(instrumentationScopeInfo); | ||
assertThat(logData.getBody().asString()).isEqualTo("log message 1"); | ||
assertThat(logData.getAttributes().size()).isEqualTo(2); | ||
assertThat(logData.getAttributes().size()).isEqualTo(2 + 4); // 4 code attributes | ||
AssertionsForClassTypes.assertThat( | ||
logData.getAttributes().get(AttributeKey.stringKey("logback.mdc.key1"))) | ||
.isEqualTo("val1"); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we use this instead? (this is the property we check for java version in the agent)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No.
System.getProperty("java.version")
contains the minor JVM version and the number parsing just after will fail