diff options
author | Ola Aunrønning <olaa@verizonmedia.com> | 2019-09-05 10:49:23 +0200 |
---|---|---|
committer | Ola Aunrønning <olaa@verizonmedia.com> | 2019-09-05 10:49:23 +0200 |
commit | d84dddd3d6544c5f0f640a908e673cf90ce3667b (patch) | |
tree | 6295dc547c30c7d7be33029174ddf3390af58e9d /metrics-proxy/src | |
parent | e21eed87ff09cd6a5b1a848568786db648f35e08 (diff) |
Read /proc for uptime
Diffstat (limited to 'metrics-proxy/src')
3 files changed, 13 insertions, 22 deletions
diff --git a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/FileWrapper.java b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/FileWrapper.java index b21f2a004b9..aa01802883e 100644 --- a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/FileWrapper.java +++ b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/FileWrapper.java @@ -4,6 +4,7 @@ package ai.vespa.metricsproxy.node; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.time.Instant; import java.util.List; import java.util.stream.Stream; @@ -12,8 +13,9 @@ import java.util.stream.Stream; */ public class FileWrapper { - List<String> readAllLines(Path path) throws IOException { - return Files.readAllLines(path); + long getFileAgeInSeconds(Path path) throws IOException { + Instant lastModifiedTime = Files.getLastModifiedTime(path).toInstant(); + return Instant.now().getEpochSecond() - lastModifiedTime.getEpochSecond(); } Stream<Path> walkTree(Path path) throws IOException { diff --git a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/HostLifeGatherer.java b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/HostLifeGatherer.java index d2b403e0567..8fdfc93022c 100644 --- a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/HostLifeGatherer.java +++ b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/HostLifeGatherer.java @@ -1,7 +1,6 @@ // Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package ai.vespa.metricsproxy.node; -import ai.vespa.metricsproxy.metric.Metric; import ai.vespa.metricsproxy.metric.model.ConsumerId; import ai.vespa.metricsproxy.metric.model.MetricId; import ai.vespa.metricsproxy.metric.model.MetricsPacket; @@ -10,9 +9,7 @@ import ai.vespa.metricsproxy.metric.model.ServiceId; import java.io.IOException; import java.nio.file.Path; import java.time.Instant; -import java.util.List; import java.util.Set; -import java.util.logging.Logger; /** @@ -20,17 +17,17 @@ import java.util.logging.Logger; */ public class HostLifeGatherer { - private static final Path UPTIME_PATH = Path.of("/proc/uptime"); + private static final Path UPTIME_PATH = Path.of("/proc"); protected static MetricsPacket.Builder gatherHostLifeMetrics(FileWrapper fileWrapper) { - double upTime; + long upTime; int statusCode = 0; String statusMessage = "OK"; try { - upTime = getHostLife(fileWrapper); + upTime = fileWrapper.getFileAgeInSeconds(UPTIME_PATH); } catch (IOException e) { - upTime = 0d; + upTime = 0; statusCode = 1; statusMessage = e.getMessage(); } @@ -44,13 +41,4 @@ public class HostLifeGatherer { .addConsumers(Set.of(ConsumerId.toConsumerId("Vespa"))); } - - - private static double getHostLife(FileWrapper fileWrapper) throws IOException { - return fileWrapper.readAllLines(UPTIME_PATH) - .stream() - .mapToDouble(line -> Double.valueOf(line.split("\\s")[0])) - .findFirst() - .orElseThrow(); - } } diff --git a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/node/HostLifeGathererTest.java b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/node/HostLifeGathererTest.java index 783f95f2f27..8317229b73a 100644 --- a/metrics-proxy/src/test/java/ai/vespa/metricsproxy/node/HostLifeGathererTest.java +++ b/metrics-proxy/src/test/java/ai/vespa/metricsproxy/node/HostLifeGathererTest.java @@ -6,6 +6,7 @@ import ai.vespa.metricsproxy.metric.model.MetricsPacket; import org.junit.Test; import java.nio.file.Path; +import java.time.Instant; import java.util.List; import java.util.Map; @@ -20,17 +21,17 @@ public class HostLifeGathererTest { public void host_is_alive() { MetricsPacket packet = HostLifeGatherer.gatherHostLifeMetrics(new MockFileWrapper()).build(); - Map<MetricId, Number> expectedMetrics = Map.of(MetricId.toMetricId("uptime"), 123d, MetricId.toMetricId("alive"), 1); assertEquals("host_life", packet.service.id); assertEquals(0, packet.statusCode); - assertEquals(expectedMetrics, packet.metrics()); + assertEquals(123l, packet.metrics().get(MetricId.toMetricId("uptime"))); + assertEquals(1, packet.metrics().get(MetricId.toMetricId("alive"))); } static class MockFileWrapper extends FileWrapper { @Override - List<String> readAllLines(Path path) { - return List.of("123 432"); + long getFileAgeInSeconds(Path path) { + return 123; } } } |