Skip to content

Commit

Permalink
Get fault interval from chaos mesh log file when using chaos-mesh (#64)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
chi3316 authored Oct 4, 2024
1 parent fcbc5f4 commit d129a32
Showing 1 changed file with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -136,9 +137,16 @@ private void generateLatencyPointGraph() throws Exception {
List<Point> invokeFailureList = new ArrayList<>();
List<Point> invokeUnknownList = new ArrayList<>();

//Fault interval
List<String[]> 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<String[]> 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")) {
Expand Down Expand Up @@ -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<Point> dataSet, String title, int pointType, NamedPlotColor color) {
DataSetPlot dataSetPlot = new DataSetPlot(pointList2Array(dataSet));
PlotStyle plotStyle = new PlotStyle();
Expand Down

0 comments on commit d129a32

Please sign in to comment.