Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collect metrics for application instances #4270

Merged
merged 15 commits into from
Apr 25, 2024
12 changes: 12 additions & 0 deletions docs/modules/ROOT/pages/spring-cloud-netflix.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,18 @@ when running a Eureka server you must include these dependencies in your POM or

Spring Cloud Netflix Eureka Server does not support Spring AOT transformations or native images.

=== 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.instance.metrics.enabled` to `true`.

The multi-gauge is named `eureka.server.instance.status` and has the following tags (value are merged based on tags)

- `application`: application name

- `status`: instance status

You can add additional tags by implementing `EurekaInstanceTagsProvider` separately. However, status is added separately because it is always needed.

== Configuration properties

To see the list of all Spring Cloud Netflix related configuration properties please check link:appendix.html[the Appendix page].
Expand Down
211 changes: 106 additions & 105 deletions docs/modules/ROOT/partials/_configprops.adoc

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2013-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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;

/**
* Default implementation for {@link EurekaInstanceTagsProvider}.
*
* @author Wonchul Heo
* @since 4.1.1
heowc marked this conversation as resolved.
Show resolved Hide resolved
*/
class DefaultEurekaInstanceTagsProvider implements EurekaInstanceTagsProvider {

@Override
public Tags eurekaInstanceTags(InstanceInfo instanceInfo) {
return Tags.of(Tag.of("application", instanceInfo.getAppName()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2013-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.netflix.eureka.server.metrics;

import com.netflix.eureka.registry.PeerAwareInstanceRegistry;
import io.micrometer.core.instrument.MeterRegistry;

import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.netflix.eureka.server.EurekaServerAutoConfiguration;
import org.springframework.context.annotation.Bean;

/**
* Auto-configuration for Eureka Instance metrics.
*
* @author wonchul heo
* @since 4.1.1
*/
@ConditionalOnClass(MeterRegistry.class)
@ConditionalOnBean(MeterRegistry.class)
heowc marked this conversation as resolved.
Show resolved Hide resolved
@AutoConfigureAfter({ MetricsAutoConfiguration.class, CompositeMeterRegistryAutoConfiguration.class,
EurekaServerAutoConfiguration.class })
@ConditionalOnProperty(name = "eureka.instance.metrics.enabled", havingValue = "true")
heowc marked this conversation as resolved.
Show resolved Hide resolved
class EurekaInstanceMetricsAutoConfiguration {
heowc marked this conversation as resolved.
Show resolved Hide resolved

@ConditionalOnMissingBean
@Bean
public EurekaInstanceTagsProvider eurekaInstanceTagProvider() {
return new DefaultEurekaInstanceTagsProvider();
}

@ConditionalOnMissingBean
@Bean
public EurekaInstanceMonitor eurekaInstanceMeterBinder(MeterRegistry meterRegistry,
PeerAwareInstanceRegistry instanceRegistry, EurekaInstanceTagsProvider tagProvider) {
return new EurekaInstanceMonitor(meterRegistry, instanceRegistry, tagProvider);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2013-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.netflix.eureka.server.metrics;

import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.eureka.registry.PeerAwareInstanceRegistry;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.MultiGauge;
import io.micrometer.core.instrument.Tags;

import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceCanceledEvent;
import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRegisteredEvent;
import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRenewedEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.SmartApplicationListener;

/**
* {@link SmartApplicationListener} for collecting event metrics from
* {@link PeerAwareInstanceRegistry}.
*
* @author Wonchul Heo
* @since 4.1.1
*/
public class EurekaInstanceMonitor implements SmartApplicationListener {

private final MultiGauge eurekaInstanceStatuses;

private final PeerAwareInstanceRegistry instanceRegistry;

private final EurekaInstanceTagsProvider tagProvider;

EurekaInstanceMonitor(MeterRegistry meterRegistry, PeerAwareInstanceRegistry instanceRegistry,
EurekaInstanceTagsProvider tagProvider) {
Objects.requireNonNull(meterRegistry);
this.instanceRegistry = Objects.requireNonNull(instanceRegistry);
this.tagProvider = Objects.requireNonNull(tagProvider);
this.eurekaInstanceStatuses = MultiGauge.builder("eureka.server.instance.status")
.description("Status of application instances registered with the Eureka server.")
.register(meterRegistry);
}

@Override
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
return EurekaInstanceCanceledEvent.class.isAssignableFrom(eventType)
|| EurekaInstanceRegisteredEvent.class.isAssignableFrom(eventType)
|| EurekaInstanceRenewedEvent.class.isAssignableFrom(eventType);
heowc marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public void onApplicationEvent(ApplicationEvent event) {
final Map<Tags, Long> aggregatedCounts = instanceRegistry.getApplications().getRegisteredApplications().stream()
.flatMap(application -> application.getInstances().stream())
.collect(Collectors.groupingBy(this::tags, Collectors.counting()));
eurekaInstanceStatuses.register(aggregatedCounts.entrySet().stream()
.map(it -> MultiGauge.Row.of(it.getKey(), it.getValue())).collect(Collectors.toList()), true);
}

private Tags tags(InstanceInfo instanceInfo) {
return tagProvider.eurekaInstanceTags(instanceInfo).and("status", instanceInfo.getStatus().name());
heowc marked this conversation as resolved.
Show resolved Hide resolved
}

}
heowc marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2013-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.netflix.eureka.server.metrics;

import com.netflix.appinfo.InstanceInfo;
import io.micrometer.core.instrument.Tag;
heowc marked this conversation as resolved.
Show resolved Hide resolved
import io.micrometer.core.instrument.Tags;

/**
* Provides {@link Tag Tags} for Eureka instance metrics.
heowc marked this conversation as resolved.
Show resolved Hide resolved
*
* @author Wonchul Heo
* @since 4.1.1
*/
public interface EurekaInstanceTagsProvider {

Tags eurekaInstanceTags(InstanceInfo instanceInfo);

}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
org.springframework.cloud.netflix.eureka.server.EurekaServerAutoConfiguration
org.springframework.cloud.netflix.eureka.server.EurekaServerAutoConfiguration
org.springframework.cloud.netflix.eureka.server.metrics.EurekaInstanceMetricsAutoConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* Copyright 2013-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.netflix.eureka.server;

import java.util.Map;
import java.util.stream.Collectors;

import com.netflix.appinfo.InstanceInfo;
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;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
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.FixtureEurekaInstances.getInstanceInfo;
import static org.springframework.cloud.netflix.eureka.server.FixtureEurekaInstances.getLeaseInfo;

/**
* @author Wonchul Heo
*/
@SpringBootTest(classes = EurekaInstanceMonitorTests.Application.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
value = { "spring.application.name=eureka", "eureka.instance.metrics.enabled=true" })
class EurekaInstanceMonitorTests {
heowc marked this conversation as resolved.
Show resolved Hide resolved
heowc marked this conversation as resolved.
Show resolved Hide resolved

private static final String APP_NAME = "MY-APP-NAME";

@Autowired
private InstanceRegistry instanceRegistry;

@Autowired
private MeterRegistry meterRegistry;

private InstanceInfo instanceInfo;

private InstanceInfo instanceInfo2;

@BeforeEach
void setup() {
this.instanceRegistry.clearRegistry();
heowc marked this conversation as resolved.
Show resolved Hide resolved
this.meterRegistry.clear();
this.instanceInfo = getInstanceInfo(APP_NAME, "my-host-name", "my-host-name:8008", 8008, getLeaseInfo());
this.instanceInfo2 = getInstanceInfo(APP_NAME, "my-host-name", "my-host-name:8009", 8009, getLeaseInfo());
}

@Test
void testNoRegistration() {
assertThatThrownBy(() -> {
meterRegistry.get("eureka.server.instance.status").meter();
}).isInstanceOf(MeterNotFoundException.class);
}

@Test
void testMultipleRegistrations() {
instanceRegistry.register(instanceInfo, false);
instanceRegistry.register(instanceInfo2, false);

final Map<Tags, Long> counts = Map.of(tags(instanceInfo), 2L);
assertEurekaInstance(counts, counts);
}

@Test
void testPartialDeregistrationAfterMultipleRegistrations() {
instanceRegistry.register(instanceInfo, false);
instanceRegistry.register(instanceInfo2, false);
instanceRegistry.internalCancel(instanceInfo.getAppName(), instanceInfo.getInstanceId(), false);

final Map<Tags, Long> counts = Map.of(tags(instanceInfo), 1L);
assertEurekaInstance(counts, counts);
}

@Test
void testPartialDeregistrationAndThenRegistrationAfterMultipleRegistrations() {
instanceRegistry.register(instanceInfo, false);
instanceRegistry.register(instanceInfo2, false);
instanceRegistry.internalCancel(instanceInfo.getAppName(), instanceInfo.getInstanceId(), false);
instanceRegistry.register(instanceInfo, false);

final Map<Tags, Long> counts = Map.of(tags(instanceInfo), 2L);
assertEurekaInstance(counts, counts);
}

@Test
void testPartialNonRenewalAfterMultipleRegistrations() {
instanceRegistry.register(instanceInfo, false);
instanceRegistry.register(instanceInfo2, false);

instanceRegistry.statusUpdate(instanceInfo.getAppName(), instanceInfo.getInstanceId(),
InstanceInfo.InstanceStatus.DOWN, null, false);

final Map<Tags, Long> instanceRegistryCounts = Map.of(tags(instanceInfo), 1L, tags(instanceInfo2), 1L);
final Map<Tags, Long> meterRegistryCounts = Map.of(tags(instanceInfo2), 2L);
assertEurekaInstance(instanceRegistryCounts, meterRegistryCounts);
}

@Test
void testPartialRenewalAfterMultipleRegistrations() {
instanceRegistry.register(instanceInfo, false);
instanceRegistry.register(instanceInfo2, false);

instanceRegistry.statusUpdate(instanceInfo.getAppName(), instanceInfo.getInstanceId(),
InstanceInfo.InstanceStatus.DOWN, null, false);
instanceRegistry.renew(instanceInfo.getAppName(), instanceInfo.getInstanceId(), false);

final Map<Tags, Long> counts = Map.of(tags(instanceInfo), 1L, tags(instanceInfo2), 1L);
assertEurekaInstance(counts, counts);
}

private static Tags tags(InstanceInfo instanceInfo) {
heowc marked this conversation as resolved.
Show resolved Hide resolved
return Tags.of(Tag.of("application", instanceInfo.getAppName()),
Tag.of("status", instanceInfo.getStatus().name()));
}

private void assertEurekaInstance(Map<Tags, Long> instanceRegistryCounts, Map<Tags, Long> meterRegistryCounts) {
heowc marked this conversation as resolved.
Show resolved Hide resolved
final Map<Tags, Long> actualInstanceRegistryCounts = instanceRegistry.getApplications()
.getRegisteredApplications().stream().flatMap(application -> application.getInstances().stream())
.collect(Collectors.groupingBy(EurekaInstanceMonitorTests::tags, Collectors.counting()));
assertThat(actualInstanceRegistryCounts).isEqualTo(instanceRegistryCounts);

meterRegistryCounts.forEach((tags, count) -> {
heowc marked this conversation as resolved.
Show resolved Hide resolved
assertThat((long) meterRegistry.get("eureka.server.instance.status").tags(tags).gauge().value())
.isEqualTo(count);
});
}

@Configuration(proxyBeanMethods = false)
@EnableAutoConfiguration
@EnableEurekaServer
protected static class Application {

}
}
Loading