Skip to content

Commit

Permalink
[release/v1.44.x] Fix regression in event attributes (#6866)
Browse files Browse the repository at this point in the history
Co-authored-by: Trask Stalnaker <[email protected]>
  • Loading branch information
opentelemetrybot and trask authored Nov 9, 2024
1 parent 08349dc commit d694852
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
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

0 comments on commit d694852

Please sign in to comment.