Update WithSpan tests to follow conventions (#13703)

This commit is contained in:
Jay DeLuca 2025-04-14 13:55:04 -04:00 committed by GitHub
parent 1e7ddc8258
commit 7a974c97ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 237 additions and 384 deletions

View File

@ -5,10 +5,11 @@
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 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.testing.junit.AgentInstrumentationExtension;
@ -35,131 +36,88 @@ class WithSpanInstrumentationTest {
AgentInstrumentationExtension.create();
@Test
void deriveAutomaticName() throws Exception {
void deriveAutomaticName() {
new TracedWithSpan().otel();
assertThat(testing.waitForTraces(1))
.satisfiesExactly(
testing.waitAndAssertTraces(
trace ->
assertThat(trace)
.satisfiesExactly(
trace.hasSpansSatisfyingExactly(
span ->
assertThat(span)
.hasName("TracedWithSpan.otel")
span.hasName("TracedWithSpan.otel")
.hasKind(SpanKind.INTERNAL)
.hasParentSpanId(SpanId.getInvalid())
.hasAttributesSatisfying(
attributes ->
assertThat(attributes)
.containsOnly(
entry(
.hasNoParent()
.hasAttributesSatisfyingExactly(
equalTo(
CodeIncubatingAttributes.CODE_NAMESPACE,
TracedWithSpan.class.getName()),
entry(
CodeIncubatingAttributes.CODE_FUNCTION,
"otel")))));
equalTo(CodeIncubatingAttributes.CODE_FUNCTION, "otel"))));
}
@Test
void manualName() throws Exception {
void manualName() {
new TracedWithSpan().namedOtel();
assertThat(testing.waitForTraces(1))
.satisfiesExactly(
testing.waitAndAssertTraces(
trace ->
assertThat(trace)
.satisfiesExactly(
trace.hasSpansSatisfyingExactly(
span ->
assertThat(span)
.hasName("manualName")
span.hasName("manualName")
.hasKind(SpanKind.INTERNAL)
.hasParentSpanId(SpanId.getInvalid())
.hasAttributesSatisfying(
attributes ->
assertThat(attributes)
.containsOnly(
entry(
.hasNoParent()
.hasAttributesSatisfyingExactly(
equalTo(
CodeIncubatingAttributes.CODE_NAMESPACE,
TracedWithSpan.class.getName()),
entry(
CodeIncubatingAttributes.CODE_FUNCTION,
"namedOtel")))));
equalTo(CodeIncubatingAttributes.CODE_FUNCTION, "namedOtel"))));
}
@Test
void manualKind() throws Exception {
void manualKind() {
new TracedWithSpan().someKind();
assertThat(testing.waitForTraces(1))
.satisfiesExactly(
testing.waitAndAssertTraces(
trace ->
assertThat(trace)
.satisfiesExactly(
trace.hasSpansSatisfyingExactly(
span ->
assertThat(span)
.hasName("TracedWithSpan.someKind")
span.hasName("TracedWithSpan.someKind")
.hasKind(SpanKind.PRODUCER)
.hasParentSpanId(SpanId.getInvalid())
.hasAttributesSatisfying(
attributes ->
assertThat(attributes)
.containsOnly(
entry(
.hasNoParent()
.hasAttributesSatisfyingExactly(
equalTo(
CodeIncubatingAttributes.CODE_NAMESPACE,
TracedWithSpan.class.getName()),
entry(
CodeIncubatingAttributes.CODE_FUNCTION,
"someKind")))));
equalTo(CodeIncubatingAttributes.CODE_FUNCTION, "someKind"))));
}
@Test
void multipleSpans() throws Exception {
void multipleSpans() {
new TracedWithSpan().server();
assertThat(testing.waitForTraces(1))
.satisfiesExactly(
testing.waitAndAssertTraces(
trace ->
assertThat(trace)
.satisfiesExactly(
trace.hasSpansSatisfyingExactly(
span ->
assertThat(span)
.hasName("TracedWithSpan.server")
span.hasName("TracedWithSpan.server")
.hasKind(SpanKind.SERVER)
.hasNoParent()
.hasAttributesSatisfying(
attributes ->
assertThat(attributes)
.containsOnly(
entry(
.hasAttributesSatisfyingExactly(
equalTo(
CodeIncubatingAttributes.CODE_NAMESPACE,
TracedWithSpan.class.getName()),
entry(
CodeIncubatingAttributes.CODE_FUNCTION,
"server"))),
equalTo(CodeIncubatingAttributes.CODE_FUNCTION, "server")),
span ->
assertThat(span)
.hasName("TracedWithSpan.otel")
span.hasName("TracedWithSpan.otel")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.get(0))
.hasAttributesSatisfying(
attributes ->
assertThat(attributes)
.containsOnly(
entry(
.hasParent(trace.getSpan(0))
.hasAttributesSatisfyingExactly(
equalTo(
CodeIncubatingAttributes.CODE_NAMESPACE,
TracedWithSpan.class.getName()),
entry(
CodeIncubatingAttributes.CODE_FUNCTION,
"otel")))));
equalTo(CodeIncubatingAttributes.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
@ -167,93 +125,67 @@ class WithSpanInstrumentationTest {
}
@Test
void completedCompletionStage() throws Exception {
void completedCompletionStage() {
CompletableFuture<String> future = CompletableFuture.completedFuture("Done");
new TracedWithSpan().completionStage(future);
assertThat(testing.waitForTraces(1))
.satisfiesExactly(
testing.waitAndAssertTraces(
trace ->
assertThat(trace)
.satisfiesExactly(
trace.hasSpansSatisfyingExactly(
span ->
assertThat(span)
.hasName("TracedWithSpan.completionStage")
span.hasName("TracedWithSpan.completionStage")
.hasKind(SpanKind.INTERNAL)
.hasParentSpanId(SpanId.getInvalid())
.hasAttributesSatisfying(
attributes ->
assertThat(attributes)
.containsOnly(
entry(
.hasNoParent()
.hasAttributesSatisfyingExactly(
equalTo(
CodeIncubatingAttributes.CODE_NAMESPACE,
TracedWithSpan.class.getName()),
entry(
CodeIncubatingAttributes.CODE_FUNCTION,
"completionStage")))));
equalTo(CodeIncubatingAttributes.CODE_FUNCTION, "completionStage"))));
}
@Test
void exceptionallyCompletedCompletionStage() throws Exception {
void exceptionallyCompletedCompletionStage() {
CompletableFuture<String> future = new CompletableFuture<>();
future.completeExceptionally(new IllegalArgumentException("Boom"));
Exception exception = new IllegalArgumentException("Boom");
future.completeExceptionally(exception);
new TracedWithSpan().completionStage(future);
assertThat(testing.waitForTraces(1))
.satisfiesExactly(
testing.waitAndAssertTraces(
trace ->
assertThat(trace)
.satisfiesExactly(
trace.hasSpansSatisfyingExactly(
span ->
assertThat(span)
.hasName("TracedWithSpan.completionStage")
span.hasName("TracedWithSpan.completionStage")
.hasKind(SpanKind.INTERNAL)
.hasParentSpanId(SpanId.getInvalid())
.hasNoParent()
.hasStatus(StatusData.error())
.hasAttributesSatisfying(
attributes ->
assertThat(attributes)
.containsOnly(
entry(
.hasException(exception)
.hasAttributesSatisfyingExactly(
equalTo(
CodeIncubatingAttributes.CODE_NAMESPACE,
TracedWithSpan.class.getName()),
entry(
CodeIncubatingAttributes.CODE_FUNCTION,
"completionStage")))));
equalTo(CodeIncubatingAttributes.CODE_FUNCTION, "completionStage"))));
}
@Test
void nullCompletionStage() throws Exception {
void nullCompletionStage() {
new TracedWithSpan().completionStage(null);
assertThat(testing.waitForTraces(1))
.satisfiesExactly(
testing.waitAndAssertTraces(
trace ->
assertThat(trace)
.satisfiesExactly(
trace.hasSpansSatisfyingExactly(
span ->
assertThat(span)
.hasName("TracedWithSpan.completionStage")
span.hasName("TracedWithSpan.completionStage")
.hasKind(SpanKind.INTERNAL)
.hasParentSpanId(SpanId.getInvalid())
.hasAttributesSatisfying(
attributes ->
assertThat(attributes)
.containsOnly(
entry(
.hasNoParent()
.hasAttributesSatisfyingExactly(
equalTo(
CodeIncubatingAttributes.CODE_NAMESPACE,
TracedWithSpan.class.getName()),
entry(
CodeIncubatingAttributes.CODE_FUNCTION,
"completionStage")))));
equalTo(CodeIncubatingAttributes.CODE_FUNCTION, "completionStage"))));
}
@Test
void completingCompletionStage() throws Exception {
CompletableFuture<String> future = new CompletableFuture<>();
new TracedWithSpan().completionStage(future);
@ -262,150 +194,109 @@ class WithSpanInstrumentationTest {
future.complete("Done");
assertThat(testing.waitForTraces(1))
.satisfiesExactly(
testing.waitAndAssertTraces(
trace ->
assertThat(trace)
.satisfiesExactly(
trace.hasSpansSatisfyingExactly(
span ->
assertThat(span)
.hasName("TracedWithSpan.completionStage")
span.hasName("TracedWithSpan.completionStage")
.hasKind(SpanKind.INTERNAL)
.hasParentSpanId(SpanId.getInvalid())
.hasAttributesSatisfying(
attributes ->
assertThat(attributes)
.containsOnly(
entry(
.hasNoParent()
.hasAttributesSatisfyingExactly(
equalTo(
CodeIncubatingAttributes.CODE_NAMESPACE,
TracedWithSpan.class.getName()),
entry(
CodeIncubatingAttributes.CODE_FUNCTION,
"completionStage")))));
equalTo(CodeIncubatingAttributes.CODE_FUNCTION, "completionStage"))));
}
@Test
void exceptionallyCompletingCompletionStage() throws Exception {
CompletableFuture<String> future = new CompletableFuture<>();
new TracedWithSpan().completionStage(future);
Thread.sleep(500); // sleep a bit just to make sure no span is captured
assertThat(testing.waitForTraces(0)).isEmpty();
future.completeExceptionally(new IllegalArgumentException("Boom"));
Exception exception = new IllegalArgumentException("Boom");
future.completeExceptionally(exception);
assertThat(testing.waitForTraces(1))
.satisfiesExactly(
testing.waitAndAssertTraces(
trace ->
assertThat(trace)
.satisfiesExactly(
trace.hasSpansSatisfyingExactly(
span ->
assertThat(span)
.hasName("TracedWithSpan.completionStage")
span.hasName("TracedWithSpan.completionStage")
.hasKind(SpanKind.INTERNAL)
.hasParentSpanId(SpanId.getInvalid())
.hasNoParent()
.hasStatus(StatusData.error())
.hasAttributesSatisfying(
attributes ->
assertThat(attributes)
.containsOnly(
entry(
.hasException(exception)
.hasAttributesSatisfyingExactly(
equalTo(
CodeIncubatingAttributes.CODE_NAMESPACE,
TracedWithSpan.class.getName()),
entry(
CodeIncubatingAttributes.CODE_FUNCTION,
"completionStage")))));
equalTo(CodeIncubatingAttributes.CODE_FUNCTION, "completionStage"))));
}
@Test
void completedCompletableFuture() throws Exception {
void completedCompletableFuture() {
CompletableFuture<String> future = CompletableFuture.completedFuture("Done");
new TracedWithSpan().completableFuture(future);
assertThat(testing.waitForTraces(1))
.satisfiesExactly(
testing.waitAndAssertTraces(
trace ->
assertThat(trace)
.satisfiesExactly(
trace.hasSpansSatisfyingExactly(
span ->
assertThat(span)
.hasName("TracedWithSpan.completableFuture")
span.hasName("TracedWithSpan.completableFuture")
.hasKind(SpanKind.INTERNAL)
.hasParentSpanId(SpanId.getInvalid())
.hasAttributesSatisfying(
attributes ->
assertThat(attributes)
.containsOnly(
entry(
.hasNoParent()
.hasAttributesSatisfyingExactly(
equalTo(
CodeIncubatingAttributes.CODE_NAMESPACE,
TracedWithSpan.class.getName()),
entry(
CodeIncubatingAttributes.CODE_FUNCTION,
"completableFuture")))));
equalTo(CodeIncubatingAttributes.CODE_FUNCTION, "completableFuture"))));
}
@Test
void exceptionallyCompletedCompletableFuture() throws Exception {
void exceptionallyCompletedCompletableFuture() {
CompletableFuture<String> future = new CompletableFuture<>();
future.completeExceptionally(new IllegalArgumentException("Boom"));
Exception exception = new IllegalArgumentException("Boom");
future.completeExceptionally(exception);
new TracedWithSpan().completableFuture(future);
assertThat(testing.waitForTraces(1))
.satisfiesExactly(
testing.waitAndAssertTraces(
trace ->
assertThat(trace)
.satisfiesExactly(
trace.hasSpansSatisfyingExactly(
span ->
assertThat(span)
.hasName("TracedWithSpan.completableFuture")
span.hasName("TracedWithSpan.completableFuture")
.hasKind(SpanKind.INTERNAL)
.hasParentSpanId(SpanId.getInvalid())
.hasNoParent()
.hasStatus(StatusData.error())
.hasAttributesSatisfying(
attributes ->
assertThat(attributes)
.containsOnly(
entry(
.hasException(exception)
.hasAttributesSatisfyingExactly(
equalTo(
CodeIncubatingAttributes.CODE_NAMESPACE,
TracedWithSpan.class.getName()),
entry(
CodeIncubatingAttributes.CODE_FUNCTION,
"completableFuture")))));
equalTo(CodeIncubatingAttributes.CODE_FUNCTION, "completableFuture"))));
}
@Test
void nullCompletableFuture() throws Exception {
void nullCompletableFuture() {
new TracedWithSpan().completableFuture(null);
assertThat(testing.waitForTraces(1))
.satisfiesExactly(
testing.waitAndAssertTraces(
trace ->
assertThat(trace)
.satisfiesExactly(
trace.hasSpansSatisfyingExactly(
span ->
assertThat(span)
.hasName("TracedWithSpan.completableFuture")
span.hasName("TracedWithSpan.completableFuture")
.hasKind(SpanKind.INTERNAL)
.hasParentSpanId(SpanId.getInvalid())
.hasAttributesSatisfying(
attributes ->
assertThat(attributes)
.containsOnly(
entry(
.hasNoParent()
.hasAttributesSatisfyingExactly(
equalTo(
CodeIncubatingAttributes.CODE_NAMESPACE,
TracedWithSpan.class.getName()),
entry(
CodeIncubatingAttributes.CODE_FUNCTION,
"completableFuture")))));
equalTo(CodeIncubatingAttributes.CODE_FUNCTION, "completableFuture"))));
}
@Test
void completingCompletableFuture() throws Exception {
CompletableFuture<String> future = new CompletableFuture<>();
new TracedWithSpan().completableFuture(future);
@ -414,92 +305,65 @@ class WithSpanInstrumentationTest {
future.complete("Done");
assertThat(testing.waitForTraces(1))
.satisfiesExactly(
testing.waitAndAssertTraces(
trace ->
assertThat(trace)
.satisfiesExactly(
trace.hasSpansSatisfyingExactly(
span ->
assertThat(span)
.hasName("TracedWithSpan.completableFuture")
span.hasName("TracedWithSpan.completableFuture")
.hasKind(SpanKind.INTERNAL)
.hasParentSpanId(SpanId.getInvalid())
.hasAttributesSatisfying(
attributes ->
assertThat(attributes)
.containsOnly(
entry(
.hasNoParent()
.hasAttributesSatisfyingExactly(
equalTo(
CodeIncubatingAttributes.CODE_NAMESPACE,
TracedWithSpan.class.getName()),
entry(
CodeIncubatingAttributes.CODE_FUNCTION,
"completableFuture")))));
equalTo(CodeIncubatingAttributes.CODE_FUNCTION, "completableFuture"))));
}
@Test
void exceptionallyCompletingCompletableFuture() throws Exception {
CompletableFuture<String> future = new CompletableFuture<>();
new TracedWithSpan().completableFuture(future);
Thread.sleep(500); // sleep a bit just to make sure no span is captured
assertThat(testing.waitForTraces(0)).isEmpty();
future.completeExceptionally(new IllegalArgumentException("Boom"));
Exception exception = new IllegalArgumentException("Boom");
future.completeExceptionally(exception);
assertThat(testing.waitForTraces(1))
.satisfiesExactly(
testing.waitAndAssertTraces(
trace ->
assertThat(trace)
.satisfiesExactly(
trace.hasSpansSatisfyingExactly(
span ->
assertThat(span)
.hasName("TracedWithSpan.completableFuture")
span.hasName("TracedWithSpan.completableFuture")
.hasKind(SpanKind.INTERNAL)
.hasParentSpanId(SpanId.getInvalid())
.hasNoParent()
.hasStatus(StatusData.error())
.hasAttributesSatisfying(
attributes ->
assertThat(attributes)
.containsOnly(
entry(
.hasException(exception)
.hasAttributesSatisfyingExactly(
equalTo(
CodeIncubatingAttributes.CODE_NAMESPACE,
TracedWithSpan.class.getName()),
entry(
CodeIncubatingAttributes.CODE_FUNCTION,
"completableFuture")))));
equalTo(CodeIncubatingAttributes.CODE_FUNCTION, "completableFuture"))));
}
@Test
void captureAttributes() throws Exception {
void captureAttributes() {
new TracedWithSpan().withSpanAttributes("foo", "bar", null, "baz");
assertThat(testing.waitForTraces(1))
.satisfiesExactly(
testing.waitAndAssertTraces(
trace ->
assertThat(trace)
.satisfiesExactly(
trace.hasSpansSatisfyingExactly(
span ->
assertThat(span)
.hasName("TracedWithSpan.withSpanAttributes")
span.hasName("TracedWithSpan.withSpanAttributes")
.hasKind(SpanKind.INTERNAL)
.hasParentSpanId(SpanId.getInvalid())
.hasAttributesSatisfying(
attributes ->
assertThat(attributes)
.containsOnly(
entry(
.hasNoParent()
.hasAttributesSatisfyingExactly(
equalTo(
CodeIncubatingAttributes.CODE_NAMESPACE,
TracedWithSpan.class.getName()),
entry(
CodeIncubatingAttributes.CODE_FUNCTION,
"withSpanAttributes"),
entry(
AttributeKey.stringKey("implicitName"), "foo"),
entry(
AttributeKey.stringKey("explicitName"),
"bar")))));
equalTo(CodeIncubatingAttributes.CODE_FUNCTION, "withSpanAttributes"),
equalTo(stringKey("implicitName"), "foo"),
equalTo(stringKey("explicitName"), "bar"))));
}
// Needs to be public for ByteBuddy
@ -541,32 +405,21 @@ class WithSpanInstrumentationTest {
Runnable runnable = (Runnable) generatedClass.getConstructor().newInstance();
runnable.run();
assertThat(testing.waitForTraces(1))
.satisfiesExactly(
testing.waitAndAssertTraces(
trace ->
assertThat(trace)
.satisfiesExactly(
trace.hasSpansSatisfyingExactly(
span ->
assertThat(span)
.hasName("GeneratedJava6TestClass.run")
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"))),
.hasAttributesSatisfyingExactly(
equalTo(
CodeIncubatingAttributes.CODE_NAMESPACE, "GeneratedJava6TestClass"),
equalTo(CodeIncubatingAttributes.CODE_FUNCTION, "run")),
span ->
assertThat(span)
.hasName("intercept")
span.hasName("intercept")
.hasKind(SpanKind.INTERNAL)
.hasParentSpanId(trace.get(0).getSpanId())
.hasAttributesSatisfying(
attributes -> assertThat(attributes).isEmpty())));
.hasParentSpanId(trace.getSpan(0).getSpanId())
.hasAttributes(Attributes.empty())));
}
}