diff options
author | Ola Aunrønning <olaa@verizonmedia.com> | 2019-12-02 13:17:12 +0100 |
---|---|---|
committer | Ola Aunrønning <olaa@verizonmedia.com> | 2019-12-02 13:32:59 +0100 |
commit | 8175316fae82332f74e2cb18bd47e6d8b308127c (patch) | |
tree | 84934d0412f65abb8d2e878b19eb3b3cc3571a74 /metrics-proxy | |
parent | 0dc610dd56b0b655745829fbb0256046039a99e2 (diff) |
Allow flushing external metrics through RPC
Diffstat (limited to 'metrics-proxy')
3 files changed, 28 insertions, 10 deletions
diff --git a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/core/MetricsManager.java b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/core/MetricsManager.java index 27d4f06ab40..e4c40f55f6c 100644 --- a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/core/MetricsManager.java +++ b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/core/MetricsManager.java @@ -47,7 +47,7 @@ public class MetricsManager { private volatile Map<DimensionId, String> extraDimensions = new HashMap<>(); private volatile Instant externalMetricsUpdateTime = Instant.now(); - private static final Duration EXTERNAL_DIMENSIONS_TTL = Duration.ofMinutes(10); + private static final Duration EXTERNAL_METRICS_TTL = Duration.ofMinutes(10); public MetricsManager(VespaServices vespaServices, VespaMetrics vespaMetrics, @@ -90,7 +90,7 @@ public class MetricsManager { List<MetricsPacket.Builder> result = vespaMetrics.getMetrics(services); log.log(DEBUG, () -> "Got " + result.size() + " metrics packets for vespa services."); - clearStaleMetrics(); + flushStaleMetrics(); List<MetricsPacket.Builder> externalPackets = externalMetrics.getMetrics().stream() .filter(MetricsPacket.Builder::hasMetrics) .collect(toList()); @@ -154,17 +154,21 @@ public class MetricsManager { } public Map<DimensionId, String> getExtraDimensions() { - clearStaleMetrics(); + flushStaleMetrics(); return this.extraDimensions; } - private void clearStaleMetrics() { - if (Duration.between(externalMetricsUpdateTime, Instant.now()).getSeconds() > EXTERNAL_DIMENSIONS_TTL.getSeconds()) { - this.extraDimensions = new HashMap<>(); - externalMetrics.setExtraMetrics(Collections.emptyList()); + private void flushStaleMetrics() { + if (Duration.between(externalMetricsUpdateTime, Instant.now()).getSeconds() > EXTERNAL_METRICS_TTL.getSeconds()) { + flushExtraMetrics(); } } + public void flushExtraMetrics() { + extraDimensions = new HashMap<>(); + externalMetrics.setExtraMetrics(Collections.emptyList()); + } + /** * Returns a space separated list of all distinct service names. */ diff --git a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/rpc/RpcServer.java b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/rpc/RpcServer.java index 7157e3c40a0..315cfcc2c63 100644 --- a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/rpc/RpcServer.java +++ b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/rpc/RpcServer.java @@ -60,6 +60,10 @@ public class RpcServer { .paramDesc(0, "metricsJson", "The metrics in json format")); connector.addMethod( + new Method("flushExtraMetrics", "", "", this::flushExtraMetrics) + .methodDesc("Flush metrics and dimensions populated by setExtraMetrics")); + + connector.addMethod( new Method("getMetricsById", "s", "s", this::getMetricsById) .methodDesc("Get Vespa metrics for the service with the given Id") .paramDesc(0, "id", "The id of the service") @@ -149,6 +153,10 @@ public class RpcServer { withExceptionHandling(req, () -> metricsManager.setExtraMetrics(toMetricsPackets(metricsJson))); } + void flushExtraMetrics(Request req) { + withExceptionHandling(req, metricsManager::flushExtraMetrics); + } + private static void withExceptionHandling(Request req, ThrowingRunnable runnable) { try { TimeTracker timeTracker = new TimeTracker(req); diff --git a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/rpc/RpcMetricsTest.java b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/rpc/RpcMetricsTest.java index d6084e3e03a..6b47689a602 100644 --- a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/rpc/RpcMetricsTest.java +++ b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/rpc/RpcMetricsTest.java @@ -33,6 +33,7 @@ import static ai.vespa.metricsproxy.rpc.IntegrationTester.SERVICE_2_CONFIG_ID; import static ai.vespa.metricsproxy.service.VespaServices.ALL_SERVICES; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; @@ -65,7 +66,7 @@ public class RpcMetricsTest { } @Test - public void extra_metrics_are_added_to_output() throws Exception { + public void extra_metrics_are_added_to_and_flushed_from_output() throws Exception { String extraMetricsPayload = "{\"timestamp\":1557754772,\"application\":\"" + EXTRA_APP + "\",\"metrics\":{\"foo.count\":3},\"dimensions\":{\"role\":\"extra-role\"}}"; @@ -80,6 +81,12 @@ public class RpcMetricsTest { JSONObject extraMetrics = findExtraMetricsObject(allServicesResponse); assertThat(extraMetrics.getJSONObject("metrics").getInt("foo.count"), is(3)); assertThat(extraMetrics.getJSONObject("dimensions").getString("role"), is("extra-role")); + + req = new Request("flushExtraMetrics"); + invoke(req, rpcClient, false); + allServicesResponse = getMetricsForYamas(ALL_SERVICES, rpcClient).trim(); + extraMetrics = findExtraMetricsObject(allServicesResponse); + assertEquals(extraMetrics.toString(), "{}"); } } } @@ -91,8 +98,7 @@ public class RpcMetricsTest { assertTrue(jsonObject.has("application")); if (jsonObject.getString("application").equals(EXTRA_APP)) return jsonObject; } - fail("Metrics from setExtraMetrics was missing."); - throw new RuntimeException(); + return new JSONObject(); } @Test |