Skip to content

Commit

Permalink
Tweak Guava WithSpan test pattern. (#5744)
Browse files Browse the repository at this point in the history
  • Loading branch information
anuraaga authored Apr 4, 2022
1 parent 2b7fe69 commit 79cd093
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
package io.opentelemetry.javaagent.instrumentation.guava;

import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.attributeEntry;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.SettableFuture;
import com.google.common.util.concurrent.UncheckedExecutionException;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
Expand All @@ -24,15 +27,14 @@ class GuavaWithSpanInstrumentationTest {

@Test
void success() {
SettableFuture<String> future = SettableFuture.create();
new TracedWithSpan().listenableFuture(future);
SettableFuture<String> future = new TracedWithSpan().completable();
future.set("Value");

testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("TracedWithSpan.listenableFuture")
span.hasName("TracedWithSpan.completable")
.hasKind(SpanKind.INTERNAL)
.hasNoParent()
.hasAttributes(Attributes.empty())));
Expand All @@ -41,15 +43,14 @@ void success() {
@Test
void failure() {
IllegalArgumentException error = new IllegalArgumentException("Boom");
SettableFuture<String> future = SettableFuture.create();
new TracedWithSpan().listenableFuture(future);
SettableFuture<String> future = new TracedWithSpan().completable();
future.setException(error);

testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("TracedWithSpan.listenableFuture")
span.hasName("TracedWithSpan.completable")
.hasKind(SpanKind.INTERNAL)
.hasNoParent()
.hasStatus(StatusData.error())
Expand All @@ -59,48 +60,48 @@ void failure() {

@Test
void canceled() {
SettableFuture<String> future = SettableFuture.create();
new TracedWithSpan().listenableFuture(future);
SettableFuture<String> future = new TracedWithSpan().completable();
future.cancel(true);

testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("TracedWithSpan.listenableFuture")
span.hasName("TracedWithSpan.completable")
.hasKind(SpanKind.INTERNAL)
.hasNoParent()
.hasAttributes(attributeEntry("guava.canceled", true))));
}

@Test
void immediateSuccess() {
new TracedWithSpan().listenableFuture(Futures.immediateFuture("Value"));
assertThat(Futures.getUnchecked(new TracedWithSpan().alreadySucceeded())).isEqualTo("Value");

testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("TracedWithSpan.listenableFuture")
span.hasName("TracedWithSpan.alreadySucceeded")
.hasKind(SpanKind.INTERNAL)
.hasNoParent()
.hasAttributes(Attributes.empty())));
}

@Test
void immediateFailure() {
IllegalArgumentException error = new IllegalArgumentException("Boom");
new TracedWithSpan().listenableFuture(Futures.immediateFailedFuture(error));
assertThatThrownBy(() -> Futures.getUnchecked(new TracedWithSpan().alreadyFailed()))
.isInstanceOf(UncheckedExecutionException.class)
.hasCause(TracedWithSpan.FAILURE);

testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("TracedWithSpan.listenableFuture")
span.hasName("TracedWithSpan.alreadyFailed")
.hasKind(SpanKind.INTERNAL)
.hasNoParent()
.hasStatus(StatusData.error())
.hasException(error)
.hasException(TracedWithSpan.FAILURE)
.hasAttributes(Attributes.empty())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,26 @@

package io.opentelemetry.javaagent.instrumentation.guava;

import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.SettableFuture;
import io.opentelemetry.extension.annotations.WithSpan;

final class TracedWithSpan {
static final IllegalArgumentException FAILURE = new IllegalArgumentException("Boom");

@WithSpan
SettableFuture<String> completable() {
return SettableFuture.create();
}

@WithSpan
ListenableFuture<String> alreadySucceeded() {
return Futures.immediateFuture("Value");
}

@WithSpan
ListenableFuture<String> listenableFuture(ListenableFuture<String> future) {
return future;
ListenableFuture<String> alreadyFailed() {
return Futures.immediateFailedFuture(FAILURE);
}
}

0 comments on commit 79cd093

Please sign in to comment.