Skip to content

Commit

Permalink
Intercept and report exceptions when using the WithSpan annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
brunobat committed May 15, 2023
1 parent 564e6a5 commit e723c0b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import static io.opentelemetry.api.trace.SpanKind.CLIENT;
import static io.opentelemetry.api.trace.SpanKind.INTERNAL;
import static io.opentelemetry.api.trace.SpanKind.SERVER;
import static io.opentelemetry.api.trace.StatusCode.ERROR;
import static io.quarkus.opentelemetry.deployment.common.TestSpanExporter.getSpanByKindAndParentId;
import static java.net.HttpURLConnection.HTTP_OK;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;

import java.util.List;

Expand All @@ -27,6 +31,7 @@
import io.opentelemetry.instrumentation.annotations.SpanAttribute;
import io.opentelemetry.instrumentation.annotations.WithSpan;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.internal.data.ExceptionEventData;
import io.quarkus.opentelemetry.deployment.common.TestSpanExporter;
import io.quarkus.opentelemetry.deployment.common.TestSpanExporterProvider;
import io.quarkus.runtime.StartupEvent;
Expand Down Expand Up @@ -62,6 +67,7 @@ void span() {
List<SpanData> spanItems = spanExporter.getFinishedSpanItems(1);
assertEquals("SpanBean.span", spanItems.get(0).getName());
assertEquals(INTERNAL, spanItems.get(0).getKind());
assertNotEquals(ERROR, spanItems.get(0).getStatus().getStatusCode());
}

@Test
Expand Down Expand Up @@ -112,13 +118,37 @@ void spanCdiRest() {
final SpanData server = getSpanByKindAndParentId(spans, SERVER, client.getSpanId());
}

@Test
void spanWithException() {
try {
spanBean.spanWithException();
fail("Exception expected");
} catch (Exception e) {
assertThrows(RuntimeException.class, () -> {
throw e;
});
}
List<SpanData> spanItems = spanExporter.getFinishedSpanItems(1);
assertEquals("SpanBean.spanWithException", spanItems.get(0).getName());
assertEquals(INTERNAL, spanItems.get(0).getKind());
assertEquals(ERROR, spanItems.get(0).getStatus().getStatusCode());
assertEquals(1, spanItems.get(0).getEvents().size());
assertEquals("spanWithException for tests",
((ExceptionEventData) spanItems.get(0).getEvents().get(0)).getException().getMessage());
}

@ApplicationScoped
public static class SpanBean {
@WithSpan
public void span() {

}

@WithSpan
public void spanWithException() {
throw new RuntimeException("spanWithException for tests");
}

@WithSpan("name")
public void spanName() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public Object span(final ArcInvocationContext invocationContext) throws Exceptio
}

return result;
} catch (Throwable t) {
if (shouldStart) {
instrumenter.end(spanContext, methodRequest, null, t);
}
throw t;
} finally {
if (scope != null) {
scope.close();
Expand Down

0 comments on commit e723c0b

Please sign in to comment.