-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
86c103b
commit 6521ac8
Showing
2 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
...ter-prometheus/src/main/java/io/quarkus/ts/micrometer/prometheus/PathPatternResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.quarkus.ts.micrometer.prometheus; | ||
|
||
import java.net.URI; | ||
|
||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.PathParam; | ||
import jakarta.ws.rs.core.Response; | ||
|
||
@Path("/test") | ||
public class PathPatternResource { | ||
@GET | ||
@Path("/redirect") | ||
public Response redirect() { | ||
return Response.status(Response.Status.FOUND) //302 | ||
.location(URI.create("/new-location")) | ||
.build(); | ||
} | ||
|
||
@GET | ||
@Path("/not-found") | ||
public Response notFound() { | ||
return Response.status(Response.Status.NOT_FOUND).build(); //404 | ||
} | ||
|
||
@GET | ||
@Path("/moved/{id}") | ||
public Response moved(@PathParam("id") String id) { | ||
return Response.status(Response.Status.MOVED_PERMANENTLY).build(); // 301 | ||
} | ||
|
||
@GET | ||
@Path("") | ||
public Response emptyPath() { | ||
return Response.status(Response.Status.NO_CONTENT).build(); //204 | ||
} | ||
|
||
} |
97 changes: 97 additions & 0 deletions
97
...rometheus/src/test/java/io/quarkus/ts/micrometer/prometheus/HttpPathMetricsPatternIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package io.quarkus.ts.micrometer.prometheus; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.awaitility.Awaitility.await; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.apache.http.HttpStatus; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.test.bootstrap.RestService; | ||
import io.quarkus.test.scenarios.QuarkusScenario; | ||
import io.quarkus.test.services.QuarkusApplication; | ||
|
||
@QuarkusScenario | ||
public class HttpPathMetricsPatternIT { | ||
|
||
private static final int ASSERT_METRICS_TIMEOUT_SECONDS = 30; | ||
private static final List<String> HTTP_SERVER_REQUESTS_METRICS_SUFFIX = Arrays.asList("count", "sum", "max"); | ||
private static final String HTTP_SERVER_REQUESTS_METRICS_FORMAT = "http_server_requests_seconds_%s{method=\"GET\",outcome=\"%s\",status=\"%d\",uri=\"%s\"}"; | ||
private static final String REDIRECT_ENDPOINT = "/test/redirect"; | ||
private static final String NOT_FOUND_ENDPOINT = "/test/not-found"; | ||
private static final String MOVED_ENDPOINT = "/test/moved/{id}"; | ||
private static final String EMPTY_ENDPOINT = "/test"; | ||
|
||
@QuarkusApplication | ||
static RestService app = new RestService() | ||
.withProperty("quarkus.management.enabled", "true"); | ||
|
||
@Test | ||
public void verifyUriNotFoundInMetrics() { | ||
whenCallNotFoundEndpoint(); | ||
thenMetricIsExposedInServiceEndpoint(404, "CLIENT_ERROR", NOT_FOUND_ENDPOINT); | ||
} | ||
|
||
@Test | ||
public void verifyRedirectionInMetrtics() { | ||
whenCallRedirectEndpoint(); | ||
thenMetricIsExposedInServiceEndpoint(302, "REDIRECTION", REDIRECT_ENDPOINT); | ||
} | ||
|
||
@Test | ||
public void verifyEmptyPathInMetrics() { | ||
whenCallEmptyPathEndpoint(); | ||
thenMetricIsExposedInServiceEndpoint(204, "SUCCESS", EMPTY_ENDPOINT); | ||
} | ||
|
||
@Test | ||
public void verifyDynamicRedirectionInMetrics() { | ||
String id = "41"; | ||
whenCallDynamicSegmentEndpoint(id); | ||
thenMetricIsExposedInServiceEndpoint(301, "REDIRECTION", MOVED_ENDPOINT); | ||
} | ||
|
||
private void whenCallRedirectEndpoint() { | ||
given() | ||
.redirects().follow(false) | ||
.when().get(REDIRECT_ENDPOINT) | ||
.then().statusCode(302); | ||
} | ||
|
||
private void whenCallEmptyPathEndpoint() { | ||
given() | ||
.when().get("/test") | ||
.then().statusCode(HttpStatus.SC_NO_CONTENT); | ||
} | ||
|
||
private void whenCallNotFoundEndpoint() { | ||
given() | ||
.when().get(NOT_FOUND_ENDPOINT) | ||
.then().statusCode(HttpStatus.SC_NOT_FOUND); | ||
} | ||
|
||
private void whenCallDynamicSegmentEndpoint(String id) { | ||
given() | ||
.pathParam("id", id) | ||
.redirects().follow(false) | ||
.when().get(MOVED_ENDPOINT) | ||
.then().statusCode(HttpStatus.SC_MOVED_PERMANENTLY); | ||
} | ||
|
||
private void thenMetricIsExposedInServiceEndpoint(int statusCode, String outcome, String uri) { | ||
await().ignoreExceptions().atMost(ASSERT_METRICS_TIMEOUT_SECONDS, TimeUnit.SECONDS).untilAsserted(() -> { | ||
String metrics = app.management().get("/q/metrics").then() | ||
.statusCode(HttpStatus.SC_OK).extract().asString(); | ||
for (String metricSuffix : HTTP_SERVER_REQUESTS_METRICS_SUFFIX) { | ||
String metric = String.format(HTTP_SERVER_REQUESTS_METRICS_FORMAT, metricSuffix, outcome, statusCode, uri); | ||
|
||
assertTrue(metrics.contains(metric), "Expected metric : " + metric + ". Metrics: " + metrics); | ||
} | ||
}); | ||
} | ||
|
||
} |