Skip to content

Commit

Permalink
Refactor to manage concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
aureamunoz committed Sep 28, 2023
1 parent 717ef6b commit d73f80d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import io.quarkus.micrometer.test.Util;
import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;
import io.smallrye.stork.api.observability.ObservationPoints;
import io.smallrye.stork.api.observability.StorkObservationPoints;

@DisabledOnOs(OS.WINDOWS)
public class StorkMetricsLoadBalancerFailTest {
Expand Down Expand Up @@ -81,7 +81,7 @@ public void shouldGetStorkMetricsWhenServiceSelectorFails() {
}

private static void assertStorkMetrics() {
ObservationPoints.StorkResolutionEvent metrics = StorkObservationCollectorBean.STORK_METRICS
StorkObservationPoints metrics = StorkObservationCollectorBean.STORK_METRICS
.get("pingpong-service" + StorkObservationCollectorBean.METRICS_SUFIX);
Assertions.assertThat(metrics.getDiscoveredInstancesCount()).isEqualTo(1);
Assertions.assertThat(metrics.getSelectedInstanceId()).isEqualTo(-1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;
import io.smallrye.mutiny.Uni;
import io.smallrye.stork.api.observability.ObservationPoints;
import io.smallrye.stork.api.observability.StorkObservationPoints;

public class StorkMetricsServiceDiscoveryFailTest {

Expand Down Expand Up @@ -77,7 +77,7 @@ public void shouldGetStorkMetricsWhenServiceDiscoveryFails() {
}

private static void assertStorkMetrics() {
ObservationPoints.StorkResolutionEvent metrics = StorkObservationCollectorBean.STORK_METRICS
StorkObservationPoints metrics = StorkObservationCollectorBean.STORK_METRICS
.get("pingpong-service" + StorkObservationCollectorBean.METRICS_SUFIX);
Assertions.assertThat(metrics.getDiscoveredInstancesCount()).isNegative();
Assertions.assertThat(metrics.getSelectedInstanceId()).isEqualTo(-1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package io.quarkus.micrometer.deployment.binder;

import static io.restassured.RestAssured.when;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

import java.util.concurrent.TimeUnit;

Expand All @@ -22,7 +21,7 @@
import io.quarkus.micrometer.test.PingPongResource;
import io.quarkus.micrometer.test.Util;
import io.quarkus.test.QuarkusUnitTest;
import io.smallrye.stork.api.observability.ObservationPoints;
import io.smallrye.stork.api.observability.StorkObservationPoints;

public class StorkMetricsTest {

Expand Down Expand Up @@ -79,7 +78,7 @@ public void assertStorkMetricsInMicrometerRegistry(String serviceName) {
}

public static void assertStorkMetrics(String serviceName) {
ObservationPoints.StorkResolutionEvent metrics = StorkObservationCollectorBean.STORK_METRICS
StorkObservationPoints metrics = StorkObservationCollectorBean.STORK_METRICS
.get(serviceName + StorkObservationCollectorBean.METRICS_SUFIX);
Assertions.assertThat(metrics.getDiscoveredInstancesCount()).isEqualTo(1);
Assertions.assertThat(metrics.getSelectedInstanceId()).isNotNegative();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,28 @@
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.Timer;
import io.smallrye.stork.api.observability.ObservationCollector;
import io.smallrye.stork.api.observability.ObservationPoints;
import io.smallrye.stork.api.observability.StorkEventHandler;
import io.smallrye.stork.api.observability.StorkObservationPoints;

@ApplicationScoped
@Typed(ObservationCollector.class)
public class StorkObservationCollectorBean implements ObservationCollector, ObservationCollector.EventCompletionHandler {
public class StorkObservationCollectorBean implements ObservationCollector, StorkEventHandler {

public static final String METRICS_SUFIX = "-metrics";
final MeterRegistry registry = Metrics.globalRegistry;

public final static Map<String, ObservationPoints.StorkResolutionEvent> STORK_METRICS = new ConcurrentHashMap<>();
public final static Map<String, StorkObservationPoints> STORK_METRICS = new ConcurrentHashMap<>();

@Override
public ObservationPoints.StorkResolutionEvent create(String serviceName, String serviceDiscoveryType,
public StorkObservationPoints create(String serviceName, String serviceDiscoveryType,
String serviceSelectionType) {
return STORK_METRICS.computeIfAbsent(serviceName + METRICS_SUFIX,
key -> new ObservationPoints.StorkResolutionEvent(serviceName, serviceDiscoveryType, serviceSelectionType,
key -> new StorkObservationPoints(serviceName, serviceDiscoveryType, serviceSelectionType,
this));
}

@Override
public void complete(ObservationPoints.StorkResolutionEvent event) {
public void complete(StorkObservationPoints event) {
Tags tags = Tags.of(Tag.of("service-name", event.getServiceName()));

Counter instanceCounter = Counter.builder("stork.instances.count")
Expand Down Expand Up @@ -83,7 +84,7 @@ public void complete(ObservationPoints.StorkResolutionEvent event) {
overallTimer.record(event.getServiceSelectionDuration().getNano(), TimeUnit.NANOSECONDS);

if (event.failure() != null) {
if (event.isServiceDiscoveryDone()) {
if (event.isServiceDiscoverySuccessful()) {
serviceSelectionExceptions.add(event.failure());
} else {// SD failure
serviceDiscoveryExceptions.add(event.failure());
Expand Down

0 comments on commit d73f80d

Please sign in to comment.