From d129a3202def3e6272fd68643fcef575f5522510 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E9=99=88?= Date: Fri, 4 Oct 2024 15:33:25 +0800 Subject: [PATCH] Get fault interval from chaos mesh log file when using chaos-mesh (#64) * Get fault interval from chaos mesh log file when using chaos-mesh * Simplified the code structure * Validate if the log file path is valid * Use default path if environment variable is not set or invalid --- .../io/openchaos/checker/PerfChecker.java | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/chaos-framework/src/main/java/io/openchaos/checker/PerfChecker.java b/chaos-framework/src/main/java/io/openchaos/checker/PerfChecker.java index 1a4bfd7..e4910a8 100644 --- a/chaos-framework/src/main/java/io/openchaos/checker/PerfChecker.java +++ b/chaos-framework/src/main/java/io/openchaos/checker/PerfChecker.java @@ -29,6 +29,7 @@ import java.io.FileInputStream; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; @@ -136,9 +137,16 @@ private void generateLatencyPointGraph() throws Exception { List invokeFailureList = new ArrayList<>(); List invokeUnknownList = new ArrayList<>(); - //Fault interval - List faultLines = Files.lines(Paths.get(originFilePath)). - filter(x -> x.startsWith("fault")).map(x -> x.split("\t")).collect(Collectors.toList()); + // Get fault interval from chaos mesh log file + String chaosMeshLogFilePath = System.getenv("CHAOS_MESH_LOG_FILE"); + // Validate if the file path is valid + String logFilePath = getValidLogFilePath(chaosMeshLogFilePath, originFilePath); + + // Read fault intervals from the log file + List faultLines = Files.lines(Paths.get(logFilePath)) + .filter(line -> line.startsWith("fault")) + .map(line -> line.split("\t")) + .collect(Collectors.toList()); for (int i = 0; i < faultLines.size(); ) { if (faultLines.get(i)[2].equals("start")) { @@ -216,6 +224,16 @@ private void generateLatencyPointGraph() throws Exception { ImageIO.write(png.getImage(), "png", file); } + private String getValidLogFilePath(String envLogFilePath, String defaultFilePath) { + if (envLogFilePath != null && !envLogFilePath.isEmpty()) { + Path path = Paths.get(envLogFilePath); + if (Files.exists(path) && Files.isReadable(path)) { + return envLogFilePath; + } + } + return defaultFilePath; // Use default path if environment variable is not set or invalid + } + private void renderPoint(JavaPlot plot, List dataSet, String title, int pointType, NamedPlotColor color) { DataSetPlot dataSetPlot = new DataSetPlot(pointList2Array(dataSet)); PlotStyle plotStyle = new PlotStyle();