-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7cd49cb
commit 4957b27
Showing
11 changed files
with
415 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...st/java/io/quarkus/micrometer/deployment/binder/StorkMetricsServiceDiscoveryFailTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
|
||
} |
59 changes: 58 additions & 1 deletion
59
...er/deployment/src/test/java/io/quarkus/micrometer/deployment/binder/StorkMetricsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
...ometer/deployment/src/test/java/io/quarkus/micrometer/test/FakeSelectorConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...ons/micrometer/deployment/src/test/java/io/quarkus/micrometer/test/MockConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...rometer/deployment/src/test/java/io/quarkus/micrometer/test/MockLoadBalancerProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...ter/deployment/src/test/java/io/quarkus/micrometer/test/MockServiceDiscoveryProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
Oops, something went wrong.