Skip to content
Open
Show file tree
Hide file tree
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 @@ -94,9 +94,8 @@ static ExporterPushgatewayProperties load(PropertySource propertySource)
if (scheme != null) {
if (!scheme.equals("http") && !scheme.equals("https")) {
throw new PrometheusPropertiesException(
String.format(
"%s.%s: Illegal value. Expecting 'http' or 'https'. Found: %s",
PREFIX, SCHEME, scheme));
Util.invalidValueMessage(
PREFIX + "." + SCHEME, "Illegal value. Expecting 'http' or 'https'."));
}
}

Expand All @@ -119,10 +118,9 @@ static ExporterPushgatewayProperties load(PropertySource propertySource)
return EscapingScheme.DOTS_ESCAPING;
default:
throw new PrometheusPropertiesException(
String.format(
"%s.%s: Illegal value. Expecting 'allow-utf-8', 'values', 'underscores', "
+ "or 'dots'. Found: %s",
PREFIX, ESCAPING_SCHEME, scheme));
Util.invalidValueMessage(
PREFIX + "." + ESCAPING_SCHEME,
"Illegal value. Expecting 'allow-utf-8', 'values', 'underscores', or 'dots'."));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@ private static Properties loadPropertiesFromFile() throws PrometheusPropertiesEx
try (InputStream stream = Files.newInputStream(Paths.get(path))) {
properties.load(stream);
} catch (IOException e) {
throw new PrometheusPropertiesException(
"Failed to read Prometheus properties from " + path + ": " + e.getMessage(), e);
throw new PrometheusPropertiesException("Failed to read Prometheus properties file.", e);
}
}
return properties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static Boolean loadBoolean(String prefix, String propertyName, PropertySource pr
String fullKey = prefix.isEmpty() ? propertyName : prefix + "." + propertyName;
if (!"true".equalsIgnoreCase(property) && !"false".equalsIgnoreCase(property)) {
throw new PrometheusPropertiesException(
String.format("%s: Expecting 'true' or 'false'. Found: %s", fullKey, property));
invalidValueMessage(fullKey, "Expecting 'true' or 'false'."));
}
return Boolean.parseBoolean(property);
}
Expand Down Expand Up @@ -88,7 +88,7 @@ static List<Double> loadDoubleList(
}
} catch (NumberFormatException e) {
throw new PrometheusPropertiesException(
fullKey + "=" + property + ": Expecting comma separated list of double values");
invalidValueMessage(fullKey, "Expecting comma separated list of double values"));
}
}
return Arrays.asList(result);
Expand Down Expand Up @@ -130,7 +130,7 @@ static Integer loadInteger(String prefix, String propertyName, PropertySource pr
return Integer.parseInt(property);
} catch (NumberFormatException e) {
throw new PrometheusPropertiesException(
fullKey + "=" + property + ": Expecting integer value");
invalidValueMessage(fullKey, "Expecting integer value"));
}
}
return null;
Expand All @@ -146,7 +146,7 @@ static Double loadDouble(String prefix, String propertyName, PropertySource prop
return Double.parseDouble(property);
} catch (NumberFormatException e) {
throw new PrometheusPropertiesException(
fullKey + "=" + property + ": Expecting double value");
invalidValueMessage(fullKey, "Expecting double value"));
}
}
return null;
Expand All @@ -162,7 +162,7 @@ static Long loadLong(String prefix, String propertyName, PropertySource property
return Long.parseLong(property);
} catch (NumberFormatException e) {
throw new PrometheusPropertiesException(
fullKey + "=" + property + ": Expecting long value");
invalidValueMessage(fullKey, "Expecting long value"));
}
}
return null;
Expand Down Expand Up @@ -197,4 +197,8 @@ static <T extends Number> void assertValue(
throw new PrometheusPropertiesException(fullMessage);
}
}

static String invalidValueMessage(String fullKey, String message) {
return fullKey + ": " + message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,15 @@ void load() {
new HashMap<>(
Map.of("io.prometheus.exporter.include_created_timestamps", "invalid"))))
.withMessage(
"io.prometheus.exporter.include_created_timestamps: Expecting 'true' or 'false'. Found:"
+ " invalid");
"io.prometheus.exporter.include_created_timestamps: Expecting 'true' or 'false'.");
assertThatExceptionOfType(PrometheusPropertiesException.class)
.isThrownBy(
() ->
load(
new HashMap<>(
Map.of("io.prometheus.exporter.exemplars_on_all_metric_types", "invalid"))))
.withMessage(
"io.prometheus.exporter.exemplars_on_all_metric_types: Expecting 'true' or 'false'."
+ " Found: invalid");
"io.prometheus.exporter.exemplars_on_all_metric_types: Expecting 'true' or 'false'.");
}

private static ExporterProperties load(Map<String, String> map) {
Expand Down Expand Up @@ -84,7 +82,6 @@ void prometheusTimestampsInMs() {
new HashMap<>(
Map.of("io.prometheus.exporter.prometheus_timestamps_in_ms", "invalid"))))
.withMessage(
"io.prometheus.exporter.prometheus_timestamps_in_ms: Expecting 'true' or 'false'."
+ " Found: invalid");
"io.prometheus.exporter.prometheus_timestamps_in_ms: Expecting 'true' or 'false'.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void load() {
.isThrownBy(() -> load(Map.of("io.prometheus.exporter.pushgateway.scheme", "foo")))
.withMessage(
"io.prometheus.exporter.pushgateway.scheme: Illegal value. Expecting 'http' or 'https'."
+ " Found: foo");
+ "");
}

@Test
Expand Down Expand Up @@ -60,7 +60,7 @@ void loadWithInvalidEscapingScheme() {
() -> load(Map.of("io.prometheus.exporter.pushgateway.escaping_scheme", "invalid")))
.withMessage(
"io.prometheus.exporter.pushgateway.escaping_scheme: Illegal value. Expecting"
+ " 'allow-utf-8', 'values', 'underscores', or 'dots'. Found: invalid");
+ " 'allow-utf-8', 'values', 'underscores', or 'dots'.");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,44 +37,37 @@ void loadInvalidValue() {
assertThatExceptionOfType(PrometheusPropertiesException.class)
.isThrownBy(
() -> load(new HashMap<>(Map.of("io.prometheus.openmetrics2.enabled", "invalid"))))
.withMessage(
"io.prometheus.openmetrics2.enabled: Expecting 'true' or 'false'. Found: invalid");
.withMessage("io.prometheus.openmetrics2.enabled: Expecting 'true' or 'false'.");
assertThatExceptionOfType(PrometheusPropertiesException.class)
.isThrownBy(
() ->
load(
new HashMap<>(
Map.of("io.prometheus.openmetrics2.content_negotiation", "invalid"))))
.withMessage(
"io.prometheus.openmetrics2.content_negotiation: Expecting 'true' or 'false'. Found:"
+ " invalid");
"io.prometheus.openmetrics2.content_negotiation: Expecting 'true' or 'false'.");
assertThatExceptionOfType(PrometheusPropertiesException.class)
.isThrownBy(
() ->
load(
new HashMap<>(
Map.of("io.prometheus.openmetrics2.composite_values", "invalid"))))
.withMessage(
"io.prometheus.openmetrics2.composite_values: Expecting 'true' or 'false'. Found:"
+ " invalid");
.withMessage("io.prometheus.openmetrics2.composite_values: Expecting 'true' or 'false'.");
assertThatExceptionOfType(PrometheusPropertiesException.class)
.isThrownBy(
() ->
load(
new HashMap<>(
Map.of("io.prometheus.openmetrics2.exemplar_compliance", "invalid"))))
.withMessage(
"io.prometheus.openmetrics2.exemplar_compliance: Expecting 'true' or 'false'. Found:"
+ " invalid");
"io.prometheus.openmetrics2.exemplar_compliance: Expecting 'true' or 'false'.");
assertThatExceptionOfType(PrometheusPropertiesException.class)
.isThrownBy(
() ->
load(
new HashMap<>(
Map.of("io.prometheus.openmetrics2.native_histograms", "invalid"))))
.withMessage(
"io.prometheus.openmetrics2.native_histograms: Expecting 'true' or 'false'. Found:"
+ " invalid");
.withMessage("io.prometheus.openmetrics2.native_histograms: Expecting 'true' or 'false'.");
}

private static OpenMetrics2Properties load(Map<String, String> map) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ void propertiesShouldBeLoadedFromPropertiesFile() {
void cantLoadPropertiesFile() {
assertThatExceptionOfType(PrometheusPropertiesException.class)
.isThrownBy(() -> PrometheusPropertiesLoader.load(new Properties()))
.withMessage(
"Failed to read Prometheus properties from nonexistent.properties:"
+ " nonexistent.properties");
.withMessage("Failed to read Prometheus properties file.")
.withMessageNotContaining("nonexistent.properties");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ void loadOptionalDuration_invalidNumber_throws() {

assertThatExceptionOfType(PrometheusPropertiesException.class)
.isThrownBy(() -> Util.loadOptionalDuration("", "foo", propertySource))
.withMessage("foo=abc: Expecting long value");
.withMessage("foo: Expecting long value");
}

@Test
void invalidValueMessageRedactsRawValue() {
String secret = "bad\n\"secret-value";
Map<Object, Object> regularProperties = new HashMap<>(Map.of("foo", secret));
PropertySource propertySource = new PropertySource(regularProperties);

assertThatExceptionOfType(PrometheusPropertiesException.class)
.isThrownBy(() -> Util.loadBoolean("", "foo", propertySource))
.withMessage("foo: Expecting 'true' or 'false'.")
.withMessageNotContaining(secret);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -554,9 +554,9 @@ public PushGateway build() {
getEscapingScheme(properties),
getConnectionTimeout(properties),
getReadTimeout(properties));
} catch (MalformedURLException e) {
} catch (MalformedURLException | IllegalArgumentException e) {
throw new PrometheusPropertiesException(
address + ": Invalid address. Expecting <host>:<port>");
"Invalid Pushgateway address. Expecting <host>:<port>");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e); // cannot happen, UTF-8 is always supported
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.mockserver.model.HttpResponse.response;

import io.prometheus.metrics.config.EscapingScheme;
import io.prometheus.metrics.config.PrometheusPropertiesException;
import io.prometheus.metrics.core.metrics.Gauge;
import io.prometheus.metrics.model.registry.PrometheusRegistry;
import java.io.IOException;
Expand Down Expand Up @@ -48,6 +49,15 @@ void testInvalidURLThrowsRuntimeException() {
});
}

@Test
void testInvalidURLDoesNotExposeCredentials() {
String secretAddress = "user:secret@[::";
assertThatExceptionOfType(PrometheusPropertiesException.class)
.isThrownBy(() -> PushGateway.builder().address(secretAddress).build())
.withMessage("Invalid Pushgateway address. Expecting <host>:<port>")
.withMessageNotContaining("secret");
}

@Test
void testMultipleSlashesAreStrippedFromURL() throws NoSuchFieldException, IllegalAccessException {
final PushGateway pushGateway =
Expand Down