Skip to content
This repository has been archived by the owner on Oct 1, 2020. It is now read-only.

Commit

Permalink
Added metrics to logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ixuz committed Jan 2, 2019
1 parent ccbc527 commit 0860b68
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 3 deletions.
70 changes: 70 additions & 0 deletions src/main/java/com/ictreport/ixi/api/Metrics.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
8 changes: 5 additions & 3 deletions src/main/java/com/ictreport/ixi/api/Receiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/ictreport/ixi/api/Sender.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public void run() {
Constants.VERSION,
neighborUuids);
send(statusPayload, Constants.RCS_HOST, Constants.RCS_PORT);

Metrics.logMetrics(reportIxi);
}
}, 0, 60000);

Expand Down
27 changes: 27 additions & 0 deletions src/main/java/com/ictreport/ixi/model/Neighbor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 0860b68

Please sign in to comment.