NodeMetricsFetcher extends AbstractComponent implements NodeMetrics { @Override public Collection<MetricValue> fetchMetrics(ApplicationId application) { NodeList applicationNodes = nodeRepository.list(application).state(Node.State.active); if (Autoscaler.unstable(applicationNodes.asList())) return Collections.emptyList(); Optional<Node> metricsV2Container = applicationNodes.container() .matching(node -> expectedUp(node)) .stream() .findFirst(); if (metricsV2Container.isEmpty()) return Collections.emptyList(); String url = "http: String response = httpClient.get(url); return new MetricsResponse(response).metrics(); } @Inject @SuppressWarnings("unused")  NodeMetricsFetcher(NodeRepository nodeRepository, Orchestrator orchestrator);   NodeMetricsFetcher(NodeRepository nodeRepository, Orchestrator orchestrator, HttpClient httpClient); @Override Collection<MetricValue> fetchMetrics(ApplicationId application); @Override void deconstruct();  }
@Test public void testMetricsFetch() { NodeResources resources = new NodeResources(1, 10, 100, 1); ProvisioningTester tester = new ProvisioningTester.Builder().build(); OrchestratorMock orchestrator = new OrchestratorMock(); MockHttpClient httpClient = new MockHttpClient(); NodeMetricsFetcher fetcher = new NodeMetricsFetcher(tester.nodeRepository(), orchestrator, httpClient); tester.makeReadyNodes(4, resources); tester.activateTenantHosts(); ApplicationId application1 = ProvisioningTester.makeApplicationId(); ApplicationId application2 = ProvisioningTester.makeApplicationId(); tester.deploy(application1, Capacity.from(new ClusterResources(2, 1, resources))); tester.deploy(application2, Capacity.from(new ClusterResources(2, 1, resources))); orchestrator.suspend(new HostName("host-4.yahoo.com")); { httpClient.cannedResponse = cannedResponseForApplication1; List<NodeMetrics.MetricValue> values = new ArrayList<>(fetcher.fetchMetrics(application1)); assertEquals("http: httpClient.requestsReceived.get(0)); assertEquals(5, values.size()); assertEquals("metric value cpu.util: 16.2 at 1970-01-01T00:20:34Z for host-1.yahoo.com", values.get(0).toString()); assertEquals("metric value mem_total.util: 23.1 at 1970-01-01T00:20:34Z for host-1.yahoo.com", values.get(1).toString()); assertEquals("metric value disk.util: 82.0 at 1970-01-01T00:20:34Z for host-1.yahoo.com", values.get(2).toString()); assertEquals("metric value cpu.util: 20.0 at 1970-01-01T00:20:00Z for host-2.yahoo.com", values.get(3).toString()); assertEquals("metric value disk.util: 40.0 at 1970-01-01T00:20:00Z for host-2.yahoo.com", values.get(4).toString()); } { httpClient.cannedResponse = cannedResponseForApplication2; List<NodeMetrics.MetricValue> values = new ArrayList<>(fetcher.fetchMetrics(application2)); assertEquals("http: httpClient.requestsReceived.get(1)); assertEquals(3, values.size()); assertEquals("metric value cpu.util: 10.0 at 1970-01-01T00:21:40Z for host-3.yahoo.com", values.get(0).toString()); assertEquals("metric value mem_total.util: 15.0 at 1970-01-01T00:21:40Z for host-3.yahoo.com", values.get(1).toString()); assertEquals("metric value disk.util: 20.0 at 1970-01-01T00:21:40Z for host-3.yahoo.com", values.get(2).toString()); } { tester.nodeRepository().write(tester.nodeRepository().getNodes(application1, Node.State.active).get(0).retire(tester.clock().instant()), tester.nodeRepository().lock(application1)); assertTrue("No metrics fetching while unstable", fetcher.fetchMetrics(application1).isEmpty()); } }