From 11643b97ac00abd4f034fb9dfbe6bb26fd2caee6 Mon Sep 17 00:00:00 2001 From: Matheus Cruz Date: Thu, 20 Jun 2024 12:42:16 -0300 Subject: [PATCH] Increase Micrometer performance using Meter.MeterProvider --- .../StorkMetricsLoadBalancerFailTest.java | 8 +- .../StorkMetricsServiceDiscoveryFailTest.java | 8 +- .../deployment/binder/StorkMetricsTest.java | 10 +-- .../stork/StorkObservationCollectorBean.java | 74 ++++++++++--------- 4 files changed, 52 insertions(+), 48 deletions(-) diff --git a/extensions/micrometer/deployment/src/test/java/io/quarkus/micrometer/deployment/binder/StorkMetricsLoadBalancerFailTest.java b/extensions/micrometer/deployment/src/test/java/io/quarkus/micrometer/deployment/binder/StorkMetricsLoadBalancerFailTest.java index ab80ef83b8e2d..db194f741a533 100644 --- a/extensions/micrometer/deployment/src/test/java/io/quarkus/micrometer/deployment/binder/StorkMetricsLoadBalancerFailTest.java +++ b/extensions/micrometer/deployment/src/test/java/io/quarkus/micrometer/deployment/binder/StorkMetricsLoadBalancerFailTest.java @@ -76,7 +76,7 @@ public void shouldGetStorkMetricsWhenServiceSelectorFails() { private static void assertStorkMetrics(String serviceName) { StorkObservation metrics = StorkObservationCollectorBean.STORK_METRICS - .get(serviceName + StorkObservationCollectorBean.METRICS_SUFIX); + .get(serviceName + StorkObservationCollectorBean.METRICS_SUFFIX); Assertions.assertThat(metrics.getDiscoveredInstancesCount()).isEqualTo(1); Assertions.assertThat(metrics.getServiceName()).isEqualTo(serviceName); Assertions.assertThat(metrics.isDone()).isTrue(); @@ -94,10 +94,8 @@ private void assertStorkMetricsInMicrometerRegistry(String serviceName) { Counter instanceCounter = registry.counter("stork.service-discovery.instances.count", "service-name", serviceName); Timer serviceDiscoveryDuration = registry.timer("stork.service-discovery.duration", "service-name", serviceName); Timer serviceSelectionDuration = registry.timer("stork.service-selection.duration", "service-name", serviceName); - Counter serviceDiscoveryFailures = registry.get("stork.service-discovery.failures") - .tags("service-name", serviceName).counter(); - Counter loadBalancerFailures = registry.get("stork.service-selection.failures").tags("service-name", serviceName) - .counter(); + Counter serviceDiscoveryFailures = registry.counter("stork.service-discovery.failures", "service-name", serviceName); + Counter loadBalancerFailures = registry.counter("stork.service-selection.failures", "service-name", serviceName); Util.assertTags(Tag.of("service-name", serviceName), instanceCounter, serviceDiscoveryDuration, serviceSelectionDuration); diff --git a/extensions/micrometer/deployment/src/test/java/io/quarkus/micrometer/deployment/binder/StorkMetricsServiceDiscoveryFailTest.java b/extensions/micrometer/deployment/src/test/java/io/quarkus/micrometer/deployment/binder/StorkMetricsServiceDiscoveryFailTest.java index e9e4437bda7d9..bb7bf88b562f2 100644 --- a/extensions/micrometer/deployment/src/test/java/io/quarkus/micrometer/deployment/binder/StorkMetricsServiceDiscoveryFailTest.java +++ b/extensions/micrometer/deployment/src/test/java/io/quarkus/micrometer/deployment/binder/StorkMetricsServiceDiscoveryFailTest.java @@ -76,10 +76,8 @@ private void assertStorkMetricsInMicrometerRegistry(String serviceName) { Counter instanceCounter = registry.counter("stork.service-discovery.instances.count", "service-name", serviceName); Timer serviceDiscoveryDuration = registry.timer("stork.service-discovery.duration", "service-name", serviceName); Timer serviceSelectionDuration = registry.timer("stork.service-selection.duration", "service-name", serviceName); - Counter serviceDiscoveryFailures = registry.get("stork.service-discovery.failures") - .tags("service-name", serviceName).counter(); - Counter loadBalancerFailures = registry.get("stork.service-selection.failures").tags("service-name", serviceName) - .counter(); + Counter serviceDiscoveryFailures = registry.counter("stork.service-discovery.failures", "service-name", serviceName); + Counter loadBalancerFailures = registry.counter("stork.service-selection.failures", "service-name", serviceName); Util.assertTags(Tag.of("service-name", serviceName), instanceCounter, serviceDiscoveryDuration, serviceSelectionDuration); @@ -93,7 +91,7 @@ private void assertStorkMetricsInMicrometerRegistry(String serviceName) { private static void assertStorkMetrics(String serviceName) { StorkObservation metrics = StorkObservationCollectorBean.STORK_METRICS - .get(serviceName + StorkObservationCollectorBean.METRICS_SUFIX); + .get(serviceName + StorkObservationCollectorBean.METRICS_SUFFIX); Assertions.assertThat(metrics.getDiscoveredInstancesCount()).isNegative(); Assertions.assertThat(metrics.getServiceName()).isEqualTo(serviceName); Assertions.assertThat(metrics.isDone()).isTrue(); 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 317dbaa8e9151..42ff35297e6e6 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 @@ -60,13 +60,12 @@ public void shouldGetStorkMetricsForTwoServicesWhenEverythingSucceded() { } private void assertStorkMetricsInMicrometerRegistry(String serviceName) { + Counter instanceCounter = registry.counter("stork.service-discovery.instances.count", "service-name", serviceName); Timer serviceDiscoveryDuration = registry.timer("stork.service-discovery.duration", "service-name", serviceName); Timer serviceSelectionDuration = registry.timer("stork.service-selection.duration", "service-name", serviceName); - Counter serviceDiscoveryFailures = registry.get("stork.service-discovery.failures") - .tags("service-name", serviceName).counter(); - Counter loadBalancerFailures = registry.get("stork.service-selection.failures").tags("service-name", serviceName) - .counter(); + Counter serviceDiscoveryFailures = registry.counter("stork.service-discovery.failures", "service-name", serviceName); + Counter loadBalancerFailures = registry.counter("stork.service-selection.failures", "service-name", serviceName); Util.assertTags(Tag.of("service-name", serviceName), instanceCounter, serviceDiscoveryDuration, serviceSelectionDuration); @@ -80,7 +79,8 @@ private void assertStorkMetricsInMicrometerRegistry(String serviceName) { public static void assertStorkMetrics(String serviceName) { StorkObservation metrics = StorkObservationCollectorBean.STORK_METRICS - .get(serviceName + StorkObservationCollectorBean.METRICS_SUFIX); + .get(serviceName + StorkObservationCollectorBean.METRICS_SUFFIX); + Assertions.assertThat(metrics.getDiscoveredInstancesCount()).isEqualTo(1); Assertions.assertThat(metrics.getServiceName()).isEqualTo(serviceName); Assertions.assertThat(metrics.isDone()).isTrue(); diff --git a/extensions/micrometer/runtime/src/main/java/io/quarkus/micrometer/runtime/binder/stork/StorkObservationCollectorBean.java b/extensions/micrometer/runtime/src/main/java/io/quarkus/micrometer/runtime/binder/stork/StorkObservationCollectorBean.java index 8d51d62eba095..c4b1552c280dc 100644 --- a/extensions/micrometer/runtime/src/main/java/io/quarkus/micrometer/runtime/binder/stork/StorkObservationCollectorBean.java +++ b/extensions/micrometer/runtime/src/main/java/io/quarkus/micrometer/runtime/binder/stork/StorkObservationCollectorBean.java @@ -8,6 +8,7 @@ import jakarta.enterprise.inject.Typed; import io.micrometer.core.instrument.Counter; +import io.micrometer.core.instrument.Meter; import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.Metrics; import io.micrometer.core.instrument.Tag; @@ -21,59 +22,66 @@ @Typed(ObservationCollector.class) public class StorkObservationCollectorBean implements ObservationCollector, StorkEventHandler { - public static final String METRICS_SUFIX = "-metrics"; + public static final String METRICS_SUFFIX = "-metrics"; final MeterRegistry registry = Metrics.globalRegistry; - public final static Map STORK_METRICS = new ConcurrentHashMap<>(); + private final Meter.MeterProvider instanceCounter; + private final Meter.MeterProvider serviceDiscoveryTimer; + private final Meter.MeterProvider serviceSelectionTimer; + private final Meter.MeterProvider serviceDiscoveryFailures; + private final Meter.MeterProvider serviceSelectionFailures; - @Override - public StorkObservation create(String serviceName, String serviceDiscoveryType, - String serviceSelectionType) { - return STORK_METRICS.computeIfAbsent(serviceName + METRICS_SUFIX, - key -> new StorkObservation(serviceName, serviceDiscoveryType, serviceSelectionType, - this)); - } - - @Override - public void complete(StorkObservation observation) { - Tags tags = Tags.of(Tag.of("service-name", observation.getServiceName())); + public StorkObservationCollectorBean() { - Counter instanceCounter = Counter.builder("stork.service-discovery.instances.count") + this.instanceCounter = Counter + .builder("stork.service-discovery.instances.count") .description("The number of service instances discovered") - .tags(tags) - .register(registry); + .withRegistry(registry); - Timer serviceDiscoveryTimer = Timer + this.serviceDiscoveryTimer = Timer .builder("stork.service-discovery.duration") .description("The duration of the discovery operation") - .tags(tags) - .register(registry); + .withRegistry(registry); - Timer serviceSelectionTimer = Timer + this.serviceSelectionTimer = Timer .builder("stork.service-selection.duration") .description("The duration of the selection operation ") - .tags(tags) - .register(registry); + .withRegistry(registry); - Counter serviceDiscoveryFailures = Counter + this.serviceDiscoveryFailures = Counter .builder("stork.service-discovery.failures") - .description("The number of failures during service discovery").tags(tags) - .register(registry); + .description("The number of failures during service discovery") + .withRegistry(registry); - Counter serviceSelectionFailures = Counter + this.serviceSelectionFailures = Counter .builder("stork.service-selection.failures") - .description("The number of failures during service selection.").tags(tags) - .register(registry); + .description("The number of failures during service selection.") + .withRegistry(registry); + } + + @Override + public StorkObservation create(String serviceName, String serviceDiscoveryType, + String serviceSelectionType) { + return STORK_METRICS.computeIfAbsent(serviceName + METRICS_SUFFIX, + key -> new StorkObservation(serviceName, serviceDiscoveryType, serviceSelectionType, + this)); + } + + @Override + public void complete(StorkObservation observation) { + Tags tags = Tags.of(Tag.of("service-name", observation.getServiceName())); - instanceCounter.increment(observation.getDiscoveredInstancesCount()); - serviceDiscoveryTimer.record(observation.getServiceDiscoveryDuration().getNano(), TimeUnit.NANOSECONDS); - serviceSelectionTimer.record(observation.getServiceSelectionDuration().getNano(), TimeUnit.NANOSECONDS); + this.instanceCounter.withTags(tags).increment(observation.getDiscoveredInstancesCount()); + this.serviceDiscoveryTimer.withTags(tags).record(observation.getServiceDiscoveryDuration().getNano(), + TimeUnit.NANOSECONDS); + this.serviceSelectionTimer.withTags(tags).record(observation.getServiceSelectionDuration().getNano(), + TimeUnit.NANOSECONDS); if (observation.failure() != null) { if (observation.isServiceDiscoverySuccessful()) { - serviceSelectionFailures.increment(); + this.serviceSelectionFailures.withTags(tags).increment(); } else {// SD failure - serviceDiscoveryFailures.increment(); + this.serviceDiscoveryFailures.withTags(tags).increment(); } }