Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only Log Warning if environment variable is defined and invalid #565

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@
private boolean appendStatusLabel = false;
private boolean perBuildMetrics = false;

private boolean environmentVariableSet;

private String labeledBuildParameterNames = "";

private boolean collectDiskUsage = true;
Expand All @@ -69,7 +67,6 @@
setPath(getPath());
setCollectingMetricsPeriodInSeconds(collectingMetricsPeriodInSeconds);
setCollectDiskUsageBasedOnEnvironmentVariableIfDefined();
environmentVariableSet = isValidBooleanEnv(COLLECT_DISK_USAGE);
}

public static PrometheusConfiguration get() {
Expand Down Expand Up @@ -124,27 +121,22 @@

public void setCollectDiskUsageBasedOnEnvironmentVariableIfDefined() {
try {
this.collectDiskUsage = getBooleanEnvironmentVariableOrThrowException(COLLECT_DISK_USAGE);
final String envValue = System.getenv(COLLECT_DISK_USAGE);
if (envValue != null) {
this.collectDiskUsage = getValidBooleanValueOrThrowException(envValue);
}
} catch (IllegalArgumentException e) {
logger.warn("Unable to parse environment variable '{}'. Must either be 'true' or 'false'. Ignoring...", COLLECT_DISK_USAGE);
}
}

private boolean getBooleanEnvironmentVariableOrThrowException(String key) throws IllegalArgumentException {
if (isValidBooleanEnv(key)) {
return Boolean.parseBoolean(System.getenv(key));
private boolean getValidBooleanValueOrThrowException(String value) throws IllegalArgumentException {
if ("true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value)) {

Check warning on line 134 in src/main/java/org/jenkinsci/plugins/prometheus/config/PrometheusConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 134 is only partially covered, one branch is missing
return Boolean.parseBoolean(value);
}
throw new IllegalArgumentException();
}

private boolean isValidBooleanEnv(String key) {
String value = System.getenv(key);
if (value != null) {
return ("true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value));
}
return false;
}

public boolean getCollectDiskUsage() {
return collectDiskUsage;
}
Expand Down Expand Up @@ -357,8 +349,4 @@
}
return stringValue.split("\\s*,\\s*");
}

public boolean isEnvironmentVariableSet() {
return environmentVariableSet;
}
}
Loading