Skip to content

Commit

Permalink
Prevent failure when heap section is missing in JVMStatisticsData (#223)
Browse files Browse the repository at this point in the history
Co-authored-by: William Montaz <[email protected]>
  • Loading branch information
Willymontaz and William Montaz authored Oct 24, 2022
1 parent e3ae055 commit cfe0aa1
Showing 1 changed file with 23 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,38 +132,39 @@ private static void addJvmStatHeapMessage(Map<Integer, GarmadonEventDescriptor>
JVMStatisticsExplodedProtos.JvmStatisticsHeap.newBuilder(),
body -> {
JVMStatisticsEventsProtos.JVMStatisticsData jvmStatisticsData = (JVMStatisticsEventsProtos.JVMStatisticsData) body;
JVMStatisticsEventsProtos.JVMStatisticsData.Section heapSection = jvmStatisticsData.getSectionList()
Optional<JVMStatisticsEventsProtos.JVMStatisticsData.Section> 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());
}
);
}
Expand Down

0 comments on commit cfe0aa1

Please sign in to comment.