From 0860b68316292e5d39dee84985d5f4e0ab6c1dd0 Mon Sep 17 00:00:00 2001 From: Anton Wiklund Date: Wed, 2 Jan 2019 21:12:55 +0100 Subject: [PATCH] Added metrics to logging --- .../java/com/ictreport/ixi/api/Metrics.java | 70 +++++++++++++++++++ .../java/com/ictreport/ixi/api/Receiver.java | 8 ++- .../java/com/ictreport/ixi/api/Sender.java | 2 + .../com/ictreport/ixi/model/Neighbor.java | 27 +++++++ 4 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/ictreport/ixi/api/Metrics.java diff --git a/src/main/java/com/ictreport/ixi/api/Metrics.java b/src/main/java/com/ictreport/ixi/api/Metrics.java new file mode 100644 index 0000000..90f7276 --- /dev/null +++ b/src/main/java/com/ictreport/ixi/api/Metrics.java @@ -0,0 +1,70 @@ +package com.ictreport.ixi.api; + +import com.ictreport.ixi.ReportIxi; +import com.ictreport.ixi.model.Neighbor; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +public class Metrics { + public final static Logger LOGGER = LogManager.getLogger(Metrics.class); + private static int nonNeighborPingCount = 0; + private static int nonNeighborInvalidCount = 0; + + public static void logMetrics(ReportIxi reportIxi) { + + StringBuilder stringBuilder = new StringBuilder(); + + stringBuilder.append("Report.ixi metrics:\n"); + stringBuilder.append(String.format("| %1$-8s | %2$-5s | %3$-8s | %4$-10s\n", + "METADATA", + "PINGS", + "INVALID", + "NEIGHBOR")); + + stringBuilder.append(String.format("| %1$-8s | %2$-5s | %3$-8s | %4$-10s\n", + "COUNT", + "COUNT", + "COUNT", + "ADDRESS")); + + for (Neighbor neighbor : reportIxi.getNeighbors()) { + stringBuilder.append(String.format("| %1$-8d | %2$-5d | %3$-8d | %4$-10s\n", + neighbor.getMetadataCount(), + neighbor.getPingCount(), + neighbor.getInvalidCount(), + neighbor.getAddress())); + } + + stringBuilder.append(String.format("| %1$-8d | %2$-5d | %3$-8d | %4$-10s\n", + 0, + getNonNeighborPingCount(), + getNonNeighborInvalidCount(), + "Other/Non-neighbor...")); + + LOGGER.info(stringBuilder.toString()); + + for (Neighbor neighbor : reportIxi.getNeighbors()) { + neighbor.setMetadataCount(0); + neighbor.setPingCount(0); + neighbor.setInvalidCount(0); + setNonNeighborPingCount(0); + } + + } + + public static int getNonNeighborPingCount() { + return nonNeighborPingCount; + } + + public static void setNonNeighborPingCount(int nonNeighborPingCount) { + Metrics.nonNeighborPingCount = nonNeighborPingCount; + } + + public static int getNonNeighborInvalidCount() { + return nonNeighborInvalidCount; + } + + public static void setNonNeighborInvalidCount(int nonNeighborInvalidCount) { + Metrics.nonNeighborInvalidCount = nonNeighborInvalidCount; + } +} diff --git a/src/main/java/com/ictreport/ixi/api/Receiver.java b/src/main/java/com/ictreport/ixi/api/Receiver.java index b659434..e549869 100644 --- a/src/main/java/com/ictreport/ixi/api/Receiver.java +++ b/src/main/java/com/ictreport/ixi/api/Receiver.java @@ -78,6 +78,7 @@ private Neighbor determineNeighborWhoSent(DatagramPacket packet) { if (nb.sentPacketFromSameIP(packet)) return nb; LOGGER.warn("Received packet from unknown address: " + packet.getAddress()); + Metrics.setNonNeighborInvalidCount(Metrics.getNonNeighborInvalidCount()+1); return null; } @@ -114,6 +115,8 @@ public void processMetadataPacket(final Neighbor neighbor, final MetadataPayload LOGGER.info(String.format("Received new publicKey from neighbor[%s]", neighbor.getAddress())); } + + neighbor.setMetadataCount(neighbor.getMetadataCount()+1); } public void processSignedPayload(final SignedPayload signedPayload) { @@ -132,12 +135,11 @@ public void processSignedPayload(final SignedPayload signedPayload) { PingPayload pingPayload = (PingPayload) signedPayload.getPayload(); ReceivedPingPayload receivedPingPayload; if (signee != null) { - LOGGER.info(String.format("Received signed ping from neighbor [%s]", - signee.getAddress())); - receivedPingPayload = new ReceivedPingPayload(reportIxi.getProperties().getUuid(), pingPayload, true); + signee.setPingCount(signee.getPingCount()+1); } else { receivedPingPayload = new ReceivedPingPayload(reportIxi.getProperties().getUuid(), pingPayload, false); + Metrics.setNonNeighborPingCount(Metrics.getNonNeighborPingCount()+1); } reportIxi.getApi().getSender().send(receivedPingPayload, Constants.RCS_HOST, Constants.RCS_PORT); } diff --git a/src/main/java/com/ictreport/ixi/api/Sender.java b/src/main/java/com/ictreport/ixi/api/Sender.java index 21e20d3..740c2bc 100644 --- a/src/main/java/com/ictreport/ixi/api/Sender.java +++ b/src/main/java/com/ictreport/ixi/api/Sender.java @@ -61,6 +61,8 @@ public void run() { Constants.VERSION, neighborUuids); send(statusPayload, Constants.RCS_HOST, Constants.RCS_PORT); + + Metrics.logMetrics(reportIxi); } }, 0, 60000); diff --git a/src/main/java/com/ictreport/ixi/model/Neighbor.java b/src/main/java/com/ictreport/ixi/model/Neighbor.java index 01379e8..f073b80 100644 --- a/src/main/java/com/ictreport/ixi/model/Neighbor.java +++ b/src/main/java/com/ictreport/ixi/model/Neighbor.java @@ -11,6 +11,9 @@ public class Neighbor { private String uuid = null; private PublicKey publicKey = null; private String reportIxiVersion = null; + private int pingCount = 0; + private int metadataCount = 0; + private int invalidCount = 0; public Neighbor(InetSocketAddress address) { this.address = address; @@ -76,4 +79,28 @@ public String getReportIxiVersion() { public void setReportIxiVersion(String reportIxiVersion) { this.reportIxiVersion = reportIxiVersion; } + + public int getPingCount() { + return pingCount; + } + + public void setPingCount(int pingCount) { + this.pingCount = pingCount; + } + + public int getMetadataCount() { + return metadataCount; + } + + public void setMetadataCount(int metadataCount) { + this.metadataCount = metadataCount; + } + + public int getInvalidCount() { + return invalidCount; + } + + public void setInvalidCount(int invalidCount) { + this.invalidCount = invalidCount; + } } \ No newline at end of file