Skip to content

Commit

Permalink
Merge pull request #653 from jsmrcka/feature/micrometer-gauge
Browse files Browse the repository at this point in the history
Extend test of annotated Gauge metrics
  • Loading branch information
Pablo Gonzalez Granados authored May 19, 2022
2 parents f31301d + 9bd4284 commit 482077d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,72 @@
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 {

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);
}
}

0 comments on commit 482077d

Please sign in to comment.