diff --git a/monitoring/micrometer-prometheus/src/main/java/io/quarkus/ts/micrometer/prometheus/UsingMicroProfilePingPongResource.java b/monitoring/micrometer-prometheus/src/main/java/io/quarkus/ts/micrometer/prometheus/UsingMicroProfilePingPongResource.java index 6b1f729df..b7bb7d72d 100644 --- a/monitoring/micrometer-prometheus/src/main/java/io/quarkus/ts/micrometer/prometheus/UsingMicroProfilePingPongResource.java +++ b/monitoring/micrometer-prometheus/src/main/java/io/quarkus/ts/micrometer/prometheus/UsingMicroProfilePingPongResource.java @@ -3,6 +3,7 @@ import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import org.eclipse.microprofile.metrics.MetricUnits; @@ -14,6 +15,7 @@ public class UsingMicroProfilePingPongResource { private static final String PING_PONG = "ping pong"; private static final long DEFAULT_GAUGE_VALUE = 100L; + private static long gaugeValue = DEFAULT_GAUGE_VALUE; @GET @Counted(name = "simple_counter_mp", absolute = true) @@ -25,13 +27,24 @@ public String simpleScenario() { @GET @Produces(MediaType.TEXT_PLAIN) - @Path("/gauge") - public long highestPrimeNumberSoFar() { - return getDefaultGauge(); + @Path("/gauges") + public long gauges(@QueryParam("inc") long inc) { + gaugeValue += inc; + return getFirstGauge() + getSecondGauge() + getThirdGauge(); } - @Gauge(name = "simple_gauge_mp", unit = MetricUnits.NONE) - public long getDefaultGauge() { - return DEFAULT_GAUGE_VALUE; + @Gauge(name = "first_gauge_mp", unit = MetricUnits.NONE) + public long getFirstGauge() { + return gaugeValue; + } + + @Gauge(name = "second_gauge_mp", unit = MetricUnits.NONE) + public long getSecondGauge() { + return gaugeValue; + } + + @Gauge(unit = MetricUnits.NONE) + public long getThirdGauge() { + return gaugeValue; } } diff --git a/monitoring/micrometer-prometheus/src/test/java/io/quarkus/ts/micrometer/prometheus/UsingMicroProfilePingPongResourceIT.java b/monitoring/micrometer-prometheus/src/test/java/io/quarkus/ts/micrometer/prometheus/UsingMicroProfilePingPongResourceIT.java index 092039866..3c9799db8 100644 --- a/monitoring/micrometer-prometheus/src/test/java/io/quarkus/ts/micrometer/prometheus/UsingMicroProfilePingPongResourceIT.java +++ b/monitoring/micrometer-prometheus/src/test/java/io/quarkus/ts/micrometer/prometheus/UsingMicroProfilePingPongResourceIT.java @@ -6,9 +6,12 @@ import static org.hamcrest.Matchers.containsString; import org.apache.http.HttpStatus; +import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import io.quarkus.test.scenarios.QuarkusScenario; +import io.restassured.response.ValidatableResponse; +import io.restassured.specification.RequestSpecification; @QuarkusScenario public class UsingMicroProfilePingPongResourceIT { @@ -16,37 +19,59 @@ public class UsingMicroProfilePingPongResourceIT { private static final String PING_PONG = "ping pong"; private static final String PING_PONG_ENDPOINT = "/using-microprofile-pingpong"; private static final String COUNTER_FORMAT = "simple_counter_mp_total{scope=\"application\",} %s.0"; - private static final String GAUGE_FORMAT = "simple_gauge_mp{scope=\"application\",} %s"; + private static final String FIRST_GAUGE_FORMAT = "first_gauge_mp{scope=\"application\",} %s"; + private static final String SECOND_GAUGE_FORMAT = "second_gauge_mp{scope=\"application\",} %s"; + private static final String THIRD_GAUGE_FORMAT = "getThirdGauge{scope=\"application\",} %s"; private static final long DEFAULT_GAUGE_VALUE = 100; + private static final long GAUGE_INCREMENT = 1; @Test public void testShouldReturnCountOne() { - whenCallPingPong("/counter", PING_PONG); + whenCallCounter(); thenCounterIs(1); } + @Tag("QUARKUS-1545") @Test - public void testShouldReturnDefaultGauge() { - whenCallPingPong("/gauge", "" + DEFAULT_GAUGE_VALUE); - thenGaugeIs(DEFAULT_GAUGE_VALUE); + public void testShouldReturnAndModifyGauges() { + final long incrementedGaugeValue = DEFAULT_GAUGE_VALUE + GAUGE_INCREMENT; + whenCallGauges(GAUGE_INCREMENT, incrementedGaugeValue * 3); + thenGaugesAreSampled(incrementedGaugeValue); + whenCallGauges(-GAUGE_INCREMENT, DEFAULT_GAUGE_VALUE * 3); + thenGaugesAreSampled(DEFAULT_GAUGE_VALUE); } - private void whenCallPingPong(String path, String expectedBody) { - given() - .when().get(PING_PONG_ENDPOINT + path) - .then().statusCode(HttpStatus.SC_OK) + private void whenCallCounter() { + callPingPong(given(), "/counter", PING_PONG); + } + + private void whenCallGauges(long increment, long expectedGaugeValue) { + callPingPong(given().queryParam("inc", increment), "/gauges", String.valueOf(expectedGaugeValue)); + } + + private void callPingPong(RequestSpecification requestSpecification, String path, String expectedBody) { + requestSpecification.when() + .get(PING_PONG_ENDPOINT + path) + .then() + .statusCode(HttpStatus.SC_OK) .body(is(expectedBody)); } private void thenCounterIs(int expectedCounter) { - when().get("/q/metrics").then() - .statusCode(HttpStatus.SC_OK) - .body(containsString(String.format(COUNTER_FORMAT, expectedCounter))); + callMetrics().body(containsString(String.format(COUNTER_FORMAT, expectedCounter))); } - private void thenGaugeIs(double expectedGauge) { - when().get("/q/metrics").then() - .statusCode(HttpStatus.SC_OK) - .body(containsString(String.format(GAUGE_FORMAT, expectedGauge))); + private void thenGaugesAreSampled(double expectedGaugeValue) { + callMetrics().body( + containsString(String.format(FIRST_GAUGE_FORMAT, expectedGaugeValue)), + containsString(String.format(SECOND_GAUGE_FORMAT, expectedGaugeValue)), + containsString(String.format(THIRD_GAUGE_FORMAT, expectedGaugeValue))); + } + + private ValidatableResponse callMetrics() { + return when() + .get("/q/metrics") + .then() + .statusCode(HttpStatus.SC_OK); } }