From 09934b965530b53ae2f4f731316ac4ffac03f2a3 Mon Sep 17 00:00:00 2001 From: Jay DeLuca Date: Tue, 24 Dec 2024 06:36:33 -0500 Subject: [PATCH] update withspan tests --- ...dingSpanAttributesInstrumentationTest.java | 162 ++--- .../WithSpanInstrumentationTest.java | 578 ++++++------------ 2 files changed, 252 insertions(+), 488 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/AddingSpanAttributesInstrumentationTest.java b/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/AddingSpanAttributesInstrumentationTest.java index 686a8264f92e..2ec738d7a422 100644 --- a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/AddingSpanAttributesInstrumentationTest.java +++ b/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/AddingSpanAttributesInstrumentationTest.java @@ -5,15 +5,15 @@ package io.opentelemetry.test.annotation; +import static io.opentelemetry.api.common.AttributeKey.stringKey; import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; -import static org.assertj.core.api.Assertions.entry; +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; +import static io.opentelemetry.semconv.incubating.CodeIncubatingAttributes.CODE_FUNCTION; +import static io.opentelemetry.semconv.incubating.CodeIncubatingAttributes.CODE_NAMESPACE; -import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.trace.Span; -import io.opentelemetry.api.trace.SpanId; import io.opentelemetry.api.trace.SpanKind; import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; -import io.opentelemetry.semconv.incubating.CodeIncubatingAttributes; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; @@ -24,89 +24,60 @@ class AddingSpanAttributesInstrumentationTest { AgentInstrumentationExtension.create(); @Test - void captureAttributesInNewSpan() throws Exception { - + void captureAttributesInNewSpan() { testing.runWithSpan( "root", () -> new ExtractAttributesUsingAddingSpanAttributes() .withSpanTakesPrecedence("foo", "bar", null, "baz")); - assertThat(testing.waitForTraces(1)) - .satisfiesExactly( - trace -> - assertThat(trace) - .satisfiesExactly( - span -> - assertThat(span) - .hasName("root") - .hasKind(SpanKind.INTERNAL) - .hasParentSpanId(SpanId.getInvalid()), - span -> - assertThat(span) - .hasName( - "ExtractAttributesUsingAddingSpanAttributes.withSpanTakesPrecedence") - .hasKind(SpanKind.INTERNAL) - .hasParentSpanId(trace.get(0).getSpanId()) - .hasAttributesSatisfying( - attributes -> - assertThat(attributes) - .containsOnly( - entry( - CodeIncubatingAttributes.CODE_NAMESPACE, - ExtractAttributesUsingAddingSpanAttributes.class - .getName()), - entry( - CodeIncubatingAttributes.CODE_FUNCTION, - "withSpanTakesPrecedence"), - entry( - AttributeKey.stringKey("implicitName"), "foo"), - entry( - AttributeKey.stringKey("explicitName"), - "bar"))))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> span.hasName("root").hasKind(SpanKind.INTERNAL).hasNoParent(), + span -> + span.hasName( + "ExtractAttributesUsingAddingSpanAttributes.withSpanTakesPrecedence") + .hasKind(SpanKind.INTERNAL) + .hasParentSpanId(trace.getSpan(0).getSpanId()) + .hasAttributesSatisfyingExactly( + equalTo( + CODE_NAMESPACE, + ExtractAttributesUsingAddingSpanAttributes.class.getName()), + equalTo(CODE_FUNCTION, "withSpanTakesPrecedence"), + equalTo(stringKey("implicitName"), "foo"), + equalTo(stringKey("explicitName"), "bar")))); } @Test - void captureAttributesInCurrentSpan() throws Exception { - + void captureAttributesInCurrentSpan() { testing.runWithSpan( "root", () -> new ExtractAttributesUsingAddingSpanAttributes() .withSpanAttributes("foo", "bar", null, "baz")); - assertThat(testing.waitForTraces(1)) - .satisfiesExactly( - trace -> - assertThat(trace) - .satisfiesExactly( - span -> - assertThat(span) - .hasName("root") - .hasKind(SpanKind.INTERNAL) - .hasParentSpanId(SpanId.getInvalid()) - .hasAttributesSatisfying( - attributes -> - assertThat(attributes) - .containsOnly( - entry( - AttributeKey.stringKey("implicitName"), "foo"), - entry( - AttributeKey.stringKey("explicitName"), - "bar"))))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("root") + .hasKind(SpanKind.INTERNAL) + .hasNoParent() + .hasAttributesSatisfyingExactly( + equalTo(stringKey("implicitName"), "foo"), + equalTo(stringKey("explicitName"), "bar")))); } @Test - void noExistingSpan() throws Exception { - + void noExistingSpan() { new ExtractAttributesUsingAddingSpanAttributes().withSpanAttributes("foo", "bar", null, "baz"); assertThat(testing.waitForTraces(0)).isEmpty(); } @Test - void overwriteAttributes() throws Exception { - + void overwriteAttributes() { testing.runWithSpan( "root", () -> { @@ -116,31 +87,21 @@ void overwriteAttributes() throws Exception { .withSpanAttributes("foo", "bar", null, "baz"); }); - assertThat(testing.waitForTraces(1)) - .satisfiesExactly( - trace -> - assertThat(trace) - .satisfiesExactly( - span -> - assertThat(span) - .hasName("root") - .hasKind(SpanKind.INTERNAL) - .hasParentSpanId(SpanId.getInvalid()) - .hasAttributesSatisfying( - attributes -> - assertThat(attributes) - .containsOnly( - entry(AttributeKey.stringKey("keep"), "willbekept"), - entry( - AttributeKey.stringKey("implicitName"), "foo"), - entry( - AttributeKey.stringKey("explicitName"), - "bar"))))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("root") + .hasKind(SpanKind.INTERNAL) + .hasNoParent() + .hasAttributesSatisfyingExactly( + equalTo(stringKey("keep"), "willbekept"), + equalTo(stringKey("implicitName"), "foo"), + equalTo(stringKey("explicitName"), "bar")))); } @Test - void multiMethodOverwriteAttributes() throws Exception { - + void multiMethodOverwriteAttributes() { testing.runWithSpan( "root", () -> { @@ -150,25 +111,16 @@ void multiMethodOverwriteAttributes() throws Exception { .withSpanAttributesParent("parentbegone", "parentbegone", null, "parentbegone"); }); - assertThat(testing.waitForTraces(1)) - .satisfiesExactly( - trace -> - assertThat(trace) - .satisfiesExactly( - span -> - assertThat(span) - .hasName("root") - .hasKind(SpanKind.INTERNAL) - .hasParentSpanId(SpanId.getInvalid()) - .hasAttributesSatisfying( - attributes -> - assertThat(attributes) - .containsOnly( - entry(AttributeKey.stringKey("keep"), "willbekept"), - entry( - AttributeKey.stringKey("implicitName"), "foo"), - entry( - AttributeKey.stringKey("explicitName"), - "bar"))))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("root") + .hasKind(SpanKind.INTERNAL) + .hasNoParent() + .hasAttributesSatisfyingExactly( + equalTo(stringKey("keep"), "willbekept"), + equalTo(stringKey("implicitName"), "foo"), + equalTo(stringKey("explicitName"), "bar")))); } } diff --git a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/WithSpanInstrumentationTest.java b/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/WithSpanInstrumentationTest.java index aa15a47c5ce6..93fe05a74076 100644 --- a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/WithSpanInstrumentationTest.java +++ b/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/WithSpanInstrumentationTest.java @@ -5,17 +5,18 @@ package io.opentelemetry.test.annotation; +import static io.opentelemetry.api.common.AttributeKey.stringKey; import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.entry; +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; +import static io.opentelemetry.semconv.incubating.CodeIncubatingAttributes.CODE_FUNCTION; +import static io.opentelemetry.semconv.incubating.CodeIncubatingAttributes.CODE_NAMESPACE; -import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.trace.SpanId; import io.opentelemetry.api.trace.SpanKind; import io.opentelemetry.instrumentation.annotations.WithSpan; import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; import io.opentelemetry.sdk.trace.data.StatusData; -import io.opentelemetry.semconv.incubating.CodeIncubatingAttributes; import java.lang.reflect.Modifier; import java.util.concurrent.CompletableFuture; import net.bytebuddy.ByteBuddy; @@ -32,135 +33,82 @@ class WithSpanInstrumentationTest { @RegisterExtension - public static final AgentInstrumentationExtension testing = + private static final AgentInstrumentationExtension testing = AgentInstrumentationExtension.create(); @Test - void deriveAutomaticName() throws Exception { - + void deriveAutomaticName() { new TracedWithSpan().otel(); - assertThat(testing.waitForTraces(1)) - .satisfiesExactly( - trace -> - assertThat(trace) - .satisfiesExactly( - span -> - assertThat(span) - .hasName("TracedWithSpan.otel") - .hasKind(SpanKind.INTERNAL) - .hasParentSpanId(SpanId.getInvalid()) - .hasAttributesSatisfying( - attributes -> - assertThat(attributes) - .containsOnly( - entry( - CodeIncubatingAttributes.CODE_NAMESPACE, - TracedWithSpan.class.getName()), - entry( - CodeIncubatingAttributes.CODE_FUNCTION, - "otel"))))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("TracedWithSpan.otel") + .hasKind(SpanKind.INTERNAL) + .hasNoParent() + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, TracedWithSpan.class.getName()), + equalTo(CODE_FUNCTION, "otel")))); } @Test - void manualName() throws Exception { - + void manualName() { new TracedWithSpan().namedOtel(); - assertThat(testing.waitForTraces(1)) - .satisfiesExactly( - trace -> - assertThat(trace) - .satisfiesExactly( - span -> - assertThat(span) - .hasName("manualName") - .hasKind(SpanKind.INTERNAL) - .hasParentSpanId(SpanId.getInvalid()) - .hasAttributesSatisfying( - attributes -> - assertThat(attributes) - .containsOnly( - entry( - CodeIncubatingAttributes.CODE_NAMESPACE, - TracedWithSpan.class.getName()), - entry( - CodeIncubatingAttributes.CODE_FUNCTION, - "namedOtel"))))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("manualName") + .hasKind(SpanKind.INTERNAL) + .hasNoParent() + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, TracedWithSpan.class.getName()), + equalTo(CODE_FUNCTION, "namedOtel")))); } @Test - void manualKind() throws Exception { - + void manualKind() { new TracedWithSpan().someKind(); - assertThat(testing.waitForTraces(1)) - .satisfiesExactly( - trace -> - assertThat(trace) - .satisfiesExactly( - span -> - assertThat(span) - .hasName("TracedWithSpan.someKind") - .hasKind(SpanKind.PRODUCER) - .hasParentSpanId(SpanId.getInvalid()) - .hasAttributesSatisfying( - attributes -> - assertThat(attributes) - .containsOnly( - entry( - CodeIncubatingAttributes.CODE_NAMESPACE, - TracedWithSpan.class.getName()), - entry( - CodeIncubatingAttributes.CODE_FUNCTION, - "someKind"))))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("TracedWithSpan.someKind") + .hasKind(SpanKind.PRODUCER) + .hasParentSpanId(SpanId.getInvalid()) + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, TracedWithSpan.class.getName()), + equalTo(CODE_FUNCTION, "someKind")))); } @Test - void multipleSpans() throws Exception { - + void multipleSpans() { new TracedWithSpan().server(); - assertThat(testing.waitForTraces(1)) - .satisfiesExactly( - trace -> - assertThat(trace) - .satisfiesExactly( - span -> - assertThat(span) - .hasName("TracedWithSpan.server") - .hasKind(SpanKind.SERVER) - .hasParentSpanId(SpanId.getInvalid()) - .hasAttributesSatisfying( - attributes -> - assertThat(attributes) - .containsOnly( - entry( - CodeIncubatingAttributes.CODE_NAMESPACE, - TracedWithSpan.class.getName()), - entry( - CodeIncubatingAttributes.CODE_FUNCTION, - "server"))), - span -> - assertThat(span) - .hasName("TracedWithSpan.otel") - .hasKind(SpanKind.INTERNAL) - .hasParentSpanId(trace.get(0).getSpanId()) - .hasAttributesSatisfying( - attributes -> - assertThat(attributes) - .containsOnly( - entry( - CodeIncubatingAttributes.CODE_NAMESPACE, - TracedWithSpan.class.getName()), - entry( - CodeIncubatingAttributes.CODE_FUNCTION, - "otel"))))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("TracedWithSpan.server") + .hasKind(SpanKind.SERVER) + .hasNoParent() + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, TracedWithSpan.class.getName()), + equalTo(CODE_FUNCTION, "server")), + span -> + span.hasName("TracedWithSpan.otel") + .hasKind(SpanKind.INTERNAL) + .hasParentSpanId(trace.getSpan(0).getSpanId()) + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, TracedWithSpan.class.getName()), + equalTo(CODE_FUNCTION, "otel")))); } @Test void excludedMethod() throws Exception { - new TracedWithSpan().ignored(); Thread.sleep(500); // sleep a bit just to make sure no span is captured @@ -168,93 +116,59 @@ void excludedMethod() throws Exception { } @Test - void completedCompletionStage() throws Exception { - + void completedCompletionStage() { CompletableFuture future = CompletableFuture.completedFuture("Done"); new TracedWithSpan().completionStage(future); - assertThat(testing.waitForTraces(1)) - .satisfiesExactly( - trace -> - assertThat(trace) - .satisfiesExactly( - span -> - assertThat(span) - .hasName("TracedWithSpan.completionStage") - .hasKind(SpanKind.INTERNAL) - .hasParentSpanId(SpanId.getInvalid()) - .hasAttributesSatisfying( - attributes -> - assertThat(attributes) - .containsOnly( - entry( - CodeIncubatingAttributes.CODE_NAMESPACE, - TracedWithSpan.class.getName()), - entry( - CodeIncubatingAttributes.CODE_FUNCTION, - "completionStage"))))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("TracedWithSpan.completionStage") + .hasKind(SpanKind.INTERNAL) + .hasNoParent() + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, TracedWithSpan.class.getName()), + equalTo(CODE_FUNCTION, "completionStage")))); } @Test - void exceptionallyCompletedCompletionStage() throws Exception { - + void exceptionallyCompletedCompletionStage() { CompletableFuture future = new CompletableFuture<>(); future.completeExceptionally(new IllegalArgumentException("Boom")); new TracedWithSpan().completionStage(future); - assertThat(testing.waitForTraces(1)) - .satisfiesExactly( - trace -> - assertThat(trace) - .satisfiesExactly( - span -> - assertThat(span) - .hasName("TracedWithSpan.completionStage") - .hasKind(SpanKind.INTERNAL) - .hasParentSpanId(SpanId.getInvalid()) - .hasStatus(StatusData.error()) - .hasAttributesSatisfying( - attributes -> - assertThat(attributes) - .containsOnly( - entry( - CodeIncubatingAttributes.CODE_NAMESPACE, - TracedWithSpan.class.getName()), - entry( - CodeIncubatingAttributes.CODE_FUNCTION, - "completionStage"))))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("TracedWithSpan.completionStage") + .hasKind(SpanKind.INTERNAL) + .hasNoParent() + .hasStatus(StatusData.error()) + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, TracedWithSpan.class.getName()), + equalTo(CODE_FUNCTION, "completionStage")))); } @Test - void nullCompletionStage() throws Exception { - + void nullCompletionStage() { new TracedWithSpan().completionStage(null); - assertThat(testing.waitForTraces(1)) - .satisfiesExactly( - trace -> - assertThat(trace) - .satisfiesExactly( - span -> - assertThat(span) - .hasName("TracedWithSpan.completionStage") - .hasKind(SpanKind.INTERNAL) - .hasParentSpanId(SpanId.getInvalid()) - .hasAttributesSatisfying( - attributes -> - assertThat(attributes) - .containsOnly( - entry( - CodeIncubatingAttributes.CODE_NAMESPACE, - TracedWithSpan.class.getName()), - entry( - CodeIncubatingAttributes.CODE_FUNCTION, - "completionStage"))))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("TracedWithSpan.completionStage") + .hasKind(SpanKind.INTERNAL) + .hasNoParent() + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, TracedWithSpan.class.getName()), + equalTo(CODE_FUNCTION, "completionStage")))); } @Test void completingCompletionStage() throws Exception { - CompletableFuture future = new CompletableFuture<>(); new TracedWithSpan().completionStage(future); @@ -263,31 +177,20 @@ void completingCompletionStage() throws Exception { future.complete("Done"); - assertThat(testing.waitForTraces(1)) - .satisfiesExactly( - trace -> - assertThat(trace) - .satisfiesExactly( - span -> - assertThat(span) - .hasName("TracedWithSpan.completionStage") - .hasKind(SpanKind.INTERNAL) - .hasParentSpanId(SpanId.getInvalid()) - .hasAttributesSatisfying( - attributes -> - assertThat(attributes) - .containsOnly( - entry( - CodeIncubatingAttributes.CODE_NAMESPACE, - TracedWithSpan.class.getName()), - entry( - CodeIncubatingAttributes.CODE_FUNCTION, - "completionStage"))))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("TracedWithSpan.completionStage") + .hasKind(SpanKind.INTERNAL) + .hasNoParent() + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, TracedWithSpan.class.getName()), + equalTo(CODE_FUNCTION, "completionStage")))); } @Test void exceptionallyCompletingCompletionStage() throws Exception { - CompletableFuture future = new CompletableFuture<>(); new TracedWithSpan().completionStage(future); @@ -296,117 +199,73 @@ void exceptionallyCompletingCompletionStage() throws Exception { future.completeExceptionally(new IllegalArgumentException("Boom")); - assertThat(testing.waitForTraces(1)) - .satisfiesExactly( - trace -> - assertThat(trace) - .satisfiesExactly( - span -> - assertThat(span) - .hasName("TracedWithSpan.completionStage") - .hasKind(SpanKind.INTERNAL) - .hasParentSpanId(SpanId.getInvalid()) - .hasStatus(StatusData.error()) - .hasAttributesSatisfying( - attributes -> - assertThat(attributes) - .containsOnly( - entry( - CodeIncubatingAttributes.CODE_NAMESPACE, - TracedWithSpan.class.getName()), - entry( - CodeIncubatingAttributes.CODE_FUNCTION, - "completionStage"))))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("TracedWithSpan.completionStage") + .hasKind(SpanKind.INTERNAL) + .hasNoParent() + .hasStatus(StatusData.error()) + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, TracedWithSpan.class.getName()), + equalTo(CODE_FUNCTION, "completionStage")))); } @Test - void completedCompletableFuture() throws Exception { - + void completedCompletableFuture() { CompletableFuture future = CompletableFuture.completedFuture("Done"); new TracedWithSpan().completableFuture(future); - assertThat(testing.waitForTraces(1)) - .satisfiesExactly( - trace -> - assertThat(trace) - .satisfiesExactly( - span -> - assertThat(span) - .hasName("TracedWithSpan.completableFuture") - .hasKind(SpanKind.INTERNAL) - .hasParentSpanId(SpanId.getInvalid()) - .hasAttributesSatisfying( - attributes -> - assertThat(attributes) - .containsOnly( - entry( - CodeIncubatingAttributes.CODE_NAMESPACE, - TracedWithSpan.class.getName()), - entry( - CodeIncubatingAttributes.CODE_FUNCTION, - "completableFuture"))))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("TracedWithSpan.completableFuture") + .hasKind(SpanKind.INTERNAL) + .hasNoParent() + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, TracedWithSpan.class.getName()), + equalTo(CODE_FUNCTION, "completableFuture")))); } @Test - void exceptionallyCompletedCompletableFuture() throws Exception { - + void exceptionallyCompletedCompletableFuture() { CompletableFuture future = new CompletableFuture<>(); future.completeExceptionally(new IllegalArgumentException("Boom")); new TracedWithSpan().completableFuture(future); - assertThat(testing.waitForTraces(1)) - .satisfiesExactly( - trace -> - assertThat(trace) - .satisfiesExactly( - span -> - assertThat(span) - .hasName("TracedWithSpan.completableFuture") - .hasKind(SpanKind.INTERNAL) - .hasParentSpanId(SpanId.getInvalid()) - .hasStatus(StatusData.error()) - .hasAttributesSatisfying( - attributes -> - assertThat(attributes) - .containsOnly( - entry( - CodeIncubatingAttributes.CODE_NAMESPACE, - TracedWithSpan.class.getName()), - entry( - CodeIncubatingAttributes.CODE_FUNCTION, - "completableFuture"))))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("TracedWithSpan.completableFuture") + .hasKind(SpanKind.INTERNAL) + .hasNoParent() + .hasStatus(StatusData.error()) + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, TracedWithSpan.class.getName()), + equalTo(CODE_FUNCTION, "completableFuture")))); } @Test - void nullCompletableFuture() throws Exception { - + void nullCompletableFuture() { new TracedWithSpan().completableFuture(null); - assertThat(testing.waitForTraces(1)) - .satisfiesExactly( - trace -> - assertThat(trace) - .satisfiesExactly( - span -> - assertThat(span) - .hasName("TracedWithSpan.completableFuture") - .hasKind(SpanKind.INTERNAL) - .hasParentSpanId(SpanId.getInvalid()) - .hasAttributesSatisfying( - attributes -> - assertThat(attributes) - .containsOnly( - entry( - CodeIncubatingAttributes.CODE_NAMESPACE, - TracedWithSpan.class.getName()), - entry( - CodeIncubatingAttributes.CODE_FUNCTION, - "completableFuture"))))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("TracedWithSpan.completableFuture") + .hasKind(SpanKind.INTERNAL) + .hasNoParent() + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, TracedWithSpan.class.getName()), + equalTo(CODE_FUNCTION, "completableFuture")))); } @Test void completingCompletableFuture() throws Exception { - CompletableFuture future = new CompletableFuture<>(); new TracedWithSpan().completableFuture(future); @@ -415,31 +274,20 @@ void completingCompletableFuture() throws Exception { future.complete("Done"); - assertThat(testing.waitForTraces(1)) - .satisfiesExactly( - trace -> - assertThat(trace) - .satisfiesExactly( - span -> - assertThat(span) - .hasName("TracedWithSpan.completableFuture") - .hasKind(SpanKind.INTERNAL) - .hasParentSpanId(SpanId.getInvalid()) - .hasAttributesSatisfying( - attributes -> - assertThat(attributes) - .containsOnly( - entry( - CodeIncubatingAttributes.CODE_NAMESPACE, - TracedWithSpan.class.getName()), - entry( - CodeIncubatingAttributes.CODE_FUNCTION, - "completableFuture"))))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("TracedWithSpan.completableFuture") + .hasKind(SpanKind.INTERNAL) + .hasNoParent() + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, TracedWithSpan.class.getName()), + equalTo(CODE_FUNCTION, "completableFuture")))); } @Test void exceptionallyCompletingCompletableFuture() throws Exception { - CompletableFuture future = new CompletableFuture<>(); new TracedWithSpan().completableFuture(future); @@ -448,59 +296,35 @@ void exceptionallyCompletingCompletableFuture() throws Exception { future.completeExceptionally(new IllegalArgumentException("Boom")); - assertThat(testing.waitForTraces(1)) - .satisfiesExactly( - trace -> - assertThat(trace) - .satisfiesExactly( - span -> - assertThat(span) - .hasName("TracedWithSpan.completableFuture") - .hasKind(SpanKind.INTERNAL) - .hasParentSpanId(SpanId.getInvalid()) - .hasStatus(StatusData.error()) - .hasAttributesSatisfying( - attributes -> - assertThat(attributes) - .containsOnly( - entry( - CodeIncubatingAttributes.CODE_NAMESPACE, - TracedWithSpan.class.getName()), - entry( - CodeIncubatingAttributes.CODE_FUNCTION, - "completableFuture"))))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("TracedWithSpan.completableFuture") + .hasKind(SpanKind.INTERNAL) + .hasNoParent() + .hasStatus(StatusData.error()) + .hasAttributesSatisfying( + equalTo(CODE_NAMESPACE, TracedWithSpan.class.getName()), + equalTo(CODE_FUNCTION, "completableFuture")))); } @Test - void captureAttributes() throws Exception { - + void captureAttributes() { new TracedWithSpan().withSpanAttributes("foo", "bar", null, "baz"); - assertThat(testing.waitForTraces(1)) - .satisfiesExactly( - trace -> - assertThat(trace) - .satisfiesExactly( - span -> - assertThat(span) - .hasName("TracedWithSpan.withSpanAttributes") - .hasKind(SpanKind.INTERNAL) - .hasParentSpanId(SpanId.getInvalid()) - .hasAttributesSatisfying( - attributes -> - assertThat(attributes) - .containsOnly( - entry( - CodeIncubatingAttributes.CODE_NAMESPACE, - TracedWithSpan.class.getName()), - entry( - CodeIncubatingAttributes.CODE_FUNCTION, - "withSpanAttributes"), - entry( - AttributeKey.stringKey("implicitName"), "foo"), - entry( - AttributeKey.stringKey("explicitName"), - "bar"))))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("TracedWithSpan.withSpanAttributes") + .hasKind(SpanKind.INTERNAL) + .hasNoParent() + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, TracedWithSpan.class.getName()), + equalTo(CODE_FUNCTION, "withSpanAttributes"), + equalTo(stringKey("implicitName"), "foo"), + equalTo(stringKey("explicitName"), "bar")))); } // Needs to be public for ByteBuddy @@ -539,32 +363,20 @@ public void run() { Runnable runnable = (Runnable) generatedClass.getConstructor().newInstance(); runnable.run(); - assertThat(testing.waitForTraces(1)) - .satisfiesExactly( - trace -> - assertThat(trace) - .satisfiesExactly( - span -> - assertThat(span) - .hasName("GeneratedJava6TestClass.run") - .hasKind(SpanKind.INTERNAL) - .hasParentSpanId(SpanId.getInvalid()) - .hasAttributesSatisfying( - attributes -> - assertThat(attributes) - .containsOnly( - entry( - CodeIncubatingAttributes.CODE_NAMESPACE, - "GeneratedJava6TestClass"), - entry( - CodeIncubatingAttributes.CODE_FUNCTION, - "run"))), - span -> - assertThat(span) - .hasName("intercept") - .hasKind(SpanKind.INTERNAL) - .hasParentSpanId(trace.get(0).getSpanId()) - .hasAttributesSatisfying( - attributes -> assertThat(attributes).isEmpty()))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("GeneratedJava6TestClass.run") + .hasKind(SpanKind.INTERNAL) + .hasNoParent() + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, "GeneratedJava6TestClass"), + equalTo(CODE_FUNCTION, "run")), + span -> + span.hasName("intercept") + .hasKind(SpanKind.INTERNAL) + .hasParentSpanId(trace.getSpan(0).getSpanId()) + .hasAttributes(Attributes.empty()))); } }