Skip to content

Commit 3f0ce3f

Browse files
committed
backport - feat: Add metrics to count VIP address lookups in DiscoveryClient
1 parent 54d7be0 commit 3f0ce3f

2 files changed

Lines changed: 51 additions & 3 deletions

File tree

eureka-client/src/main/java/com/netflix/discovery/DiscoveryClient.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@ public class DiscoveryClient implements EurekaClient {
129129
private final Timer FETCH_REGISTRY_TIMER = SpectatorUtil.timer(PREFIX + "FetchRegistry", DiscoveryClient.class);
130130
private final Counter REREGISTER_COUNTER = SpectatorUtil.counter(PREFIX + "Reregister", DiscoveryClient.class);
131131

132+
// Lookup counters
133+
private final Counter LOOKUP_GET_APPLICATION = SpectatorUtil.counter(PREFIX + "Lookup", "getApplication", DiscoveryClient.class);
134+
private final Counter LOOKUP_GET_APPLICATIONS = SpectatorUtil.counter(PREFIX + "Lookup", "getApplications", DiscoveryClient.class);
135+
private final Counter LOOKUP_GET_APPLICATIONS_FOR_A_REGION = SpectatorUtil.counter(PREFIX + "Lookup", "getApplicationsForARegion", DiscoveryClient.class);
136+
private final Counter LOOKUP_GET_INSTANCES_BY_ID = SpectatorUtil.counter(PREFIX + "Lookup", "getInstancesById", DiscoveryClient.class);
137+
private final Counter LOOKUP_GET_INSTANCES_BY_VIP = SpectatorUtil.counter(PREFIX + "Lookup", "getInstancesByVipAddress", DiscoveryClient.class);
138+
private final Counter LOOKUP_GET_INSTANCES_BY_VIP_AND_APP = SpectatorUtil.counter(PREFIX + "Lookup", "getInstancesByVipAddressAndAppName", DiscoveryClient.class);
139+
private final Counter LOOKUP_GET_NEXT_SERVER = SpectatorUtil.counter(PREFIX + "Lookup", "getNextServerFromEureka", DiscoveryClient.class);
140+
private final Counter LOOKUP_GET_APPLICATIONS_SERVICE_URL = SpectatorUtil.counter(PREFIX + "Lookup", "getApplicationsServiceUrl", DiscoveryClient.class);
141+
132142
// instance variables
133143
/**
134144
* A scheduler to be used for the following 3 tasks:
@@ -459,7 +469,7 @@ public Thread newThread(Runnable r) {
459469
private void scheduleServerEndpointTask(EurekaTransport eurekaTransport,
460470
AbstractDiscoveryClientOptionalArgs args) {
461471

462-
472+
463473
Collection<?> additionalFilters = args == null
464474
? Collections.emptyList()
465475
: args.additionalFilters;
@@ -560,6 +570,7 @@ public ApplicationInfoManager getApplicationInfoManager() {
560570
*/
561571
@Override
562572
public Application getApplication(String appName) {
573+
LOOKUP_GET_APPLICATION.increment();
563574
return getApplications().getRegisteredApplications(appName);
564575
}
565576

@@ -570,11 +581,13 @@ public Application getApplication(String appName) {
570581
*/
571582
@Override
572583
public Applications getApplications() {
584+
LOOKUP_GET_APPLICATIONS.increment();
573585
return localRegionApps.get();
574586
}
575587

576588
@Override
577589
public Applications getApplicationsForARegion(@Nullable String region) {
590+
LOOKUP_GET_APPLICATIONS_FOR_A_REGION.increment();
578591
if (instanceRegionChecker.isLocalRegion(region)) {
579592
return localRegionApps.get();
580593
} else {
@@ -600,6 +613,7 @@ public Set<String> getAllKnownRegions() {
600613
*/
601614
@Override
602615
public List<InstanceInfo> getInstancesById(String id) {
616+
LOOKUP_GET_INSTANCES_BY_ID.increment();
603617
List<InstanceInfo> instancesList = new ArrayList<>();
604618
for (Application app : this.getApplications()
605619
.getRegisteredApplications()) {
@@ -684,6 +698,7 @@ public List<InstanceInfo> getInstancesByVipAddress(String vipAddress, boolean se
684698
@Override
685699
public List<InstanceInfo> getInstancesByVipAddress(String vipAddress, boolean secure,
686700
@Nullable String region) {
701+
LOOKUP_GET_INSTANCES_BY_VIP.increment();
687702
if (vipAddress == null) {
688703
throw new IllegalArgumentException(
689704
"Supplied VIP Address cannot be null");
@@ -704,9 +719,7 @@ public List<InstanceInfo> getInstancesByVipAddress(String vipAddress, boolean se
704719
return applications.getInstancesByVirtualHostName(vipAddress);
705720
} else {
706721
return applications.getInstancesBySecureVirtualHostName(vipAddress);
707-
708722
}
709-
710723
}
711724

712725
/**
@@ -725,6 +738,7 @@ public List<InstanceInfo> getInstancesByVipAddress(String vipAddress, boolean se
725738
@Override
726739
public List<InstanceInfo> getInstancesByVipAddressAndAppName(
727740
String vipAddress, String appName, boolean secure) {
741+
LOOKUP_GET_INSTANCES_BY_VIP_AND_APP.increment();
728742

729743
List<InstanceInfo> result = new ArrayList<>();
730744
if (vipAddress == null && appName == null) {
@@ -779,6 +793,7 @@ public List<InstanceInfo> getInstancesByVipAddressAndAppName(
779793
*/
780794
@Override
781795
public InstanceInfo getNextServerFromEureka(String virtualHostname, boolean secure) {
796+
LOOKUP_GET_NEXT_SERVER.increment();
782797
List<InstanceInfo> instanceInfoList = this.getInstancesByVipAddress(
783798
virtualHostname, secure);
784799
if (instanceInfoList == null || instanceInfoList.isEmpty()) {
@@ -800,6 +815,7 @@ public InstanceInfo getNextServerFromEureka(String virtualHostname, boolean secu
800815
*/
801816
@Override
802817
public Applications getApplications(String serviceUrl) {
818+
LOOKUP_GET_APPLICATIONS_SERVICE_URL.increment();
803819
try {
804820
EurekaHttpResponse<Applications> response = clientConfig.getRegistryRefreshSingleVipAddress() == null
805821
? eurekaTransport.queryClient.getApplications()

eureka-client/src/test/java/com/netflix/discovery/DiscoveryClientRegistryTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
import com.netflix.discovery.shared.transport.EurekaHttpResponse;
1717
import com.netflix.discovery.shared.transport.SimpleEurekaHttpServer;
1818
import com.netflix.discovery.util.InstanceInfoGenerator;
19+
import com.netflix.spectator.api.Counter;
20+
import com.netflix.spectator.api.DefaultRegistry;
21+
import com.netflix.spectator.api.Registry;
22+
import com.netflix.spectator.api.Spectator;
1923
import org.junit.AfterClass;
2024
import org.junit.Before;
2125
import org.junit.BeforeClass;
@@ -52,6 +56,7 @@ public class DiscoveryClientRegistryTest {
5256

5357
private static final EurekaHttpClient requestHandler = mock(EurekaHttpClient.class);
5458
private static SimpleEurekaHttpServer eurekaHttpServer;
59+
private static DefaultRegistry testRegistry;
5560

5661
@Rule
5762
public DiscoveryClientResource discoveryClientResource = DiscoveryClientResource.newBuilder()
@@ -67,13 +72,18 @@ public class DiscoveryClientRegistryTest {
6772
@BeforeClass
6873
public static void setUpClass() throws IOException {
6974
eurekaHttpServer = new SimpleEurekaHttpServer(requestHandler);
75+
testRegistry = new DefaultRegistry();
76+
Spectator.globalRegistry().add(testRegistry);
7077
}
7178

7279
@AfterClass
7380
public static void tearDownClass() throws Exception {
7481
if (eurekaHttpServer != null) {
7582
eurekaHttpServer.shutdown();
7683
}
84+
if (testRegistry != null) {
85+
Spectator.globalRegistry().remove(testRegistry);
86+
}
7787
}
7888

7989
@Before
@@ -314,6 +324,28 @@ public void testEurekaClientPeriodicCacheRefreshForDeleteAndNoApplication() thro
314324
assertEquals(client.getApplications().getRegisteredApplications(), new ArrayList<>());
315325
}
316326

327+
@Test
328+
public void testLookupMetricsIncremented() throws Exception {
329+
Applications applications = InstanceInfoGenerator.newBuilder(2, "app1").build().toApplications();
330+
InstanceInfo instance = applications.getRegisteredApplications("app1").getInstances().get(0);
331+
String vipAddress = instance.getVIPAddress();
332+
333+
when(requestHandler.getApplications(TEST_REMOTE_REGION)).thenReturn(
334+
anEurekaHttpResponse(200, applications).type(MediaType.APPLICATION_JSON_TYPE).build()
335+
);
336+
337+
Registry registry = Spectator.globalRegistry();
338+
Counter vipCounter = registry.counter(
339+
registry.createId("DiscoveryClient_Lookup")
340+
.withTag("id", "getInstancesByVipAddress")
341+
.withTag("class", "DiscoveryClient"));
342+
long initialCount = vipCounter.count();
343+
344+
discoveryClientResource.getClient().getInstancesByVipAddress(vipAddress, false);
345+
346+
assertThat(vipCounter.count(), is(equalTo(initialCount + 1)));
347+
}
348+
317349
/**
318350
* There is a bug, because of which remote registry data structures are not initialized during full registry fetch, only during delta.
319351
*/

0 commit comments

Comments
 (0)