Commit bc83e983 authored by Matteo Melli's avatar Matteo Melli

Show ignored errors only on debug mode

parent 16e70f20
......@@ -119,6 +119,8 @@ public class Main {
"Interval in milliseconds to gather stats")
.withRequiredArg()
.defaultsTo("3000");
parser.acceptsAll(Lists.newArrayList("d", "debug"),
"Show debug messages");
parser.acceptsAll(Lists.newArrayList("s", "show-system"),
"Print read/write data for the whole system");
parser.acceptsAll(Lists.newArrayList("o", "show-other"),
......@@ -158,6 +160,7 @@ public class Main {
configHelper.setIf("show-other", configBuilder::withShowOther);
configHelper.set("group", Main::readGroupConfig, configBuilder::appendProcessGroups);
configHelper.setIf("prometheus-format", configBuilder::withPrometheusFormat);
configHelper.setIf("debug", configBuilder::withDebug);
Config config = configBuilder.build();
......
......@@ -37,11 +37,13 @@ public class Config {
private final boolean showSystem;
private final boolean showOther;
private final Optional<ImmutableList<ProcessGroupInfo>> processGroups;
private final boolean debug;
private Config(Path dataDir, long interval, Optional<Integer> ppid,
boolean prometheusFormat,
boolean noPrintHeader, boolean showSystem, boolean showOther,
Optional<ImmutableList<ProcessGroupInfo>> processGroups) {
Optional<ImmutableList<ProcessGroupInfo>> processGroups,
boolean debug) {
super();
this.dataDir = dataDir;
this.interval = interval;
......@@ -51,6 +53,7 @@ public class Config {
this.showSystem = showSystem;
this.showOther = showOther;
this.processGroups = processGroups;
this.debug = debug;
}
public Path getDataDir() {
......@@ -85,6 +88,10 @@ public class Config {
return processGroups;
}
public boolean isDebug() {
return debug;
}
public static class Builder {
private Path dataDir;
private long interval;
......@@ -95,6 +102,7 @@ public class Config {
private boolean showOther;
private Optional<ImmutableList<ProcessGroupInfo>> processGroups =
Optional.of(DefaultPostgresGroups.GROUPS);
private boolean debug;
public Builder withDataDir(Path dataDir) {
this.dataDir = dataDir;
......@@ -146,13 +154,18 @@ public class Config {
return this;
}
public Builder withDebug(boolean debug) {
this.debug = debug;
return this;
}
public Config build() {
Preconditions.checkArgument(dataDir != null,
"no database directory specified and environment variable PGDATA unset");
return new Config(dataDir, interval, ppid,
prometheusFormat, noPrintHeader, showSystem, showOther,
processGroups);
processGroups, debug);
}
}
}
......@@ -163,7 +163,7 @@ public class StatProcessor {
if (previousStats.isPresent() && previousStats.get().getStats().containsKey(pid)) {
return Optional.of(previousStats.get().getStats().get(pid).getInfo());
} else {
return logAndEmptyIfException(() -> new ProcessInfoParser(pid).parse(),
return emptyIfException(() -> new ProcessInfoParser(pid).parse(),
() -> "Can not parse process info " + pid + ": ");
}
}
......@@ -171,24 +171,26 @@ public class StatProcessor {
private Optional<ProcessStat> getPreviousOrParseIo(
Optional<StatSnapshot> previousStats, ProcessInfo info) {
if (previousStats.isPresent() && previousStats.get().getStats().containsKey(info.getPid())) {
return logAndEmptyIfException(
return emptyIfException(
() -> previousStats.get().getStats().get(info.getPid())
.next(new ProcessIoParser(info.getPid()).parse()),
() -> "Can not parse process io " + info.getPid() + ": ");
} else {
return logAndEmptyIfException(
return emptyIfException(
() -> new ProcessStat(info, new ProcessIoParser(info.getPid()).parse()),
() -> "Can not parse process io " + info.getPid() + ": ");
}
}
private <T> Optional<T> logAndEmptyIfException(
private <T> Optional<T> emptyIfException(
Callable<T> callable, Supplier<String> errorPrefix) {
try {
return Optional.of(callable.call());
} catch (Throwable throwable) {
err.println(errorPrefix.get() + throwable.getMessage());
throwable.printStackTrace(err);
if (config.isDebug()) {
err.println(errorPrefix.get() + throwable.getMessage());
throwable.printStackTrace(err);
}
return Optional.empty();
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment