Skip to content
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

Fix regression in event attributes #6865

Merged
merged 2 commits into from
Nov 9, 2024
Merged
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 @@ -445,8 +445,7 @@ public ReadWriteSpan setStatus(StatusCode statusCode, @Nullable String descripti

@Override
public ReadWriteSpan recordException(Throwable exception) {
Attributes attributes = this.getAttributes();
recordException(exception, attributes);
recordException(exception, Attributes.empty());
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,7 @@ void attributeLength() {
assertThat(event.getAttributes().get(stringKey("exception.message"))).isEqualTo(strVal);
assertThat(event.getAttributes().get(stringKey("exception.stacktrace")).length())
.isLessThanOrEqualTo(maxLength);
assertThat(event.getAttributes().size()).isEqualTo(3);
} finally {
span.end();
}
Expand Down Expand Up @@ -1159,6 +1160,9 @@ void recordException() {
testClock.advance(Duration.ofNanos(1000));
long timestamp = testClock.now();

// make sure that span attributes don't leak down to the exception event
span.setAttribute("spankey", "val");

span.recordException(exception);

List<EventData> events = span.toSpanData().getEvents();
Expand All @@ -1171,6 +1175,7 @@ void recordException() {
assertThat(event.getAttributes().get(stringKey("exception.type")))
.isEqualTo(exception.getClass().getName());
assertThat(event.getAttributes().get(stringKey("exception.stacktrace"))).isEqualTo(stacktrace);
assertThat(event.getAttributes().size()).isEqualTo(3);
assertThat(event)
.isInstanceOfSatisfying(
ExceptionEventData.class,
Expand All @@ -1184,12 +1189,20 @@ void recordException_noMessage() {
IllegalStateException exception = new IllegalStateException();
SdkSpan span = createTestRootSpan();

StringWriter writer = new StringWriter();
exception.printStackTrace(new PrintWriter(writer));
String stacktrace = writer.toString();

span.recordException(exception);

List<EventData> events = span.toSpanData().getEvents();
assertThat(events).hasSize(1);
EventData event = events.get(0);
assertThat(event.getAttributes().get(stringKey("exception.message"))).isNull();
assertThat(event.getAttributes().get(stringKey("exception.type")))
.isEqualTo("java.lang.IllegalStateException");
assertThat(event.getAttributes().get(stringKey("exception.stacktrace"))).isEqualTo(stacktrace);
assertThat(event.getAttributes().size()).isEqualTo(2);
}

private static class InnerClassException extends Exception {}
Expand All @@ -1199,13 +1212,19 @@ void recordException_innerClassException() {
InnerClassException exception = new InnerClassException();
SdkSpan span = createTestRootSpan();

StringWriter writer = new StringWriter();
exception.printStackTrace(new PrintWriter(writer));
String stacktrace = writer.toString();

span.recordException(exception);

List<EventData> events = span.toSpanData().getEvents();
assertThat(events).hasSize(1);
EventData event = events.get(0);
assertThat(event.getAttributes().get(stringKey("exception.type")))
.isEqualTo("io.opentelemetry.sdk.trace.SdkSpanTest.InnerClassException");
assertThat(event.getAttributes().get(stringKey("exception.stacktrace"))).isEqualTo(stacktrace);
assertThat(event.getAttributes().size()).isEqualTo(2);
}

@Test
Expand All @@ -1220,6 +1239,9 @@ void recordException_additionalAttributes() {
testClock.advance(Duration.ofNanos(1000));
long timestamp = testClock.now();

// make sure that span attributes don't leak down to the exception event
span.setAttribute("spankey", "val");

span.recordException(
exception,
Attributes.of(
Expand All @@ -1240,6 +1262,7 @@ void recordException_additionalAttributes() {
assertThat(event.getAttributes().get(stringKey("exception.type")))
.isEqualTo("java.lang.IllegalStateException");
assertThat(event.getAttributes().get(stringKey("exception.stacktrace"))).isEqualTo(stacktrace);
assertThat(event.getAttributes().size()).isEqualTo(4);

assertThat(event)
.isInstanceOfSatisfying(
Expand Down
Loading