convert spark tests from groovy to java (#8200)

Related to #7195
This commit is contained in:
Nitesh S 2023-04-03 17:08:02 +05:30 committed by GitHub
parent 5db149e1fa
commit 8704510619
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 83 additions and 69 deletions

View File

@ -1,69 +0,0 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification
import io.opentelemetry.instrumentation.test.utils.PortUtils
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes
import io.opentelemetry.testing.internal.armeria.client.WebClient
import spark.Spark
import spock.lang.Shared
import static io.opentelemetry.api.trace.SpanKind.SERVER
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NetTransportValues.IP_TCP
class SparkJavaBasedTest extends AgentInstrumentationSpecification {
@Shared
int port
@Shared
WebClient client
def setupSpec() {
port = PortUtils.findOpenPort()
TestSparkJavaApplication.initSpark(port)
client = WebClient.of("http://localhost:${port}")
}
def cleanupSpec() {
Spark.stop()
}
def "generates spans"() {
when:
def response = client.get("/param/asdf1234").aggregate().join()
then:
port != 0
def content = response.contentUtf8()
content == "Hello asdf1234"
assertTraces(1) {
trace(0, 1) {
span(0) {
name "GET /param/:param"
kind SERVER
hasNoParent()
attributes {
"$SemanticAttributes.HTTP_SCHEME" "http"
"$SemanticAttributes.HTTP_TARGET" "/param/asdf1234"
"$SemanticAttributes.HTTP_METHOD" "GET"
"$SemanticAttributes.HTTP_STATUS_CODE" 200
"$SemanticAttributes.HTTP_FLAVOR" "1.1"
"$SemanticAttributes.HTTP_USER_AGENT" String
"$SemanticAttributes.HTTP_ROUTE" "/param/:param"
"$SemanticAttributes.NET_TRANSPORT" IP_TCP
"$SemanticAttributes.NET_HOST_NAME" "localhost"
"$SemanticAttributes.NET_HOST_PORT" port
"$SemanticAttributes.NET_SOCK_PEER_ADDR" "127.0.0.1"
"$SemanticAttributes.NET_SOCK_PEER_PORT" Long
"$SemanticAttributes.NET_SOCK_HOST_ADDR" "127.0.0.1"
}
}
}
}
}
}

View File

@ -0,0 +1,81 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.javaagent.instrumentation.sparkjava;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies;
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NetTransportValues.IP_TCP;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.test.utils.PortUtils;
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import io.opentelemetry.testing.internal.armeria.client.WebClient;
import io.opentelemetry.testing.internal.armeria.common.AggregatedHttpResponse;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import spark.Spark;
public class SparkJavaBasedTest {
@RegisterExtension
private static final InstrumentationExtension testing = AgentInstrumentationExtension.create();
static int port;
static WebClient client;
@BeforeAll
static void setupSpec() {
port = PortUtils.findOpenPort();
TestSparkJavaApplication.initSpark(port);
client = WebClient.of("http://localhost:" + port);
}
@AfterAll
static void cleanupSpec() {
Spark.stop();
}
@Test
void generatesSpans() {
AggregatedHttpResponse response = client.get("/param/asdf1234").aggregate().join();
String content = response.contentUtf8();
assertNotEquals(port, 0);
assertEquals(content, "Hello asdf1234");
testing.waitAndAssertTraces(
trace ->
trace
.hasSize(1)
.hasSpansSatisfyingExactly(
span ->
span.hasName("GET /param/:param")
.hasKind(SpanKind.SERVER)
.hasNoParent()
.hasAttributesSatisfyingExactly(
equalTo(SemanticAttributes.HTTP_SCHEME, "http"),
equalTo(SemanticAttributes.HTTP_TARGET, "/param/asdf1234"),
equalTo(SemanticAttributes.HTTP_METHOD, "GET"),
equalTo(SemanticAttributes.HTTP_STATUS_CODE, 200),
equalTo(SemanticAttributes.HTTP_FLAVOR, "1.1"),
satisfies(
SemanticAttributes.HTTP_USER_AGENT,
val -> val.isInstanceOf(String.class)),
equalTo(SemanticAttributes.HTTP_ROUTE, "/param/:param"),
equalTo(SemanticAttributes.NET_TRANSPORT, IP_TCP),
equalTo(SemanticAttributes.NET_HOST_NAME, "localhost"),
equalTo(SemanticAttributes.NET_HOST_PORT, port),
equalTo(SemanticAttributes.NET_SOCK_PEER_ADDR, "127.0.0.1"),
satisfies(
SemanticAttributes.NET_SOCK_PEER_PORT,
val -> val.isInstanceOf(Long.class)),
equalTo(SemanticAttributes.NET_SOCK_HOST_ADDR, "127.0.0.1"))));
}
}

View File

@ -3,6 +3,8 @@
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.javaagent.instrumentation.sparkjava;
import spark.Spark;
public class TestSparkJavaApplication {