-
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 8 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 |
---|---|---|
|
@@ -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 you add code attributes assertions to the javaagent test too?
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.
Because of the nature of the Groovy test, the attribute values are not very readable:
The attribute values are checked in a library test.
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.
Eh, forgot that was groovy. Nevermind then 👍
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.
ya, it would be nice to have validation that the attributes are working inside of the Java agent as well, but can get to that later, I've opened #6604 to track