Explorar el Código

checking record sanity for sysstat

Grega Bremec hace 2 años
padre
commit
3bef075ace

+ 50 - 16
exporter/src/main/java/net/p0f/openshift/metrics/exporter/SysstatMetrics.java

@@ -21,24 +21,58 @@ public class SysstatMetrics {
 
     SysstatMeasurement lastMeasurement = null;
 
-    public void processMetricRecord(SysstatMeasurement sm) {
-        // Sanity check first.
-        if (sm.getCpuLoad() == null ||
-                sm.getDisk() == null ||
-                sm.getHugepages() == null ||
-                sm.getIo() == null ||
-                sm.getKernel() == null ||
-                sm.getMemory() == null ||
-                sm.getNetwork() == null ||
-                sm.getPaging() == null ||
-                sm.getProcessAndContextSwitch() == null ||
-                sm.getPsi() == null ||
-                sm.getQueue() == null ||
-                sm.getSwapPages() == null) {
-            LOG.severe("Some of the sysstat measurement fields are null. Rejecting record.");
-            throw new IllegalStateException("Some of the sysstat measurement fields are null. Rejecting record.");
+    public boolean isRecordValid(SysstatMeasurement sm) {
+        String nullMetrics = "";
+
+        if (sm.getCpuLoad() == null) {
+            nullMetrics += "CpuLoad, ";
+        }
+        if (sm.getDisk() == null) {
+            nullMetrics += "Disk, ";
+        }
+        if (sm.getHugepages() == null) {
+            nullMetrics += "Hugepages, ";
+        }
+        if (sm.getIo() == null) {
+            nullMetrics += "Io, ";
+        }
+        if (sm.getKernel() == null) {
+            nullMetrics += "Kernel, ";
+        }
+        if (sm.getMemory() == null) {
+            nullMetrics += "Memory, ";
+        }
+        if (sm.getNetwork() == null) {
+            nullMetrics += "Network, ";
+        }
+        if (sm.getPaging() == null) {
+            nullMetrics += "Paging, ";
+        }
+        if (sm.getProcessAndContextSwitch() == null) {
+            nullMetrics += "ProcessAndContextSwitch, ";
+        }
+        if (sm.getPsi() == null) {
+            nullMetrics += "Psi, ";
+        }
+        if (sm.getQueue() == null) {
+            nullMetrics += "Queue, ";
+        }
+        if (sm.getSwapPages() == null) {
+            nullMetrics += "SwapPages, ";
         }
 
+        nullMetrics.replaceAll(", $", "");
+
+        if (nullMetrics.length() != 0) {
+            LOG.severe("Null sysstat fields, rejecting record: " + nullMetrics);
+            // throw new IllegalStateException("Null sysstat fields, rejecting record: " + nullMetrics);
+            return false;
+        }
+
+        return true;
+    }
+
+    public void processMetricRecord(SysstatMeasurement sm) {
         LOG.fine("Updating sysstat metrics records...");
 
         if (this.lastMeasurement == null) {

+ 7 - 1
exporter/src/main/java/net/p0f/openshift/metrics/routes/SysstatConsumer.java

@@ -4,6 +4,7 @@ import org.apache.camel.LoggingLevel;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.jackson.JacksonDataFormat;
 
+import net.p0f.openshift.metrics.exporter.SysstatMetrics;
 import net.p0f.openshift.metrics.model.SysstatMeasurement;
 
 public class SysstatConsumer extends RouteBuilder {
@@ -36,6 +37,11 @@ public class SysstatConsumer extends RouteBuilder {
             .log(LoggingLevel.DEBUG, "Transformed Sysstat Json: ${body}")
             .unmarshal(new JacksonDataFormat(SysstatMeasurement.class))
             .log(LoggingLevel.INFO, "Unmarshaled Sysstat: ${body}")
-            .to("bean:sysstatMetrics?method=processMetricRecord&scope=Request");
+            .choice()
+                .when(method(SysstatMetrics.class, "isRecordValid").not())
+                    .log(LoggingLevel.WARN, "Illegal record: ${body}")
+                .otherwise()
+                    .to("bean:sysstatMetrics?method=processMetricRecord&scope=Request")
+            .endChoice();
     }
 }