Skip to content

Commit

Permalink
greeting resource works
Browse files Browse the repository at this point in the history
  • Loading branch information
aureamunoz committed Sep 26, 2023
1 parent 9fdf296 commit 95c6a3d
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class StorkMetricsDisabledTest {
@Test
void testNoInstancePresentIfNoRedisClientsClass() {
assertTrue(bean.isUnsatisfied(),
"No redis metrics bean");
"No Stork metrics bean");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,36 +19,91 @@
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")
.overrideConfigKey("quarkus.micrometer.binder.stork.enabled", "true")
.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<Meter> collect = registry.getMeters().stream().filter(meter -> meter.getId().getTags().contains()).collect(Collectors.toList());
//
// Optional<Meter> 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();
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}

}

0 comments on commit 95c6a3d

Please sign in to comment.