From 2d6f0b80648258bc9b89a8a6053aa378dbb418a8 Mon Sep 17 00:00:00 2001 From: heowc Date: Wed, 24 Apr 2024 15:20:02 +0900 Subject: [PATCH] apply review --- .../ROOT/pages/spring-cloud-netflix.adoc | 9 ++++---- .../DefaultEurekaInstanceTagsProvider.java | 2 +- ...urekaInstanceMetricsAutoConfiguration.java | 4 ++-- .../server/metrics/EurekaInstanceMonitor.java | 8 ++++--- .../metrics/EurekaInstanceTagsProvider.java | 5 ++--- ...ixture.java => EurekaInstanceFixture.java} | 4 ++-- .../server/EurekaInstanceMonitorTests.java | 22 +++++++++---------- .../eureka/server/InstanceRegistryTests.java | 4 ++-- 8 files changed, 29 insertions(+), 29 deletions(-) rename spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/{EurekaInstancesFixture.java => EurekaInstanceFixture.java} (94%) diff --git a/docs/modules/ROOT/pages/spring-cloud-netflix.adoc b/docs/modules/ROOT/pages/spring-cloud-netflix.adoc index b4ada6630..f2b73ae27 100755 --- a/docs/modules/ROOT/pages/spring-cloud-netflix.adoc +++ b/docs/modules/ROOT/pages/spring-cloud-netflix.adoc @@ -606,15 +606,14 @@ Spring Cloud Netflix Eureka Server does not support Spring AOT transformations o === Metrics -Starting with version 4.1.1, `MultuGuage` listens events related to Eureka instances and creates and updates Eureka instance information in the metrics registry. By default, this setting is disabled, if you want to enable it, you need to change `eureka.server.metrics.enabled` to `true`. +`EurekaInstanceMonitor` listens to events related to Eureka instance registration and creates/updates `Gauge`s for Eureka instance information Micrometer's `MeterRegistry`. By default, this behavior is disabled. Ff you want to enable it, you need to set `eureka.server.metrics.enabled` to `true`. -The multi-gauge is named `eureka.server.instance.status` and has the following tags (value are merged based on tags) +By default, the `Gauge`s are named `eureka.server.instances` and have the following tags: - `application`: application name +- `status`: instance status (`UP`, `DOWN`, `STARTING`, `OUT_OF_SERVICE`, `UNKNOWN`, see: `com.netflix.appinfo.InstanceInfo.InstanceStatus`) -- `status`: instance status - -You can add additional tags by implementing `EurekaInstanceTagsProvider` separately. However, status is added separately because it is always needed. +You can add additional tags by injecting your own implementation of `EurekaInstanceTagsProvider`. == Configuration properties diff --git a/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/metrics/DefaultEurekaInstanceTagsProvider.java b/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/metrics/DefaultEurekaInstanceTagsProvider.java index 38a60c8a8..3dbf097f9 100644 --- a/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/metrics/DefaultEurekaInstanceTagsProvider.java +++ b/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/metrics/DefaultEurekaInstanceTagsProvider.java @@ -24,7 +24,7 @@ * Default implementation for {@link EurekaInstanceTagsProvider}. * * @author Wonchul Heo - * @since 4.1.1 + * @since 4.1.2 */ class DefaultEurekaInstanceTagsProvider implements EurekaInstanceTagsProvider { diff --git a/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/metrics/EurekaInstanceMetricsAutoConfiguration.java b/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/metrics/EurekaInstanceMetricsAutoConfiguration.java index 2fea31991..62bce04e1 100644 --- a/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/metrics/EurekaInstanceMetricsAutoConfiguration.java +++ b/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/metrics/EurekaInstanceMetricsAutoConfiguration.java @@ -32,8 +32,8 @@ /** * Auto-configuration for Eureka Instance metrics. * - * @author wonchul heo - * @since 4.1.1 + * @author Wonchul Heo + * @since 4.1.2 */ @ConditionalOnClass(MeterRegistry.class) @ConditionalOnBean(MeterRegistry.class) diff --git a/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/metrics/EurekaInstanceMonitor.java b/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/metrics/EurekaInstanceMonitor.java index c92478b3d..5c029fed5 100644 --- a/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/metrics/EurekaInstanceMonitor.java +++ b/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/metrics/EurekaInstanceMonitor.java @@ -36,7 +36,7 @@ * {@link PeerAwareInstanceRegistry}. * * @author Wonchul Heo - * @since 4.1.1 + * @since 4.1.2 */ public class EurekaInstanceMonitor implements SmartApplicationListener { @@ -52,12 +52,13 @@ public class EurekaInstanceMonitor implements SmartApplicationListener { this.instanceRegistry = Objects.requireNonNull(instanceRegistry); this.tagProvider = Objects.requireNonNull(tagProvider); this.eurekaInstances = MultiGauge.builder("eureka.server.instances") - .description("Count of application instances registered with the Eureka server.") + .description("Number of application instances registered with the Eureka server.") .register(meterRegistry); } @Override public boolean supportsEventType(Class eventType) { + // If events that change state are added, an event class must be added. return EurekaInstanceCanceledEvent.class.isAssignableFrom(eventType) || EurekaInstanceRegisteredEvent.class.isAssignableFrom(eventType) || EurekaInstanceRenewedEvent.class.isAssignableFrom(eventType); @@ -69,6 +70,7 @@ public void onApplicationEvent(ApplicationEvent event) { .flatMap(application -> application.getInstances().stream()) .collect(Collectors.groupingBy(tagProvider::eurekaInstanceTags, Collectors.counting())); eurekaInstances.register(aggregatedCounts.entrySet().stream() - .map(it -> MultiGauge.Row.of(it.getKey(), it.getValue())).collect(Collectors.toList()), true); + .map(entry -> MultiGauge.Row.of(entry.getKey(), entry.getValue())).collect(Collectors.toList()), true); } + } diff --git a/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/metrics/EurekaInstanceTagsProvider.java b/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/metrics/EurekaInstanceTagsProvider.java index 5d514ceca..d162d35d4 100644 --- a/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/metrics/EurekaInstanceTagsProvider.java +++ b/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/metrics/EurekaInstanceTagsProvider.java @@ -17,14 +17,13 @@ package org.springframework.cloud.netflix.eureka.server.metrics; import com.netflix.appinfo.InstanceInfo; -import io.micrometer.core.instrument.Tag; import io.micrometer.core.instrument.Tags; /** - * Provides {@link Tag Tags} for Eureka instance metrics. + * Provides {@link Tags} for Eureka instance metrics. * * @author Wonchul Heo - * @since 4.1.1 + * @since 4.1.2 */ public interface EurekaInstanceTagsProvider { diff --git a/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/EurekaInstancesFixture.java b/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/EurekaInstanceFixture.java similarity index 94% rename from spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/EurekaInstancesFixture.java rename to spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/EurekaInstanceFixture.java index e8a41e6ba..738deea18 100644 --- a/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/EurekaInstancesFixture.java +++ b/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/EurekaInstanceFixture.java @@ -19,9 +19,9 @@ import com.netflix.appinfo.InstanceInfo; import com.netflix.appinfo.LeaseInfo; -public final class EurekaInstancesFixture { +public final class EurekaInstanceFixture { - private EurekaInstancesFixture() { + private EurekaInstanceFixture() { } public static LeaseInfo getLeaseInfo() { diff --git a/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/EurekaInstanceMonitorTests.java b/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/EurekaInstanceMonitorTests.java index 37a829dcd..b548c4f6d 100644 --- a/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/EurekaInstanceMonitorTests.java +++ b/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/EurekaInstanceMonitorTests.java @@ -22,7 +22,6 @@ import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.Tag; import io.micrometer.core.instrument.Tags; -import io.micrometer.core.instrument.search.MeterNotFoundException; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -32,9 +31,8 @@ import org.springframework.context.annotation.Configuration; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.springframework.cloud.netflix.eureka.server.EurekaInstancesFixture.getInstanceInfo; -import static org.springframework.cloud.netflix.eureka.server.EurekaInstancesFixture.getLeaseInfo; +import static org.springframework.cloud.netflix.eureka.server.EurekaInstanceFixture.getInstanceInfo; +import static org.springframework.cloud.netflix.eureka.server.EurekaInstanceFixture.getLeaseInfo; /** * @author Wonchul Heo @@ -45,6 +43,7 @@ class EurekaInstanceMonitorTests { private static final String FOO_APP_NAME = "FOO-APP-NAME"; + private static final String BAR_APP_NAME = "BAR-APP-NAME"; @Autowired @@ -54,8 +53,11 @@ class EurekaInstanceMonitorTests { private MeterRegistry meterRegistry; private InstanceInfo fooInstanceInfo; + private InstanceInfo fooInstanceInfo2; + private InstanceInfo barInstanceInfo; + private InstanceInfo barInstanceInfo2; @BeforeEach @@ -70,9 +72,7 @@ void setup() { @Test void testNoRegistration() { - assertThatThrownBy(() -> { - meterRegistry.get("eureka.server.instances").meter(); - }).isInstanceOf(MeterNotFoundException.class); + assertThat(meterRegistry.find("eureka.server.instances").gauge()).isNull(); } @Test @@ -151,10 +151,9 @@ private static Tags tags(InstanceInfo instanceInfo) { } private void assertEurekaInstance(Map meterRegistryCounts) { - meterRegistryCounts.forEach((tags, count) -> { - assertThat((long) meterRegistry.get("eureka.server.instances").tags(tags).gauge().value()) - .isEqualTo(count); - }); + meterRegistryCounts.forEach((tags, + count) -> assertThat((long) meterRegistry.get("eureka.server.instances").tags(tags).gauge().value()) + .isEqualTo(count)); } @Configuration(proxyBeanMethods = false) @@ -163,4 +162,5 @@ private void assertEurekaInstance(Map meterRegistryCounts) { protected static class Application { } + } diff --git a/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/InstanceRegistryTests.java b/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/InstanceRegistryTests.java index 505ec9ffe..a2310a3bf 100644 --- a/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/InstanceRegistryTests.java +++ b/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/InstanceRegistryTests.java @@ -37,8 +37,8 @@ import org.springframework.context.event.SmartApplicationListener; import static org.assertj.core.api.Assertions.assertThat; -import static org.springframework.cloud.netflix.eureka.server.EurekaInstancesFixture.getInstanceInfo; -import static org.springframework.cloud.netflix.eureka.server.EurekaInstancesFixture.getLeaseInfo; +import static org.springframework.cloud.netflix.eureka.server.EurekaInstanceFixture.getInstanceInfo; +import static org.springframework.cloud.netflix.eureka.server.EurekaInstanceFixture.getLeaseInfo; /** * @author Bartlomiej Slota