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

Tweak Guava WithSpan test pattern. #5744

Merged
merged 1 commit into from
Apr 4, 2022
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 @@ -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() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went with this name expecting to extract an interface in a shared location

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);
}
}