diff options
author | Ola Aunrønning <olaa@verizonmedia.com> | 2019-09-01 14:09:53 +0200 |
---|---|---|
committer | Ola Aunrønning <olaa@verizonmedia.com> | 2019-09-01 14:20:02 +0200 |
commit | f2a04e2e15111ee07c59e803fa198c4f2c3d6805 (patch) | |
tree | 2d16d99bbe087650335cda81c7fb4a1e9b0498bd /metrics-proxy/src/main | |
parent | b133086a1851fe15d61fc5d77177b2e4950ad041 (diff) |
Use metric packet builders
Diffstat (limited to 'metrics-proxy/src/main')
5 files changed, 43 insertions, 104 deletions
diff --git a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/CoredumpMetricGatherer.java b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/CoredumpMetricGatherer.java index 4b959137625..7691f89706b 100644 --- a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/CoredumpMetricGatherer.java +++ b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/CoredumpMetricGatherer.java @@ -1,69 +1,46 @@ // 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.model.ConsumerId; import ai.vespa.metricsproxy.metric.model.MetricsPacket; -import ai.vespa.metricsproxy.metric.model.json.YamasJsonUtil; +import ai.vespa.metricsproxy.metric.model.ServiceId; 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.Path; import java.time.Instant; -import java.time.temporal.ChronoUnit; -import java.util.Collections; -import java.util.List; -import java.util.logging.Level; +import java.util.Set; import java.util.logging.Logger; -import static ai.vespa.metricsproxy.node.NodeMetricGatherer.ROUTING_JSON; /** * @author olaa */ public class CoredumpMetricGatherer { - private static final int COREDUMP_AGE_IN_MINUTES = 12600; private static final Path COREDUMP_PATH = Path.of(Defaults.getDefaults().underVespaHome("var/crash/processing")); private static final Logger logger = Logger.getLogger(CoredumpMetricGatherer.class.getSimpleName()); - protected static List<MetricsPacket.Builder> gatherCoredumpMetrics(FileWrapper fileWrapper) { - long coredumps = getCoredumpsFromLastPeriod(fileWrapper); - try { - JSONObject jsonObject = new JSONObject(); - jsonObject.put("timestamp", Instant.now().getEpochSecond()); - jsonObject.put("application", "system-coredumps-processing"); - jsonObject.put("status_code", coredumps); - jsonObject.put("status_message", coredumps == 0 ? "OK" : String.format("Found %d coredumps in past %d minutes", coredumps, COREDUMP_AGE_IN_MINUTES)); - jsonObject.put("routing", ROUTING_JSON); - return YamasJsonUtil.toMetricsPackets(jsonObject.toString()); - } catch (JSONException e) { - logger.log(Level.WARNING, "Error writing JSON", e); - return Collections.emptyList(); - } + protected static MetricsPacket.Builder gatherCoredumpMetrics(FileWrapper fileWrapper) { + int coredumps = getCoredumpsFromLastPeriod(fileWrapper); + return new MetricsPacket.Builder(ServiceId.toServiceId("system-coredumps-processing")) + .timestamp(Instant.now().getEpochSecond()) + .statusCode(coredumps) + .statusMessage(coredumps == 0 ? "OK" : String.format("Found %d coredumps", coredumps)) + .addConsumers(Set.of(ConsumerId.toConsumerId("Vespa"))); } - private static long getCoredumpsFromLastPeriod(FileWrapper fileWrapper) { + private static int getCoredumpsFromLastPeriod(FileWrapper fileWrapper) { try { - return fileWrapper.walkTree(COREDUMP_PATH) - .filter(file -> fileWrapper.isRegularFile(file)) - .filter(file -> isNewFile(fileWrapper, file)) + return (int) fileWrapper.walkTree(COREDUMP_PATH) + .filter(fileWrapper::isRegularFile) .count(); } catch (IOException e) { throw new UncheckedIOException(e); } } - private static boolean isNewFile(FileWrapper filewrapper, Path file) { - try { - return filewrapper.getLastModifiedTime(file) - .plus(COREDUMP_AGE_IN_MINUTES, ChronoUnit.MINUTES) - .isAfter(Instant.now()); - } catch (IOException e) { - throw new UncheckedIOException(e); - } - } } 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 d20a44e09bb..b21f2a004b9 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,7 +4,6 @@ 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; @@ -21,10 +20,6 @@ public class FileWrapper { return Files.walk(path); } - Instant getLastModifiedTime(Path path) throws IOException { - return Files.getLastModifiedTime(path).toInstant(); - } - boolean isRegularFile(Path path) { return Files.isRegularFile(path); } 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 f5a53ddab50..d2b403e0567 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,20 +1,19 @@ // 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; -import ai.vespa.metricsproxy.metric.model.json.YamasJsonUtil; -import org.json.JSONException; -import org.json.JSONObject; +import ai.vespa.metricsproxy.metric.model.ServiceId; import java.io.IOException; import java.nio.file.Path; import java.time.Instant; -import java.util.Collections; import java.util.List; -import java.util.logging.Level; +import java.util.Set; import java.util.logging.Logger; -import static ai.vespa.metricsproxy.node.NodeMetricGatherer.ROUTING_JSON; /** * @author olaa @@ -23,13 +22,11 @@ public class HostLifeGatherer { private static final Path UPTIME_PATH = Path.of("/proc/uptime"); - private static final Logger logger = Logger.getLogger(HostLifeGatherer.class.getSimpleName()); - - protected static List<MetricsPacket.Builder> gatherHostLifeMetrics(FileWrapper fileWrapper) { - JSONObject jsonObject = new JSONObject(); + protected static MetricsPacket.Builder gatherHostLifeMetrics(FileWrapper fileWrapper) { double upTime; int statusCode = 0; String statusMessage = "OK"; + try { upTime = getHostLife(fileWrapper); } catch (IOException e) { @@ -38,23 +35,13 @@ public class HostLifeGatherer { statusMessage = e.getMessage(); } - try { - jsonObject.put("application", "host_life"); - jsonObject.put("timestamp", Instant.now().getEpochSecond()); - jsonObject.put("status_message", statusMessage); - jsonObject.put("status_code", statusCode); - JSONObject metrics = new JSONObject(); - metrics.put("uptime", upTime); - metrics.put("alive", 1); - jsonObject.put("metrics", metrics); - jsonObject.put("routing", ROUTING_JSON); - return YamasJsonUtil.toMetricsPackets(jsonObject.toString()); - } catch (JSONException e) { - logger.log(Level.WARNING, "Error writing JSON", e); - return Collections.emptyList(); - } - - + return new MetricsPacket.Builder(ServiceId.toServiceId("host_life")) + .timestamp(Instant.now().getEpochSecond()) + .statusMessage(statusMessage) + .statusCode(statusCode) + .putMetric(MetricId.toMetricId("uptime"), upTime) + .putMetric(MetricId.toMetricId("alive"), 1) + .addConsumers(Set.of(ConsumerId.toConsumerId("Vespa"))); } diff --git a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/NodeMetricGatherer.java b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/NodeMetricGatherer.java index acddfd13c19..b511a23113d 100644 --- a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/NodeMetricGatherer.java +++ b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/NodeMetricGatherer.java @@ -7,8 +7,6 @@ import ai.vespa.metricsproxy.metric.dimensions.NodeDimensions; import ai.vespa.metricsproxy.metric.model.MetricsPacket; import ai.vespa.metricsproxy.service.VespaServices; import com.google.inject.Inject; -import org.json.JSONException; -import org.json.JSONObject; import java.util.ArrayList; import java.util.List; @@ -30,7 +28,6 @@ import static ai.vespa.metricsproxy.node.ServiceHealthGatherer.gatherServiceHeal public class NodeMetricGatherer { - protected static final JSONObject ROUTING_JSON = createRoutingJSON(); private final VespaServices vespaServices; private final ApplicationDimensions applicationDimensions; private final NodeDimensions nodeDimensions; @@ -47,9 +44,9 @@ public class NodeMetricGatherer { public List<MetricsPacket> gatherMetrics() { FileWrapper fileWrapper = new FileWrapper(); List<MetricsPacket.Builder> metricPacketBuilders = new ArrayList<>(); - metricPacketBuilders.addAll(gatherCoredumpMetrics(fileWrapper)); + metricPacketBuilders.add(gatherCoredumpMetrics(fileWrapper)); metricPacketBuilders.addAll(gatherServiceHealthMetrics(vespaServices)); - metricPacketBuilders.addAll(gatherHostLifeMetrics(fileWrapper)); + metricPacketBuilders.add(gatherHostLifeMetrics(fileWrapper)); return metricPacketBuilders.stream() .map(metricPacketBuilder -> @@ -59,13 +56,4 @@ public class NodeMetricGatherer { ).collect(Collectors.toList()); } - private static JSONObject createRoutingJSON() { - try { - JSONObject jsonObject = new JSONObject("{\"yamas\":{\"namespaces\":[\"Vespa\"]}}"); - return jsonObject; - } catch (JSONException e) { - throw new RuntimeException(e); - } - } - } diff --git a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/ServiceHealthGatherer.java b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/ServiceHealthGatherer.java index b7e441d21f7..923fb4d646d 100644 --- a/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/ServiceHealthGatherer.java +++ b/metrics-proxy/src/main/java/ai/vespa/metricsproxy/node/ServiceHealthGatherer.java @@ -1,17 +1,17 @@ // 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.model.ConsumerId; +import ai.vespa.metricsproxy.metric.model.DimensionId; import ai.vespa.metricsproxy.metric.model.MetricsPacket; -import ai.vespa.metricsproxy.metric.model.StatusCode; -import ai.vespa.metricsproxy.metric.model.json.YamasJsonUtil; +import ai.vespa.metricsproxy.metric.model.ServiceId; import ai.vespa.metricsproxy.service.VespaServices; -import org.json.JSONException; -import org.json.JSONObject; +import java.time.Instant; import java.util.List; +import java.util.Set; import java.util.stream.Collectors; -import static ai.vespa.metricsproxy.node.NodeMetricGatherer.ROUTING_JSON; /** * @author olaa @@ -22,23 +22,15 @@ public class ServiceHealthGatherer { protected static List<MetricsPacket.Builder> gatherServiceHealthMetrics(VespaServices vespaServices) { return vespaServices.getVespaServices() .stream() - .map(service -> { - try { - StatusCode healthStatus = service.getHealth().getStatus(); - JSONObject jsonObject = new JSONObject(); - jsonObject.put("status_code", healthStatus.code); - jsonObject.put("status_message", healthStatus.status); - jsonObject.put("application", service.getMonitoringName()); - JSONObject dimensions = new JSONObject(); - dimensions.put("instance", service.getInstanceName()); - dimensions.put("metrictype", "health"); - jsonObject.put("dimensions", dimensions); - jsonObject.put("routing", ROUTING_JSON); - return YamasJsonUtil.toMetricsPackets(jsonObject.toString()).get(0); - } catch (JSONException e) { - throw new RuntimeException(e.getMessage()); - } - }) + .map(service -> + new MetricsPacket.Builder(ServiceId.toServiceId(service.getMonitoringName())) + .timestamp(Instant.now().getEpochSecond()) + .statusMessage(service.getHealth().getStatus().status) + .statusCode(service.getHealth().getStatus().code) + .putDimension(DimensionId.toDimensionId("instance"), service.getInstanceName()) + .putDimension(DimensionId.toDimensionId("metrictype"), "health") + .addConsumers(Set.of(ConsumerId.toConsumerId("Vespa"))) + ) .collect(Collectors.toList()); } } |