From cfe0aa1c59c765cc5c9016afdaaf28578d0e0921 Mon Sep 17 00:00:00 2001 From: William Montaz Date: Mon, 24 Oct 2022 16:58:52 +0200 Subject: [PATCH] Prevent failure when heap section is missing in JVMStatisticsData (#223) Co-authored-by: William Montaz --- .../hadoop/garmadon/hdfs/ReaderFactory.java | 45 ++++++++++--------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/readers/hdfs/src/main/java/com/criteo/hadoop/garmadon/hdfs/ReaderFactory.java b/readers/hdfs/src/main/java/com/criteo/hadoop/garmadon/hdfs/ReaderFactory.java index c1e6462a..bf712222 100644 --- a/readers/hdfs/src/main/java/com/criteo/hadoop/garmadon/hdfs/ReaderFactory.java +++ b/readers/hdfs/src/main/java/com/criteo/hadoop/garmadon/hdfs/ReaderFactory.java @@ -132,38 +132,39 @@ private static void addJvmStatHeapMessage(Map JVMStatisticsExplodedProtos.JvmStatisticsHeap.newBuilder(), body -> { JVMStatisticsEventsProtos.JVMStatisticsData jvmStatisticsData = (JVMStatisticsEventsProtos.JVMStatisticsData) body; - JVMStatisticsEventsProtos.JVMStatisticsData.Section heapSection = jvmStatisticsData.getSectionList() + Optional maybeHeapSection = jvmStatisticsData.getSectionList() .stream() .filter(section -> section.getName().equals("heap")) - .findFirst() - .orElseThrow(() -> new RuntimeException("JVMStatisticsData is supposed to have a heap section but could not find one")); - - long init = 0; - long committed = 0; - long used = 0; - long max = 0; - for (JVMStatisticsEventsProtos.JVMStatisticsData.Property property : heapSection.getPropertyList()) { - if (property.getName().equals("init")) { - init = Long.parseLong(property.getValue()); - } - if (property.getName().equals("committed")) { - committed = Long.parseLong(property.getValue()); - } - if (property.getName().equals("used")) { - used = Long.parseLong(property.getValue()); - } - if (property.getName().equals("max")) { - max = Long.parseLong(property.getValue()); + .findFirst(); + + return maybeHeapSection.map(heapSection -> { + long init = 0; + long committed = 0; + long used = 0; + long max = 0; + for (JVMStatisticsEventsProtos.JVMStatisticsData.Property property : heapSection.getPropertyList()) { + if (property.getName().equals("init")) { + init = Long.parseLong(property.getValue()); + } + if (property.getName().equals("committed")) { + committed = Long.parseLong(property.getValue()); + } + if (property.getName().equals("used")) { + used = Long.parseLong(property.getValue()); + } + if (property.getName().equals("max")) { + max = Long.parseLong(property.getValue()); + } } - } - return JVMStatisticsExplodedProtos.JvmStatisticsHeap + return JVMStatisticsExplodedProtos.JvmStatisticsHeap .newBuilder() .setInit(init) .setCommitted(committed) .setUsed(used) .setMax(max) .build(); + }).orElseGet(() -> JVMStatisticsExplodedProtos.JvmStatisticsHeap.newBuilder().build()); } ); }