Add an overload to the recordException method to support additional attributes (#1599)

* Add an overload to the recordException method to support additional attributes

* Fix Codecov

* Fix review

* Fix review
This commit is contained in:
dengliming 2020-08-29 03:41:36 +08:00 committed by GitHub
parent c6c179c267
commit ed08e9f287
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 55 additions and 1 deletions

View File

@ -99,6 +99,9 @@ public final class DefaultSpan implements Span {
@Override
public void recordException(Throwable exception) {}
@Override
public void recordException(Throwable exception, Attributes additionalAttributes) {}
@Override
public void updateName(String name) {}

View File

@ -229,6 +229,15 @@ public interface Span {
*/
void recordException(Throwable exception);
/**
* Records information about the {@link Throwable} to the {@link Span}.
*
* @param exception the {@link Throwable} to record.
* @param additionalAttributes the additional {@link Attributes} to record.
* @since 0.8.0
*/
void recordException(Throwable exception, Attributes additionalAttributes);
/**
* Updates the {@code Span} name.
*

View File

@ -60,6 +60,7 @@ class DefaultSpanTest {
span.addEvent((Event) null);
span.setStatus(Status.OK);
span.recordException(new IllegalStateException());
span.recordException(new IllegalStateException(), Attributes.empty());
span.end();
span.end(EndSpanOptions.getDefault());
span.end(null);

View File

@ -428,11 +428,17 @@ final class RecordEventsReadableSpan implements ReadWriteSpan {
@Override
public void recordException(Throwable exception) {
recordException(exception, null);
}
@Override
public void recordException(Throwable exception, Attributes additionalAttributes) {
if (exception == null) {
return;
}
long timestamp = clock.now();
Attributes.Builder attributes = Attributes.newBuilder();
Attributes.Builder attributes =
additionalAttributes != null ? additionalAttributes.toBuilder() : Attributes.newBuilder();
SemanticAttributes.EXCEPTION_TYPE.set(attributes, exception.getClass().getCanonicalName());
if (exception.getMessage() != null) {
SemanticAttributes.EXCEPTION_MESSAGE.set(attributes, exception.getMessage());

View File

@ -720,6 +720,41 @@ class RecordEventsReadableSpanTest {
"io.opentelemetry.sdk.trace.RecordEventsReadableSpanTest.InnerClassException"));
}
@Test
void recordException_additionalAttributes() {
IllegalStateException exception = new IllegalStateException("there was an exception");
RecordEventsReadableSpan span = createTestRootSpan();
StringWriter writer = new StringWriter();
exception.printStackTrace(new PrintWriter(writer));
String stacktrace = writer.toString();
testClock.advanceNanos(1000);
long timestamp = testClock.now();
span.recordException(
exception,
Attributes.of(
"key1",
stringAttributeValue("this is an additional attribute"),
"exception.message",
stringAttributeValue("this is a precedence attribute")));
List<Event> events = span.toSpanData().getEvents();
assertThat(events).hasSize(1);
Event event = events.get(0);
assertThat(event.getName()).isEqualTo("exception");
assertThat(event.getEpochNanos()).isEqualTo(timestamp);
assertThat(event.getAttributes())
.isEqualTo(
Attributes.newBuilder()
.setAttribute("key1", "this is an additional attribute")
.setAttribute("exception.type", "java.lang.IllegalStateException")
.setAttribute("exception.message", "this is a precedence attribute")
.setAttribute("exception.stacktrace", stacktrace)
.build());
}
@Test
void badArgsIgnored() {
RecordEventsReadableSpan span = createTestRootSpan();