Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
aureamunoz committed Sep 21, 2023
1 parent 7cd49cb commit 4957b27
Show file tree
Hide file tree
Showing 11 changed files with 415 additions and 18 deletions.
27 changes: 23 additions & 4 deletions extensions/micrometer/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,31 @@

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-deployment</artifactId>
<artifactId>quarkus-resteasy-reactive-deployment</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-deployment</artifactId>
<artifactId>quarkus-rest-client-reactive-deployment</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jackson-deployment</artifactId>
<artifactId>quarkus-resteasy-reactive-jackson-deployment</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.smallrye.stork</groupId>
<artifactId>stork-service-discovery-static-list</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.smallrye.stork</groupId>
<artifactId>stork-configuration-generator</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-undertow-deployment</artifactId>
Expand Down Expand Up @@ -132,6 +141,16 @@
<artifactId>resteasy-reactive-client</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@

package io.quarkus.micrometer.deployment.binder;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

import java.util.concurrent.TimeUnit;

import jakarta.inject.Inject;

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 org.mockito.Mockito;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
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.FakeSelectorConfiguration;
import io.quarkus.micrometer.test.MockConfiguration;
import io.quarkus.micrometer.test.MockLoadBalancerProvider;
import io.quarkus.micrometer.test.MockServiceDiscoveryProvider;
import io.quarkus.micrometer.test.MockServiceDiscoveryProviderLoader;
import io.quarkus.micrometer.test.PingPongResource;
import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;
import io.smallrye.mutiny.Uni;

@DisabledOnOs(OS.WINDOWS)
public class StorkMetricsServiceDiscoveryFailTest {

@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", "mock")
.overrideConfigKey("pingpong/mp-rest/url", "stork://pingpong-service")
.withApplicationRoot((jar) -> jar
.addClasses(PingPongResource.class, PingPongResource.PingPongRestClient.class,
MockServiceDiscoveryProvider.class, MockLoadBalancerProvider.class, MockConfiguration.class,
FakeSelectorConfiguration.class, MockServiceDiscoveryProviderLoader.class));

@Inject
MeterRegistry registry;

@Inject
MockServiceDiscoveryProvider provider;

@Test
public void shouldGetStorkMetricsWhenEverythingSucceded() {

Mockito.when(provider.getServiceDiscovery().getServiceInstances())
.thenReturn(Uni.createFrom().failure(new RuntimeException("Service Discovery induced failure")));
RestAssured.when().get("/ping/one").then().statusCode(500);

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 failures = registry.get("stork.failures.number").gauge();

assertTags(Tag.of("service-name", "pingpong-service"), instanceCounter, serviceDiscoveryDuration,
serviceSelectionDuration, overallDuration);

assertThat(StorkObservationCollectorBean.STORK_METRICS.failure()).isNotNull();
assertThat(instanceCounter.count()).isEqualTo(0);
assertThat(failures.value()).isGreaterThan(0);
assertThat(serviceDiscoveryDuration.totalTime(TimeUnit.NANOSECONDS)).isGreaterThan(0);
assertThat(serviceSelectionDuration.totalTime(TimeUnit.NANOSECONDS)).isEqualTo(0);
assertThat(overallDuration.totalTime(TimeUnit.NANOSECONDS)).isGreaterThan(0);

}

private static void assertTags(Tag tag, Meter... meters) {
for (Meter meter : meters) {
assertThat(meter.getId().getTags().contains(tag));
}
}

}
Original file line number Diff line number Diff line change
@@ -1,22 +1,79 @@

package io.quarkus.micrometer.deployment.binder;

import static io.restassured.RestAssured.when;

import java.util.concurrent.TimeUnit;

import jakarta.inject.Inject;

import org.junit.jupiter.api.Assertions;
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;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Timer;
import io.quarkus.micrometer.test.PingPongResource;
import io.quarkus.test.QuarkusUnitTest;

@DisabledOnOs(OS.WINDOWS)
public class StorkMetricsTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest();
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")
.withApplicationRoot((jar) -> jar
.addClasses(PingPongResource.class, PingPongResource.PingPongRestClient.class));

@Inject
MeterRegistry registry;

@Test
public void shouldGetStorkMetricsWhenEverythingSucceded() {
when().get("/ping/one").then().statusCode(200);

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

Assertions.assertTrue(instanceCounter.getId().getTags().contains(Tag.of("service-name", "pingpong-service")));
Assertions.assertTrue(serviceDiscoveryDuration.getId().getTags().contains(Tag.of("service-name", "pingpong-service")));
Assertions.assertTrue(serviceSelectionDuration.getId().getTags().contains(Tag.of("service-name", "pingpong-service")));

Assertions.assertEquals(instanceCounter.count(), 1);
Assertions.assertTrue(1 == serviceDiscoveryDuration.count());
Assertions.assertTrue(serviceDiscoveryDuration.totalTime(TimeUnit.NANOSECONDS) > 0);
Assertions.assertTrue(1 == serviceSelectionDuration.count());
Assertions.assertTrue(serviceSelectionDuration.totalTime(TimeUnit.NANOSECONDS) > 0);

}

@Test
public void shouldGetStorkMetricsWhenServiceDiscoveryFails() {
when().get("/ping/one").then().statusCode(200);

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

Assertions.assertTrue(instanceCounter.getId().getTags().contains(Tag.of("service-name", "pingpong-service")));
Assertions.assertTrue(serviceDiscoveryDuration.getId().getTags().contains(Tag.of("service-name", "pingpong-service")));
Assertions.assertTrue(serviceSelectionDuration.getId().getTags().contains(Tag.of("service-name", "pingpong-service")));

Assertions.assertEquals(instanceCounter.count(), 1);
Assertions.assertTrue(1 == serviceDiscoveryDuration.count());
Assertions.assertTrue(serviceDiscoveryDuration.totalTime(TimeUnit.NANOSECONDS) > 0);
Assertions.assertTrue(1 == serviceSelectionDuration.count());
Assertions.assertTrue(serviceSelectionDuration.totalTime(TimeUnit.NANOSECONDS) > 0);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.quarkus.micrometer.test;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import io.smallrye.stork.api.config.ConfigWithType;

/**
* Configuration for the {@code MockLoadBalancerProvider} LoadBalancer.
*/
public class FakeSelectorConfiguration implements io.smallrye.stork.api.config.ConfigWithType {
private final Map<String, String> parameters;

/**
* Creates a new FakeSelectorConfiguration
*
* @param params the parameters, must not be {@code null}
*/
public FakeSelectorConfiguration(Map<String, String> params) {
parameters = Collections.unmodifiableMap(params);
}

/**
* Creates a new FakeSelectorConfiguration
*/
public FakeSelectorConfiguration() {
parameters = Collections.emptyMap();
}

/**
* @return the type
*/
@Override
public String type() {
return "fake-selector";
}

/**
* @return the parameters
*/
@Override
public Map<String, String> parameters() {
return parameters;
}

private FakeSelectorConfiguration extend(String key, String value) {
Map<String, String> copy = new HashMap<>(parameters);
copy.put(key, value);
return new FakeSelectorConfiguration(copy);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.quarkus.micrometer.test;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import io.smallrye.stork.api.config.ConfigWithType;

/**
* Configuration for the {@code MockServiceDiscoveryProvider} ServiceDiscovery.
*/
public class MockConfiguration implements io.smallrye.stork.api.config.ConfigWithType {
private final Map<String, String> parameters;

/**
* Creates a new MockConfiguration
*
* @param params the parameters, must not be {@code null}
*/
public MockConfiguration(Map<String, String> params) {
parameters = Collections.unmodifiableMap(params);
}

/**
* Creates a new MockConfiguration
*/
public MockConfiguration() {
parameters = Collections.emptyMap();
}

/**
* @return the type
*/
@Override
public String type() {
return "mock";
}

/**
* @return the parameters
*/
@Override
public Map<String, String> parameters() {
return parameters;
}

private MockConfiguration extend(String key, String value) {
Map<String, String> copy = new HashMap<>(parameters);
copy.put(key, value);
return new MockConfiguration(copy);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.quarkus.micrometer.test;

import jakarta.enterprise.context.ApplicationScoped;

import org.mockito.Mockito;

import io.smallrye.stork.api.LoadBalancer;
import io.smallrye.stork.api.ServiceDiscovery;
import io.smallrye.stork.api.config.LoadBalancerType;
import io.smallrye.stork.spi.LoadBalancerProvider;

@LoadBalancerType("fake-selector")
@ApplicationScoped
public class MockLoadBalancerProvider implements LoadBalancerProvider<FakeSelectorConfiguration> {

@Override
public LoadBalancer createLoadBalancer(FakeSelectorConfiguration config, ServiceDiscovery serviceDiscovery) {
return Mockito.mock(LoadBalancer.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.quarkus.micrometer.test;

import jakarta.enterprise.context.ApplicationScoped;

import org.mockito.Mockito;

import io.smallrye.stork.api.ServiceDiscovery;
import io.smallrye.stork.api.config.ServiceConfig;
import io.smallrye.stork.api.config.ServiceDiscoveryType;
import io.smallrye.stork.spi.ServiceDiscoveryProvider;
import io.smallrye.stork.spi.StorkInfrastructure;

@ServiceDiscoveryType("mock")
//@ServiceDiscoveryAttribute(name = "failure", description = "indicates if service discovery should fail")
@ApplicationScoped
public class MockServiceDiscoveryProvider implements ServiceDiscoveryProvider<MockConfiguration> {

public ServiceDiscovery getServiceDiscovery() {
return serviceDiscovery;
}

private ServiceDiscovery serviceDiscovery;

@Override
public ServiceDiscovery createServiceDiscovery(MockConfiguration config, String serviceName, ServiceConfig serviceConfig,
StorkInfrastructure storkInfrastructure) {
serviceDiscovery = Mockito.mock(ServiceDiscovery.class);
return serviceDiscovery;
}

}
Loading

0 comments on commit 4957b27

Please sign in to comment.