Skip to content

Commit

Permalink
Test for non app uris
Browse files Browse the repository at this point in the history
  • Loading branch information
brunobat authored and gsmet committed Jan 17, 2025
1 parent c377d4a commit 9f806c0
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 1 deletion.
5 changes: 5 additions & 0 deletions extensions/opentelemetry/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@
<artifactId>quarkus-security-deployment</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx-http-dev-ui-tests</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-testing</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package io.quarkus.opentelemetry.deployment;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Arrays;
import java.util.List;

import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;

import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.opentelemetry.sdk.trace.data.SpanData;
import io.quarkus.opentelemetry.deployment.common.exporter.TestSpanExporter;
import io.quarkus.opentelemetry.deployment.common.exporter.TestSpanExporterProvider;
import io.quarkus.test.QuarkusDevModeTest;
import io.restassured.RestAssured;

public class OpenTelemetrySuppressNonAppUriTest {

@RegisterExtension
final static QuarkusDevModeTest TEST = new QuarkusDevModeTest()
.withApplicationRoot((jar) -> jar
.addClasses(HelloResource.class, TestSpanExporter.class, TestSpanExporterProvider.class)
.addAsResource(new StringAsset(TestSpanExporterProvider.class.getCanonicalName()),
"META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSpanExporterProvider")
.add(new StringAsset(
"""
quarkus.otel.traces.exporter=test-span-exporter
quarkus.otel.metrics.exporter=none
quarkus.otel.bsp.export.timeout=1s
quarkus.otel.bsp.schedule.delay=50
"""),
"application.properties"));

@Test
void test() {

// Must not be traced
RestAssured.given()
.get("/q/health/")
.then()
.statusCode(200);
RestAssured.given()
.get("/q/dev-ui")
.then()
.statusCode(200);
RestAssured.given()
.get("/q/dev-ui/icon/font-awesome.js")
.then()
.statusCode(200);
// Valid trace
RestAssured.given()
.get("/hello")
.then()
.statusCode(200);
// Get span names
List<String> spans = Arrays.asList(
RestAssured.given()
.get("/hello/spans")
.then()
.statusCode(200)
.extract().body()
.asString()
.split(";"));

assertThat(spans.size())
.withFailMessage("Expected only one span but found: " + spans)
.isEqualTo(1);

assertThat(spans).contains("GET /hello");
}

@Path("/hello")
public static class HelloResource {

@Inject
TestSpanExporter spanExporter;

@GET
public String greetings() {
return "Hello test";
}

/**
* Gets a string with the received spans names concatenated by ;
*
* @return
*/
@GET
@Path("/spans")
public String greetingsInsertAtLeast() {
String spanNames = spanExporter.getFinishedSpanItemsAtLeast(1).stream()
.map(SpanData::getName)
.reduce((s1, s2) -> s1 + ";" + s2).orElse("");
System.out.println(spanNames);
return spanNames;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static java.util.stream.Collectors.toList;
import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -50,11 +51,27 @@ public List<SpanData> getFinishedSpanItems(int spanCount) {
return finishedSpanItems.stream().collect(toList());
}

/**
* Careful when retrieving the list of finished spans. There is a chance when the response is already sent to the
* client and Vert.x still writing the end of the spans. This means that a response is available to assert from the
* test side but not all spans may be available yet. For this reason, this method requires the number of expected
* spans.
*/
public List<SpanData> getFinishedSpanItemsAtLeast(int spanCount) {
assertSpanAtLeast(spanCount);
return finishedSpanItems;
}

public void assertSpanCount(int spanCount) {
await().atMost(30, SECONDS).untilAsserted(
await().atMost(5, SECONDS).untilAsserted(
() -> assertEquals(spanCount, finishedSpanItems.size(), "Spans: " + finishedSpanItems.toString()));
}

public void assertSpanAtLeast(int spanCount) {
await().atMost(5, SECONDS).untilAsserted(
() -> assertTrue(spanCount < finishedSpanItems.size(), "Spans: " + finishedSpanItems.toString()));
}

public void reset() {
finishedSpanItems.clear();
}
Expand Down

0 comments on commit 9f806c0

Please sign in to comment.