Skip to content

Commit

Permalink
apply review
Browse files Browse the repository at this point in the history
  • Loading branch information
heowc committed Apr 25, 2024
1 parent 2d6f0b8 commit c605590
Showing 1 changed file with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* 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 com.netflix.appinfo.InstanceInfo;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tags;
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.cloud.netflix.eureka.server.metrics.EurekaInstanceTagsProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.cloud.netflix.eureka.server.EurekaInstanceFixture.getInstanceInfo;
import static org.springframework.cloud.netflix.eureka.server.EurekaInstanceFixture.getLeaseInfo;

/**
* @author Wonchul Heo
*/
@SpringBootTest(classes = EurekaInstanceMonitorWithCustomTagsProviderTests.Application.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
value = { "spring.application.name=eureka", "eureka.server.metrics.enabled=true" })
class EurekaInstanceMonitorWithCustomTagsProviderTests {

private static final String FOO_APP_NAME = "FOO-APP-NAME";

@Autowired
private InstanceRegistry instanceRegistry;

@Autowired
private MeterRegistry meterRegistry;

private InstanceInfo fooInstanceInfo;

private InstanceInfo fooInstanceInfo2;

@BeforeEach
void setup() {
instanceRegistry.clearRegistry();
meterRegistry.clear();
fooInstanceInfo = getInstanceInfo(FOO_APP_NAME, "my-host-name", "my-host-name:8008", 8008, getLeaseInfo());
fooInstanceInfo2 = getInstanceInfo(FOO_APP_NAME, "my-host-name", "my-host-name:8009", 8009, getLeaseInfo());
}

@Test
void testNoRegistration() {
assertThat(meterRegistry.find("eureka.server.instances").gauge()).isNull();
}

@Test
void testMultipleRegistrations() {
instanceRegistry.register(fooInstanceInfo, false);
instanceRegistry.register(fooInstanceInfo2, false);

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

private static Tags tags(InstanceInfo instanceInfo) {
return Tags.of("port", String.valueOf(instanceInfo.getPort()));
}

private void assertEurekaInstance(Map<Tags, Long> meterRegistryCounts) {
meterRegistryCounts.forEach((tags,
count) -> assertThat((long) meterRegistry.get("eureka.server.instances").tags(tags).gauge().value())
.isEqualTo(count));
}

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

@Bean
EurekaInstanceTagsProvider customEurekaInstanceTagsProvider() {
return instanceInfo -> Tags.of("port", String.valueOf(instanceInfo.getPort()));
}

}

}

0 comments on commit c605590

Please sign in to comment.