From cce1d7872328f95c32567da873b061d974b5c862 Mon Sep 17 00:00:00 2001 From: Ola Aunrønning Date: Wed, 23 Oct 2019 14:02:05 +0200 Subject: Added metrics for cfg --- .../container/jdisc/state/CoredumpGatherer.java | 48 +++++++++++++++++++++ .../container/jdisc/state/HostLifeGatherer.java | 50 ++++++++++++++++++++++ .../container/jdisc/state/MetricGatherer.java | 22 ++++++++++ .../jdisc/state/MetricsPacketsHandler.java | 1 + .../jdisc/state/MetricsPacketsHandlerTest.java | 17 +------- 5 files changed, 122 insertions(+), 16 deletions(-) create mode 100644 container-core/src/main/java/com/yahoo/container/jdisc/state/CoredumpGatherer.java create mode 100644 container-core/src/main/java/com/yahoo/container/jdisc/state/HostLifeGatherer.java create mode 100644 container-core/src/main/java/com/yahoo/container/jdisc/state/MetricGatherer.java diff --git a/container-core/src/main/java/com/yahoo/container/jdisc/state/CoredumpGatherer.java b/container-core/src/main/java/com/yahoo/container/jdisc/state/CoredumpGatherer.java new file mode 100644 index 00000000000..915cced842b --- /dev/null +++ b/container-core/src/main/java/com/yahoo/container/jdisc/state/CoredumpGatherer.java @@ -0,0 +1,48 @@ +// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.container.jdisc.state; + +import com.yahoo.vespa.defaults.Defaults; +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.file.Files; +import java.nio.file.NoSuchFileException; +import java.nio.file.Path; +import java.time.Instant; +import java.util.Set; + +/** + * @author olaa + */ +public class CoredumpGatherer { + + private static final Path COREDUMP_PATH = Path.of(Defaults.getDefaults().underVespaHome("var/crash/processing")); + + protected static JSONObject gatherCoredumpMetrics() { + int coredumps = getNumberOfCoredumps(); + JSONObject packet = new JSONObject(); + + try { + packet.put("status_code", coredumps == 0 ? 0 : 1); + packet.put("status_msg", coredumps == 0 ? "OK" : String.format("Found %d coredumps", coredumps)); + packet.put("timestamp", Instant.now().getEpochSecond()); + packet.put("application", "system-coredumps-processing"); + + } catch (JSONException e) {} + return packet; + } + + private static int getNumberOfCoredumps() { + try { + return (int) Files.walk(COREDUMP_PATH) + .filter(Files::isRegularFile) + .count(); + } catch (NoSuchFileException e) { + return 0; + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } +} diff --git a/container-core/src/main/java/com/yahoo/container/jdisc/state/HostLifeGatherer.java b/container-core/src/main/java/com/yahoo/container/jdisc/state/HostLifeGatherer.java new file mode 100644 index 00000000000..91d1c468d8a --- /dev/null +++ b/container-core/src/main/java/com/yahoo/container/jdisc/state/HostLifeGatherer.java @@ -0,0 +1,50 @@ +// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.container.jdisc.state; + +import com.yahoo.vespa.defaults.Defaults; +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.time.Duration; +import java.time.Instant; + +/** + * @author olaa + */ +public class HostLifeGatherer { + + private static final Path UPTIME_PATH = Path.of(Defaults.getDefaults().underVespaHome("/proc")); + + protected static JSONObject getHostLifePacket() { + long upTime; + int statusCode = 0; + String statusMessage = "OK"; + + try { + upTime = Files.getLastModifiedTime(UPTIME_PATH).toInstant().getEpochSecond(); + } catch (IOException e) { + upTime = 0; + statusCode = 1; + statusMessage = "Unable to read proc folder"; + } + + + JSONObject jsonObject = new JSONObject(); + try { + jsonObject.put("status_code", statusCode); + jsonObject.put("status_msg", statusMessage); + jsonObject.put("timestamp", Instant.now().getEpochSecond()); + jsonObject.put("application", "host_life"); + JSONObject metrics = new JSONObject(); + metrics.put("uptime", upTime); + metrics.put("alive", 1); + jsonObject.put("metrics", metrics); + + } catch (JSONException e) {} + + return jsonObject; + } +} diff --git a/container-core/src/main/java/com/yahoo/container/jdisc/state/MetricGatherer.java b/container-core/src/main/java/com/yahoo/container/jdisc/state/MetricGatherer.java new file mode 100644 index 00000000000..97fba188593 --- /dev/null +++ b/container-core/src/main/java/com/yahoo/container/jdisc/state/MetricGatherer.java @@ -0,0 +1,22 @@ +// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.container.jdisc.state; + +import org.json.JSONObject; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author olaa + * Gathers metrics regarding currently processing coredumps and host life + */ +public class MetricGatherer { + + static List getAdditionalMetrics() { + List packetList = new ArrayList<>(); + packetList.add(CoredumpGatherer.gatherCoredumpMetrics()); + if (System.getProperty("os.name").contains("nux")) + packetList.add(HostLifeGatherer.getHostLifePacket()); + return packetList; + } +} diff --git a/container-core/src/main/java/com/yahoo/container/jdisc/state/MetricsPacketsHandler.java b/container-core/src/main/java/com/yahoo/container/jdisc/state/MetricsPacketsHandler.java index d7760ac9c91..d1036db5c6f 100644 --- a/container-core/src/main/java/com/yahoo/container/jdisc/state/MetricsPacketsHandler.java +++ b/container-core/src/main/java/com/yahoo/container/jdisc/state/MetricsPacketsHandler.java @@ -107,6 +107,7 @@ public class MetricsPacketsHandler extends AbstractRequestHandler { jsonArray.put(getStatusPacket()); getPacketsForSnapshot(getSnapshot(), applicationName, timer.currentTimeMillis()) .forEach(jsonArray::put); + MetricGatherer.getAdditionalMetrics().forEach(jsonArray::put); root.put("metrics", jsonArray); return jsonToString(root) .getBytes(StandardCharsets.UTF_8); diff --git a/container-core/src/test/java/com/yahoo/container/jdisc/state/MetricsPacketsHandlerTest.java b/container-core/src/test/java/com/yahoo/container/jdisc/state/MetricsPacketsHandlerTest.java index e933f042cec..98dfb2e281b 100644 --- a/container-core/src/test/java/com/yahoo/container/jdisc/state/MetricsPacketsHandlerTest.java +++ b/container-core/src/test/java/com/yahoo/container/jdisc/state/MetricsPacketsHandlerTest.java @@ -121,22 +121,7 @@ public class MetricsPacketsHandlerTest extends StateHandlerTestBase { List packets = incrementTimeAndGetJsonPackets(); assertEquals(3, packets.size()); } - - @Test - public void get_metrics_in_json_array() throws Exception { - metric.add("counter", 1, null); - incrementCurrentTimeAndAssertSnapshot(SNAPSHOT_INTERVAL); - String response = requestAsString("http://localhost/metrics-packets?array-formatted"); - List responseJson = toJsonPackets(response); - assertEquals(1, responseJson.size()); - JsonNode metricsNode = responseJson.get(0).get(METRICS_KEY); - assertEquals(2, metricsNode.size()); - - JsonNode counterPacket = metricsNode.get(1); - assertCountMetric(counterPacket, "counter.count", 1); - - } - + private List incrementTimeAndGetJsonPackets() throws Exception { incrementCurrentTimeAndAssertSnapshot(SNAPSHOT_INTERVAL); String response = requestAsString("http://localhost/metrics-packets"); -- cgit v1.2.3