Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public ExtendedSdkLogRecordBuilder setException(Throwable throwable) {
loggerSharedState
.getExceptionAttributeResolver()
.setExceptionAttributes(
this::setAttribute,
this::setExceptionAttribute,
throwable,
loggerSharedState.getLogLimits().getMaxAttributeValueLength());

Expand Down Expand Up @@ -154,4 +154,17 @@ public void emit() {
body,
extendedAttributes));
}

/**
* Sets an exception-derived attribute only if it hasn't already been set by the user. This
* ensures user-set attributes take precedence over exception-derived attributes.
*/
private <T> void setExceptionAttribute(AttributeKey<T> key, @Nullable T value) {
if (key == null || key.getKey().isEmpty() || value == null) {
return;
}
if (extendedAttributes == null || extendedAttributes.get(key) == null) {
setAttribute(key, value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,27 @@ public void setExceptionAttributes(
equalTo(EXCEPTION_TYPE, "type"),
equalTo(EXCEPTION_STACKTRACE, "stacktrace")));
}

@Test
void setException_UserAttributesTakePrecedence() {
Logger logger = loggerProviderBuilder.build().get("logger");

((ExtendedLogRecordBuilder) logger.logRecordBuilder())
.setAttribute(EXCEPTION_MESSAGE, "custom message")
.setException(new Exception("error"))
.emit();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user exception.message should take priority regardless of the invocation order yeah?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


assertThat(exporter.getFinishedLogRecordItems())
.satisfiesExactly(
logRecord ->
assertThat(logRecord)
.hasAttributesSatisfyingExactly(
equalTo(EXCEPTION_TYPE, "java.lang.Exception"),
equalTo(EXCEPTION_MESSAGE, "custom message"),
satisfies(
EXCEPTION_STACKTRACE,
stacktrace ->
stacktrace.startsWith(
"java.lang.Exception: error" + System.lineSeparator()))));
}
}
Loading