From 0c301c98d930a5e0a48b62bf8a5eda6abfc58257 Mon Sep 17 00:00:00 2001 From: gjoranv Date: Fri, 3 Jan 2020 00:13:21 +0100 Subject: Properly support multiple consumers when caching metrics. --- .../http/application/NodeMetricsClientTest.java | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'metrics-proxy/src/test/java/ai') diff --git a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/application/NodeMetricsClientTest.java b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/application/NodeMetricsClientTest.java index d028db93d43..6c319a4d74c 100644 --- a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/application/NodeMetricsClientTest.java +++ b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/http/application/NodeMetricsClientTest.java @@ -20,11 +20,15 @@ import java.util.List; import static ai.vespa.metricsproxy.TestUtil.getFileContents; import static ai.vespa.metricsproxy.http.ValuesFetcher.DEFAULT_PUBLIC_CONSUMER_ID; +import static ai.vespa.metricsproxy.metric.model.ConsumerId.toConsumerId; +import static ai.vespa.metricsproxy.metric.model.MetricId.toMetricId; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; import static com.github.tomakehurst.wiremock.client.WireMock.get; import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; /** * Two optimizations worth noting: @@ -40,6 +44,10 @@ public class NodeMetricsClientTest { private static final String RESPONSE = getFileContents(TEST_FILE); private static final CloseableHttpClient httpClient = HttpClients.createDefault(); + private static final String CPU_METRIC = "cpu.util"; + private static final String REPLACED_CPU_METRIC = "replaced_cpu_util"; + private static final String CUSTOM_CONSUMER = "custom-consumer"; + private static Node node; private ManualClock clock; @@ -54,6 +62,17 @@ public class NodeMetricsClientTest { URI metricsUri = node.metricsUri(DEFAULT_PUBLIC_CONSUMER_ID); wireMockRule.stubFor(get(urlPathEqualTo(metricsUri.getPath())) .willReturn(aResponse().withBody(RESPONSE))); + + wireMockRule.stubFor(get(urlPathEqualTo(metricsUri.getPath())) + .withQueryParam("consumer", equalTo(DEFAULT_PUBLIC_CONSUMER_ID.id)) + .willReturn(aResponse().withBody(RESPONSE))); + + // Add a slightly different response for a custom consumer. + String myConsumerResponse = RESPONSE.replaceAll(CPU_METRIC, REPLACED_CPU_METRIC); + wireMockRule.stubFor(get(urlPathEqualTo(metricsUri.getPath())) + .withQueryParam("consumer", equalTo(CUSTOM_CONSUMER)) + .willReturn(aResponse().withBody(myConsumerResponse))); + } @Before @@ -94,4 +113,17 @@ public class NodeMetricsClientTest { assertEquals(2, nodeMetricsClient.snapshotsRetrieved()); } + @Test + public void metrics_for_different_consumers_are_cached_separately() { + List defaultMetrics = nodeMetricsClient.getMetrics(DEFAULT_PUBLIC_CONSUMER_ID); + assertEquals(1, nodeMetricsClient.snapshotsRetrieved()); + assertEquals(4, defaultMetrics.size()); + + List customMetrics = nodeMetricsClient.getMetrics(toConsumerId(CUSTOM_CONSUMER)); + assertEquals(2, nodeMetricsClient.snapshotsRetrieved()); + assertEquals(4, customMetrics.size()); + + MetricsPacket replacedCpuMetric = customMetrics.get(0).build(); + assertTrue(replacedCpuMetric.metrics().containsKey(toMetricId(REPLACED_CPU_METRIC))); + } } -- cgit v1.2.3