Skip to content

Commit

Permalink
Fix some assertions (#12520)
Browse files Browse the repository at this point in the history
  • Loading branch information
trask authored Oct 26, 2024
1 parent d3f808e commit bb90e08
Show file tree
Hide file tree
Showing 15 changed files with 131 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ void testSpan() {
span.hasName("hello")
.hasKind(SpanKind.INTERNAL)
.hasStatus(StatusData.ok())
.hasAttributesSatisfying(Attributes::isEmpty)));
.hasAttributes(Attributes.empty())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ void testSpan() {
span.hasName("hello")
.hasKind(SpanKind.INTERNAL)
.hasStatus(StatusData.ok())
.hasAttributesSatisfying(Attributes::isEmpty)));
.hasAttributes(Attributes.empty())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.opentelemetry.javaagent.instrumentation.azurecore.v1_36;

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

import com.azure.core.annotation.ExpectedResponses;
Expand All @@ -22,6 +23,7 @@
import com.azure.core.util.ClientOptions;
import com.azure.core.util.Context;
import com.azure.core.util.TracingOptions;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.api.internal.SpanKey;
Expand Down Expand Up @@ -66,7 +68,8 @@ void testSpan() {
span.hasName("hello")
.hasKind(SpanKind.INTERNAL)
.hasStatus(StatusData.unset())
.hasAttributesSatisfying(Attributes::isEmpty)));
.hasAttributesSatisfyingExactly(
equalTo(AttributeKey.stringKey("az.namespace"), "otel.tests"))));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void testSpan() {
span.hasName("hello")
.hasKind(SpanKind.INTERNAL)
.hasStatus(StatusData.unset())
.hasAttributesSatisfying(Attributes::isEmpty)));
.hasAttributes(Attributes.empty())));
}

private static com.azure.core.util.tracing.Tracer createAzTracer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.QUERY_PARAM;
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.REDIRECT;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies;

import grails.boot.GrailsApp;
import grails.boot.config.GrailsAutoConfiguration;
Expand All @@ -27,6 +29,7 @@
import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint;
import io.opentelemetry.sdk.testing.assertj.SpanDataAssert;
import io.opentelemetry.sdk.trace.data.StatusData;
import io.opentelemetry.semconv.incubating.CodeIncubatingAttributes;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -35,6 +38,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.function.Consumer;
import org.assertj.core.api.AbstractStringAssert;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.ServerProperties;
Expand Down Expand Up @@ -160,12 +164,19 @@ public SpanDataAssert assertHandlerSpan(
@Override
public SpanDataAssert assertResponseSpan(
SpanDataAssert span, String method, ServerEndpoint endpoint) {
String methodName;
if (endpoint == REDIRECT) {
span.satisfies(spanData -> assertThat(spanData.getName()).endsWith(".sendRedirect"));
methodName = "sendRedirect";
} else if (endpoint == ERROR || endpoint == NOT_FOUND) {
methodName = "sendError";
} else {
span.satisfies(spanData -> assertThat(spanData.getName()).endsWith(".sendError"));
throw new AssertionError("Unexpected endpoint: " + endpoint.name());
}
span.hasKind(SpanKind.INTERNAL).hasAttributesSatisfying(Attributes::isEmpty);
span.hasKind(SpanKind.INTERNAL)
.satisfies(spanData -> assertThat(spanData.getName()).endsWith("." + methodName))
.hasAttributesSatisfyingExactly(
equalTo(CodeIncubatingAttributes.CODE_FUNCTION, methodName),
satisfies(CodeIncubatingAttributes.CODE_NAMESPACE, AbstractStringAssert::isNotEmpty));
return span;
}

Expand All @@ -178,13 +189,17 @@ public List<Consumer<SpanDataAssert>> errorPageSpanAssertions(
span.hasName(
endpoint == NOT_FOUND ? "ErrorController.notFound" : "ErrorController.index")
.hasKind(SpanKind.INTERNAL)
.hasAttributesSatisfying(Attributes::isEmpty));
.hasAttributes(Attributes.empty()));
if (endpoint == NOT_FOUND) {
spanAssertions.add(
span ->
span.satisfies(spanData -> assertThat(spanData.getName()).endsWith(".sendError"))
.hasKind(SpanKind.INTERNAL)
.hasAttributesSatisfying(Attributes::isEmpty));
.hasAttributesSatisfyingExactly(
equalTo(CodeIncubatingAttributes.CODE_FUNCTION, "sendError"),
satisfies(
CodeIncubatingAttributes.CODE_NAMESPACE,
AbstractStringAssert::isNotEmpty)));
}
return spanAssertions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ void parseError() {
span.hasName("GraphQL Operation")
.hasKind(SpanKind.INTERNAL)
.hasNoParent()
.hasAttributesSatisfying(Attributes::isEmpty)
.hasAttributes(Attributes.empty())
.hasStatus(StatusData.error())
.hasEventsSatisfyingExactly(
event ->
Expand Down Expand Up @@ -280,7 +280,7 @@ void validationError() {
span.hasName("GraphQL Operation")
.hasKind(SpanKind.INTERNAL)
.hasNoParent()
.hasAttributesSatisfying(Attributes::isEmpty)
.hasAttributes(Attributes.empty())
.hasStatus(StatusData.error())
.hasEventsSatisfyingExactly(
event ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.QUERY_PARAM;
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.REDIRECT;
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.SUCCESS;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static org.assertj.core.api.Assertions.assertThat;

import com.google.common.collect.Sets;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpServerTest;
Expand All @@ -26,6 +26,7 @@
import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint;
import io.opentelemetry.sdk.testing.assertj.SpanDataAssert;
import io.opentelemetry.semconv.HttpAttributes;
import io.opentelemetry.semconv.incubating.CodeIncubatingAttributes;
import jakarta.servlet.DispatcherType;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -89,12 +90,19 @@ protected void configure(HttpServerTestOptions options) {
@Override
protected SpanDataAssert assertResponseSpan(
SpanDataAssert span, String method, ServerEndpoint endpoint) {
String methodName;
if (endpoint == REDIRECT) {
span.satisfies(spanData -> assertThat(spanData.getName()).endsWith(".sendRedirect"));
methodName = "sendRedirect";
} else if (endpoint == ERROR) {
span.satisfies(spanData -> assertThat(spanData.getName()).endsWith(".sendError"));
methodName = "sendError";
} else {
throw new AssertionError("Unexpected endpoint: " + endpoint.name());
}
span.hasKind(SpanKind.INTERNAL).hasAttributesSatisfying(Attributes::isEmpty);
span.hasKind(SpanKind.INTERNAL)
.satisfies(spanData -> assertThat(spanData.getName()).endsWith("." + methodName))
.hasAttributesSatisfyingExactly(
equalTo(CodeIncubatingAttributes.CODE_FUNCTION, methodName),
equalTo(CodeIncubatingAttributes.CODE_NAMESPACE, "org.eclipse.jetty.server.Response"));
return span;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.QUERY_PARAM;
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.REDIRECT;
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.SUCCESS;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static org.assertj.core.api.Assertions.assertThat;

import com.google.common.collect.Sets;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpServerTest;
Expand All @@ -26,6 +26,7 @@
import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint;
import io.opentelemetry.sdk.testing.assertj.SpanDataAssert;
import io.opentelemetry.semconv.HttpAttributes;
import io.opentelemetry.semconv.incubating.CodeIncubatingAttributes;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
Expand Down Expand Up @@ -69,12 +70,19 @@ protected void configure(HttpServerTestOptions options) {
@Override
protected SpanDataAssert assertResponseSpan(
SpanDataAssert span, String method, ServerEndpoint endpoint) {
String methodName;
if (endpoint == REDIRECT) {
span.satisfies(spanData -> assertThat(spanData.getName()).endsWith(".sendRedirect"));
methodName = "sendRedirect";
} else if (endpoint == ERROR) {
span.satisfies(spanData -> assertThat(spanData.getName()).endsWith(".sendError"));
methodName = "sendError";
} else {
throw new AssertionError("Unexpected endpoint: " + endpoint.name());
}
span.hasKind(SpanKind.INTERNAL).hasAttributesSatisfying(Attributes::isEmpty);
span.hasKind(SpanKind.INTERNAL)
.satisfies(spanData -> assertThat(spanData.getName()).endsWith("." + methodName))
.hasAttributesSatisfyingExactly(
equalTo(CodeIncubatingAttributes.CODE_FUNCTION, methodName),
equalTo(CodeIncubatingAttributes.CODE_NAMESPACE, "org.eclipse.jetty.server.Response"));
return span;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.QUERY_PARAM;
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.REDIRECT;
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.SUCCESS;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static org.assertj.core.api.Assertions.assertThat;

import com.google.common.collect.Sets;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpServerTest;
Expand All @@ -26,6 +26,7 @@
import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint;
import io.opentelemetry.sdk.testing.assertj.SpanDataAssert;
import io.opentelemetry.semconv.HttpAttributes;
import io.opentelemetry.semconv.incubating.CodeIncubatingAttributes;
import java.io.IOException;
import java.io.Writer;
import java.util.Collections;
Expand Down Expand Up @@ -89,12 +90,19 @@ protected void configure(HttpServerTestOptions options) {
@Override
protected SpanDataAssert assertResponseSpan(
SpanDataAssert span, String method, ServerEndpoint endpoint) {
String methodName;
if (endpoint == REDIRECT) {
span.satisfies(spanData -> assertThat(spanData.getName()).endsWith(".sendRedirect"));
methodName = "sendRedirect";
} else if (endpoint == ERROR) {
span.satisfies(spanData -> assertThat(spanData.getName()).endsWith(".sendError"));
methodName = "sendError";
} else {
throw new AssertionError("Unexpected endpoint: " + endpoint.name());
}
span.hasKind(SpanKind.INTERNAL).hasAttributesSatisfying(Attributes::isEmpty);
span.hasKind(SpanKind.INTERNAL)
.satisfies(spanData -> assertThat(spanData.getName()).endsWith("." + methodName))
.hasAttributesSatisfyingExactly(
equalTo(CodeIncubatingAttributes.CODE_FUNCTION, methodName),
equalTo(CodeIncubatingAttributes.CODE_NAMESPACE, "org.eclipse.jetty.server.Response"));
return span;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ protected List<Consumer<SpanDataAssert>> errorPageSpanAssertions(
span ->
span.hasName("BasicErrorController.error")
.hasKind(SpanKind.INTERNAL)
.hasAttributesSatisfying(Attributes::isEmpty));
.hasAttributes(Attributes.empty()));
return spanAssertions;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ protected List<Consumer<SpanDataAssert>> errorPageSpanAssertions(
span ->
span.hasName("BasicErrorController.error")
.hasKind(SpanKind.INTERNAL)
.hasAttributesSatisfying(Attributes::isEmpty));
.hasAttributes(Attributes.empty()));
return spanAssertions;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.QUERY_PARAM;
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.REDIRECT;
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.SUCCESS;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies;
import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.api.internal.HttpConstants;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
Expand All @@ -25,13 +26,15 @@
import io.opentelemetry.instrumentation.testing.junit.http.HttpServerTestOptions;
import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint;
import io.opentelemetry.sdk.testing.assertj.SpanDataAssert;
import io.opentelemetry.semconv.incubating.CodeIncubatingAttributes;
import jakarta.servlet.Servlet;
import java.io.File;
import java.nio.file.Files;
import java.util.UUID;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;
import org.assertj.core.api.AbstractStringAssert;
import org.junit.jupiter.api.extension.RegisterExtension;

class TomcatAsyncTest extends AbstractHttpServerTest<Tomcat> {
Expand Down Expand Up @@ -116,12 +119,19 @@ protected void configure(HttpServerTestOptions options) {
@Override
protected SpanDataAssert assertResponseSpan(
SpanDataAssert span, String method, ServerEndpoint endpoint) {
if (endpoint.equals(REDIRECT)) {
span.satisfies(spanData -> assertThat(spanData.getName()).endsWith(".sendRedirect"));
} else if (endpoint.equals(NOT_FOUND)) {
span.satisfies(spanData -> assertThat(spanData.getName()).endsWith(".sendError"));
String methodName;
if (endpoint == REDIRECT) {
methodName = "sendRedirect";
} else if (endpoint == NOT_FOUND) {
methodName = "sendError";
} else {
throw new AssertionError("Unexpected endpoint: " + endpoint.name());
}
span.hasKind(SpanKind.INTERNAL).hasAttributesSatisfying(Attributes::isEmpty);
span.hasKind(SpanKind.INTERNAL)
.satisfies(spanData -> assertThat(spanData.getName()).endsWith("." + methodName))
.hasAttributesSatisfyingExactly(
equalTo(CodeIncubatingAttributes.CODE_FUNCTION, methodName),
satisfies(CodeIncubatingAttributes.CODE_NAMESPACE, AbstractStringAssert::isNotEmpty));
return span;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.QUERY_PARAM;
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.REDIRECT;
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.SUCCESS;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.api.internal.HttpConstants;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
Expand All @@ -30,13 +31,15 @@
import io.opentelemetry.instrumentation.testing.junit.http.HttpServerTestOptions;
import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint;
import io.opentelemetry.sdk.testing.assertj.SpanDataAssert;
import io.opentelemetry.semconv.incubating.CodeIncubatingAttributes;
import java.io.File;
import java.nio.file.Files;
import java.util.List;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.core.StandardHost;
import org.apache.catalina.startup.Tomcat;
import org.assertj.core.api.AbstractStringAssert;
import org.junit.jupiter.api.extension.RegisterExtension;

class TomcatHandlerTest extends AbstractHttpServerTest<Tomcat> {
Expand Down Expand Up @@ -116,12 +119,19 @@ protected void configure(HttpServerTestOptions options) {
@Override
protected SpanDataAssert assertResponseSpan(
SpanDataAssert span, String method, ServerEndpoint endpoint) {
if (endpoint.equals(REDIRECT)) {
span.satisfies(spanData -> assertThat(spanData.getName()).endsWith(".sendRedirect"));
} else if (endpoint.equals(NOT_FOUND)) {
span.satisfies(spanData -> assertThat(spanData.getName()).endsWith(".sendError"));
String methodName;
if (endpoint == REDIRECT) {
methodName = "sendRedirect";
} else if (endpoint == NOT_FOUND || endpoint == ERROR) {
methodName = "sendError";
} else {
throw new AssertionError("Unexpected endpoint: " + endpoint.name());
}
span.hasKind(SpanKind.INTERNAL).hasAttributesSatisfying(Attributes::isEmpty);
span.hasKind(SpanKind.INTERNAL)
.satisfies(spanData -> assertThat(spanData.getName()).endsWith("." + methodName))
.hasAttributesSatisfyingExactly(
equalTo(CodeIncubatingAttributes.CODE_FUNCTION, methodName),
satisfies(CodeIncubatingAttributes.CODE_NAMESPACE, AbstractStringAssert::isNotEmpty));
return span;
}
}
Loading

0 comments on commit bb90e08

Please sign in to comment.