From 95c6a3dc0bb6f7f4ee72e35e5292253d37149818 Mon Sep 17 00:00:00 2001 From: Auri Munoz Date: Tue, 26 Sep 2023 13:08:14 +0200 Subject: [PATCH] greeting resource works --- .../binder/StorkMetricsDisabledTest.java | 2 +- .../deployment/binder/StorkMetricsTest.java | 68 +++++++++++++++++-- .../micrometer/test/GreetingResource.java | 41 +++++++++++ 3 files changed, 103 insertions(+), 8 deletions(-) create mode 100644 extensions/micrometer/deployment/src/test/java/io/quarkus/micrometer/test/GreetingResource.java diff --git a/extensions/micrometer/deployment/src/test/java/io/quarkus/micrometer/deployment/binder/StorkMetricsDisabledTest.java b/extensions/micrometer/deployment/src/test/java/io/quarkus/micrometer/deployment/binder/StorkMetricsDisabledTest.java index 9d68109900f458..8264018946251d 100644 --- a/extensions/micrometer/deployment/src/test/java/io/quarkus/micrometer/deployment/binder/StorkMetricsDisabledTest.java +++ b/extensions/micrometer/deployment/src/test/java/io/quarkus/micrometer/deployment/binder/StorkMetricsDisabledTest.java @@ -27,7 +27,7 @@ public class StorkMetricsDisabledTest { @Test void testNoInstancePresentIfNoRedisClientsClass() { assertTrue(bean.isUnsatisfied(), - "No redis metrics bean"); + "No Stork metrics bean"); } } diff --git a/extensions/micrometer/deployment/src/test/java/io/quarkus/micrometer/deployment/binder/StorkMetricsTest.java b/extensions/micrometer/deployment/src/test/java/io/quarkus/micrometer/deployment/binder/StorkMetricsTest.java index a546961636422a..58e1882f90e149 100644 --- a/extensions/micrometer/deployment/src/test/java/io/quarkus/micrometer/deployment/binder/StorkMetricsTest.java +++ b/extensions/micrometer/deployment/src/test/java/io/quarkus/micrometer/deployment/binder/StorkMetricsTest.java @@ -9,9 +9,8 @@ import jakarta.inject.Inject; import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.DisabledOnOs; -import org.junit.jupiter.api.condition.OS; import org.junit.jupiter.api.extension.RegisterExtension; import io.micrometer.core.instrument.Counter; @@ -20,14 +19,22 @@ import io.micrometer.core.instrument.Tag; import io.micrometer.core.instrument.Timer; import io.quarkus.micrometer.runtime.binder.stork.StorkObservationCollectorBean; +import io.quarkus.micrometer.test.GreetingResource; import io.quarkus.micrometer.test.PingPongResource; import io.quarkus.micrometer.test.Util; import io.quarkus.test.QuarkusUnitTest; import io.smallrye.stork.api.observability.ObservationPoints; -@DisabledOnOs(OS.WINDOWS) public class StorkMetricsTest { + // quarkus.stork.hello-service.service-discovery.type=my + // quarkus.stork.hello-service.load-balancer.type=least-response-time + // hello/mp-rest/url=stork://hello-service/hello + // # slow-service and fast-service come from Slow- and FastWiremockServer + // quarkus.stork.hello-service.service-discovery.address-list=${slow-service},${fast-service} + // quarkus.stork.hello-service.service-discovery.secure=true + // quarkus.tls.trust-all=true + @RegisterExtension static final QuarkusUnitTest config = new QuarkusUnitTest() .withConfigurationResource("test-logging.properties") @@ -35,21 +42,68 @@ public class StorkMetricsTest { .overrideConfigKey("quarkus.stork.pingpong-service.service-discovery.type", "static") .overrideConfigKey("quarkus.stork.pingpong-service.service-discovery.address-list", "${test.url}") .overrideConfigKey("pingpong/mp-rest/url", "stork://pingpong-service") + + // .overrideConfigKey("hello/mp-rest/url", "stork://hello-service/hello") + // .overrideConfigKey("quarkus.stork.hello-service.service-discovery.type", "static") + // .overrideConfigKey("quarkus.stork.hello-service.service-discovery.address-list", "${test.url}") + .overrideConfigKey("greeting/mp-rest/url", "stork://greeting-service") + .overrideConfigKey("quarkus.stork.greeting-service.service-discovery.type", "static") + .overrideConfigKey("quarkus.stork.greeting-service.service-discovery.address-list", "${test.url}") .withApplicationRoot((jar) -> jar - .addClasses(PingPongResource.class, PingPongResource.PingPongRestClient.class, Util.class)); + .addClasses(PingPongResource.class, PingPongResource.PingPongRestClient.class, GreetingResource.class, + GreetingResource.GreetingRestClient.class, Util.class)); @Inject MeterRegistry registry; @Test + @Disabled public void shouldGetStorkMetricsWhenEverythingSucceded() { when().get("/ping/one").then().statusCode(200); //Stork metrics - assertStorkMetrics(); + assertStorkMetrics("pingpong-service"); + + // Stork metrics exposed to Micrometer + Counter instanceCounter = registry.get("stork.instances.count").counter(); + Timer serviceDiscoveryDuration = registry.get("stork.service-discovery.duration").timer(); + Timer serviceSelectionDuration = registry.get("stork.service-selection.duration").timer(); + Timer overallDuration = registry.get("stork.overall.duration").timer(); + Gauge serviceDiscoveryFailures = registry.get("stork.service-discovery.failures").gauge(); + Gauge loadBalancerFailures = registry.get("stork.load-balancer.failures").gauge(); + + Util.assertTags(Tag.of("service-name", "pingpong-service"), instanceCounter, serviceDiscoveryDuration, + serviceSelectionDuration, overallDuration); + + Assertions.assertThat(instanceCounter.count()).isEqualTo(1); + Assertions.assertThat(serviceDiscoveryFailures.value()).isEqualTo(0); + Assertions.assertThat(loadBalancerFailures.value()).isEqualTo(0); + Assertions.assertThat(serviceDiscoveryDuration.totalTime(TimeUnit.NANOSECONDS)).isGreaterThan(0); + Assertions.assertThat(serviceSelectionDuration.totalTime(TimeUnit.NANOSECONDS)).isGreaterThan(0); + Assertions.assertThat(overallDuration.totalTime(TimeUnit.NANOSECONDS)).isGreaterThan(0); + + } + + @Test + public void shouldGetStorkMetricsWhenEverythingSuccededRef() { + // when().get("/ping/one").then().statusCode(200); + when().get("greeting/hola").then().statusCode(200); + + //Stork metrics + assertStorkMetrics("pingpong-service"); + assertStorkMetrics("greeting-service"); + + // Tag pingpongTag = Tag.of("service-name", "pingpong-service"); + // + // List collect = registry.getMeters().stream().filter(meter -> meter.getId().getTags().contains()).collect(Collectors.toList()); + // + // Optional counterMeter = collect.stream().filter(meter -> meter.getId().getName().equals("stork.instances.count")).findFirst(); + // + // assertThat(counterMeter).isPresent(); // Stork metrics exposed to Micrometer Counter instanceCounter = registry.get("stork.instances.count").counter(); + Counter instanceCounter2 = registry.counter("stork.instances.count", "pingpong-service"); Timer serviceDiscoveryDuration = registry.get("stork.service-discovery.duration").timer(); Timer serviceSelectionDuration = registry.get("stork.service-selection.duration").timer(); Timer overallDuration = registry.get("stork.overall.duration").timer(); @@ -68,11 +122,11 @@ public void shouldGetStorkMetricsWhenEverythingSucceded() { } - private static void assertStorkMetrics() { + private static void assertStorkMetrics(String serviceName) { ObservationPoints.StorkResolutionEvent metrics = StorkObservationCollectorBean.STORK_METRICS; Assertions.assertThat(metrics.getDiscoveredInstancesCount()).isEqualTo(1); Assertions.assertThat(metrics.getSelectedInstanceId()).isEqualTo(0); - Assertions.assertThat(metrics.getServiceName()).isEqualTo("pingpong-service"); + Assertions.assertThat(metrics.getServiceName()).isEqualTo(serviceName); Assertions.assertThat(metrics.isDone()).isTrue(); Assertions.assertThat(metrics.failure()).isNull(); Assertions.assertThat(metrics.getOverallDuration()).isNotNull(); diff --git a/extensions/micrometer/deployment/src/test/java/io/quarkus/micrometer/test/GreetingResource.java b/extensions/micrometer/deployment/src/test/java/io/quarkus/micrometer/test/GreetingResource.java new file mode 100644 index 00000000000000..7832966ce6f0e3 --- /dev/null +++ b/extensions/micrometer/deployment/src/test/java/io/quarkus/micrometer/test/GreetingResource.java @@ -0,0 +1,41 @@ +package io.quarkus.micrometer.test; + +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.ws.rs.Consumes; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.PathParam; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; + +import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; +import org.eclipse.microprofile.rest.client.inject.RestClient; + +@Path("/") +@ApplicationScoped +public class GreetingResource { + + @RegisterRestClient(configKey = "greeting") + public interface GreetingRestClient { + @GET + @Path("/echo/{message}") + @Consumes(MediaType.TEXT_PLAIN) + String echo(@PathParam("message") String name); + } + + @RestClient + GreetingRestClient greetingRestClient; + + @GET + @Path("greeting/{message}") + public String passThrough(@PathParam("message") String message) { + return greetingRestClient.echo(message + " World!"); + } + + @GET + @Path("/echo/{message}") + public Response echo(@PathParam("message") String message) { + return Response.ok(message, "text/plain").build(); + } + +}