Skip to content
Draft
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 @@ -13,6 +13,7 @@
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import io.opentelemetry.sdk.internal.ExceptionAttributeResolver;
import io.opentelemetry.sdk.internal.ExtendedAttributesMap;
import java.time.Instant;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -44,7 +45,7 @@ public ExtendedSdkLogRecordBuilder setException(Throwable throwable) {
loggerSharedState
.getExceptionAttributeResolver()
.setExceptionAttributes(
this::setAttribute,
new ExceptionAttributeSetterWithPrecedence(),
Copy link
Member

Choose a reason for hiding this comment

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

Could add a private method setExceptionAttribute (or similar) with the same signature as setAttribute. This would avoid allocating the extra ExceptionAttributeSetterWithPrecedence object. Not that its hugely important because we're in the setException code path which is allocation-heavy.

throwable,
loggerSharedState.getLogLimits().getMaxAttributeValueLength());

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

/**
* AttributeSetter that only sets attributes if they haven't already been set by the user. This
* ensures user-set attributes take precedence over exception-derived attributes.
*/
private class ExceptionAttributeSetterWithPrecedence
implements ExceptionAttributeResolver.AttributeSetter {

@Override
public <T> void setAttribute(AttributeKey<T> key, @javax.annotation.Nullable T value) {
if (key == null || key.getKey().isEmpty() || value == null) {
return;
}
// Only set the attribute if it hasn't been set already
if (extendedAttributes == null || extendedAttributes.get(key) == null) {
ExtendedSdkLogRecordBuilder.this.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())
.setException(new Exception("error"))
.setAttribute(EXCEPTION_MESSAGE, "custom message")
.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?


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