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

Don't log warning when environment variable COLLECT_DISK_USAGE is not set #563

Closed
AB-xdev opened this issue Sep 25, 2023 · 0 comments · Fixed by #565
Closed

Don't log warning when environment variable COLLECT_DISK_USAGE is not set #563

AB-xdev opened this issue Sep 25, 2023 · 0 comments · Fixed by #565

Comments

@AB-xdev
Copy link

AB-xdev commented Sep 25, 2023

Steps to reproduce

Install the plugin and start Jenkins

Actual behaviour

The following warning occurs:

2023-09-25 08:53:00.318+0000 [id=31]    WARNING o.j.p.p.c.PrometheusConfiguration#setCollectDiskUsageBasedOnEnvironmentVariableIfDefined: Unable to parse environment variable 'COLLECT_DISK_USAGE'. Must either be 'true' or 'false'. Ignoring...

Expected behaviour

No warning occurs because COLLECT_DISK_USAGE is not set.

We e.g. configured Jenkins via CasC and therefore don't use environment variables.

The warning occurs due to

public void setCollectDiskUsageBasedOnEnvironmentVariableIfDefined() {
try {
this.collectDiskUsage = getBooleanEnvironmentVariableOrThrowException(COLLECT_DISK_USAGE);
} 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));
}
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;
}

I think the warning should only occur if the environment variable is set but it's invalid.
This could be achieved with the following code:

    public void setCollectDiskUsageBasedOnEnvironmentVariableIfDefined() {
        try {
            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 getValidBooleanValueOrThrowException(String value) throws IllegalArgumentException {
        if (i"true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value)) {
            return Boolean.parseBoolean(value);
        }
        throw new IllegalArgumentException();
    }

Server configuration

Operating system: N/A
Jenkins Version: N/A
Plugin Version: 2.3.3 (currently latest)

Notify

@Waschndolos

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant