Files
error-prone-support/integration-tests/prometheus-java-client-expected-changes.patch
Rick Ossendrijver 24e3251eb0 Introduce Prometheus Java Client integration test (#1468)
And document some possible future improvements.
2024-12-24 10:36:23 +01:00

10364 lines
419 KiB
Diff

--- a/integration-tests/it-common/src/test/java/io/prometheus/client/it/common/ExporterTest.java
+++ b/integration-tests/it-common/src/test/java/io/prometheus/client/it/common/ExporterTest.java
@@ -111,7 +111,7 @@ public abstract class ExporterTest {
if (exception != null) {
exception.printStackTrace();
}
- fail("timeout while getting metrics from " + url);
+ fail("timeout while getting metrics from %s", url);
return null; // will not happen
}
--- a/integration-tests/it-common/src/test/java/io/prometheus/client/it/common/Volume.java
+++ b/integration-tests/it-common/src/test/java/io/prometheus/client/it/common/Volume.java
@@ -8,6 +8,7 @@ import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.*;
+import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.function.Predicate;
@@ -21,7 +22,7 @@ public class Volume {
}
public static Volume create(String prefix) throws IOException, URISyntaxException {
- Path targetDir = Paths.get(Volume.class.getResource("/").toURI()).getParent();
+ Path targetDir = Path.of(Volume.class.getResource("/").toURI()).getParent();
assertThat(targetDir.getFileName().toString())
.as("failed to locate target/ directory")
.isEqualTo("target");
@@ -61,7 +62,7 @@ public class Volume {
}
});
} else {
- fail(src + ": No such file or directory");
+ fail("%s: No such file or directory", src);
}
return this;
}
--- a/integration-tests/it-exporter/it-exporter-test/src/test/java/io/prometheus/metrics/it/exporter/test/ExporterIT.java
+++ b/integration-tests/it-exporter/it-exporter-test/src/test/java/io/prometheus/metrics/it/exporter/test/ExporterIT.java
@@ -87,12 +87,12 @@ abstract class ExporterIT extends ExporterTest {
assertThat(metrics.get(2).getName()).isEqualTo("uptime_seconds_total");
}
- @ParameterizedTest
@CsvSource({
"openmetrics, debug-openmetrics.txt",
- "text, debug-text.txt",
"prometheus-protobuf, debug-protobuf.txt",
+ "text, debug-text.txt"
})
+ @ParameterizedTest
public void testPrometheusProtobufDebugFormat(String format, String expected) throws IOException {
start();
Response response = scrape("GET", "debug=" + format);
@@ -147,7 +147,7 @@ abstract class ExporterIT extends ExporterTest {
start();
Response fullResponse = scrape("GET", "");
int size = fullResponse.body.length;
- assertThat(size).isGreaterThan(0);
+ assertThat(size).isPositive();
Response headResponse = scrape("HEAD", "");
assertThat(headResponse.status).isEqualTo(200);
assertThat(headResponse.getHeader("Content-Length")).isEqualTo(Integer.toString(size));
--- a/integration-tests/it-exporter/it-no-protobuf-test/src/test/java/io/prometheus/metrics/it/noprotobuf/NoProtobufIT.java
+++ b/integration-tests/it-exporter/it-no-protobuf-test/src/test/java/io/prometheus/metrics/it/noprotobuf/NoProtobufIT.java
@@ -7,14 +7,14 @@ import java.io.IOException;
import java.net.URISyntaxException;
import org.junit.jupiter.api.Test;
-class NoProtobufIT extends ExporterTest {
+final class NoProtobufIT extends ExporterTest {
public NoProtobufIT() throws IOException, URISyntaxException {
super("exporter-no-protobuf");
}
@Test
- public void testPrometheusProtobufDebugFormat() throws IOException {
+ void prometheusProtobufDebugFormat() throws IOException {
start();
assertThat(scrape("GET", "debug=text").status).isEqualTo(200);
// protobuf is not supported
--- a/integration-tests/it-pushgateway/src/test/java/io/prometheus/metrics/it/pushgateway/PushGatewayIT.java
+++ b/integration-tests/it-pushgateway/src/test/java/io/prometheus/metrics/it/pushgateway/PushGatewayIT.java
@@ -22,7 +22,7 @@ import org.testcontainers.containers.Network;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.utility.MountableFile;
-public class PushGatewayIT {
+final class PushGatewayIT {
private GenericContainer<?> sampleAppContainer;
private GenericContainer<?> pushGatewayContainer;
@@ -30,7 +30,7 @@ public class PushGatewayIT {
private Volume sampleAppVolume;
@BeforeEach
- public void setUp() throws IOException, URISyntaxException {
+ void setUp() throws IOException, URISyntaxException {
Network network = Network.newNetwork();
sampleAppVolume = Volume.create("it-pushgateway").copy("pushgateway-test-app.jar");
pushGatewayContainer =
@@ -56,7 +56,7 @@ public class PushGatewayIT {
}
@AfterEach
- public void tearDown() throws IOException {
+ void tearDown() throws IOException {
prometheusContainer.stop();
pushGatewayContainer.stop();
sampleAppContainer.stop();
@@ -66,7 +66,7 @@ public class PushGatewayIT {
final OkHttpClient client = new OkHttpClient();
@Test
- public void testSimple() throws IOException, InterruptedException {
+ void simple() throws IOException, InterruptedException {
pushGatewayContainer.start();
sampleAppContainer
.withCommand(
@@ -86,7 +86,7 @@ public class PushGatewayIT {
}
@Test
- public void testTextFormat() throws IOException, InterruptedException {
+ void textFormat() throws IOException, InterruptedException {
pushGatewayContainer.start();
sampleAppContainer
.withCommand(
@@ -106,7 +106,7 @@ public class PushGatewayIT {
}
@Test
- public void testBasicAuth() throws IOException, InterruptedException {
+ void basicAuth() throws IOException, InterruptedException {
pushGatewayContainer
.withCopyFileToContainer(
MountableFile.forClasspathResource("/pushgateway-basicauth.yaml"),
@@ -131,7 +131,7 @@ public class PushGatewayIT {
}
@Test
- public void testSsl() throws InterruptedException, IOException {
+ void ssl() throws InterruptedException, IOException {
pushGatewayContainer
.withCopyFileToContainer(
MountableFile.forClasspathResource("/pushgateway-ssl.yaml"),
@@ -156,7 +156,7 @@ public class PushGatewayIT {
}
@Test
- public void testProtobuf() throws IOException, InterruptedException {
+ void protobuf() throws IOException, InterruptedException {
pushGatewayContainer.start();
sampleAppContainer
.withCommand(
@@ -197,7 +197,7 @@ public class PushGatewayIT {
JSONArray result =
JsonPath.parse(scrapeResponseJson)
.read("$.data.result" + Filter.filter(criteria) + ".value[1]");
- assertThat(result.size()).isOne();
+ assertThat(result).hasSize(1);
return Double.parseDouble(result.get(0).toString());
}
@@ -244,7 +244,7 @@ public class PushGatewayIT {
Thread.sleep(250);
timeRemaining -= 250;
}
- fail("timeout while scraping " + url);
+ fail("timeout while scraping %s", url);
return null;
}
@@ -253,13 +253,7 @@ public class PushGatewayIT {
long waitTimeMillis = 0;
while (container.isRunning()) {
if (waitTimeMillis > unit.toMillis(timeout)) {
- fail(
- container.getContainerName()
- + " did not terminate after "
- + timeout
- + " "
- + unit
- + ".");
+ fail("%s did not terminate after %s %s.", container.getContainerName(), timeout, unit);
}
Thread.sleep(20);
waitTimeMillis += 20;
--- a/prometheus-metrics-config/src/main/java/io/prometheus/metrics/config/ExporterFilterProperties.java
+++ b/prometheus-metrics-config/src/main/java/io/prometheus/metrics/config/ExporterFilterProperties.java
@@ -1,8 +1,9 @@
package io.prometheus.metrics.config;
+import static java.util.Collections.unmodifiableList;
+
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -26,17 +27,13 @@ public class ExporterFilterProperties {
List<String> allowedPrefixes,
List<String> excludedPrefixes) {
this.allowedNames =
- allowedNames == null ? null : Collections.unmodifiableList(new ArrayList<>(allowedNames));
+ allowedNames == null ? null : unmodifiableList(new ArrayList<>(allowedNames));
this.excludedNames =
- excludedNames == null ? null : Collections.unmodifiableList(new ArrayList<>(excludedNames));
+ excludedNames == null ? null : unmodifiableList(new ArrayList<>(excludedNames));
this.allowedPrefixes =
- allowedPrefixes == null
- ? null
- : Collections.unmodifiableList(new ArrayList<>(allowedPrefixes));
+ allowedPrefixes == null ? null : unmodifiableList(new ArrayList<>(allowedPrefixes));
this.excludedPrefixes =
- excludedPrefixes == null
- ? null
- : Collections.unmodifiableList(new ArrayList<>(excludedPrefixes));
+ excludedPrefixes == null ? null : unmodifiableList(new ArrayList<>(excludedPrefixes));
}
public List<String> getAllowedMetricNames() {
--- a/prometheus-metrics-config/src/main/java/io/prometheus/metrics/config/ExporterOpenTelemetryProperties.java
+++ b/prometheus-metrics-config/src/main/java/io/prometheus/metrics/config/ExporterOpenTelemetryProperties.java
@@ -1,5 +1,7 @@
package io.prometheus.metrics.config;
+import static com.google.common.base.Preconditions.checkArgument;
+
import java.util.HashMap;
import java.util.Map;
@@ -145,10 +147,10 @@ public class ExporterOpenTelemetryProperties {
private Builder() {}
public Builder protocol(String protocol) {
- if (!protocol.equals("grpc") && !protocol.equals("http/protobuf")) {
- throw new IllegalArgumentException(
- protocol + ": Unsupported protocol. Expecting grpc or http/protobuf");
- }
+ checkArgument(
+ protocol.equals("grpc") || protocol.equals("http/protobuf"),
+ "%s: Unsupported protocol. Expecting grpc or http/protobuf",
+ protocol);
this.protocol = protocol;
return this;
}
@@ -165,17 +167,13 @@ public class ExporterOpenTelemetryProperties {
}
public Builder intervalSeconds(int intervalSeconds) {
- if (intervalSeconds <= 0) {
- throw new IllegalArgumentException(intervalSeconds + ": Expecting intervalSeconds > 0");
- }
+ checkArgument(intervalSeconds > 0, "%s: Expecting intervalSeconds > 0", intervalSeconds);
this.interval = intervalSeconds + "s";
return this;
}
public Builder timeoutSeconds(int timeoutSeconds) {
- if (timeoutSeconds <= 0) {
- throw new IllegalArgumentException(timeoutSeconds + ": Expecting timeoutSeconds > 0");
- }
+ checkArgument(timeoutSeconds > 0, "%s: Expecting timeoutSeconds > 0", timeoutSeconds);
this.timeout = timeoutSeconds + "s";
return this;
}
--- a/prometheus-metrics-config/src/main/java/io/prometheus/metrics/config/MetricsProperties.java
+++ b/prometheus-metrics-config/src/main/java/io/prometheus/metrics/config/MetricsProperties.java
@@ -2,8 +2,8 @@ package io.prometheus.metrics.config;
import static java.util.Collections.unmodifiableList;
+import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -301,7 +301,7 @@ public class MetricsProperties {
public List<Double> getSummaryQuantileErrors() {
if (summaryQuantiles != null) {
if (summaryQuantileErrors == null) {
- return Collections.emptyList();
+ return ImmutableList.of();
}
}
return summaryQuantileErrors;
--- a/prometheus-metrics-config/src/main/java/io/prometheus/metrics/config/PrometheusProperties.java
+++ b/prometheus-metrics-config/src/main/java/io/prometheus/metrics/config/PrometheusProperties.java
@@ -10,7 +10,7 @@ import java.util.Map;
*/
public class PrometheusProperties {
- private static final PrometheusProperties instance = PrometheusPropertiesLoader.load();
+ private static final PrometheusProperties INSTANCE = PrometheusPropertiesLoader.load();
private final MetricsProperties defaultMetricsProperties;
private final Map<String, MetricsProperties> metricProperties = new HashMap<>();
@@ -33,7 +33,7 @@ public class PrometheusProperties {
* </ul>
*/
public static PrometheusProperties get() throws PrometheusPropertiesException {
- return instance;
+ return INSTANCE;
}
public PrometheusProperties(
--- a/prometheus-metrics-config/src/main/java/io/prometheus/metrics/config/PrometheusPropertiesLoader.java
+++ b/prometheus-metrics-config/src/main/java/io/prometheus/metrics/config/PrometheusPropertiesLoader.java
@@ -3,7 +3,7 @@ package io.prometheus.metrics.config;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
-import java.nio.file.Paths;
+import java.nio.file.Path;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
@@ -126,7 +126,7 @@ public class PrometheusPropertiesLoader {
path = System.getenv("PROMETHEUS_CONFIG");
}
if (path != null) {
- try (InputStream stream = Files.newInputStream(Paths.get(path))) {
+ try (InputStream stream = Files.newInputStream(Path.of(path))) {
properties.load(stream);
} catch (IOException e) {
throw new PrometheusPropertiesException(
--- a/prometheus-metrics-config/src/main/java/io/prometheus/metrics/config/Util.java
+++ b/prometheus-metrics-config/src/main/java/io/prometheus/metrics/config/Util.java
@@ -23,7 +23,7 @@ class Util {
if (property != null) {
if (!"true".equalsIgnoreCase(property) && !"false".equalsIgnoreCase(property)) {
throw new PrometheusPropertiesException(
- String.format("%s: Expecting 'true' or 'false'. Found: %s", name, property));
+ String.join(": Expecting 'true' or 'false'. Found: ", name, property));
}
return Boolean.parseBoolean(property);
}
--- a/prometheus-metrics-config/src/test/java/io/prometheus/metrics/config/ExemplarsPropertiesTest.java
+++ b/prometheus-metrics-config/src/test/java/io/prometheus/metrics/config/ExemplarsPropertiesTest.java
@@ -1,39 +1,46 @@
package io.prometheus.metrics.config;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import com.google.common.collect.ImmutableMap;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;
-class ExemplarsPropertiesTest {
+final class ExemplarsPropertiesTest {
@Test
void load() {
ExemplarsProperties properties =
load(
- Map.of(
- "io.prometheus.exemplars.minRetentionPeriodSeconds", "1",
- "io.prometheus.exemplars.maxRetentionPeriodSeconds", "2",
- "io.prometheus.exemplars.sampleIntervalMilliseconds", "3"));
- assertThat(properties.getMinRetentionPeriodSeconds()).isOne();
+ ImmutableMap.of(
+ "io.prometheus.exemplars.minRetentionPeriodSeconds",
+ "1",
+ "io.prometheus.exemplars.maxRetentionPeriodSeconds",
+ "2",
+ "io.prometheus.exemplars.sampleIntervalMilliseconds",
+ "3"));
+ assertThat(properties.getMinRetentionPeriodSeconds()).isEqualTo(1);
assertThat(properties.getMaxRetentionPeriodSeconds()).isEqualTo(2);
assertThat(properties.getSampleIntervalMilliseconds()).isEqualTo(3);
- assertThatExceptionOfType(PrometheusPropertiesException.class)
- .isThrownBy(() -> load(Map.of("io.prometheus.exemplars.minRetentionPeriodSeconds", "-1")))
- .withMessage(
+ assertThatThrownBy(
+ () -> load(ImmutableMap.of("io.prometheus.exemplars.minRetentionPeriodSeconds", "-1")))
+ .isInstanceOf(PrometheusPropertiesException.class)
+ .hasMessage(
"io.prometheus.exemplars.minRetentionPeriodSeconds: Expecting value > 0. Found: -1");
- assertThatExceptionOfType(PrometheusPropertiesException.class)
- .isThrownBy(() -> load(Map.of("io.prometheus.exemplars.maxRetentionPeriodSeconds", "0")))
- .withMessage(
+ assertThatThrownBy(
+ () -> load(ImmutableMap.of("io.prometheus.exemplars.maxRetentionPeriodSeconds", "0")))
+ .isInstanceOf(PrometheusPropertiesException.class)
+ .hasMessage(
"io.prometheus.exemplars.maxRetentionPeriodSeconds: Expecting value > 0. Found: 0");
- assertThatExceptionOfType(PrometheusPropertiesException.class)
- .isThrownBy(() -> load(Map.of("io.prometheus.exemplars.sampleIntervalMilliseconds", "-1")))
- .withMessage(
+ assertThatThrownBy(
+ () -> load(ImmutableMap.of("io.prometheus.exemplars.sampleIntervalMilliseconds", "-1")))
+ .isInstanceOf(PrometheusPropertiesException.class)
+ .hasMessage(
"io.prometheus.exemplars.sampleIntervalMilliseconds: Expecting value > 0. Found: -1");
}
@@ -49,7 +56,7 @@ class ExemplarsPropertiesTest {
.maxRetentionPeriodSeconds(2)
.sampleIntervalMilliseconds(3)
.build();
- assertThat(properties.getMinRetentionPeriodSeconds()).isOne();
+ assertThat(properties.getMinRetentionPeriodSeconds()).isEqualTo(1);
assertThat(properties.getMaxRetentionPeriodSeconds()).isEqualTo(2);
assertThat(properties.getSampleIntervalMilliseconds()).isEqualTo(3);
}
--- a/prometheus-metrics-config/src/test/java/io/prometheus/metrics/config/ExporterFilterPropertiesTest.java
+++ b/prometheus-metrics-config/src/test/java/io/prometheus/metrics/config/ExporterFilterPropertiesTest.java
@@ -2,21 +2,26 @@ package io.prometheus.metrics.config;
import static org.assertj.core.api.Assertions.assertThat;
+import com.google.common.collect.ImmutableMap;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;
-class ExporterFilterPropertiesTest {
+final class ExporterFilterPropertiesTest {
@Test
void load() {
ExporterFilterProperties properties =
load(
- Map.of(
- "io.prometheus.exporter.filter.metricNameMustBeEqualTo", "a,b,c",
- "io.prometheus.exporter.filter.metricNameMustNotBeEqualTo", "d,e,f",
- "io.prometheus.exporter.filter.metricNameMustStartWith", "g,h,i",
- "io.prometheus.exporter.filter.metricNameMustNotStartWith", "j,k,l"));
+ ImmutableMap.of(
+ "io.prometheus.exporter.filter.metricNameMustBeEqualTo",
+ "a,b,c",
+ "io.prometheus.exporter.filter.metricNameMustNotBeEqualTo",
+ "d,e,f",
+ "io.prometheus.exporter.filter.metricNameMustStartWith",
+ "g,h,i",
+ "io.prometheus.exporter.filter.metricNameMustNotStartWith",
+ "j,k,l"));
assertThat(properties.getAllowedMetricNames()).containsExactly("a", "b", "c");
assertThat(properties.getExcludedMetricNames()).containsExactly("d", "e", "f");
assertThat(properties.getAllowedMetricNamePrefixes()).containsExactly("g", "h", "i");
--- a/prometheus-metrics-config/src/test/java/io/prometheus/metrics/config/ExporterHttpServerPropertiesTest.java
+++ b/prometheus-metrics-config/src/test/java/io/prometheus/metrics/config/ExporterHttpServerPropertiesTest.java
@@ -1,27 +1,28 @@
package io.prometheus.metrics.config;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import com.google.common.collect.ImmutableMap;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;
-class ExporterHttpServerPropertiesTest {
+final class ExporterHttpServerPropertiesTest {
@Test
void load() {
ExporterHttpServerProperties properties =
- load(Map.of("io.prometheus.exporter.httpServer.port", "1"));
- assertThat(properties.getPort()).isOne();
+ load(ImmutableMap.of("io.prometheus.exporter.httpServer.port", "1"));
+ assertThat(properties.getPort()).isEqualTo(1);
- assertThatExceptionOfType(PrometheusPropertiesException.class)
- .isThrownBy(() -> load(Map.of("io.prometheus.exporter.httpServer.port", "0")))
- .withMessage("io.prometheus.exporter.httpServer.port: Expecting value > 0. Found: 0");
+ assertThatThrownBy(() -> load(ImmutableMap.of("io.prometheus.exporter.httpServer.port", "0")))
+ .isInstanceOf(PrometheusPropertiesException.class)
+ .hasMessage("io.prometheus.exporter.httpServer.port: Expecting value > 0. Found: 0");
}
@Test
void builder() {
- assertThat(ExporterHttpServerProperties.builder().port(1).build().getPort()).isOne();
+ assertThat(ExporterHttpServerProperties.builder().port(1).build().getPort()).isEqualTo(1);
}
private static ExporterHttpServerProperties load(Map<String, String> map) {
--- a/prometheus-metrics-config/src/test/java/io/prometheus/metrics/config/ExporterOpenTelemetryPropertiesTest.java
+++ b/prometheus-metrics-config/src/test/java/io/prometheus/metrics/config/ExporterOpenTelemetryPropertiesTest.java
@@ -2,11 +2,12 @@ package io.prometheus.metrics.config;
import static org.assertj.core.api.Assertions.assertThat;
+import com.google.common.collect.ImmutableMap;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;
-class ExporterOpenTelemetryPropertiesTest {
+final class ExporterOpenTelemetryPropertiesTest {
@Test
void load() {
@@ -32,7 +33,7 @@ class ExporterOpenTelemetryPropertiesTest {
assertThat(properties.getProtocol()).isEqualTo("grpc");
assertThat(properties.getEndpoint()).isEqualTo("http://localhost:8080");
assertThat(properties.getHeaders())
- .containsExactlyInAnyOrderEntriesOf(Map.of("key1", "value1", "key2", "value2"));
+ .containsExactlyInAnyOrderEntriesOf(ImmutableMap.of("key1", "value1", "key2", "value2"));
assertThat(properties.getInterval()).isEqualTo("10s");
assertThat(properties.getTimeout()).isEqualTo("5s");
assertThat(properties.getServiceName()).isEqualTo("serviceName");
@@ -40,7 +41,7 @@ class ExporterOpenTelemetryPropertiesTest {
assertThat(properties.getServiceInstanceId()).isEqualTo("serviceInstanceId");
assertThat(properties.getServiceVersion()).isEqualTo("serviceVersion");
assertThat(properties.getResourceAttributes())
- .containsExactlyInAnyOrderEntriesOf(Map.of("key1", "value1", "key2", "value2"));
+ .containsExactlyInAnyOrderEntriesOf(ImmutableMap.of("key1", "value1", "key2", "value2"));
}
private static ExporterOpenTelemetryProperties load(Map<String, String> map) {
--- a/prometheus-metrics-config/src/test/java/io/prometheus/metrics/config/ExporterPropertiesTest.java
+++ b/prometheus-metrics-config/src/test/java/io/prometheus/metrics/config/ExporterPropertiesTest.java
@@ -1,40 +1,45 @@
package io.prometheus.metrics.config;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import com.google.common.collect.ImmutableMap;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;
-class ExporterPropertiesTest {
+final class ExporterPropertiesTest {
@Test
void load() {
ExporterProperties properties =
load(
new HashMap<>(
- Map.of(
- "io.prometheus.exporter.includeCreatedTimestamps", "true",
- "io.prometheus.exporter.exemplarsOnAllMetricTypes", "true")));
+ ImmutableMap.of(
+ "io.prometheus.exporter.includeCreatedTimestamps",
+ "true",
+ "io.prometheus.exporter.exemplarsOnAllMetricTypes",
+ "true")));
assertThat(properties.getIncludeCreatedTimestamps()).isTrue();
assertThat(properties.getExemplarsOnAllMetricTypes()).isTrue();
- assertThatExceptionOfType(PrometheusPropertiesException.class)
- .isThrownBy(
+ assertThatThrownBy(
() ->
load(
new HashMap<>(
- Map.of("io.prometheus.exporter.includeCreatedTimestamps", "invalid"))))
- .withMessage(
+ ImmutableMap.of(
+ "io.prometheus.exporter.includeCreatedTimestamps", "invalid"))))
+ .isInstanceOf(PrometheusPropertiesException.class)
+ .hasMessage(
"io.prometheus.exporter.includeCreatedTimestamps: Expecting 'true' or 'false'. Found: invalid");
- assertThatExceptionOfType(PrometheusPropertiesException.class)
- .isThrownBy(
+ assertThatThrownBy(
() ->
load(
new HashMap<>(
- Map.of("io.prometheus.exporter.exemplarsOnAllMetricTypes", "invalid"))))
- .withMessage(
+ ImmutableMap.of(
+ "io.prometheus.exporter.exemplarsOnAllMetricTypes", "invalid"))))
+ .isInstanceOf(PrometheusPropertiesException.class)
+ .hasMessage(
"io.prometheus.exporter.exemplarsOnAllMetricTypes: Expecting 'true' or 'false'. Found: invalid");
}
--- a/prometheus-metrics-config/src/test/java/io/prometheus/metrics/config/ExporterPushgatewayPropertiesTest.java
+++ b/prometheus-metrics-config/src/test/java/io/prometheus/metrics/config/ExporterPushgatewayPropertiesTest.java
@@ -1,30 +1,35 @@
package io.prometheus.metrics.config;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import com.google.common.collect.ImmutableMap;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;
-class ExporterPushgatewayPropertiesTest {
+final class ExporterPushgatewayPropertiesTest {
@Test
void load() {
ExporterPushgatewayProperties properties =
load(
- Map.of(
- "io.prometheus.exporter.pushgateway.address", "http://localhost",
- "io.prometheus.exporter.pushgateway.job", "job",
- "io.prometheus.exporter.pushgateway.scheme", "http"));
+ ImmutableMap.of(
+ "io.prometheus.exporter.pushgateway.address",
+ "http://localhost",
+ "io.prometheus.exporter.pushgateway.job",
+ "job",
+ "io.prometheus.exporter.pushgateway.scheme",
+ "http"));
assertThat(properties.getAddress()).isEqualTo("http://localhost");
assertThat(properties.getJob()).isEqualTo("job");
assertThat(properties.getScheme()).isEqualTo("http");
- assertThatExceptionOfType(PrometheusPropertiesException.class)
- .isThrownBy(() -> load(Map.of("io.prometheus.exporter.pushgateway.scheme", "foo")))
- .withMessage(
+ assertThatThrownBy(
+ () -> load(ImmutableMap.of("io.prometheus.exporter.pushgateway.scheme", "foo")))
+ .isInstanceOf(PrometheusPropertiesException.class)
+ .hasMessage(
"io.prometheus.exporter.pushgateway.scheme: Illegal value. Expecting 'http' or 'https'. Found: foo");
}
--- a/prometheus-metrics-config/src/test/java/io/prometheus/metrics/config/MetricsPropertiesTest.java
+++ b/prometheus-metrics-config/src/test/java/io/prometheus/metrics/config/MetricsPropertiesTest.java
@@ -1,11 +1,11 @@
package io.prometheus.metrics.config;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import org.junit.jupiter.api.Test;
-class MetricsPropertiesTest {
+final class MetricsPropertiesTest {
@Test
void builder() {
assertThat(MetricsProperties.builder().exemplarsEnabled(true).build().getExemplarsEnabled())
@@ -30,7 +30,7 @@ class MetricsPropertiesTest {
.containsExactly(0.1, 0.2);
assertThat(
MetricsProperties.builder().summaryMaxAgeSeconds(1L).build().getSummaryMaxAgeSeconds())
- .isOne();
+ .isEqualTo(1);
assertThat(
MetricsProperties.builder()
.summaryQuantiles(0.2)
@@ -43,38 +43,38 @@ class MetricsPropertiesTest {
.summaryNumberOfAgeBuckets(1)
.build()
.getSummaryNumberOfAgeBuckets())
- .isOne();
+ .isEqualTo(1);
- assertThatExceptionOfType(PrometheusPropertiesException.class)
- .isThrownBy(() -> MetricsProperties.builder().summaryNumberOfAgeBuckets(0).build())
- .withMessage(".summaryNumberOfAgeBuckets: Expecting value > 0. Found: 0");
+ assertThatThrownBy(() -> MetricsProperties.builder().summaryNumberOfAgeBuckets(0).build())
+ .isInstanceOf(PrometheusPropertiesException.class)
+ .hasMessage(".summaryNumberOfAgeBuckets: Expecting value > 0. Found: 0");
- assertThatExceptionOfType(PrometheusPropertiesException.class)
- .isThrownBy(() -> MetricsProperties.builder().summaryQuantiles(2L).build())
- .withMessage(".summaryQuantiles: Expecting 0.0 <= quantile <= 1.0. Found: 2.0");
+ assertThatThrownBy(() -> MetricsProperties.builder().summaryQuantiles(2L).build())
+ .isInstanceOf(PrometheusPropertiesException.class)
+ .hasMessage(".summaryQuantiles: Expecting 0.0 <= quantile <= 1.0. Found: 2.0");
- assertThatExceptionOfType(PrometheusPropertiesException.class)
- .isThrownBy(() -> MetricsProperties.builder().summaryQuantileErrors(0.9).build())
- .withMessage(
+ assertThatThrownBy(() -> MetricsProperties.builder().summaryQuantileErrors(0.9).build())
+ .isInstanceOf(PrometheusPropertiesException.class)
+ .hasMessage(
".summaryQuantileErrors: Can't configure summaryQuantileErrors without configuring summaryQuantiles");
- assertThatExceptionOfType(PrometheusPropertiesException.class)
- .isThrownBy(
+ assertThatThrownBy(
() ->
MetricsProperties.builder()
.summaryQuantiles(0.1)
.summaryQuantileErrors(0.1, 0.9)
.build())
- .withMessage(".summaryQuantileErrors: must have the same length as summaryQuantiles");
+ .isInstanceOf(PrometheusPropertiesException.class)
+ .hasMessage(".summaryQuantileErrors: must have the same length as summaryQuantiles");
- assertThatExceptionOfType(PrometheusPropertiesException.class)
- .isThrownBy(
+ assertThatThrownBy(
() ->
MetricsProperties.builder()
.summaryQuantiles(0.1)
.summaryQuantileErrors(-0.9)
.build())
- .withMessage(".summaryQuantileErrors: Expecting 0.0 <= error <= 1.0");
+ .isInstanceOf(PrometheusPropertiesException.class)
+ .hasMessage(".summaryQuantileErrors: Expecting 0.0 <= error <= 1.0");
}
@Test
@@ -84,7 +84,7 @@ class MetricsPropertiesTest {
.histogramNativeInitialSchema(1)
.build()
.getHistogramNativeInitialSchema())
- .isOne();
+ .isEqualTo(1);
assertThat(
MetricsProperties.builder()
.histogramNativeMinZeroThreshold(.1)
@@ -102,53 +102,55 @@ class MetricsPropertiesTest {
.histogramNativeMaxNumberOfBuckets(1)
.build()
.getHistogramNativeMaxNumberOfBuckets())
- .isOne();
+ .isEqualTo(1);
assertThat(
MetricsProperties.builder()
.histogramNativeResetDurationSeconds(1L)
.build()
.getHistogramNativeResetDurationSeconds())
- .isOne();
+ .isEqualTo(1);
- assertThatExceptionOfType(PrometheusPropertiesException.class)
- .isThrownBy(() -> MetricsProperties.builder().histogramNativeInitialSchema(10).build())
- .withMessage(
- ".histogramNativeInitialSchema: Expecting number between -4 and +8. Found: 10");
+ assertThatThrownBy(() -> MetricsProperties.builder().histogramNativeInitialSchema(10).build())
+ .isInstanceOf(PrometheusPropertiesException.class)
+ .hasMessage(".histogramNativeInitialSchema: Expecting number between -4 and +8. Found: 10");
- assertThatExceptionOfType(PrometheusPropertiesException.class)
- .isThrownBy(() -> MetricsProperties.builder().histogramNativeMinZeroThreshold(-1.0).build())
- .withMessage(".histogramNativeMinZeroThreshold: Expecting value >= 0. Found: -1.0");
+ assertThatThrownBy(
+ () -> MetricsProperties.builder().histogramNativeMinZeroThreshold(-1.0).build())
+ .isInstanceOf(PrometheusPropertiesException.class)
+ .hasMessage(".histogramNativeMinZeroThreshold: Expecting value >= 0. Found: -1.0");
- assertThatExceptionOfType(PrometheusPropertiesException.class)
- .isThrownBy(() -> MetricsProperties.builder().histogramNativeMaxZeroThreshold(-1.0).build())
- .withMessage(".histogramNativeMaxZeroThreshold: Expecting value >= 0. Found: -1.0");
+ assertThatThrownBy(
+ () -> MetricsProperties.builder().histogramNativeMaxZeroThreshold(-1.0).build())
+ .isInstanceOf(PrometheusPropertiesException.class)
+ .hasMessage(".histogramNativeMaxZeroThreshold: Expecting value >= 0. Found: -1.0");
- assertThatExceptionOfType(PrometheusPropertiesException.class)
- .isThrownBy(() -> MetricsProperties.builder().histogramNativeMaxNumberOfBuckets(-1).build())
- .withMessage(".histogramNativeMaxNumberOfBuckets: Expecting value >= 0. Found: -1");
+ assertThatThrownBy(
+ () -> MetricsProperties.builder().histogramNativeMaxNumberOfBuckets(-1).build())
+ .isInstanceOf(PrometheusPropertiesException.class)
+ .hasMessage(".histogramNativeMaxNumberOfBuckets: Expecting value >= 0. Found: -1");
- assertThatExceptionOfType(PrometheusPropertiesException.class)
- .isThrownBy(
+ assertThatThrownBy(
() -> MetricsProperties.builder().histogramNativeResetDurationSeconds(-1L).build())
- .withMessage(".histogramNativeResetDurationSeconds: Expecting value >= 0. Found: -1");
+ .isInstanceOf(PrometheusPropertiesException.class)
+ .hasMessage(".histogramNativeResetDurationSeconds: Expecting value >= 0. Found: -1");
- assertThatExceptionOfType(PrometheusPropertiesException.class)
- .isThrownBy(
+ assertThatThrownBy(
() ->
MetricsProperties.builder()
.histogramNativeOnly(true)
.histogramClassicOnly(true)
.build())
- .withMessage(".histogramNativeOnly and .histogramClassicOnly cannot both be true");
+ .isInstanceOf(PrometheusPropertiesException.class)
+ .hasMessage(".histogramNativeOnly and .histogramClassicOnly cannot both be true");
- assertThatExceptionOfType(PrometheusPropertiesException.class)
- .isThrownBy(
+ assertThatThrownBy(
() ->
MetricsProperties.builder()
.histogramNativeMinZeroThreshold(0.1)
.histogramNativeMaxZeroThreshold(0.01)
.build())
- .withMessage(
+ .isInstanceOf(PrometheusPropertiesException.class)
+ .hasMessage(
".histogramNativeMinZeroThreshold cannot be greater than .histogramNativeMaxZeroThreshold");
}
}
--- a/prometheus-metrics-config/src/test/java/io/prometheus/metrics/config/PrometheusPropertiesLoaderTest.java
+++ b/prometheus-metrics-config/src/test/java/io/prometheus/metrics/config/PrometheusPropertiesLoaderTest.java
@@ -1,17 +1,17 @@
package io.prometheus.metrics.config;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.util.Properties;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.SetSystemProperty;
/** Tests for {@link PrometheusPropertiesLoader}. */
-class PrometheusPropertiesLoaderTest {
+final class PrometheusPropertiesLoaderTest {
@Test
- public void propertiesShouldBeLoadedFromPropertiesFile() {
+ void propertiesShouldBeLoadedFromPropertiesFile() {
PrometheusProperties prometheusProperties = PrometheusPropertiesLoader.load();
assertThat(prometheusProperties.getDefaultMetricProperties().getHistogramClassicUpperBounds())
.hasSize(11);
@@ -24,20 +24,20 @@ class PrometheusPropertiesLoaderTest {
.isTrue();
}
- @Test
@SetSystemProperty(key = "prometheus.config", value = "nonexistent.properties")
+ @Test
void cantLoadPropertiesFile() {
- assertThatExceptionOfType(PrometheusPropertiesException.class)
- .isThrownBy(
+ assertThatThrownBy(
() -> {
PrometheusPropertiesLoader.load(new Properties());
})
- .withMessage(
+ .isInstanceOf(PrometheusPropertiesException.class)
+ .hasMessage(
"Failed to read Prometheus properties from nonexistent.properties: nonexistent.properties");
}
@Test
- public void externalPropertiesShouldOverridePropertiesFile() {
+ void externalPropertiesShouldOverridePropertiesFile() {
Properties properties = new Properties();
properties.setProperty("io.prometheus.metrics.histogramClassicUpperBounds", ".005, .01");
properties.setProperty(
--- a/prometheus-metrics-config/src/test/java/io/prometheus/metrics/config/PrometheusPropertiesTest.java
+++ b/prometheus-metrics-config/src/test/java/io/prometheus/metrics/config/PrometheusPropertiesTest.java
@@ -7,10 +7,10 @@ import java.io.InputStream;
import java.util.Properties;
import org.junit.jupiter.api.Test;
-class PrometheusPropertiesTest {
+final class PrometheusPropertiesTest {
@Test
- public void testPrometheusConfig() {
+ void prometheusConfig() {
PrometheusProperties result = PrometheusProperties.get();
assertThat(result.getDefaultMetricProperties().getHistogramClassicUpperBounds()).hasSize(11);
assertThat(result.getMetricProperties("http_duration_seconds").getHistogramClassicUpperBounds())
@@ -18,7 +18,7 @@ class PrometheusPropertiesTest {
}
@Test
- public void testEmptyUpperBounds() throws IOException {
+ void emptyUpperBounds() throws IOException {
Properties properties = new Properties();
try (InputStream stream =
Thread.currentThread()
--- a/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/exemplars/ExemplarSamplerConfig.java
+++ b/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/exemplars/ExemplarSamplerConfig.java
@@ -1,5 +1,7 @@
package io.prometheus.metrics.core.exemplars;
+import static com.google.common.base.Preconditions.checkArgument;
+
import io.prometheus.metrics.config.ExemplarsProperties;
import io.prometheus.metrics.config.PrometheusProperties;
import java.util.concurrent.TimeUnit;
@@ -78,36 +80,31 @@ public class ExemplarSamplerConfig {
}
private void validate() {
- if (minRetentionPeriodMillis <= 0) {
- throw new IllegalArgumentException(
- minRetentionPeriodMillis + ": minRetentionPeriod must be > 0.");
- }
- if (maxRetentionPeriodMillis <= 0) {
- throw new IllegalArgumentException(
- maxRetentionPeriodMillis + ": maxRetentionPeriod must be > 0.");
- }
+ checkArgument(
+ minRetentionPeriodMillis > 0,
+ "%s: minRetentionPeriod must be > 0.",
+ minRetentionPeriodMillis);
+ checkArgument(
+ maxRetentionPeriodMillis > 0,
+ "%s: maxRetentionPeriod must be > 0.",
+ maxRetentionPeriodMillis);
if (histogramClassicUpperBounds != null) {
- if (histogramClassicUpperBounds.length == 0
- || histogramClassicUpperBounds[histogramClassicUpperBounds.length - 1]
- != Double.POSITIVE_INFINITY) {
- throw new IllegalArgumentException(
- "histogramClassicUpperBounds must contain the +Inf bucket.");
- }
- if (histogramClassicUpperBounds.length != numberOfExemplars) {
- throw new IllegalArgumentException(
- "histogramClassicUpperBounds.length must be equal to numberOfExemplars.");
- }
+ checkArgument(
+ histogramClassicUpperBounds.length != 0
+ && histogramClassicUpperBounds[histogramClassicUpperBounds.length - 1]
+ == Double.POSITIVE_INFINITY,
+ "histogramClassicUpperBounds must contain the +Inf bucket.");
+ checkArgument(
+ histogramClassicUpperBounds.length == numberOfExemplars,
+ "histogramClassicUpperBounds.length must be equal to numberOfExemplars.");
double bound = histogramClassicUpperBounds[0];
for (int i = 1; i < histogramClassicUpperBounds.length; i++) {
- if (bound >= histogramClassicUpperBounds[i]) {
- throw new IllegalArgumentException(
- "histogramClassicUpperBounds must be sorted and must not contain duplicates.");
- }
+ checkArgument(
+ bound < histogramClassicUpperBounds[i],
+ "histogramClassicUpperBounds must be sorted and must not contain duplicates.");
}
}
- if (numberOfExemplars <= 0) {
- throw new IllegalArgumentException(numberOfExemplars + ": numberOfExemplars must be > 0.");
- }
+ checkArgument(numberOfExemplars > 0, "%s: numberOfExemplars must be > 0.", numberOfExemplars);
}
private static <T> T getOrDefault(T result, T defaultValue) {
--- a/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/Buffer.java
+++ b/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/Buffer.java
@@ -17,7 +17,7 @@ import java.util.function.Supplier;
*/
class Buffer {
- private static final long bufferActiveBit = 1L << 63;
+ private static final long BUFFER_ACTIVE_BIT = 1L << 63;
private final AtomicLong observationCount = new AtomicLong(0);
private double[] observationBuffer = new double[0];
private int bufferPos = 0;
@@ -29,7 +29,7 @@ class Buffer {
boolean append(double value) {
long count = observationCount.incrementAndGet();
- if ((count & bufferActiveBit) == 0) {
+ if ((count & BUFFER_ACTIVE_BIT) == 0) {
return false; // sign bit not set -> buffer not active.
} else {
doAppend(value);
@@ -69,7 +69,7 @@ class Buffer {
runLock.lock();
try {
// Signal that the buffer is active.
- Long expectedCount = observationCount.getAndAdd(bufferActiveBit);
+ Long expectedCount = observationCount.getAndAdd(BUFFER_ACTIVE_BIT);
while (!complete.apply(expectedCount)) {
// Wait until all in-flight threads have added their observations to the histogram /
@@ -84,10 +84,10 @@ class Buffer {
int expectedBufferSize;
if (reset) {
expectedBufferSize =
- (int) ((observationCount.getAndSet(0) & ~bufferActiveBit) - expectedCount);
+ (int) ((observationCount.getAndSet(0) & ~BUFFER_ACTIVE_BIT) - expectedCount);
reset = false;
} else {
- expectedBufferSize = (int) (observationCount.addAndGet(bufferActiveBit) - expectedCount);
+ expectedBufferSize = (int) (observationCount.addAndGet(BUFFER_ACTIVE_BIT) - expectedCount);
}
appendLock.lock();
--- a/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/CKMSQuantiles.java
+++ b/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/CKMSQuantiles.java
@@ -21,6 +21,8 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
+import static com.google.common.base.Preconditions.checkArgument;
+
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
@@ -62,9 +64,7 @@ final class CKMSQuantiles {
private int bufferPos = 0;
public CKMSQuantiles(Quantile... quantiles) {
- if (quantiles.length == 0) {
- throw new IllegalArgumentException("quantiles cannot be empty");
- }
+ checkArgument(quantiles.length != 0, "quantiles cannot be empty");
this.quantiles = quantiles;
}
--- a/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/CallbackMetric.java
+++ b/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/CallbackMetric.java
@@ -1,5 +1,7 @@
package io.prometheus.metrics.core.metrics;
+import static com.google.common.base.Preconditions.checkArgument;
+
import io.prometheus.metrics.config.PrometheusProperties;
import io.prometheus.metrics.model.snapshots.Labels;
import java.util.List;
@@ -17,29 +19,22 @@ abstract class CallbackMetric extends MetricWithFixedMetadata {
protected Labels makeLabels(String... labelValues) {
if (labelNames.length == 0) {
- if (labelValues != null && labelValues.length > 0) {
- throw new IllegalArgumentException(
- "Cannot pass label values to a "
- + this.getClass().getSimpleName()
- + " that was created without label names.");
- }
+ checkArgument(
+ labelValues == null || labelValues.length <= 0,
+ "Cannot pass label values to a %s that was created without label names.",
+ this.getClass().getSimpleName());
return constLabels;
} else {
- if (labelValues == null) {
- throw new IllegalArgumentException(
- this.getClass().getSimpleName()
- + " was created with label names, "
- + "but the callback was called without label values.");
- }
- if (labelValues.length != labelNames.length) {
- throw new IllegalArgumentException(
- this.getClass().getSimpleName()
- + " was created with "
- + labelNames.length
- + " label names, but the callback was called with "
- + labelValues.length
- + " label values.");
- }
+ checkArgument(
+ labelValues != null,
+ "%s was created with label names, but the callback was called without label values.",
+ this.getClass().getSimpleName());
+ checkArgument(
+ labelValues.length == labelNames.length,
+ "%s was created with %s label names, but the callback was called with %s label values.",
+ this.getClass().getSimpleName(),
+ labelNames.length,
+ labelValues.length);
return constLabels.merge(Labels.of(labelNames, labelValues));
}
}
--- a/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/Counter.java
+++ b/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/Counter.java
@@ -1,5 +1,8 @@
package io.prometheus.metrics.core.metrics;
+import static com.google.common.base.Preconditions.checkArgument;
+
+import com.google.common.collect.ImmutableList;
import io.prometheus.metrics.config.MetricsProperties;
import io.prometheus.metrics.config.PrometheusProperties;
import io.prometheus.metrics.core.datapoints.CounterDataPoint;
@@ -9,7 +12,6 @@ import io.prometheus.metrics.model.snapshots.CounterSnapshot;
import io.prometheus.metrics.model.snapshots.Exemplar;
import io.prometheus.metrics.model.snapshots.Labels;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.DoubleAdder;
import java.util.concurrent.atomic.LongAdder;
@@ -169,18 +171,12 @@ public class Counter extends StatefulMetric<CounterDataPoint, Counter.DataPoint>
}
private void validateAndAdd(long amount) {
- if (amount < 0) {
- throw new IllegalArgumentException(
- "Negative increment " + amount + " is illegal for Counter metrics.");
- }
+ checkArgument(amount >= 0, "Negative increment %s is illegal for Counter metrics.", amount);
longValue.add(amount);
}
private void validateAndAdd(double amount) {
- if (amount < 0) {
- throw new IllegalArgumentException(
- "Negative increment " + amount + " is illegal for Counter metrics.");
- }
+ checkArgument(amount >= 0, "Negative increment %s is illegal for Counter metrics.", amount);
doubleValue.add(amount);
}
@@ -213,7 +209,7 @@ public class Counter extends StatefulMetric<CounterDataPoint, Counter.DataPoint>
public static class Builder extends StatefulMetric.Builder<Builder, Counter> {
private Builder(PrometheusProperties properties) {
- super(Collections.emptyList(), properties);
+ super(ImmutableList.of(), properties);
}
/**
--- a/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/CounterWithCallback.java
+++ b/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/CounterWithCallback.java
@@ -1,9 +1,11 @@
package io.prometheus.metrics.core.metrics;
+import static com.google.common.base.Preconditions.checkArgument;
+
+import com.google.common.collect.ImmutableList;
import io.prometheus.metrics.config.PrometheusProperties;
import io.prometheus.metrics.model.snapshots.CounterSnapshot;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
@@ -32,9 +34,7 @@ public class CounterWithCallback extends CallbackMetric {
private CounterWithCallback(Builder builder) {
super(builder);
this.callback = builder.callback;
- if (callback == null) {
- throw new IllegalArgumentException("callback cannot be null");
- }
+ checkArgument(callback != null, "callback cannot be null");
}
@Override
@@ -68,7 +68,7 @@ public class CounterWithCallback extends CallbackMetric {
}
private Builder(PrometheusProperties properties) {
- super(Collections.emptyList(), properties);
+ super(ImmutableList.of(), properties);
}
/**
--- a/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/Gauge.java
+++ b/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/Gauge.java
@@ -1,5 +1,6 @@
package io.prometheus.metrics.core.metrics;
+import com.google.common.collect.ImmutableList;
import io.prometheus.metrics.config.MetricsProperties;
import io.prometheus.metrics.config.PrometheusProperties;
import io.prometheus.metrics.core.datapoints.GaugeDataPoint;
@@ -9,7 +10,6 @@ import io.prometheus.metrics.model.snapshots.Exemplar;
import io.prometheus.metrics.model.snapshots.GaugeSnapshot;
import io.prometheus.metrics.model.snapshots.Labels;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
@@ -184,7 +184,7 @@ public class Gauge extends StatefulMetric<GaugeDataPoint, Gauge.DataPoint>
public static class Builder extends StatefulMetric.Builder<Builder, Gauge> {
private Builder(PrometheusProperties config) {
- super(Collections.emptyList(), config);
+ super(ImmutableList.of(), config);
}
@Override
--- a/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/GaugeWithCallback.java
+++ b/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/GaugeWithCallback.java
@@ -1,9 +1,11 @@
package io.prometheus.metrics.core.metrics;
+import static com.google.common.base.Preconditions.checkArgument;
+
+import com.google.common.collect.ImmutableList;
import io.prometheus.metrics.config.PrometheusProperties;
import io.prometheus.metrics.model.snapshots.GaugeSnapshot;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
@@ -37,9 +39,7 @@ public class GaugeWithCallback extends CallbackMetric {
private GaugeWithCallback(Builder builder) {
super(builder);
this.callback = builder.callback;
- if (callback == null) {
- throw new IllegalArgumentException("callback cannot be null");
- }
+ checkArgument(callback != null, "callback cannot be null");
}
@Override
@@ -72,7 +72,7 @@ public class GaugeWithCallback extends CallbackMetric {
}
private Builder(PrometheusProperties properties) {
- super(Collections.emptyList(), properties);
+ super(ImmutableList.of(), properties);
}
@Override
--- a/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/Histogram.java
+++ b/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/Histogram.java
@@ -1,5 +1,8 @@
package io.prometheus.metrics.core.metrics;
+import static com.google.common.base.Preconditions.checkArgument;
+
+import com.google.common.collect.ImmutableList;
import io.prometheus.metrics.config.ExemplarsProperties;
import io.prometheus.metrics.config.MetricsProperties;
import io.prometheus.metrics.config.PrometheusProperties;
@@ -14,7 +17,6 @@ import io.prometheus.metrics.model.snapshots.Labels;
import io.prometheus.metrics.model.snapshots.NativeHistogramBuckets;
import java.math.BigDecimal;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
@@ -372,8 +374,7 @@ public class Histogram extends StatefulMetric<DistributionDataPoint, Histogram.D
bucketIndex--;
}
int offset = (1 << -nativeSchema) - 1;
- bucketIndex = (bucketIndex + offset) >> -nativeSchema;
- return bucketIndex;
+ return (bucketIndex + offset) >> -nativeSchema;
}
}
@@ -720,7 +721,7 @@ public class Histogram extends StatefulMetric<DistributionDataPoint, Histogram.D
}
private Builder(PrometheusProperties config) {
- super(Collections.singletonList("le"), config);
+ super(ImmutableList.of("le"), config);
}
/**
@@ -728,9 +729,9 @@ public class Histogram extends StatefulMetric<DistributionDataPoint, Histogram.D
* See {@link Histogram} for more info.
*/
public Builder nativeOnly() {
- if (Boolean.TRUE.equals(classicOnly)) {
- throw new IllegalArgumentException("Cannot call nativeOnly() after calling classicOnly().");
- }
+ checkArgument(
+ !Boolean.TRUE.equals(classicOnly),
+ "Cannot call nativeOnly() after calling classicOnly().");
nativeOnly = true;
return this;
}
@@ -740,9 +741,9 @@ public class Histogram extends StatefulMetric<DistributionDataPoint, Histogram.D
* See {@link Histogram} for more info.
*/
public Builder classicOnly() {
- if (Boolean.TRUE.equals(nativeOnly)) {
- throw new IllegalArgumentException("Cannot call classicOnly() after calling nativeOnly().");
- }
+ checkArgument(
+ !Boolean.TRUE.equals(nativeOnly),
+ "Cannot call classicOnly() after calling nativeOnly().");
classicOnly = true;
return this;
}
@@ -755,9 +756,7 @@ public class Histogram extends StatefulMetric<DistributionDataPoint, Histogram.D
public Builder classicUpperBounds(double... upperBounds) {
this.classicUpperBounds = upperBounds;
for (double bound : upperBounds) {
- if (Double.isNaN(bound)) {
- throw new IllegalArgumentException("Cannot use NaN as upper bound for a histogram");
- }
+ checkArgument(!Double.isNaN(bound), "Cannot use NaN as upper bound for a histogram");
}
return this;
}
@@ -778,7 +777,7 @@ public class Histogram extends StatefulMetric<DistributionDataPoint, Histogram.D
BigDecimal s = new BigDecimal(Double.toString(start));
BigDecimal w = new BigDecimal(Double.toString(width));
for (int i = 0; i < count; i++) {
- classicUpperBounds[i] = s.add(w.multiply(new BigDecimal(i))).doubleValue();
+ classicUpperBounds[i] = s.add(w.multiply(BigDecimal.valueOf(i))).doubleValue();
}
return this;
}
@@ -867,12 +866,10 @@ public class Histogram extends StatefulMetric<DistributionDataPoint, Histogram.D
* </table>
*/
public Builder nativeInitialSchema(int nativeSchema) {
- if (nativeSchema < -4 || nativeSchema > 8) {
- throw new IllegalArgumentException(
- "Unsupported native histogram schema "
- + nativeSchema
- + ": expecting -4 <= schema <= 8.");
- }
+ checkArgument(
+ nativeSchema >= -4 && nativeSchema <= 8,
+ "Unsupported native histogram schema %s: expecting -4 <= schema <= 8.",
+ nativeSchema);
this.nativeInitialSchema = nativeSchema;
return this;
}
@@ -888,10 +885,10 @@ public class Histogram extends StatefulMetric<DistributionDataPoint, Histogram.D
* <p>Default is {@link Builder#DEFAULT_NATIVE_MAX_NUMBER_OF_BUCKETS}.
*/
public Builder nativeMaxZeroThreshold(double nativeMaxZeroThreshold) {
- if (nativeMaxZeroThreshold < 0) {
- throw new IllegalArgumentException(
- "Illegal native max zero threshold " + nativeMaxZeroThreshold + ": must be >= 0");
- }
+ checkArgument(
+ nativeMaxZeroThreshold >= 0,
+ "Illegal native max zero threshold %s: must be >= 0",
+ nativeMaxZeroThreshold);
this.nativeMaxZeroThreshold = nativeMaxZeroThreshold;
return this;
}
@@ -907,10 +904,10 @@ public class Histogram extends StatefulMetric<DistributionDataPoint, Histogram.D
* <p>Default is {@link Builder#DEFAULT_NATIVE_MIN_ZERO_THRESHOLD}.
*/
public Builder nativeMinZeroThreshold(double nativeMinZeroThreshold) {
- if (nativeMinZeroThreshold < 0) {
- throw new IllegalArgumentException(
- "Illegal native min zero threshold " + nativeMinZeroThreshold + ": must be >= 0");
- }
+ checkArgument(
+ nativeMinZeroThreshold >= 0,
+ "Illegal native min zero threshold %s: must be >= 0",
+ nativeMinZeroThreshold);
this.nativeMinZeroThreshold = nativeMinZeroThreshold;
return this;
}
@@ -940,9 +937,7 @@ public class Histogram extends StatefulMetric<DistributionDataPoint, Histogram.D
*/
public Builder nativeResetDuration(long duration, TimeUnit unit) {
// TODO: reset interval isn't tested yet
- if (duration <= 0) {
- throw new IllegalArgumentException(duration + ": value > 0 expected");
- }
+ checkArgument(duration > 0, "%s: value > 0 expected", duration);
nativeResetDurationSeconds = unit.toSeconds(duration);
return this;
}
--- a/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/Info.java
+++ b/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/Info.java
@@ -1,11 +1,13 @@
package io.prometheus.metrics.core.metrics;
+import static com.google.common.base.Preconditions.checkArgument;
+
+import com.google.common.collect.ImmutableList;
import io.prometheus.metrics.config.PrometheusProperties;
import io.prometheus.metrics.model.snapshots.InfoSnapshot;
import io.prometheus.metrics.model.snapshots.Labels;
import io.prometheus.metrics.model.snapshots.Unit;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
@@ -42,51 +44,39 @@ public class Info extends MetricWithFixedMetadata {
* target_info} where you want only one single data point.
*/
public void setLabelValues(String... labelValues) {
- if (labelValues.length != labelNames.length) {
- throw new IllegalArgumentException(
- getClass().getSimpleName()
- + " "
- + getMetadata().getName()
- + " was created with "
- + labelNames.length
- + " label names, but you called setLabelValues() with "
- + labelValues.length
- + " label values.");
- }
+ checkArgument(
+ labelValues.length == labelNames.length,
+ "%s %s was created with %s label names, but you called setLabelValues() with %s label values.",
+ getClass().getSimpleName(),
+ getMetadata().getName(),
+ labelNames.length,
+ labelValues.length);
Labels newLabels = Labels.of(labelNames, labelValues);
labels.add(newLabels);
- labels.retainAll(Collections.singletonList(newLabels));
+ labels.retainAll(ImmutableList.of(newLabels));
}
/** Create an info data point with the given label values. */
public void addLabelValues(String... labelValues) {
- if (labelValues.length != labelNames.length) {
- throw new IllegalArgumentException(
- getClass().getSimpleName()
- + " "
- + getMetadata().getName()
- + " was created with "
- + labelNames.length
- + " label names, but you called addLabelValues() with "
- + labelValues.length
- + " label values.");
- }
+ checkArgument(
+ labelValues.length == labelNames.length,
+ "%s %s was created with %s label names, but you called addLabelValues() with %s label values.",
+ getClass().getSimpleName(),
+ getMetadata().getName(),
+ labelNames.length,
+ labelValues.length);
labels.add(Labels.of(labelNames, labelValues));
}
/** Remove the data point with the specified label values. */
public void remove(String... labelValues) {
- if (labelValues.length != labelNames.length) {
- throw new IllegalArgumentException(
- getClass().getSimpleName()
- + " "
- + getMetadata().getName()
- + " was created with "
- + labelNames.length
- + " label names, but you called remove() with "
- + labelValues.length
- + " label values.");
- }
+ checkArgument(
+ labelValues.length == labelNames.length,
+ "%s %s was created with %s label names, but you called remove() with %s label values.",
+ getClass().getSimpleName(),
+ getMetadata().getName(),
+ labelNames.length,
+ labelValues.length);
Labels toBeRemoved = Labels.of(labelNames, labelValues);
labels.remove(toBeRemoved);
}
@@ -115,7 +105,7 @@ public class Info extends MetricWithFixedMetadata {
public static class Builder extends MetricWithFixedMetadata.Builder<Builder, Info> {
private Builder(PrometheusProperties config) {
- super(Collections.emptyList(), config);
+ super(ImmutableList.of(), config);
}
/**
--- a/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/Metric.java
+++ b/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/Metric.java
@@ -1,5 +1,7 @@
package io.prometheus.metrics.core.metrics;
+import static com.google.common.base.Preconditions.checkArgument;
+
import io.prometheus.metrics.config.PrometheusProperties;
import io.prometheus.metrics.model.registry.Collector;
import io.prometheus.metrics.model.registry.PrometheusRegistry;
@@ -40,10 +42,10 @@ public abstract class Metric implements Collector {
// https://prometheus.io/docs/instrumenting/writing_exporters/#target-labels-not-static-scraped-labels
public B constLabels(Labels constLabels) {
for (Label label : constLabels) { // NPE if constLabels is null
- if (illegalLabelNames.contains(label.getName())) {
- throw new IllegalArgumentException(
- label.getName() + ": illegal label name for this metric type");
- }
+ checkArgument(
+ !illegalLabelNames.contains(label.getName()),
+ "%s: illegal label name for this metric type",
+ label.getName());
}
this.constLabels = constLabels;
return self();
--- a/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/MetricWithFixedMetadata.java
+++ b/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/MetricWithFixedMetadata.java
@@ -1,5 +1,7 @@
package io.prometheus.metrics.core.metrics;
+import static com.google.common.base.Preconditions.checkArgument;
+
import io.prometheus.metrics.config.PrometheusProperties;
import io.prometheus.metrics.model.snapshots.Labels;
import io.prometheus.metrics.model.snapshots.MetricMetadata;
@@ -58,9 +60,7 @@ public abstract class MetricWithFixedMetadata extends Metric {
public B name(String name) {
String error = PrometheusNaming.validateMetricName(name);
- if (error != null) {
- throw new IllegalArgumentException("'" + name + "': Illegal metric name: " + error);
- }
+ checkArgument(error == null, "'%s': Illegal metric name: %s", name, error);
this.name = name;
return self();
}
@@ -77,16 +77,13 @@ public abstract class MetricWithFixedMetadata extends Metric {
public B labelNames(String... labelNames) {
for (String labelName : labelNames) {
- if (!PrometheusNaming.isValidLabelName(labelName)) {
- throw new IllegalArgumentException(labelName + ": illegal label name");
- }
- if (illegalLabelNames.contains(labelName)) {
- throw new IllegalArgumentException(
- labelName + ": illegal label name for this metric type");
- }
- if (constLabels.contains(labelName)) {
- throw new IllegalArgumentException(labelName + ": duplicate label name");
- }
+ checkArgument(
+ PrometheusNaming.isValidLabelName(labelName), "%s: illegal label name", labelName);
+ checkArgument(
+ !illegalLabelNames.contains(labelName),
+ "%s: illegal label name for this metric type",
+ labelName);
+ checkArgument(!constLabels.contains(labelName), "%s: duplicate label name", labelName);
}
this.labelNames = labelNames;
return self();
--- a/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/StateSet.java
+++ b/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/StateSet.java
@@ -1,16 +1,18 @@
package io.prometheus.metrics.core.metrics;
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkState;
import static io.prometheus.metrics.model.snapshots.PrometheusNaming.prometheusName;
+import com.google.common.collect.ImmutableList;
import io.prometheus.metrics.config.MetricsProperties;
import io.prometheus.metrics.config.PrometheusProperties;
import io.prometheus.metrics.core.datapoints.StateSetDataPoint;
import io.prometheus.metrics.model.snapshots.Labels;
import io.prometheus.metrics.model.snapshots.StateSetSnapshot;
import java.util.ArrayList;
-import java.util.Collections;
+import java.util.Arrays;
import java.util.List;
-import java.util.stream.Stream;
/**
* StateSet metric. Example:
@@ -62,12 +64,10 @@ public class StateSet extends StatefulMetric<StateSetDataPoint, StateSet.DataPoi
exemplarsEnabled = getConfigProperty(properties, MetricsProperties::getExemplarsEnabled);
this.names = builder.names; // builder.names is already a validated copy
for (String name : names) {
- if (this.getMetadata().getPrometheusName().equals(prometheusName(name))) {
- throw new IllegalArgumentException(
- "Label name "
- + name
- + " is illegal (can't use the metric name as label name in state set metrics)");
- }
+ checkArgument(
+ !this.getMetadata().getPrometheusName().equals(prometheusName(name)),
+ "Label name %s is illegal (can't use the metric name as label name in state set metrics)",
+ name);
}
}
@@ -147,29 +147,25 @@ public class StateSet extends StatefulMetric<StateSetDataPoint, StateSet.DataPoi
private String[] names;
private Builder(PrometheusProperties config) {
- super(Collections.emptyList(), config);
+ super(ImmutableList.of(), config);
}
/** Declare the states that should be represented by this StateSet. */
public Builder states(Class<? extends Enum<?>> enumClass) {
return states(
- Stream.of(enumClass.getEnumConstants()).map(Enum::toString).toArray(String[]::new));
+ Arrays.stream(enumClass.getEnumConstants()).map(Enum::toString).toArray(String[]::new));
}
/** Declare the states that should be represented by this StateSet. */
public Builder states(String... stateNames) {
- if (stateNames.length == 0) {
- throw new IllegalArgumentException("states cannot be empty");
- }
- this.names = Stream.of(stateNames).distinct().sorted().toArray(String[]::new);
+ checkArgument(stateNames.length != 0, "states cannot be empty");
+ this.names = Arrays.stream(stateNames).distinct().sorted().toArray(String[]::new);
return this;
}
@Override
public StateSet build() {
- if (names == null) {
- throw new IllegalStateException("State names are required when building a StateSet.");
- }
+ checkState(names != null, "State names are required when building a StateSet.");
return new StateSet(this, properties);
}
--- a/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/Summary.java
+++ b/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/Summary.java
@@ -1,5 +1,9 @@
package io.prometheus.metrics.core.metrics;
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Collections.unmodifiableList;
+
+import com.google.common.collect.ImmutableList;
import io.prometheus.metrics.config.MetricsProperties;
import io.prometheus.metrics.config.PrometheusProperties;
import io.prometheus.metrics.core.datapoints.DistributionDataPoint;
@@ -11,7 +15,6 @@ import io.prometheus.metrics.model.snapshots.Quantile;
import io.prometheus.metrics.model.snapshots.Quantiles;
import io.prometheus.metrics.model.snapshots.SummarySnapshot;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.DoubleAdder;
@@ -51,7 +54,7 @@ public class Summary extends StatefulMetric<DistributionDataPoint, Summary.DataP
super(builder);
MetricsProperties[] properties = getMetricProperties(builder, prometheusProperties);
this.exemplarsEnabled = getConfigProperty(properties, MetricsProperties::getExemplarsEnabled);
- this.quantiles = Collections.unmodifiableList(makeQuantiles(properties));
+ this.quantiles = unmodifiableList(makeQuantiles(properties));
this.maxAgeSeconds = getConfigProperty(properties, MetricsProperties::getSummaryMaxAgeSeconds);
this.ageBuckets =
getConfigProperty(properties, MetricsProperties::getSummaryNumberOfAgeBuckets);
@@ -66,7 +69,7 @@ public class Summary extends StatefulMetric<DistributionDataPoint, Summary.DataP
getConfigProperty(properties, MetricsProperties::getSummaryQuantileErrors);
if (quantiles != null) {
for (int i = 0; i < quantiles.size(); i++) {
- if (quantileErrors.size() > 0) {
+ if (!quantileErrors.isEmpty()) {
result.add(new CKMSQuantiles.Quantile(quantiles.get(i), quantileErrors.get(i)));
} else {
result.add(
@@ -122,7 +125,7 @@ public class Summary extends StatefulMetric<DistributionDataPoint, Summary.DataP
private final long createdTimeMillis = System.currentTimeMillis();
private DataPoint() {
- if (quantiles.size() > 0) {
+ if (!quantiles.isEmpty()) {
CKMSQuantiles.Quantile[] quantilesArray = quantiles.toArray(new CKMSQuantiles.Quantile[0]);
quantileValues =
new SlidingWindow<>(
@@ -228,7 +231,7 @@ public class Summary extends StatefulMetric<DistributionDataPoint, Summary.DataP
private Integer ageBuckets;
private Builder(PrometheusProperties properties) {
- super(Collections.singletonList("quantile"), properties);
+ super(ImmutableList.of("quantile"), properties);
}
private static double defaultError(double quantile) {
@@ -276,14 +279,14 @@ public class Summary extends StatefulMetric<DistributionDataPoint, Summary.DataP
* </ul>
*/
public Builder quantile(double quantile, double error) {
- if (quantile < 0.0 || quantile > 1.0) {
- throw new IllegalArgumentException(
- "Quantile " + quantile + " invalid: Expected number between 0.0 and 1.0.");
- }
- if (error < 0.0 || error > 1.0) {
- throw new IllegalArgumentException(
- "Error " + error + " invalid: Expected number between 0.0 and 1.0.");
- }
+ checkArgument(
+ quantile >= 0.0 && quantile <= 1.0,
+ "Quantile %s invalid: Expected number between 0.0 and 1.0.",
+ quantile);
+ checkArgument(
+ error >= 0.0 && error <= 1.0,
+ "Error %s invalid: Expected number between 0.0 and 1.0.",
+ error);
quantiles.add(new CKMSQuantiles.Quantile(quantile, error));
return this;
}
@@ -293,9 +296,7 @@ public class Summary extends StatefulMetric<DistributionDataPoint, Summary.DataP
* time window. Default is {@link #DEFAULT_MAX_AGE_SECONDS}.
*/
public Builder maxAgeSeconds(long maxAgeSeconds) {
- if (maxAgeSeconds <= 0) {
- throw new IllegalArgumentException("maxAgeSeconds cannot be " + maxAgeSeconds);
- }
+ checkArgument(maxAgeSeconds > 0, "maxAgeSeconds cannot be %s", maxAgeSeconds);
this.maxAgeSeconds = maxAgeSeconds;
return this;
}
@@ -307,9 +308,7 @@ public class Summary extends StatefulMetric<DistributionDataPoint, Summary.DataP
* #DEFAULT_NUMBER_OF_AGE_BUCKETS}.
*/
public Builder numberOfAgeBuckets(int ageBuckets) {
- if (ageBuckets <= 0) {
- throw new IllegalArgumentException("ageBuckets cannot be " + ageBuckets);
- }
+ checkArgument(ageBuckets > 0, "ageBuckets cannot be %s", ageBuckets);
this.ageBuckets = ageBuckets;
return this;
}
--- a/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/SummaryWithCallback.java
+++ b/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/SummaryWithCallback.java
@@ -1,11 +1,13 @@
package io.prometheus.metrics.core.metrics;
+import static com.google.common.base.Preconditions.checkArgument;
+
+import com.google.common.collect.ImmutableList;
import io.prometheus.metrics.config.PrometheusProperties;
import io.prometheus.metrics.model.snapshots.Exemplars;
import io.prometheus.metrics.model.snapshots.Quantiles;
import io.prometheus.metrics.model.snapshots.SummarySnapshot;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
@@ -45,9 +47,7 @@ public class SummaryWithCallback extends CallbackMetric {
private SummaryWithCallback(Builder builder) {
super(builder);
this.callback = builder.callback;
- if (callback == null) {
- throw new IllegalArgumentException("callback cannot be null");
- }
+ checkArgument(callback != null, "callback cannot be null");
}
@Override
@@ -81,7 +81,7 @@ public class SummaryWithCallback extends CallbackMetric {
}
private Builder(PrometheusProperties properties) {
- super(Collections.singletonList("quantile"), properties);
+ super(ImmutableList.of("quantile"), properties);
}
@Override
--- a/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/util/Scheduler.java
+++ b/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/util/Scheduler.java
@@ -22,11 +22,11 @@ public class Scheduler {
}
}
- private static final ScheduledExecutorService executor =
+ private static final ScheduledExecutorService EXECUTOR =
Executors.newSingleThreadScheduledExecutor(new DaemonThreadFactory());
public static ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
- return executor.schedule(command, delay, unit);
+ return EXECUTOR.schedule(command, delay, unit);
}
/** For unit test. Wait until the executor Thread is running. */
--- a/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/datapoints/CounterDataPointTest.java
+++ b/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/datapoints/CounterDataPointTest.java
@@ -5,7 +5,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import io.prometheus.metrics.model.snapshots.Labels;
import org.junit.jupiter.api.Test;
-class CounterDataPointTest {
+final class CounterDataPointTest {
private double value = 0;
@@ -32,7 +32,7 @@ class CounterDataPointTest {
}
};
counterDataPoint.inc(1);
- assertThat(value).isOne();
+ assertThat(value).isEqualTo(1);
counterDataPoint.incWithExemplar(1, null);
assertThat(value).isEqualTo(2);
}
--- a/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/datapoints/TimerApiTest.java
+++ b/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/datapoints/TimerApiTest.java
@@ -4,7 +4,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
-class TimerApiTest {
+final class TimerApiTest {
private double time = 0;
--- a/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/exemplars/ExemplarSamplerTest.java
+++ b/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/exemplars/ExemplarSamplerTest.java
@@ -11,7 +11,7 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-class ExemplarSamplerTest {
+final class ExemplarSamplerTest {
private final int tick = 10; // Time step in milliseconds. Make this larger if the test is flaky.
private final int sampleInterval = 10 * tick; // do not change this
@@ -52,24 +52,24 @@ class ExemplarSamplerTest {
}
@Test
- public void testCustomExemplarsBuckets() throws Exception {
+ void customExemplarsBuckets() throws Exception {
// TODO
}
private io.prometheus.metrics.tracer.common.SpanContext origContext;
@BeforeEach
- public void setUp() {
+ void setUp() {
origContext = SpanContextSupplier.getSpanContext();
}
@AfterEach
- public void tearDown() {
+ void tearDown() {
SpanContextSupplier.setSpanContext(origContext);
}
@Test
- public void testIsSampled() throws Exception {
+ void isSampled() throws Exception {
SpanContext context = new SpanContext();
context.isSampled = false;
ExemplarSampler sampler = new ExemplarSampler(makeConfig(), context);
@@ -79,7 +79,7 @@ class ExemplarSamplerTest {
}
@Test
- public void testDefaultConfigHasFourExemplars() throws Exception {
+ void defaultConfigHasFourExemplars() throws Exception {
ExemplarSampler sampler = new ExemplarSampler(makeConfig(), new SpanContext());
Thread.sleep(tick); // t = 1 tick
sampler.observe(0.3);
@@ -96,7 +96,7 @@ class ExemplarSamplerTest {
}
@Test
- public void testEmptyBuckets() throws Exception {
+ void emptyBuckets() throws Exception {
ExemplarSampler sampler =
new ExemplarSampler(makeConfig(Double.POSITIVE_INFINITY), new SpanContext());
Thread.sleep(tick); // t = 1 tick
@@ -108,7 +108,7 @@ class ExemplarSamplerTest {
}
@Test
- public void testDefaultExemplarsBuckets() throws Exception {
+ void defaultExemplarsBuckets() throws Exception {
ExemplarSampler sampler =
new ExemplarSampler(
makeConfig(0.2, 0.4, 0.6, 0.8, 1.0, Double.POSITIVE_INFINITY), new SpanContext());
@@ -136,12 +136,12 @@ class ExemplarSamplerTest {
}
@Test
- public void testCustomExemplarsNoBuckets() throws Exception {
+ void customExemplarsNoBuckets() throws Exception {
// TODO
}
@Test
- public void testDefaultExemplarsNoBuckets() throws Exception {
+ void defaultExemplarsNoBuckets() throws Exception {
ExemplarSampler sampler = new ExemplarSampler(makeConfig(), new SpanContext());
Scheduler.awaitInitialization();
Thread.sleep(tick); // t = 1 tick
@@ -192,7 +192,7 @@ class ExemplarSamplerTest {
break;
}
}
- assertThat(found).as(value + " not found").isTrue();
+ assertThat(found).as("%s not found", value).isTrue();
}
}
--- a/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/exemplars/SpanContextSupplierTest.java
+++ b/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/exemplars/SpanContextSupplierTest.java
@@ -12,7 +12,7 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-class SpanContextSupplierTest {
+final class SpanContextSupplierTest {
public SpanContext makeSpanContext(String traceId, String spanId) {
@@ -51,12 +51,12 @@ class SpanContextSupplierTest {
);
@BeforeEach
- public void setUp() {
+ void setUp() {
origSpanContext = SpanContextSupplier.getSpanContext();
}
@AfterEach
- public void tearDown() {
+ void tearDown() {
SpanContextSupplier.setSpanContext(origSpanContext);
}
@@ -66,7 +66,7 @@ class SpanContextSupplierTest {
* SpanContextSupplier}.
*/
@Test
- public void testConstructorInjection() {
+ void constructorInjection() {
ExemplarsProperties properties = ExemplarsProperties.builder().build();
ExemplarSamplerConfig config = new ExemplarSamplerConfig(properties, 1);
ExemplarSampler exemplarSampler = new ExemplarSampler(config, spanContextA);
@@ -74,7 +74,7 @@ class SpanContextSupplierTest {
SpanContextSupplier.setSpanContext(spanContextB);
exemplarSampler.observe(1.0);
Exemplars exemplars = exemplarSampler.collect();
- assertThat(exemplars.size()).isOne();
+ assertThat(exemplars.size()).isEqualTo(1);
Exemplar exemplar = exemplars.get(0);
assertThat(exemplar.getLabels().get(TRACE_ID)).isEqualTo("A");
}
@@ -86,13 +86,13 @@ class SpanContextSupplierTest {
* ExemplarSampler}).
*/
@Test
- public void testUpdateSpanContext() throws InterruptedException {
+ void updateSpanContext() throws InterruptedException {
ExemplarSampler exemplarSampler = new ExemplarSampler(config);
SpanContextSupplier.setSpanContext(spanContextB);
exemplarSampler.observe(1.0);
Exemplars exemplars = exemplarSampler.collect();
- assertThat(exemplars.size()).isOne();
+ assertThat(exemplars.size()).isEqualTo(1);
Exemplar exemplar = exemplars.get(0);
assertThat(exemplar.getLabels().get(TRACE_ID)).isEqualTo("B");
@@ -101,7 +101,7 @@ class SpanContextSupplierTest {
SpanContextSupplier.setSpanContext(spanContextA);
exemplarSampler.observe(1.0);
exemplars = exemplarSampler.collect();
- assertThat(exemplars.size()).isOne();
+ assertThat(exemplars.size()).isEqualTo(1);
exemplar = exemplars.get(0);
assertThat(exemplar.getLabels().get(TRACE_ID)).isEqualTo("A");
}
--- a/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/CKMSQuantilesTest.java
+++ b/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/CKMSQuantilesTest.java
@@ -11,7 +11,7 @@ import org.apache.commons.math3.random.JDKRandomGenerator;
import org.apache.commons.math3.random.RandomGenerator;
import org.junit.jupiter.api.Test;
-class CKMSQuantilesTest {
+final class CKMSQuantilesTest {
private final Quantile qMin = new Quantile(0.0, 0.00);
private final Quantile q50 = new Quantile(0.5, 0.01);
@@ -20,13 +20,13 @@ class CKMSQuantilesTest {
private final Quantile qMax = new Quantile(1.0, 0.00);
@Test
- public void testGetOnEmptyValues() {
+ void getOnEmptyValues() {
CKMSQuantiles ckms = new CKMSQuantiles(q50, q95, q99);
assertThat(Double.isNaN(ckms.get(q95.quantile))).isTrue();
}
@Test
- public void testGet() {
+ void get() {
Random random = new Random(0);
CKMSQuantiles ckms = new CKMSQuantiles(q50, q95, q99);
List<Double> input = shuffledValues(100, random);
@@ -37,7 +37,7 @@ class CKMSQuantilesTest {
}
@Test
- public void testBatchInsert() {
+ void batchInsert() {
Random random = new Random(1);
testInsertBatch(1, 1, 100, random);
testInsertBatch(1, 10, 100, random);
@@ -87,7 +87,7 @@ class CKMSQuantilesTest {
}
@Test
- public void testGetWithAMillionElements() {
+ void getWithAMillionElements() {
Random random = new Random(2);
List<Double> input = shuffledValues(1000 * 1000, random);
CKMSQuantiles ckms = new CKMSQuantiles(q50, q95, q99);
@@ -99,7 +99,7 @@ class CKMSQuantilesTest {
}
@Test
- public void testMin() {
+ void min() {
Random random = new Random(3);
List<Double> input = shuffledValues(1000, random);
CKMSQuantiles ckms = new CKMSQuantiles(qMin);
@@ -112,7 +112,7 @@ class CKMSQuantilesTest {
}
@Test
- public void testMax() {
+ void max() {
Random random = new Random(4);
List<Double> input = shuffledValues(1000, random);
CKMSQuantiles ckms = new CKMSQuantiles(qMax);
@@ -125,7 +125,7 @@ class CKMSQuantilesTest {
}
@Test
- public void testMinMax() {
+ void minMax() {
Random random = new Random(5);
List<Double> input = shuffledValues(1000, random);
CKMSQuantiles ckms = new CKMSQuantiles(qMin, qMax);
@@ -138,7 +138,7 @@ class CKMSQuantilesTest {
}
@Test
- public void testMinAndOthers() {
+ void minAndOthers() {
Random random = new Random(6);
List<Double> input = shuffledValues(1000, random);
CKMSQuantiles ckms = new CKMSQuantiles(q95, qMin);
@@ -150,7 +150,7 @@ class CKMSQuantilesTest {
}
@Test
- public void testMaxAndOthers() {
+ void maxAndOthers() {
Random random = new Random(7);
List<Double> input = shuffledValues(10000, random);
CKMSQuantiles ckms = new CKMSQuantiles(q50, q95, qMax);
@@ -162,7 +162,7 @@ class CKMSQuantilesTest {
}
@Test
- public void testMinMaxAndOthers() {
+ void minMaxAndOthers() {
Random random = new Random(8);
List<Double> input = shuffledValues(10000, random);
CKMSQuantiles ckms = new CKMSQuantiles(qMin, q50, q95, q99, qMax);
@@ -174,7 +174,7 @@ class CKMSQuantilesTest {
}
@Test
- public void testExactQuantile() {
+ void exactQuantile() {
Random random = new Random(9);
List<Double> input = shuffledValues(10000, random);
CKMSQuantiles ckms = new CKMSQuantiles(new Quantile(0.95, 0));
@@ -187,7 +187,7 @@ class CKMSQuantilesTest {
}
@Test
- public void testExactAndOthers() {
+ void exactAndOthers() {
Random random = new Random(10);
List<Double> input = shuffledValues(10000, random);
CKMSQuantiles ckms = new CKMSQuantiles(q50, new Quantile(0.95, 0), q99);
@@ -200,7 +200,7 @@ class CKMSQuantilesTest {
}
@Test
- public void testExactAndMin() {
+ void exactAndMin() {
Random random = new Random(11);
List<Double> input = shuffledValues(10000, random);
CKMSQuantiles ckms = new CKMSQuantiles(qMin, q50, new Quantile(0.95, 0));
@@ -213,7 +213,7 @@ class CKMSQuantilesTest {
}
@Test
- public void testMaxEpsilon() {
+ void maxEpsilon() {
Random random = new Random(12);
List<Double> input = shuffledValues(10000, random);
// epsilon == 1 basically gives you random results, but it should still not throw an exception.
@@ -225,7 +225,7 @@ class CKMSQuantilesTest {
}
@Test
- public void testGetGaussian() {
+ void getGaussian() {
RandomGenerator rand = new JDKRandomGenerator();
rand.setSeed(0);
@@ -284,20 +284,20 @@ class CKMSQuantilesTest {
}
@Test
- public void testIllegalArgumentException() {
+ void illegalArgumentException() {
try {
new Quantile(-1, 0);
} catch (IllegalArgumentException e) {
assertThat(e.getMessage()).isEqualTo("Quantile must be between 0 and 1");
} catch (Exception e) {
- fail("Wrong exception thrown" + e);
+ fail("Wrong exception thrown%s", e);
}
try {
new Quantile(0.95, 2);
} catch (IllegalArgumentException e) {
assertThat(e.getMessage()).isEqualTo("Epsilon must be between 0 and 1");
} catch (Exception e) {
- fail("Wrong exception thrown" + e);
+ fail("Wrong exception thrown%s", e);
}
}
--- a/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/CallbackMetricTest.java
+++ b/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/CallbackMetricTest.java
@@ -1,15 +1,14 @@
package io.prometheus.metrics.core.metrics;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import org.junit.jupiter.api.Test;
-class CallbackMetricTest {
+final class CallbackMetricTest {
@Test
void makeLabels() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(
+ assertThatThrownBy(
() ->
CounterWithCallback.builder()
.name("c")
@@ -17,11 +16,11 @@ class CallbackMetricTest {
.labelNames("label1", "label2")
.build()
.makeLabels("foo"))
- .withMessage(
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage(
"CounterWithCallback was created with 2 label names, but the callback was called with 1 label values.");
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(
+ assertThatThrownBy(
() ->
CounterWithCallback.builder()
.name("c")
@@ -29,18 +28,19 @@ class CallbackMetricTest {
.labelNames("label1", "label2")
.build()
.makeLabels((String[]) null))
- .withMessage(
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage(
"CounterWithCallback was created with label names, but the callback was called without label values.");
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(
+ assertThatThrownBy(
() ->
CounterWithCallback.builder()
.name("c")
.callback(callback -> {})
.build()
.makeLabels("foo"))
- .withMessage(
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage(
"Cannot pass label values to a CounterWithCallback that was created without label names.");
}
}
--- a/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/CounterTest.java
+++ b/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/CounterTest.java
@@ -2,7 +2,7 @@ package io.prometheus.metrics.core.metrics;
import static io.prometheus.metrics.core.metrics.TestUtil.assertExemplarEquals;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.data.Offset.offset;
import io.prometheus.metrics.core.exemplars.ExemplarSamplerConfigTestUtil;
@@ -22,28 +22,29 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-class CounterTest {
+final class CounterTest {
private Counter noLabels;
private Counter labels;
- private static final long exemplarSampleIntervalMillis = 10;
- private static final long exemplarMinAgeMillis = 100;
+ private static final long EXEMPLAR_SAMPLE_INTERVAL_MILLIS = 10;
+ private static final long EXEMPLAR_MIN_AGE_MILLIS = 100;
private SpanContext origSpanContext;
@BeforeEach
- public void setUp() throws NoSuchFieldException, IllegalAccessException {
+ void setUp() throws NoSuchFieldException, IllegalAccessException {
noLabels = Counter.builder().name("nolabels").build();
labels =
Counter.builder().name("labels").help("help").unit(Unit.SECONDS).labelNames("l").build();
origSpanContext = SpanContextSupplier.getSpanContext();
- ExemplarSamplerConfigTestUtil.setSampleIntervalMillis(noLabels, exemplarSampleIntervalMillis);
- ExemplarSamplerConfigTestUtil.setMinRetentionPeriodMillis(noLabels, exemplarMinAgeMillis);
- ExemplarSamplerConfigTestUtil.setSampleIntervalMillis(labels, exemplarSampleIntervalMillis);
- ExemplarSamplerConfigTestUtil.setMinRetentionPeriodMillis(labels, exemplarMinAgeMillis);
+ ExemplarSamplerConfigTestUtil.setSampleIntervalMillis(
+ noLabels, EXEMPLAR_SAMPLE_INTERVAL_MILLIS);
+ ExemplarSamplerConfigTestUtil.setMinRetentionPeriodMillis(noLabels, EXEMPLAR_MIN_AGE_MILLIS);
+ ExemplarSamplerConfigTestUtil.setSampleIntervalMillis(labels, EXEMPLAR_SAMPLE_INTERVAL_MILLIS);
+ ExemplarSamplerConfigTestUtil.setMinRetentionPeriodMillis(labels, EXEMPLAR_MIN_AGE_MILLIS);
}
@AfterEach
- public void tearDown() {
+ void tearDown() {
SpanContextSupplier.setSpanContext(origSpanContext);
}
@@ -66,7 +67,7 @@ class CounterTest {
}
@Test
- public void testIncrement() {
+ void increment() {
noLabels.inc();
assertThat(getValue(noLabels)).isCloseTo(1.0, offset(.001));
noLabels.inc(2);
@@ -78,23 +79,23 @@ class CounterTest {
}
@Test
- public void testNegativeIncrementFails() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> noLabels.inc(-1))
- .withMessage("Negative increment -1 is illegal for Counter metrics.");
+ void negativeIncrementFails() {
+ assertThatThrownBy(() -> noLabels.inc(-1))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Negative increment -1 is illegal for Counter metrics.");
}
@Test
- public void testEmptyCountersHaveNoLabels() {
- assertThat(getNumberOfLabels(noLabels)).isOne();
- assertThat(getNumberOfLabels(labels)).isZero();
+ void emptyCountersHaveNoLabels() {
+ assertThat(getNumberOfLabels(noLabels)).isEqualTo(1);
+ assertThat(getNumberOfLabels(labels)).isEqualTo(0);
}
@Test
- public void testLabels() {
- assertThat(getNumberOfLabels(labels)).isZero();
+ void labels() {
+ assertThat(getNumberOfLabels(labels)).isEqualTo(0);
labels.labelValues("a").inc();
- assertThat(getNumberOfLabels(labels)).isOne();
+ assertThat(getNumberOfLabels(labels)).isEqualTo(1);
assertThat(getValue(labels, "l", "a")).isCloseTo(1.0, offset(.001));
labels.labelValues("b").inc(3);
assertThat(getNumberOfLabels(labels)).isEqualTo(2);
@@ -103,7 +104,7 @@ class CounterTest {
}
@Test
- public void testTotalStrippedFromName() {
+ void totalStrippedFromName() {
for (String name :
new String[] {
"my_counter_total", "my.counter.total",
@@ -121,7 +122,7 @@ class CounterTest {
}
@Test
- public void testSnapshotComplete() {
+ void snapshotComplete() {
long before = System.currentTimeMillis();
Counter counter =
Counter.builder()
@@ -176,16 +177,16 @@ class CounterTest {
}
@Test
- public void testIncWithExemplar() throws Exception {
+ void testIncWithExemplar() throws Exception {
noLabels.incWithExemplar(Labels.of("key", "value"));
assertExemplar(noLabels, 1.0, "key", "value");
- Thread.sleep(exemplarMinAgeMillis + 2 * exemplarSampleIntervalMillis);
+ Thread.sleep(EXEMPLAR_MIN_AGE_MILLIS + 2 * EXEMPLAR_SAMPLE_INTERVAL_MILLIS);
noLabels.incWithExemplar(Labels.EMPTY);
assertExemplar(noLabels, 1.0);
- Thread.sleep(exemplarMinAgeMillis + 2 * exemplarSampleIntervalMillis);
+ Thread.sleep(EXEMPLAR_MIN_AGE_MILLIS + 2 * EXEMPLAR_SAMPLE_INTERVAL_MILLIS);
noLabels.incWithExemplar(3, Labels.of("key1", "value1", "key2", "value2"));
assertExemplar(noLabels, 3, "key1", "value1", "key2", "value2");
@@ -198,7 +199,7 @@ class CounterTest {
}
@Test
- public void testExemplarSampler() throws Exception {
+ void exemplarSampler() throws Exception {
Exemplar exemplar1 = Exemplar.builder().value(2.0).traceId("abc").spanId("123").build();
Exemplar exemplar2 = Exemplar.builder().value(1.0).traceId("def").spanId("456").build();
Exemplar exemplar3 = Exemplar.builder().value(1.0).traceId("123").spanId("abc").build();
@@ -258,34 +259,34 @@ class CounterTest {
Counter counter = Counter.builder().name("count_total").build();
SpanContextSupplier.setSpanContext(spanContext);
- ExemplarSamplerConfigTestUtil.setMinRetentionPeriodMillis(counter, exemplarMinAgeMillis);
- ExemplarSamplerConfigTestUtil.setSampleIntervalMillis(counter, exemplarSampleIntervalMillis);
+ ExemplarSamplerConfigTestUtil.setMinRetentionPeriodMillis(counter, EXEMPLAR_MIN_AGE_MILLIS);
+ ExemplarSamplerConfigTestUtil.setSampleIntervalMillis(counter, EXEMPLAR_SAMPLE_INTERVAL_MILLIS);
counter.inc(2.0);
assertExemplarEquals(exemplar1, getData(counter).getExemplar());
- Thread.sleep(2 * exemplarSampleIntervalMillis);
+ Thread.sleep(2 * EXEMPLAR_SAMPLE_INTERVAL_MILLIS);
counter.inc(
3.0); // min age not reached -> keep the previous exemplar, exemplar sampler not called
assertExemplarEquals(exemplar1, getData(counter).getExemplar());
- Thread.sleep(exemplarMinAgeMillis + 2 * exemplarSampleIntervalMillis);
+ Thread.sleep(EXEMPLAR_MIN_AGE_MILLIS + 2 * EXEMPLAR_SAMPLE_INTERVAL_MILLIS);
counter.inc(2.0); // 2nd call: isSampled() returns false -> not sampled
assertExemplarEquals(exemplar1, getData(counter).getExemplar());
- Thread.sleep(2 * exemplarSampleIntervalMillis);
+ Thread.sleep(2 * EXEMPLAR_SAMPLE_INTERVAL_MILLIS);
counter.inc(1.0); // sampled
assertExemplarEquals(exemplar2, getData(counter).getExemplar());
- Thread.sleep(exemplarMinAgeMillis + 2 * exemplarSampleIntervalMillis);
+ Thread.sleep(EXEMPLAR_MIN_AGE_MILLIS + 2 * EXEMPLAR_SAMPLE_INTERVAL_MILLIS);
counter.inc(1.0); // sampled
assertExemplarEquals(exemplar3, getData(counter).getExemplar());
- Thread.sleep(2 * exemplarSampleIntervalMillis);
+ Thread.sleep(2 * EXEMPLAR_SAMPLE_INTERVAL_MILLIS);
counter.incWithExemplar(
Labels.of(
@@ -326,7 +327,7 @@ class CounterTest {
}
@Test
- public void testExemplarSamplerDisabled() {
+ void exemplarSamplerDisabled() {
Counter counter =
Counter.builder()
// .withExemplarSampler((inc, prev) -> {throw new RuntimeException("unexpected call to
@@ -341,26 +342,26 @@ class CounterTest {
}
@Test
- public void testConstLabelsFirst() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(
+ void constLabelsFirst() {
+ assertThatThrownBy(
() ->
Counter.builder()
.name("test_total")
.constLabels(Labels.of("const_a", "const_b"))
.labelNames("const.a")
- .build());
+ .build())
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testConstLabelsSecond() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(
+ void constLabelsSecond() {
+ assertThatThrownBy(
() ->
Counter.builder()
.name("test_total")
.labelNames("const.a")
.constLabels(Labels.of("const_a", "const_b"))
- .build());
+ .build())
+ .isInstanceOf(IllegalArgumentException.class);
}
}
--- a/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/CounterWithCallbackTest.java
+++ b/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/CounterWithCallbackTest.java
@@ -1,7 +1,7 @@
package io.prometheus.metrics.core.metrics;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.data.Offset.offset;
import io.prometheus.metrics.model.snapshots.CounterSnapshot;
@@ -10,10 +10,10 @@ import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test;
-class CounterWithCallbackTest {
+final class CounterWithCallbackTest {
@Test
- public void testCounter() {
+ void counter() {
final AtomicInteger value = new AtomicInteger(1);
List<String> labelValues = Arrays.asList("v1", "v2");
CounterWithCallback counter =
@@ -25,22 +25,22 @@ class CounterWithCallbackTest {
.build();
CounterSnapshot snapshot = counter.collect();
- assertThat(snapshot.getDataPoints().size()).isOne();
+ assertThat(snapshot.getDataPoints()).hasSize(1);
CounterSnapshot.CounterDataPointSnapshot datapoint = snapshot.getDataPoints().get(0);
assertThat(datapoint.getValue()).isCloseTo(value.doubleValue(), offset(0.1));
assertThat(datapoint.getLabels().size()).isEqualTo(labelValues.size());
value.incrementAndGet();
snapshot = counter.collect();
- assertThat(snapshot.getDataPoints().size()).isOne();
+ assertThat(snapshot.getDataPoints()).hasSize(1);
datapoint = snapshot.getDataPoints().get(0);
assertThat(datapoint.getValue()).isCloseTo(value.doubleValue(), offset(0.1));
}
@Test
- public void testCounterNoCallback() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(
- () -> CounterWithCallback.builder().name("counter").labelNames("l1", "l2").build());
+ void counterNoCallback() {
+ assertThatThrownBy(
+ () -> CounterWithCallback.builder().name("counter").labelNames("l1", "l2").build())
+ .isInstanceOf(IllegalArgumentException.class);
}
}
--- a/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/GaugeTest.java
+++ b/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/GaugeTest.java
@@ -15,24 +15,24 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-class GaugeTest {
+final class GaugeTest {
- private static final long exemplarSampleIntervalMillis = 10;
- private static final long exemplarMinAgeMillis = 100;
+ private static final long EXEMPLAR_SAMPLE_INTERVAL_MILLIS = 10;
+ private static final long EXEMPLAR_MIN_AGE_MILLIS = 100;
private Gauge noLabels, labels;
private SpanContext origSpanContext;
@BeforeEach
- public void setUp() {
+ void setUp() {
noLabels = Gauge.builder().name("nolabels").build();
labels = Gauge.builder().name("labels").labelNames("l").build();
origSpanContext = SpanContextSupplier.getSpanContext();
}
@AfterEach
- public void tearDown() {
+ void tearDown() {
SpanContextSupplier.setSpanContext(origSpanContext);
}
@@ -48,7 +48,7 @@ class GaugeTest {
}
@Test
- public void testIncrement() {
+ void increment() {
noLabels.inc();
assertThat(getValue(noLabels)).isCloseTo(1.0, offset(.001));
noLabels.inc(2);
@@ -60,7 +60,7 @@ class GaugeTest {
}
@Test
- public void testDecrement() {
+ void decrement() {
noLabels.dec();
assertThat(getValue(noLabels)).isCloseTo(-1.0, offset(.001));
noLabels.dec(2);
@@ -72,16 +72,16 @@ class GaugeTest {
}
@Test
- public void testSet() {
+ void set() {
noLabels.set(42);
assertThat(getValue(noLabels)).isCloseTo(42, offset(.001));
noLabels.set(7);
assertThat(getValue(noLabels)).isCloseTo(7.0, offset(.001));
}
- @Test
@SuppressWarnings("try")
- public void testTimer() throws InterruptedException {
+ @Test
+ void timer() throws InterruptedException {
try (Timer ignored = noLabels.startTimer()) {
Thread.sleep(12);
}
@@ -90,12 +90,12 @@ class GaugeTest {
}
@Test
- public void noLabelsDefaultZeroValue() {
+ void noLabelsDefaultZeroValue() {
assertThat(getValue(noLabels)).isCloseTo(0.0, offset(.001));
}
@Test
- public void testLabels() {
+ void labels() {
labels.labelValues("a").inc();
labels.labelValues("b").inc(3);
assertThat(getValue(labels, "l", "a")).isCloseTo(1.0, offset(.001));
@@ -103,7 +103,7 @@ class GaugeTest {
}
@Test
- public void testExemplarSampler() throws Exception {
+ void exemplarSampler() throws Exception {
Exemplar exemplar1 = Exemplar.builder().value(2.0).traceId("abc").spanId("123").build();
Exemplar exemplar2 = Exemplar.builder().value(6.5).traceId("def").spanId("456").build();
Exemplar exemplar3 = Exemplar.builder().value(7.0).traceId("123").spanId("abc").build();
@@ -161,35 +161,35 @@ class GaugeTest {
};
Gauge gauge = Gauge.builder().name("my_gauge").build();
- ExemplarSamplerConfigTestUtil.setMinRetentionPeriodMillis(gauge, exemplarMinAgeMillis);
- ExemplarSamplerConfigTestUtil.setSampleIntervalMillis(gauge, exemplarSampleIntervalMillis);
+ ExemplarSamplerConfigTestUtil.setMinRetentionPeriodMillis(gauge, EXEMPLAR_MIN_AGE_MILLIS);
+ ExemplarSamplerConfigTestUtil.setSampleIntervalMillis(gauge, EXEMPLAR_SAMPLE_INTERVAL_MILLIS);
SpanContextSupplier.setSpanContext(spanContext);
gauge.inc(2.0);
assertExemplarEquals(exemplar1, getData(gauge).getExemplar());
- Thread.sleep(2 * exemplarSampleIntervalMillis);
+ Thread.sleep(2 * EXEMPLAR_SAMPLE_INTERVAL_MILLIS);
gauge.inc(
3.0); // min age not reached -> keep the previous exemplar, exemplar sampler not called
assertExemplarEquals(exemplar1, getData(gauge).getExemplar());
- Thread.sleep(exemplarMinAgeMillis + 2 * exemplarSampleIntervalMillis);
+ Thread.sleep(EXEMPLAR_MIN_AGE_MILLIS + 2 * EXEMPLAR_SAMPLE_INTERVAL_MILLIS);
gauge.inc(2.0); // 2nd call: isSampled() returns false -> not sampled
assertExemplarEquals(exemplar1, getData(gauge).getExemplar());
- Thread.sleep(2 * exemplarSampleIntervalMillis);
+ Thread.sleep(2 * EXEMPLAR_SAMPLE_INTERVAL_MILLIS);
gauge.dec(0.5); // sampled
assertExemplarEquals(exemplar2, getData(gauge).getExemplar());
- Thread.sleep(exemplarMinAgeMillis + 2 * exemplarSampleIntervalMillis);
+ Thread.sleep(EXEMPLAR_MIN_AGE_MILLIS + 2 * EXEMPLAR_SAMPLE_INTERVAL_MILLIS);
gauge.set(7.0); // sampled
assertExemplarEquals(exemplar3, getData(gauge).getExemplar());
- Thread.sleep(2 * exemplarSampleIntervalMillis);
+ Thread.sleep(2 * EXEMPLAR_SAMPLE_INTERVAL_MILLIS);
gauge.incWithExemplar(
Labels.of(
@@ -230,7 +230,7 @@ class GaugeTest {
}
@Test
- public void testExemplarSamplerDisabled() {
+ void exemplarSamplerDisabled() {
Gauge gauge = Gauge.builder().name("test").withoutExemplars().build();
gauge.setWithExemplar(3.0, Labels.of("a", "b"));
assertThat(getData(gauge).getExemplar()).isNull();
--- a/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/GaugeWithCallbackTest.java
+++ b/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/GaugeWithCallbackTest.java
@@ -1,7 +1,7 @@
package io.prometheus.metrics.core.metrics;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.data.Offset.offset;
import io.prometheus.metrics.model.snapshots.GaugeSnapshot;
@@ -10,10 +10,10 @@ import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test;
-class GaugeWithCallbackTest {
+final class GaugeWithCallbackTest {
@Test
- public void testGauge() {
+ void gauge() {
final AtomicInteger value = new AtomicInteger(1);
List<String> labelValues = Arrays.asList("v1", "v2");
GaugeWithCallback gauge =
@@ -25,21 +25,22 @@ class GaugeWithCallbackTest {
.build();
GaugeSnapshot snapshot = gauge.collect();
- assertThat(snapshot.getDataPoints().size()).isOne();
+ assertThat(snapshot.getDataPoints()).hasSize(1);
GaugeSnapshot.GaugeDataPointSnapshot datapoint = snapshot.getDataPoints().get(0);
assertThat(datapoint.getValue()).isCloseTo(value.doubleValue(), offset(0.1));
assertThat(datapoint.getLabels().size()).isEqualTo(labelValues.size());
value.incrementAndGet();
snapshot = gauge.collect();
- assertThat(snapshot.getDataPoints().size()).isOne();
+ assertThat(snapshot.getDataPoints()).hasSize(1);
datapoint = snapshot.getDataPoints().get(0);
assertThat(datapoint.getValue()).isCloseTo(value.doubleValue(), offset(0.1));
}
@Test
- public void testGaugeNoCallback() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> GaugeWithCallback.builder().name("gauge").labelNames("l1", "l2").build());
+ void gaugeNoCallback() {
+ assertThatThrownBy(
+ () -> GaugeWithCallback.builder().name("gauge").labelNames("l1", "l2").build())
+ .isInstanceOf(IllegalArgumentException.class);
}
}
--- a/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/HistogramTest.java
+++ b/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/HistogramTest.java
@@ -1,8 +1,10 @@
package io.prometheus.metrics.core.metrics;
+import static com.google.common.collect.ImmutableList.toImmutableList;
import static io.prometheus.metrics.core.metrics.TestUtil.assertExemplarEquals;
+import static java.util.Comparator.comparingDouble;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.data.Offset.offset;
import io.prometheus.metrics.core.datapoints.DistributionDataPoint;
@@ -26,8 +28,6 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Collections;
-import java.util.Comparator;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CompletionService;
@@ -39,12 +39,11 @@ import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
-import java.util.stream.Collectors;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-class HistogramTest {
+final class HistogramTest {
private static final double RESET_DURATION_REACHED =
-123.456; // just a random value indicating that we should simulate that the reset duration
@@ -53,12 +52,12 @@ class HistogramTest {
private SpanContext origSpanContext;
@BeforeEach
- public void setUp() {
+ void setUp() {
origSpanContext = SpanContextSupplier.getSpanContext();
}
@AfterEach
- public void tearDown() {
+ void tearDown() {
SpanContextSupplier.setSpanContext(origSpanContext);
}
@@ -93,14 +92,14 @@ class HistogramTest {
String expectedWithMetadata =
"name: \"test\" type: HISTOGRAM metric { histogram { " + expected + " } }";
assertThat(ProtobufUtil.shortDebugString(protobufData))
- .as("test \"" + name + "\" failed")
+ .as("test \"%s\" failed", name)
.isEqualTo(expectedWithMetadata);
}
}
/** Test cases copied from histogram_test.go in client_golang. */
@Test
- public void testGolangTests() throws NoSuchFieldException, IllegalAccessException {
+ void golangTests() throws NoSuchFieldException, IllegalAccessException {
GolangTestCase[] testCases =
new GolangTestCase[] {
new GolangTestCase(
@@ -762,7 +761,7 @@ class HistogramTest {
/** Additional tests that are not part of client_golang's test suite. */
@Test
- public void testAdditional() throws NoSuchFieldException, IllegalAccessException {
+ void additional() throws NoSuchFieldException, IllegalAccessException {
GolangTestCase[] testCases =
new GolangTestCase[] {
new GolangTestCase(
@@ -796,7 +795,7 @@ class HistogramTest {
* <p>This test is ported from client_golang's TestGetLe().
*/
@Test
- public void testNativeBucketIndexToUpperBound()
+ void nativeBucketIndexToUpperBound()
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
int[] indexes = new int[] {-1, 0, 1, 512, 513, -1, 0, 1, 1024, 1025, -1, 0, 1, 4096, 4097};
int[] schemas = new int[] {-1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2};
@@ -828,7 +827,7 @@ class HistogramTest {
Histogram.DataPoint histogramData = histogram.newDataPoint();
double result = (double) method.invoke(histogramData, schemas[i], indexes[i]);
assertThat(result)
- .as("index=" + indexes[i] + ", schema=" + schemas[i])
+ .as("index=%s, schema=%s", indexes[i], schemas[i])
.isCloseTo(expectedUpperBounds[i], offset(0.0000000000001));
}
}
@@ -838,7 +837,7 @@ class HistogramTest {
* findBucketIndex()
*/
@Test
- public void testFindBucketIndex()
+ void findBucketIndex()
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Random rand = new Random();
Method findBucketIndex =
@@ -865,17 +864,8 @@ class HistogramTest {
histogram.getNoLabels(), schema, bucketIndex);
assertThat(lowerBound < value && upperBound >= value)
.as(
- "Bucket index "
- + bucketIndex
- + " with schema "
- + schema
- + " has range ["
- + lowerBound
- + ", "
- + upperBound
- + "]. Value "
- + value
- + " is outside of that range.")
+ "Bucket index %s with schema %s has range [%s, %s]. Value %s is outside of that range.",
+ bucketIndex, schema, lowerBound, upperBound, value)
.isTrue();
}
}
@@ -883,7 +873,7 @@ class HistogramTest {
}
@Test
- public void testDefaults() throws IOException {
+ void defaults() throws IOException {
Histogram histogram = Histogram.builder().name("test").build();
histogram.observe(0.5);
HistogramSnapshot snapshot = histogram.collect();
@@ -951,7 +941,7 @@ class HistogramTest {
}
@Test
- public void testExemplarsClassicHistogram() throws Exception {
+ void exemplarsClassicHistogram() throws Exception {
SpanContext spanContext =
new SpanContext() {
int callCount = 0;
@@ -1095,7 +1085,7 @@ class HistogramTest {
}
@Test
- public void testCustomExemplarsClassicHistogram()
+ void customExemplarsClassicHistogram()
throws InterruptedException, NoSuchFieldException, IllegalAccessException {
// TODO: This was copied from the old simpleclient, can probably be refactored.
@@ -1150,11 +1140,11 @@ class HistogramTest {
}
Exemplar exemplar = data.getExemplars().get(lowerBound, upperBound);
assertThat(exemplar)
- .as("No exemplar found in bucket [" + lowerBound + ", " + upperBound + "]")
+ .as("No exemplar found in bucket [%s, %s]", lowerBound, upperBound)
.isNotNull();
assertThat(exemplar.getValue()).isEqualTo(value);
assertThat(exemplar.getLabels().size())
- .as("" + exemplar.getLabels())
+ .as("%s", exemplar.getLabels())
.isEqualTo(labels.length / 2);
for (int i = 0; i < labels.length; i += 2) {
assertThat(exemplar.getLabels().getName(i / 2)).isEqualTo(labels[i]);
@@ -1163,7 +1153,7 @@ class HistogramTest {
}
@Test
- public void testExemplarsNativeHistogram() throws NoSuchFieldException, IllegalAccessException {
+ void exemplarsNativeHistogram() throws NoSuchFieldException, IllegalAccessException {
SpanContext spanContext =
new SpanContext() {
@@ -1206,17 +1196,17 @@ class HistogramTest {
histogram.labelValues("/hello").observe(3.11);
histogram.labelValues("/world").observe(3.12);
- assertThat(getData(histogram, "path", "/hello").getExemplars().size()).isOne();
+ assertThat(getData(histogram, "path", "/hello").getExemplars().size()).isEqualTo(1);
assertExemplarEquals(
ex1, getData(histogram, "path", "/hello").getExemplars().iterator().next());
- assertThat(getData(histogram, "path", "/world").getExemplars().size()).isOne();
+ assertThat(getData(histogram, "path", "/world").getExemplars().size()).isEqualTo(1);
assertExemplarEquals(
ex2, getData(histogram, "path", "/world").getExemplars().iterator().next());
histogram
.labelValues("/world")
.observeWithExemplar(3.13, Labels.of("key1", "value1", "key2", "value2"));
- assertThat(getData(histogram, "path", "/hello").getExemplars().size()).isOne();
+ assertThat(getData(histogram, "path", "/hello").getExemplars().size()).isEqualTo(1);
assertExemplarEquals(
ex1, getData(histogram, "path", "/hello").getExemplars().iterator().next());
assertThat(getData(histogram, "path", "/world").getExemplars().size()).isEqualTo(2);
@@ -1225,86 +1215,85 @@ class HistogramTest {
for (Exemplar exemplar : exemplars) {
exemplarList.add(exemplar);
}
- exemplarList.sort(Comparator.comparingDouble(Exemplar::getValue));
+ exemplarList.sort(comparingDouble(Exemplar::getValue));
assertThat(exemplars.size()).isEqualTo(2);
assertExemplarEquals(ex2, exemplarList.get(0));
assertExemplarEquals(ex3, exemplarList.get(1));
}
@Test
- public void testIllegalLabelName() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> Histogram.builder().name("test").labelNames("label", "le"));
+ void illegalLabelName() {
+ assertThatThrownBy(() -> Histogram.builder().name("test").labelNames("label", "le"))
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testIllegalLabelNameConstLabels() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(
+ void illegalLabelNameConstLabels() {
+ assertThatThrownBy(
() ->
Histogram.builder()
.name("test")
- .constLabels(Labels.of("label1", "value1", "le", "0.3")));
+ .constLabels(Labels.of("label1", "value1", "le", "0.3")))
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testIllegalLabelNamePrefix() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> Histogram.builder().name("test").labelNames("__hello"));
+ void illegalLabelNamePrefix() {
+ assertThatThrownBy(() -> Histogram.builder().name("test").labelNames("__hello"))
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testIllegalName() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> Histogram.builder().name("my_namespace/server.durations"));
+ void illegalName() {
+ assertThatThrownBy(() -> Histogram.builder().name("my_namespace/server.durations"))
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testNoName() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> Histogram.builder().build());
+ void noName() {
+ assertThatThrownBy(() -> Histogram.builder().build())
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testNullName() {
- assertThatExceptionOfType(NullPointerException.class)
- .isThrownBy(() -> Histogram.builder().name(null));
+ void nullName() {
+ assertThatThrownBy(() -> Histogram.builder().name(null))
+ .isInstanceOf(NullPointerException.class);
}
@Test
- public void testDuplicateClassicBuckets() {
+ void duplicateClassicBuckets() {
Histogram histogram =
Histogram.builder().name("test").classicUpperBounds(0, 3, 17, 3, 21).build();
List<Double> upperBounds =
getData(histogram).getClassicBuckets().stream()
.map(ClassicHistogramBucket::getUpperBound)
- .collect(Collectors.toList());
- assertThat(upperBounds)
- .isEqualTo(Arrays.asList(0.0, 3.0, 17.0, 21.0, Double.POSITIVE_INFINITY));
+ .collect(toImmutableList());
+ assertThat(upperBounds).containsExactly(0.0, 3.0, 17.0, 21.0, Double.POSITIVE_INFINITY);
}
@Test
- public void testUnsortedBuckets() {
+ void unsortedBuckets() {
Histogram histogram = Histogram.builder().name("test").classicUpperBounds(0.2, 0.1).build();
List<Double> upperBounds =
getData(histogram).getClassicBuckets().stream()
.map(ClassicHistogramBucket::getUpperBound)
- .collect(Collectors.toList());
- assertThat(upperBounds).isEqualTo(Arrays.asList(0.1, 0.2, Double.POSITIVE_INFINITY));
+ .collect(toImmutableList());
+ assertThat(upperBounds).containsExactly(0.1, 0.2, Double.POSITIVE_INFINITY);
}
@Test
- public void testEmptyBuckets() {
+ void emptyBuckets() {
Histogram histogram = Histogram.builder().name("test").classicUpperBounds().build();
List<Double> upperBounds =
getData(histogram).getClassicBuckets().stream()
.map(ClassicHistogramBucket::getUpperBound)
- .collect(Collectors.toList());
- assertThat(upperBounds).isEqualTo(Collections.singletonList(Double.POSITIVE_INFINITY));
+ .collect(toImmutableList());
+ assertThat(upperBounds).containsExactly(Double.POSITIVE_INFINITY);
}
@Test
- public void testBucketsIncludePositiveInfinity() {
+ void bucketsIncludePositiveInfinity() {
Histogram histogram =
Histogram.builder()
.name("test")
@@ -1313,47 +1302,46 @@ class HistogramTest {
List<Double> upperBounds =
getData(histogram).getClassicBuckets().stream()
.map(ClassicHistogramBucket::getUpperBound)
- .collect(Collectors.toList());
- assertThat(upperBounds).isEqualTo(Arrays.asList(0.01, 0.1, 1.0, Double.POSITIVE_INFINITY));
+ .collect(toImmutableList());
+ assertThat(upperBounds).containsExactly(0.01, 0.1, 1.0, Double.POSITIVE_INFINITY);
}
@Test
- public void testLinearBuckets() {
+ void linearBuckets() {
Histogram histogram =
Histogram.builder().name("test").classicLinearUpperBounds(0.1, 0.1, 10).build();
List<Double> upperBounds =
getData(histogram).getClassicBuckets().stream()
.map(ClassicHistogramBucket::getUpperBound)
- .collect(Collectors.toList());
+ .collect(toImmutableList());
assertThat(upperBounds)
- .isEqualTo(
- Arrays.asList(
- 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, Double.POSITIVE_INFINITY));
+ .containsExactly(
+ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, Double.POSITIVE_INFINITY);
}
@Test
- public void testExponentialBuckets() {
+ void exponentialBuckets() {
Histogram histogram =
Histogram.builder().classicExponentialUpperBounds(2, 2.5, 3).name("test").build();
List<Double> upperBounds =
getData(histogram).getClassicBuckets().stream()
.map(ClassicHistogramBucket::getUpperBound)
- .collect(Collectors.toList());
- assertThat(upperBounds).isEqualTo(Arrays.asList(2.0, 5.0, 12.5, Double.POSITIVE_INFINITY));
+ .collect(toImmutableList());
+ assertThat(upperBounds).containsExactly(2.0, 5.0, 12.5, Double.POSITIVE_INFINITY);
}
@Test
- public void testBucketsIncludeNaN() {
- assertThatExceptionOfType(RuntimeException.class)
- .isThrownBy(
- () -> Histogram.builder().name("test").classicUpperBounds(0.01, 0.1, 1.0, Double.NaN));
+ void bucketsIncludeNaN() {
+ assertThatThrownBy(
+ () -> Histogram.builder().name("test").classicUpperBounds(0.01, 0.1, 1.0, Double.NaN))
+ .isInstanceOf(RuntimeException.class);
}
@Test
- public void testNoLabelsDefaultZeroValue() {
+ void noLabelsDefaultZeroValue() {
Histogram noLabels = Histogram.builder().name("test").build();
- assertThat(getBucket(noLabels, 0.005).getCount()).isZero();
- assertThat(getData(noLabels).getCount()).isZero();
+ assertThat(getBucket(noLabels, 0.005).getCount()).isEqualTo(0);
+ assertThat(getData(noLabels).getCount()).isEqualTo(0);
assertThat(getData(noLabels).getSum()).isEqualTo(0.0);
}
@@ -1365,26 +1353,26 @@ class HistogramTest {
}
@Test
- public void testObserve() {
+ void observe() {
Histogram noLabels = Histogram.builder().name("test").build();
noLabels.observe(2);
- assertThat(getData(noLabels).getCount()).isOne();
- assertThat(getData(noLabels).getSum()).isCloseTo(2.0, offset(.0));
- assertThat(getBucket(noLabels, 1).getCount()).isZero();
- assertThat(getBucket(noLabels, 2.5).getCount()).isOne();
+ assertThat(getData(noLabels).getCount()).isEqualTo(1);
+ assertThat(getData(noLabels).getSum()).isEqualTo(2.0);
+ assertThat(getBucket(noLabels, 1).getCount()).isEqualTo(0);
+ assertThat(getBucket(noLabels, 2.5).getCount()).isEqualTo(1);
noLabels.observe(4);
assertThat(getData(noLabels).getCount()).isEqualTo(2);
- assertThat(getData(noLabels).getSum()).isCloseTo(6.0, offset(.0));
- assertThat(getBucket(noLabels, 1).getCount()).isZero();
- assertThat(getBucket(noLabels, 2.5).getCount()).isOne();
- assertThat(getBucket(noLabels, 5).getCount()).isOne();
- assertThat(getBucket(noLabels, 10).getCount()).isZero();
- assertThat(getBucket(noLabels, Double.POSITIVE_INFINITY).getCount()).isZero();
+ assertThat(getData(noLabels).getSum()).isEqualTo(6.0);
+ assertThat(getBucket(noLabels, 1).getCount()).isEqualTo(0);
+ assertThat(getBucket(noLabels, 2.5).getCount()).isEqualTo(1);
+ assertThat(getBucket(noLabels, 5).getCount()).isEqualTo(1);
+ assertThat(getBucket(noLabels, 10).getCount()).isEqualTo(0);
+ assertThat(getBucket(noLabels, Double.POSITIVE_INFINITY).getCount()).isEqualTo(0);
}
@Test
// See https://github.com/prometheus/client_java/issues/646
- public void testNegativeAmount() {
+ void negativeAmount() {
Histogram histogram =
Histogram.builder()
.name("histogram")
@@ -1405,27 +1393,27 @@ class HistogramTest {
List<Long> actualBucketCounts =
getData(histogram).getClassicBuckets().stream()
.map(ClassicHistogramBucket::getCount)
- .collect(Collectors.toList());
- assertThat(actualBucketCounts).isEqualTo(expectedBucketCounts);
+ .collect(toImmutableList());
+ assertThat(actualBucketCounts).containsExactlyElementsOf(expectedBucketCounts);
}
@Test
- public void testBoundaryConditions() {
+ void boundaryConditions() {
Histogram histogram = Histogram.builder().name("test").build();
histogram.observe(2.5);
- assertThat(getBucket(histogram, 1).getCount()).isZero();
- assertThat(getBucket(histogram, 2.5).getCount()).isOne();
+ assertThat(getBucket(histogram, 1).getCount()).isEqualTo(0);
+ assertThat(getBucket(histogram, 2.5).getCount()).isEqualTo(1);
histogram.observe(Double.POSITIVE_INFINITY);
- assertThat(getBucket(histogram, 1).getCount()).isZero();
- assertThat(getBucket(histogram, 2.5).getCount()).isOne();
- assertThat(getBucket(histogram, 5).getCount()).isZero();
- assertThat(getBucket(histogram, 10).getCount()).isZero();
- assertThat(getBucket(histogram, Double.POSITIVE_INFINITY).getCount()).isOne();
+ assertThat(getBucket(histogram, 1).getCount()).isEqualTo(0);
+ assertThat(getBucket(histogram, 2.5).getCount()).isEqualTo(1);
+ assertThat(getBucket(histogram, 5).getCount()).isEqualTo(0);
+ assertThat(getBucket(histogram, 10).getCount()).isEqualTo(0);
+ assertThat(getBucket(histogram, Double.POSITIVE_INFINITY).getCount()).isEqualTo(1);
}
@Test
- public void testObserveWithLabels() {
+ void observeWithLabels() {
Histogram histogram =
Histogram.builder()
.name("test")
@@ -1441,20 +1429,19 @@ class HistogramTest {
getData(histogram, "env", "prod", "path", "/hello", "status", "500");
assertThat(data200.getCount()).isEqualTo(2);
assertThat(data200.getSum()).isCloseTo(0.31, offset(0.0000001));
- assertThat(data500.getCount()).isOne();
+ assertThat(data500.getCount()).isEqualTo(1);
assertThat(data500.getSum()).isCloseTo(0.19, offset(0.0000001));
histogram.labelValues("/hello", "200").observe(0.13);
data200 = getData(histogram, "env", "prod", "path", "/hello", "status", "200");
data500 = getData(histogram, "env", "prod", "path", "/hello", "status", "500");
assertThat(data200.getCount()).isEqualTo(3);
assertThat(data200.getSum()).isCloseTo(0.44, offset(0.0000001));
- assertThat(data500.getCount()).isOne();
+ assertThat(data500.getCount()).isEqualTo(1);
assertThat(data500.getSum()).isCloseTo(0.19, offset(0.0000001));
}
@Test
- public void testObserveMultithreaded()
- throws InterruptedException, ExecutionException, TimeoutException {
+ void observeMultithreaded() throws InterruptedException, ExecutionException, TimeoutException {
// Hard to test concurrency, but let's run a couple of observations in parallel and assert none
// gets lost.
Histogram histogram = Histogram.builder().name("test").labelNames("status").build();
@@ -1485,7 +1472,7 @@ class HistogramTest {
List<HistogramSnapshot> snapshots = future.get(5, TimeUnit.SECONDS);
long count = 0;
for (HistogramSnapshot snapshot : snapshots) {
- assertThat(snapshot.getDataPoints().size()).isOne();
+ assertThat(snapshot.getDataPoints()).hasSize(1);
HistogramSnapshot.HistogramDataPointSnapshot data =
snapshot.getDataPoints().stream().findFirst().orElseThrow(RuntimeException::new);
assertThat(data.getCount())
--- a/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/InfoTest.java
+++ b/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/InfoTest.java
@@ -1,7 +1,7 @@
package io.prometheus.metrics.core.metrics;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import io.prometheus.metrics.expositionformats.OpenMetricsTextFormatWriter;
import io.prometheus.metrics.expositionformats.generated.com_google_protobuf_4_29_1.Metrics;
@@ -15,10 +15,10 @@ import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Test;
-class InfoTest {
+final class InfoTest {
@Test
- public void testInfoStrippedFromName() {
+ void infoStrippedFromName() {
for (String name :
new String[] {
"jvm.runtime", "jvm_runtime",
@@ -37,7 +37,7 @@ class InfoTest {
}
@Test
- public void testAddAndRemove() {
+ void addAndRemove() {
Info info = Info.builder().name("test_info").labelNames("a", "b").build();
assertThat(info.collect().getDataPoints()).isEmpty();
info.addLabelValues("val1", "val2");
@@ -53,14 +53,14 @@ class InfoTest {
info.remove("val2", "val2");
assertThat(info.collect().getDataPoints()).isEmpty();
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> info.addLabelValues("val1", "val2", "extra"))
- .withMessage(
+ assertThatThrownBy(() -> info.addLabelValues("val1", "val2", "extra"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage(
"Info test was created with 2 label names, but you called addLabelValues() with 3 label values.");
}
@Test
- public void testSet() throws IOException {
+ void set() throws IOException {
Info info =
Info.builder()
.name("target_info")
@@ -75,19 +75,19 @@ class InfoTest {
"target_info{service_instance_id=\"123\",service_name=\"test\",service_version=\"2.0.0\"} 1\n",
info);
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> info.setLabelValues("2.0.0", "extra"))
- .withMessage(
+ assertThatThrownBy(() -> info.setLabelValues("2.0.0", "extra"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage(
"Info target was created with 1 label names, but you called setLabelValues() with 2 label values.");
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> info.remove("2.0.0", "extra"))
- .withMessage(
+ assertThatThrownBy(() -> info.remove("2.0.0", "extra"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage(
"Info target was created with 1 label names, but you called remove() with 2 label values.");
}
@Test
- public void testConstLabelsOnly() throws IOException {
+ void constLabelsOnly() throws IOException {
Info info =
Info.builder()
.name("target_info")
@@ -99,23 +99,23 @@ class InfoTest {
@Test
void unit() {
- assertThatExceptionOfType(UnsupportedOperationException.class)
- .isThrownBy(() -> Info.builder().unit(Unit.BYTES).build())
- .withMessage("Info metrics cannot have a unit.");
+ assertThatThrownBy(() -> Info.builder().unit(Unit.BYTES).build())
+ .isInstanceOf(UnsupportedOperationException.class)
+ .hasMessage("Info metrics cannot have a unit.");
}
@Test
- public void testConstLabelsDuplicate1() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(
- () -> Info.builder().constLabels(Labels.of("a_1", "val1")).labelNames("a.1").build());
+ void constLabelsDuplicate1() {
+ assertThatThrownBy(
+ () -> Info.builder().constLabels(Labels.of("a_1", "val1")).labelNames("a.1").build())
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testConstLabelsDuplicate2() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(
- () -> Info.builder().labelNames("a_1").constLabels(Labels.of("a.1", "val1")).build());
+ void constLabelsDuplicate2() {
+ assertThatThrownBy(
+ () -> Info.builder().labelNames("a_1").constLabels(Labels.of("a.1", "val1")).build())
+ .isInstanceOf(IllegalArgumentException.class);
}
private void assertTextFormat(String expected, Info info) throws IOException {
--- a/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/SlidingWindowTest.java
+++ b/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/SlidingWindowTest.java
@@ -8,7 +8,7 @@ import java.util.concurrent.atomic.AtomicLong;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-class SlidingWindowTest {
+final class SlidingWindowTest {
class Observer {
@@ -25,12 +25,8 @@ class SlidingWindowTest {
}
assertThat(values)
.as(
- "Start time: "
- + startTime
- + ", current time: "
- + currentTimeMillis.get()
- + ", elapsed time: "
- + (currentTimeMillis.get() - startTime))
+ "Start time: %s, current time: %s, elapsed time: %s",
+ startTime, currentTimeMillis.get(), currentTimeMillis.get() - startTime)
.isEqualTo(expectedList);
}
}
@@ -43,7 +39,7 @@ class SlidingWindowTest {
private final long timeBetweenRotateMillis = maxAgeSeconds * 1000 / ageBuckets + 2;
@BeforeEach
- public void setUp() {
+ void setUp() {
startTime = System.currentTimeMillis();
currentTimeMillis.set(startTime);
ringBuffer =
@@ -53,7 +49,7 @@ class SlidingWindowTest {
}
@Test
- public void testRotate() {
+ void rotate() {
for (int i = 0; i < ageBuckets; i++) {
currentTimeMillis.addAndGet(timeBetweenRotateMillis);
ringBuffer.observe(1.0);
@@ -72,7 +68,7 @@ class SlidingWindowTest {
}
@Test
- public void testMultiRotate() {
+ void multiRotate() {
ringBuffer.observe(1.0);
currentTimeMillis.addAndGet(2 * timeBetweenRotateMillis); // 2/5 of max aqe
ringBuffer.observe(2.0);
--- a/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/StateSetTest.java
+++ b/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/StateSetTest.java
@@ -1,14 +1,14 @@
package io.prometheus.metrics.core.metrics;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import io.prometheus.metrics.model.snapshots.Labels;
import io.prometheus.metrics.model.snapshots.StateSetSnapshot;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
-class StateSetTest {
+final class StateSetTest {
enum MyFeatureFlag {
EXPERIMENTAL_FEATURE_1 {
@@ -27,7 +27,7 @@ class StateSetTest {
}
@Test
- public void testEnumStateSet() {
+ void enumStateSet() {
StateSet stateSet =
StateSet.builder()
.name("feature_flags")
@@ -51,7 +51,7 @@ class StateSetTest {
}
@Test
- public void testDefaultFalse() {
+ void defaultFalse() {
StateSet stateSet =
StateSet.builder().name("test").states("state1", "state2", "state3").build();
assertThat(getData(stateSet).size()).isEqualTo(3);
@@ -65,9 +65,9 @@ class StateSetTest {
@Test
void illegalName() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> StateSet.builder().name("state1").states("state1", "state2").build())
- .withMessage(
+ assertThatThrownBy(() -> StateSet.builder().name("state1").states("state1", "state2").build())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage(
"Label name state1 is illegal (can't use the metric name as label name in state set metrics)");
}
@@ -82,8 +82,8 @@ class StateSetTest {
}
@Test
- public void testStatesCannotBeEmpty() {
- assertThatExceptionOfType(IllegalStateException.class)
- .isThrownBy(() -> StateSet.builder().name("invalid").build());
+ void statesCannotBeEmpty() {
+ assertThatThrownBy(() -> StateSet.builder().name("invalid").build())
+ .isInstanceOf(IllegalStateException.class);
}
}
--- a/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/StatefulMetricTest.java
+++ b/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/StatefulMetricTest.java
@@ -6,10 +6,10 @@ import java.lang.reflect.Field;
import java.util.Map;
import org.junit.jupiter.api.Test;
-class StatefulMetricTest {
+final class StatefulMetricTest {
@Test
- public void testLabelRemoveWhileCollecting() throws Exception {
+ void labelRemoveWhileCollecting() throws Exception {
Counter counter = Counter.builder().name("test").labelNames("label1", "label2").build();
Field data = counter.getClass().getSuperclass().getDeclaredField("data");
data.setAccessible(true);
@@ -35,7 +35,7 @@ class StatefulMetricTest {
}
@Test
- public void testClear() {
+ void clear() {
Counter counter = Counter.builder().name("test").labelNames("label1", "label2").build();
counter.labelValues("a", "b").inc(3.0);
counter.labelValues("c", "d").inc(3.0);
@@ -50,7 +50,7 @@ class StatefulMetricTest {
}
@Test
- public void testClearNoLabels() {
+ void clearNoLabels() {
Counter counter = Counter.builder().name("test").build();
counter.inc();
assertThat(counter.collect().getDataPoints()).hasSize(1);
--- a/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/SummaryTest.java
+++ b/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/SummaryTest.java
@@ -1,7 +1,7 @@
package io.prometheus.metrics.core.metrics;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.data.Offset.offset;
@@ -17,7 +17,7 @@ import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-class SummaryTest {
+final class SummaryTest {
private final Label label = new Label("name", "value");
private final Labels labels = Labels.builder().label(label.getName(), label.getValue()).build();
@@ -29,7 +29,7 @@ class SummaryTest {
private Summary noLabelsAndQuantiles;
@BeforeEach
- public void setUp() {
+ void setUp() {
registry = new PrometheusRegistry();
noLabels =
Summary.builder().name("nolabels").unit(Unit.SECONDS).help("help").register(registry);
@@ -62,16 +62,16 @@ class SummaryTest {
}
@Test
- public void testObserve() {
+ void observe() {
noLabels.observe(2);
- assertThat(getCount(noLabels, Labels.EMPTY)).isOne();
+ assertThat(getCount(noLabels, Labels.EMPTY)).isEqualTo(1);
assertThat(getSum(noLabels, Labels.EMPTY)).isCloseTo(2.0, offset(.001));
noLabels.observe(3);
assertThat(getCount(noLabels, Labels.EMPTY)).isEqualTo(2);
assertThat(getSum(noLabels, Labels.EMPTY)).isCloseTo(5.0, offset(.001));
withLabels.labelValues(label.getValue()).observe(4);
- assertThat(getCount(withLabels, labels)).isOne();
+ assertThat(getCount(withLabels, labels)).isEqualTo(1);
assertThat(getSum(withLabels, labels)).isCloseTo(4.0, offset(.001));
withLabels.labelValues(label.getValue()).observeWithExemplar(6, labels);
@@ -80,7 +80,7 @@ class SummaryTest {
}
@Test
- public void testNegativeAmount() {
+ void negativeAmount() {
noLabels.observe(-1);
noLabels.observe(-3);
assertThat(getCount(noLabels, Labels.EMPTY)).isEqualTo(2);
@@ -88,7 +88,7 @@ class SummaryTest {
}
@Test
- public void testQuantiles() {
+ void quantiles() {
int nSamples = 1000000; // simulate one million samples
for (int i = 1; i <= nSamples; i++) {
@@ -113,7 +113,7 @@ class SummaryTest {
}
@Test
- public void testMaxAge() throws InterruptedException {
+ void maxAge() throws InterruptedException {
Summary summary =
Summary.builder()
.quantile(0.99, 0.001)
@@ -132,10 +132,10 @@ class SummaryTest {
}
@Test
- public void testTimer() {
+ void timer() {
int result = noLabels.time(() -> 123);
assertThat(result).isEqualTo(123);
- assertThat(getCount(noLabels, Labels.EMPTY)).isOne();
+ assertThat(getCount(noLabels, Labels.EMPTY)).isEqualTo(1);
try (Timer timer = noLabels.startTimer()) {
timer.observeDuration();
@@ -144,33 +144,33 @@ class SummaryTest {
}
@Test
- public void noLabelsDefaultZeroValue() {
- assertThat(getCount(noLabels, Labels.EMPTY)).isZero();
+ void noLabelsDefaultZeroValue() {
+ assertThat(getCount(noLabels, Labels.EMPTY)).isEqualTo(0);
assertThat(getSum(noLabels, Labels.EMPTY)).isCloseTo(0.0, offset(.001));
}
@Test
- public void testBuilderInvalidNumberOfAgeBuckets() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> Summary.builder().name("name").numberOfAgeBuckets(-1).build());
+ void builderInvalidNumberOfAgeBuckets() {
+ assertThatThrownBy(() -> Summary.builder().name("name").numberOfAgeBuckets(-1).build())
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testBuilderInvalidMaxAge() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> Summary.builder().name("name").maxAgeSeconds(-1).build());
+ void builderInvalidMaxAge() {
+ assertThatThrownBy(() -> Summary.builder().name("name").maxAgeSeconds(-1).build())
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testBuilderInvalidQuantile() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> Summary.builder().name("name").quantile(42).build());
+ void builderInvalidQuantile() {
+ assertThatThrownBy(() -> Summary.builder().name("name").quantile(42).build())
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testBuilderInvalidQuantileError() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> Summary.builder().name("name").quantile(0.5, 20).build());
+ void builderInvalidQuantileError() {
+ assertThatThrownBy(() -> Summary.builder().name("name").quantile(0.5, 20).build())
+ .isInstanceOf(IllegalArgumentException.class);
}
private double getQuantile(Summary summary, double quantile, Labels labels) {
@@ -188,7 +188,7 @@ class SummaryTest {
private SummarySnapshot.SummaryDataPointSnapshot getDatapoint(Summary summary, Labels labels) {
SummarySnapshot snapshot = summary.collect();
List<SummarySnapshot.SummaryDataPointSnapshot> datapoints = snapshot.getDataPoints();
- assertThat(datapoints.size()).isOne();
+ assertThat(datapoints).hasSize(1);
SummarySnapshot.SummaryDataPointSnapshot datapoint = datapoints.get(0);
assertThat((Iterable<? extends Label>) datapoint.getLabels()).isEqualTo(labels);
return datapoint;
--- a/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/SummaryWithCallbackTest.java
+++ b/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/SummaryWithCallbackTest.java
@@ -1,7 +1,7 @@
package io.prometheus.metrics.core.metrics;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.data.Offset.offset;
import io.prometheus.metrics.model.snapshots.Quantile;
@@ -12,10 +12,10 @@ import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test;
-class SummaryWithCallbackTest {
+final class SummaryWithCallbackTest {
@Test
- public void testGauge() {
+ void gauge() {
final AtomicInteger count = new AtomicInteger(1);
final AtomicInteger sum = new AtomicInteger(1);
final Quantiles quantiles = Quantiles.of(new Quantile(0.5, 10));
@@ -32,7 +32,7 @@ class SummaryWithCallbackTest {
.build();
SummarySnapshot snapshot = gauge.collect();
- assertThat(snapshot.getDataPoints().size()).isOne();
+ assertThat(snapshot.getDataPoints()).hasSize(1);
SummarySnapshot.SummaryDataPointSnapshot datapoint = snapshot.getDataPoints().get(0);
assertThat(datapoint.getCount()).isEqualTo(count.get());
assertThat(datapoint.getSum()).isCloseTo(sum.doubleValue(), offset(0.1));
@@ -42,16 +42,16 @@ class SummaryWithCallbackTest {
count.incrementAndGet();
sum.incrementAndGet();
snapshot = gauge.collect();
- assertThat(snapshot.getDataPoints().size()).isOne();
+ assertThat(snapshot.getDataPoints()).hasSize(1);
datapoint = snapshot.getDataPoints().get(0);
assertThat(datapoint.getCount()).isEqualTo(count.get());
assertThat(datapoint.getSum()).isCloseTo(sum.doubleValue(), offset(0.1));
}
@Test
- public void testSummaryNoCallback() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(
- () -> SummaryWithCallback.builder().name("summary").labelNames("l1", "l2").build());
+ void summaryNoCallback() {
+ assertThatThrownBy(
+ () -> SummaryWithCallback.builder().name("summary").labelNames("l1", "l2").build())
+ .isInstanceOf(IllegalArgumentException.class);
}
}
--- a/prometheus-metrics-exporter-common/src/main/java/io/prometheus/metrics/exporter/common/PrometheusScrapeHandler.java
+++ b/prometheus-metrics-exporter-common/src/main/java/io/prometheus/metrics/exporter/common/PrometheusScrapeHandler.java
@@ -1,5 +1,7 @@
package io.prometheus.metrics.exporter.common;
+import static java.nio.charset.StandardCharsets.UTF_8;
+
import io.prometheus.metrics.config.ExporterFilterProperties;
import io.prometheus.metrics.config.PrometheusProperties;
import io.prometheus.metrics.expositionformats.ExpositionFormatWriter;
@@ -10,7 +12,6 @@ import io.prometheus.metrics.model.snapshots.MetricSnapshots;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
-import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
@@ -156,7 +157,7 @@ public class PrometheusScrapeHandler {
case "prometheus-protobuf":
String debugString =
expositionFormats.getPrometheusProtobufWriter().toDebugString(snapshots);
- body.write(debugString.getBytes(StandardCharsets.UTF_8));
+ body.write(debugString.getBytes(UTF_8));
break;
default:
body.write(
@@ -164,7 +165,7 @@ public class PrometheusScrapeHandler {
+ debugParam
+ ": Unsupported query parameter. Valid values are 'openmetrics', "
+ "'text', and 'prometheus-protobuf'.")
- .getBytes(StandardCharsets.UTF_8));
+ .getBytes(UTF_8));
break;
}
return true;
--- a/prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/DefaultHandler.java
+++ b/prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/DefaultHandler.java
@@ -1,9 +1,10 @@
package io.prometheus.metrics.exporter.httpserver;
+import static java.nio.charset.StandardCharsets.UTF_8;
+
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import java.io.IOException;
-import java.nio.charset.StandardCharsets;
/** Handler for the / endpoint */
public class DefaultHandler implements HttpHandler {
@@ -66,7 +67,7 @@ public class DefaultHandler implements HttpHandler {
+ "header for indicating which format it accepts.\n"
+ "</body>\n"
+ "</html>\n";
- this.responseBytes = responseString.getBytes(StandardCharsets.UTF_8);
+ this.responseBytes = responseString.getBytes(UTF_8);
this.contentType = "text/html; charset=utf-8";
}
--- a/prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/HTTPServer.java
+++ b/prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/HTTPServer.java
@@ -1,5 +1,8 @@
package io.prometheus.metrics.exporter.httpserver;
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkState;
+
import com.sun.net.httpserver.Authenticator;
import com.sun.net.httpserver.HttpContext;
import com.sun.net.httpserver.HttpExchange;
@@ -57,9 +60,7 @@ public class HTTPServer implements Closeable {
Authenticator authenticator,
String authenticatedSubjectAttributeName,
HttpHandler defaultHandler) {
- if (httpServer.getAddress() == null) {
- throw new IllegalArgumentException("HttpServer hasn't been bound to an address");
- }
+ checkArgument(httpServer.getAddress() != null, "HttpServer hasn't been bound to an address");
this.server = httpServer;
this.executorService = executorService;
registerHandler(
@@ -314,9 +315,7 @@ public class HTTPServer implements Closeable {
}
private void assertNull(Object o, String msg) {
- if (o != null) {
- throw new IllegalStateException(msg);
- }
+ checkState(o == null, msg);
}
}
}
--- a/prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/HealthyHandler.java
+++ b/prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/HealthyHandler.java
@@ -1,9 +1,10 @@
package io.prometheus.metrics.exporter.httpserver;
+import static java.nio.charset.StandardCharsets.UTF_8;
+
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import java.io.IOException;
-import java.nio.charset.StandardCharsets;
/** Handler for the /-/healthy endpoint */
public class HealthyHandler implements HttpHandler {
@@ -13,7 +14,7 @@ public class HealthyHandler implements HttpHandler {
public HealthyHandler() {
String responseString = "Exporter is healthy.\n";
- this.responseBytes = responseString.getBytes(StandardCharsets.UTF_8);
+ this.responseBytes = responseString.getBytes(UTF_8);
this.contentType = "text/plain; charset=utf-8";
}
--- a/prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/HttpExchangeAdapter.java
+++ b/prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/HttpExchangeAdapter.java
@@ -1,5 +1,9 @@
package io.prometheus.metrics.exporter.httpserver;
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static java.util.Collections.emptyEnumeration;
+import static java.util.Collections.enumeration;
+
import com.sun.net.httpserver.HttpExchange;
import io.prometheus.metrics.exporter.common.PrometheusHttpExchange;
import io.prometheus.metrics.exporter.common.PrometheusHttpRequest;
@@ -9,8 +13,6 @@ import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URI;
-import java.nio.charset.StandardCharsets;
-import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.logging.Level;
@@ -38,9 +40,9 @@ public class HttpExchangeAdapter implements PrometheusHttpExchange {
public Enumeration<String> getHeaders(String name) {
List<String> headers = httpExchange.getRequestHeaders().get(name);
if (headers == null) {
- return Collections.emptyEnumeration();
+ return emptyEnumeration();
} else {
- return Collections.enumeration(headers);
+ return enumeration(headers);
}
}
@@ -108,7 +110,7 @@ public class HttpExchangeAdapter implements PrometheusHttpExchange {
PrintWriter printWriter = new PrintWriter(stringWriter);
printWriter.write("An Exception occurred while scraping metrics: ");
requestHandlerException.printStackTrace(new PrintWriter(printWriter));
- byte[] stackTrace = stringWriter.toString().getBytes(StandardCharsets.UTF_8);
+ byte[] stackTrace = stringWriter.toString().getBytes(UTF_8);
httpExchange.getResponseHeaders().set("Content-Type", "text/plain; charset=utf-8");
httpExchange.sendResponseHeaders(500, stackTrace.length);
httpExchange.getResponseBody().write(stackTrace);
--- a/prometheus-metrics-exporter-httpserver/src/test/java/io/prometheus/metrics/exporter/httpserver/HTTPServerTest.java
+++ b/prometheus-metrics-exporter-httpserver/src/test/java/io/prometheus/metrics/exporter/httpserver/HTTPServerTest.java
@@ -1,7 +1,8 @@
package io.prometheus.metrics.exporter.httpserver;
+import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import com.sun.net.httpserver.Authenticator;
import com.sun.net.httpserver.HttpExchange;
@@ -15,7 +16,6 @@ import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
-import java.nio.charset.StandardCharsets;
import java.security.AccessController;
import java.security.NoSuchAlgorithmException;
import java.security.Principal;
@@ -24,11 +24,11 @@ import javax.net.ssl.SSLContext;
import javax.security.auth.Subject;
import org.junit.jupiter.api.Test;
-public class HTTPServerTest {
+final class HTTPServerTest {
+ @SuppressWarnings("removal")
@Test
- @SuppressWarnings({"removal"})
- public void testSubjectDoAs() throws Exception {
+ void subjectDoAs() throws Exception {
final String user = "joe";
final Subject subject = new Subject();
@@ -73,17 +73,15 @@ public class HTTPServerTest {
try (Socket socket = new Socket()) {
socket.connect(new InetSocketAddress("localhost", server.getPort()));
- socket
- .getOutputStream()
- .write(("GET " + path + " HTTP/1.1 \r\n").getBytes(StandardCharsets.UTF_8));
- socket.getOutputStream().write("HOST: localhost \r\n\r\n".getBytes(StandardCharsets.UTF_8));
+ socket.getOutputStream().write(("GET " + path + " HTTP/1.1 \r\n").getBytes(UTF_8));
+ socket.getOutputStream().write("HOST: localhost \r\n\r\n".getBytes(UTF_8));
socket.getOutputStream().flush();
String actualResponse = "";
byte[] resp = new byte[500];
int read = socket.getInputStream().read(resp, 0, resp.length);
if (read > 0) {
- actualResponse = new String(resp, 0, read, StandardCharsets.UTF_8);
+ actualResponse = new String(resp, 0, read, UTF_8);
}
assertThat(actualResponse).contains(expected);
}
@@ -124,15 +122,15 @@ public class HTTPServerTest {
@Test
void config() throws NoSuchAlgorithmException, IOException {
- assertThatExceptionOfType(IllegalStateException.class)
- .isThrownBy(
+ assertThatThrownBy(
() ->
HTTPServer.builder()
.port(0)
.hostname("localhost")
.inetAddress(InetAddress.getByName("localhost"))
.buildAndStart())
- .withMessage("cannot configure 'inetAddress' and 'hostname' at the same time");
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage("cannot configure 'inetAddress' and 'hostname' at the same time");
// ssl doesn't work without in tests
run(
--- a/prometheus-metrics-exporter-httpserver/src/test/java/io/prometheus/metrics/exporter/httpserver/MetricsHandlerTest.java
+++ b/prometheus-metrics-exporter-httpserver/src/test/java/io/prometheus/metrics/exporter/httpserver/MetricsHandlerTest.java
@@ -9,7 +9,7 @@ import io.prometheus.metrics.config.PrometheusProperties;
import io.prometheus.metrics.model.registry.PrometheusRegistry;
import org.junit.jupiter.api.Test;
-class MetricsHandlerTest {
+final class MetricsHandlerTest {
@Test
void ctor() {
@@ -24,7 +24,7 @@ class MetricsHandlerTest {
.extracting("registry")
.isEqualTo(registry);
- PrometheusProperties properties = mock(PrometheusProperties.class, RETURNS_MOCKS);
+ PrometheusProperties properties = mock(RETURNS_MOCKS);
assertThat(new MetricsHandler(properties))
.extracting("prometheusScrapeHandler")
--- a/prometheus-metrics-exporter-opentelemetry-otel-agent-resources/src/main/java/io/prometheus/otelagent/ResourceAttributesFromOtelAgent.java
+++ b/prometheus-metrics-exporter-opentelemetry-otel-agent-resources/src/main/java/io/prometheus/otelagent/ResourceAttributesFromOtelAgent.java
@@ -1,7 +1,10 @@
package io.prometheus.otelagent;
+import static com.google.common.base.Preconditions.checkState;
import static java.nio.file.Files.createTempDirectory;
+import static java.util.Collections.unmodifiableMap;
+import com.google.common.collect.ImmutableMap;
import java.io.File;
import java.io.InputStream;
import java.lang.reflect.Field;
@@ -11,7 +14,6 @@ import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
-import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@@ -63,7 +65,7 @@ public class ResourceAttributesFromOtelAgent {
result.put(entry.getKey().toString(), entry.getValue().toString());
}
}
- return Collections.unmodifiableMap(result);
+ return unmodifiableMap(result);
}
}
} finally {
@@ -72,7 +74,7 @@ public class ResourceAttributesFromOtelAgent {
} catch (Exception ignored) {
// ignore
}
- return Collections.emptyMap();
+ return ImmutableMap.of();
}
private static Object getField(String name, Object obj) throws Exception {
@@ -93,14 +95,11 @@ public class ResourceAttributesFromOtelAgent {
for (int i = 0; i < OTEL_JARS.length; i++) {
InputStream inputStream =
Thread.currentThread().getContextClassLoader().getResourceAsStream("lib/" + OTEL_JARS[i]);
- if (inputStream == null) {
- throw new IllegalStateException(
- "Error initializing "
- + instrumentationScopeName
- + ": lib/"
- + OTEL_JARS[i]
- + " not found in classpath.");
- }
+ checkState(
+ inputStream != null,
+ "Error initializing %s: lib/%s not found in classpath.",
+ instrumentationScopeName,
+ OTEL_JARS[i]);
File outputFile = tmpDir.resolve(OTEL_JARS[i]).toFile();
Files.copy(inputStream, outputFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
inputStream.close();
--- a/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/OpenTelemetryExporter.java
+++ b/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/OpenTelemetryExporter.java
@@ -1,5 +1,8 @@
package io.prometheus.metrics.exporter.opentelemetry;
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkState;
+
import io.opentelemetry.sdk.metrics.export.MetricReader;
import io.prometheus.metrics.config.PrometheusProperties;
import io.prometheus.metrics.model.registry.PrometheusRegistry;
@@ -60,10 +63,10 @@ public class OpenTelemetryExporter implements AutoCloseable {
* href="https://opentelemetry.io/docs/concepts/sdk-configuration/otlp-exporter-configuration/#otel_exporter_otlp_protocol">OTEL_EXPORTER_OTLP_PROTOCOL</a>.
*/
public Builder protocol(String protocol) {
- if (!protocol.equals("grpc") && !protocol.equals("http/protobuf")) {
- throw new IllegalArgumentException(
- protocol + ": Unsupported protocol. Expecting grpc or http/protobuf");
- }
+ checkArgument(
+ protocol.equals("grpc") || protocol.equals("http/protobuf"),
+ "%s: Unsupported protocol. Expecting grpc or http/protobuf",
+ protocol);
this.protocol = protocol;
return this;
}
@@ -109,9 +112,7 @@ public class OpenTelemetryExporter implements AutoCloseable {
* but in seconds rather than milliseconds.
*/
public Builder intervalSeconds(int intervalSeconds) {
- if (intervalSeconds <= 0) {
- throw new IllegalStateException(intervalSeconds + ": expecting a push interval > 0s");
- }
+ checkState(intervalSeconds > 0, "%s: expecting a push interval > 0s", intervalSeconds);
this.interval = intervalSeconds + "s";
return this;
}
@@ -124,9 +125,7 @@ public class OpenTelemetryExporter implements AutoCloseable {
* but in seconds rather than milliseconds.
*/
public Builder timeoutSeconds(int timeoutSeconds) {
- if (timeoutSeconds <= 0) {
- throw new IllegalStateException(timeoutSeconds + ": expecting a push interval > 0s");
- }
+ checkState(timeoutSeconds > 0, "%s: expecting a push interval > 0s", timeoutSeconds);
this.timeout = timeoutSeconds + "s";
return this;
}
--- a/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/PrometheusInstrumentationScope.java
+++ b/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/PrometheusInstrumentationScope.java
@@ -1,20 +1,22 @@
package io.prometheus.metrics.exporter.opentelemetry;
+import static com.google.common.base.Preconditions.checkState;
+
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import java.util.Properties;
class PrometheusInstrumentationScope {
- private static final String instrumentationScopePropertiesFile =
+ private static final String INSTRUMENTATION_SCOPE_PROPERTIES_FILE =
"instrumentationScope.properties";
- private static final String instrumentationScopeNameKey = "instrumentationScope.name";
- private static final String instrumentationScopeVersionKey = "instrumentationScope.version";
+ private static final String INSTRUMENTATION_SCOPE_NAME_KEY = "instrumentationScope.name";
+ private static final String INSTRUMENTATION_SCOPE_VERSION_KEY = "instrumentationScope.version";
public static InstrumentationScopeInfo loadInstrumentationScopeInfo() {
return loadInstrumentationScopeInfo(
- instrumentationScopePropertiesFile,
- instrumentationScopeNameKey,
- instrumentationScopeVersionKey);
+ INSTRUMENTATION_SCOPE_PROPERTIES_FILE,
+ INSTRUMENTATION_SCOPE_NAME_KEY,
+ INSTRUMENTATION_SCOPE_VERSION_KEY);
}
static InstrumentationScopeInfo loadInstrumentationScopeInfo(
@@ -24,23 +26,17 @@ class PrometheusInstrumentationScope {
properties.load(
PrometheusInstrumentationScope.class.getClassLoader().getResourceAsStream(path));
String instrumentationScopeName = properties.getProperty(nameKey);
- if (instrumentationScopeName == null) {
- throw new IllegalStateException(
- "Prometheus metrics library initialization error: "
- + nameKey
- + " not found in "
- + path
- + " in classpath.");
- }
+ checkState(
+ instrumentationScopeName != null,
+ "Prometheus metrics library initialization error: %s not found in %s in classpath.",
+ nameKey,
+ path);
String instrumentationScopeVersion = properties.getProperty(versionKey);
- if (instrumentationScopeVersion == null) {
- throw new IllegalStateException(
- "Prometheus metrics library initialization error: "
- + versionKey
- + " not found in "
- + path
- + " in classpath.");
- }
+ checkState(
+ instrumentationScopeVersion != null,
+ "Prometheus metrics library initialization error: %s not found in %s in classpath.",
+ versionKey,
+ path);
return InstrumentationScopeInfo.builder(instrumentationScopeName)
.setVersion(instrumentationScopeVersion)
.build();
--- a/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/otelmodel/PrometheusClassicHistogram.java
+++ b/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/otelmodel/PrometheusClassicHistogram.java
@@ -1,5 +1,7 @@
package io.prometheus.metrics.exporter.opentelemetry.otelmodel;
+import static com.google.common.collect.ImmutableList.toImmutableList;
+
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
import io.opentelemetry.sdk.metrics.data.HistogramData;
import io.opentelemetry.sdk.metrics.data.HistogramPointData;
@@ -10,7 +12,6 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
-import java.util.stream.Collectors;
class PrometheusClassicHistogram extends PrometheusData<HistogramPointData>
implements HistogramData {
@@ -23,7 +24,7 @@ class PrometheusClassicHistogram extends PrometheusData<HistogramPointData>
snapshot.getDataPoints().stream()
.map(dataPoint -> toOtelDataPoint(dataPoint, currentTimeMillis))
.filter(Objects::nonNull)
- .collect(Collectors.toList());
+ .collect(toImmutableList());
}
@Override
--- a/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/otelmodel/PrometheusCounter.java
+++ b/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/otelmodel/PrometheusCounter.java
@@ -1,5 +1,7 @@
package io.prometheus.metrics.exporter.opentelemetry.otelmodel;
+import static com.google.common.collect.ImmutableList.toImmutableList;
+
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
import io.opentelemetry.sdk.metrics.data.DoublePointData;
import io.opentelemetry.sdk.metrics.data.MetricDataType;
@@ -7,7 +9,6 @@ import io.opentelemetry.sdk.metrics.data.SumData;
import io.prometheus.metrics.model.snapshots.CounterSnapshot;
import java.util.Collection;
import java.util.List;
-import java.util.stream.Collectors;
class PrometheusCounter extends PrometheusData<DoublePointData>
implements SumData<DoublePointData> {
@@ -19,7 +20,7 @@ class PrometheusCounter extends PrometheusData<DoublePointData>
this.points =
snapshot.getDataPoints().stream()
.map(dataPoint -> toOtelDataPoint(dataPoint, currentTimeMillis))
- .collect(Collectors.toList());
+ .collect(toImmutableList());
}
@Override
--- a/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/otelmodel/PrometheusData.java
+++ b/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/otelmodel/PrometheusData.java
@@ -1,5 +1,8 @@
package io.prometheus.metrics.exporter.opentelemetry.otelmodel;
+import static com.google.common.collect.ImmutableList.toImmutableList;
+
+import com.google.common.collect.ImmutableList;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.api.trace.SpanContext;
@@ -15,10 +18,8 @@ import io.prometheus.metrics.model.snapshots.Exemplar;
import io.prometheus.metrics.model.snapshots.Exemplars;
import io.prometheus.metrics.model.snapshots.Label;
import io.prometheus.metrics.model.snapshots.Labels;
-import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
-import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
abstract class PrometheusData<T extends PointData> implements Data<T> {
@@ -47,7 +48,7 @@ abstract class PrometheusData<T extends PointData> implements Data<T> {
protected List<DoubleExemplarData> convertExemplar(Exemplar exemplar) {
if (exemplar == null) {
- return Collections.emptyList();
+ return ImmutableList.of();
}
return convertExemplars(Exemplars.of(exemplar));
}
@@ -55,7 +56,7 @@ abstract class PrometheusData<T extends PointData> implements Data<T> {
protected List<DoubleExemplarData> convertExemplars(Exemplars exemplars) {
return StreamSupport.stream(exemplars.spliterator(), false)
.map(this::toDoubleExemplarData)
- .collect(Collectors.toList());
+ .collect(toImmutableList());
}
protected DoubleExemplarData toDoubleExemplarData(Exemplar exemplar) {
--- a/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/otelmodel/PrometheusGauge.java
+++ b/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/otelmodel/PrometheusGauge.java
@@ -1,12 +1,13 @@
package io.prometheus.metrics.exporter.opentelemetry.otelmodel;
+import static com.google.common.collect.ImmutableList.toImmutableList;
+
import io.opentelemetry.sdk.metrics.data.DoublePointData;
import io.opentelemetry.sdk.metrics.data.GaugeData;
import io.opentelemetry.sdk.metrics.data.MetricDataType;
import io.prometheus.metrics.model.snapshots.GaugeSnapshot;
import java.util.Collection;
import java.util.List;
-import java.util.stream.Collectors;
class PrometheusGauge extends PrometheusData<DoublePointData>
implements GaugeData<DoublePointData> {
@@ -18,7 +19,7 @@ class PrometheusGauge extends PrometheusData<DoublePointData>
this.points =
snapshot.getDataPoints().stream()
.map(dataPoint -> toOtelDataPoint(dataPoint, currentTimeMillis))
- .collect(Collectors.toList());
+ .collect(toImmutableList());
}
@Override
--- a/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/otelmodel/PrometheusInfo.java
+++ b/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/otelmodel/PrometheusInfo.java
@@ -1,14 +1,15 @@
package io.prometheus.metrics.exporter.opentelemetry.otelmodel;
+import static com.google.common.collect.ImmutableList.toImmutableList;
+
+import com.google.common.collect.ImmutableList;
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
import io.opentelemetry.sdk.metrics.data.DoublePointData;
import io.opentelemetry.sdk.metrics.data.MetricDataType;
import io.opentelemetry.sdk.metrics.data.SumData;
import io.prometheus.metrics.model.snapshots.InfoSnapshot;
import java.util.Collection;
-import java.util.Collections;
import java.util.List;
-import java.util.stream.Collectors;
@SuppressWarnings("this-escape")
public class PrometheusInfo extends PrometheusData<DoublePointData>
@@ -21,7 +22,7 @@ public class PrometheusInfo extends PrometheusData<DoublePointData>
this.points =
snapshot.getDataPoints().stream()
.map(dataPoint -> toOtelDataPoint(dataPoint, currentTimeMillis))
- .collect(Collectors.toList());
+ .collect(toImmutableList());
}
@Override
@@ -46,6 +47,6 @@ public class PrometheusInfo extends PrometheusData<DoublePointData>
getStartEpochNanos(dataPoint),
getEpochNanos(dataPoint, currentTimeMillis),
labelsToAttributes(dataPoint.getLabels()),
- Collections.emptyList());
+ ImmutableList.of());
}
}
--- a/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/otelmodel/PrometheusNativeHistogram.java
+++ b/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/otelmodel/PrometheusNativeHistogram.java
@@ -1,5 +1,7 @@
package io.prometheus.metrics.exporter.opentelemetry.otelmodel;
+import static com.google.common.collect.ImmutableList.toImmutableList;
+
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
import io.opentelemetry.sdk.metrics.data.ExponentialHistogramBuckets;
import io.opentelemetry.sdk.metrics.data.ExponentialHistogramData;
@@ -10,7 +12,6 @@ import io.prometheus.metrics.model.snapshots.NativeHistogramBuckets;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
-import java.util.stream.Collectors;
class PrometheusNativeHistogram extends PrometheusData<ExponentialHistogramPointData>
implements ExponentialHistogramData {
@@ -23,7 +24,7 @@ class PrometheusNativeHistogram extends PrometheusData<ExponentialHistogramPoint
snapshot.getDataPoints().stream()
.map(dataPoint -> toOtelDataPoint(dataPoint, currentTimeMillis))
.filter(Objects::nonNull)
- .collect(Collectors.toList());
+ .collect(toImmutableList());
}
@Override
--- a/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/otelmodel/PrometheusStateSet.java
+++ b/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/otelmodel/PrometheusStateSet.java
@@ -1,5 +1,6 @@
package io.prometheus.metrics.exporter.opentelemetry.otelmodel;
+import com.google.common.collect.ImmutableList;
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
import io.opentelemetry.sdk.metrics.data.DoublePointData;
import io.opentelemetry.sdk.metrics.data.MetricDataType;
@@ -8,7 +9,6 @@ import io.prometheus.metrics.model.snapshots.Labels;
import io.prometheus.metrics.model.snapshots.StateSetSnapshot;
import java.util.ArrayList;
import java.util.Collection;
-import java.util.Collections;
import java.util.List;
public class PrometheusStateSet extends PrometheusData<DoublePointData>
@@ -55,6 +55,6 @@ public class PrometheusStateSet extends PrometheusData<DoublePointData>
dataPoint
.getLabels()
.merge(Labels.of(snapshot.getMetadata().getName(), dataPoint.getName(i)))),
- Collections.emptyList());
+ ImmutableList.of());
}
}
--- a/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/otelmodel/PrometheusSummary.java
+++ b/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/otelmodel/PrometheusSummary.java
@@ -1,5 +1,7 @@
package io.prometheus.metrics.exporter.opentelemetry.otelmodel;
+import static com.google.common.collect.ImmutableList.toImmutableList;
+
import io.opentelemetry.sdk.metrics.data.MetricDataType;
import io.opentelemetry.sdk.metrics.data.SummaryData;
import io.opentelemetry.sdk.metrics.data.SummaryPointData;
@@ -7,7 +9,6 @@ import io.prometheus.metrics.model.snapshots.Quantile;
import io.prometheus.metrics.model.snapshots.SummarySnapshot;
import java.util.Collection;
import java.util.List;
-import java.util.stream.Collectors;
class PrometheusSummary extends PrometheusData<SummaryPointData> implements SummaryData {
@@ -18,7 +19,7 @@ class PrometheusSummary extends PrometheusData<SummaryPointData> implements Summ
this.points =
snapshot.getDataPoints().stream()
.map(dataPoint -> toOtelDataPoint(dataPoint, currentTimeMillis))
- .collect(Collectors.toList());
+ .collect(toImmutableList());
}
@Override
--- a/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/otelmodel/PrometheusUnknown.java
+++ b/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/otelmodel/PrometheusUnknown.java
@@ -1,12 +1,13 @@
package io.prometheus.metrics.exporter.opentelemetry.otelmodel;
+import static com.google.common.collect.ImmutableList.toImmutableList;
+
import io.opentelemetry.sdk.metrics.data.DoublePointData;
import io.opentelemetry.sdk.metrics.data.GaugeData;
import io.opentelemetry.sdk.metrics.data.MetricDataType;
import io.prometheus.metrics.model.snapshots.UnknownSnapshot;
import java.util.Collection;
import java.util.List;
-import java.util.stream.Collectors;
class PrometheusUnknown extends PrometheusData<DoublePointData>
implements GaugeData<DoublePointData> {
@@ -18,7 +19,7 @@ class PrometheusUnknown extends PrometheusData<DoublePointData>
this.points =
snapshot.getDataPoints().stream()
.map(dataPoint -> toOtelDataPoint(dataPoint, currentTimeMillis))
- .collect(Collectors.toList());
+ .collect(toImmutableList());
}
@Override
--- a/prometheus-metrics-exporter-opentelemetry/src/test/java/io/prometheus/metrics/exporter/opentelemetry/ExemplarTest.java
+++ b/prometheus-metrics-exporter-opentelemetry/src/test/java/io/prometheus/metrics/exporter/opentelemetry/ExemplarTest.java
@@ -9,7 +9,7 @@ import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
import static java.util.concurrent.TimeUnit.SECONDS;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.awaitility.Awaitility.await;
import com.github.tomakehurst.wiremock.http.Request;
@@ -35,7 +35,7 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@WireMockTest(httpPort = 4317)
-class ExemplarTest {
+final class ExemplarTest {
private static final String ENDPOINT_PATH = "/v1/metrics";
private static final int TIMEOUT = 3;
private static final String INSTRUMENTATION_SCOPE_NAME = "testInstrumentationScope";
@@ -45,7 +45,7 @@ class ExemplarTest {
private OpenTelemetryExporter openTelemetryExporter;
@BeforeEach
- public void setUp() {
+ void setUp() {
openTelemetryExporter =
OpenTelemetryExporter.builder()
.endpoint("http://localhost:4317")
@@ -64,14 +64,14 @@ class ExemplarTest {
}
@AfterEach
- public void tearDown() {
+ void tearDown() {
PrometheusRegistry.defaultRegistry.unregister(testCounter);
openTelemetryExporter.close();
}
- @Test
@SuppressWarnings("try")
- public void sampledExemplarIsForwarded() {
+ @Test
+ void sampledExemplarIsForwarded() {
try (SdkTracerProvider sdkTracerProvider =
SdkTracerProvider.builder().setSampler(Sampler.alwaysOn()).build()) {
@@ -95,9 +95,9 @@ class ExemplarTest {
});
}
- @Test
@SuppressWarnings("try")
- public void notSampledExemplarIsNotForwarded() {
+ @Test
+ void notSampledExemplarIsNotForwarded() {
try (SdkTracerProvider sdkTracerProvider =
SdkTracerProvider.builder().setSampler(Sampler.alwaysOff()).build()) {
@@ -108,8 +108,7 @@ class ExemplarTest {
}
}
- assertThatExceptionOfType(ConditionTimeoutException.class)
- .isThrownBy(
+ assertThatThrownBy(
() ->
await()
.atMost(TIMEOUT, SECONDS)
@@ -122,7 +121,8 @@ class ExemplarTest {
.withHeader("Content-Type", equalTo("application/x-protobuf"))
.andMatching(getExemplarCountMatcher(1)));
return true;
- }));
+ }))
+ .isInstanceOf(ConditionTimeoutException.class);
}
private static ValueMatcher<Request> getExemplarCountMatcher(int expectedCount) {
--- a/prometheus-metrics-exporter-opentelemetry/src/test/java/io/prometheus/metrics/exporter/opentelemetry/ExportTest.java
+++ b/prometheus-metrics-exporter-opentelemetry/src/test/java/io/prometheus/metrics/exporter/opentelemetry/ExportTest.java
@@ -2,6 +2,7 @@ package io.prometheus.metrics.exporter.opentelemetry;
import static org.assertj.core.api.Assertions.assertThat;
+import com.google.common.collect.ImmutableList;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
@@ -23,13 +24,12 @@ import io.prometheus.metrics.model.snapshots.Labels;
import io.prometheus.metrics.model.snapshots.Unit;
import io.prometheus.metrics.model.snapshots.UnknownSnapshot;
import java.lang.reflect.Field;
-import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
-public class ExportTest {
+final class ExportTest {
private static final Attributes ATTRIBUTES =
Attributes.of(AttributeKey.stringKey("label"), "val", AttributeKey.stringKey("key"), "value");
@@ -173,7 +173,7 @@ public class ExportTest {
buckets
.hasOffset(-1)
.hasTotalCount(1)
- .hasCounts(Collections.singletonList(1L)))));
+ .hasCounts(ImmutableList.of(1L)))));
}
@Test
--- a/prometheus-metrics-exporter-opentelemetry/src/test/java/io/prometheus/metrics/exporter/opentelemetry/OtelAutoConfigTest.java
+++ b/prometheus-metrics-exporter-opentelemetry/src/test/java/io/prometheus/metrics/exporter/opentelemetry/OtelAutoConfigTest.java
@@ -1,6 +1,7 @@
package io.prometheus.metrics.exporter.opentelemetry;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.params.provider.Arguments.arguments;
import com.google.common.collect.ImmutableMap;
import io.opentelemetry.api.common.AttributeKey;
@@ -9,7 +10,6 @@ import io.opentelemetry.sdk.autoconfigure.internal.AutoConfigureUtil;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.prometheus.metrics.config.ExporterOpenTelemetryProperties;
import io.prometheus.metrics.config.PrometheusPropertiesLoader;
-import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
@@ -21,12 +21,12 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
-class OtelAutoConfigTest {
+final class OtelAutoConfigTest {
static class TestCase {
Map<String, String> systemProperties = new HashMap<>();
- Map<String, Optional<String>> expectedProperties = Collections.emptyMap();
- Map<String, String> expectedResourceAttributes = Collections.emptyMap();
+ Map<String, Optional<String>> expectedProperties = ImmutableMap.of();
+ Map<String, String> expectedResourceAttributes = ImmutableMap.of();
Consumer<OpenTelemetryExporter.Builder> exporterBuilder;
Consumer<ExporterOpenTelemetryProperties.Builder> propertiesBuilder;
@@ -61,7 +61,7 @@ class OtelAutoConfigTest {
public static Stream<Arguments> testCases() {
return Stream.of(
- Arguments.of(
+ arguments(
"values from builder",
new TestCase()
.expectedProperties(
@@ -79,7 +79,7 @@ class OtelAutoConfigTest {
"otel.service.name",
Optional.of("builder-service")))
.expectedResourceAttributes(
- Map.of(
+ ImmutableMap.of(
"key",
"builder-value",
"service.name",
@@ -91,14 +91,14 @@ class OtelAutoConfigTest {
"service.version",
"builder-version"))
.exporterBuilder(OtelAutoConfigTest::setBuilderValues)),
- Arguments.of(
+ arguments(
"builder endpoint with path",
new TestCase()
.expectedProperties(
ImmutableMap.of(
"otel.exporter.otlp.endpoint", Optional.of("http://builder:4318/")))
.exporterBuilder(builder -> builder.endpoint("http://builder:4318/v1/metrics"))),
- Arguments.of(
+ arguments(
"values from otel have precedence over builder",
new TestCase()
.expectedProperties(
@@ -128,7 +128,7 @@ class OtelAutoConfigTest {
"otel-version"))
.exporterBuilder(OtelAutoConfigTest::setBuilderValues)
.systemProperties(otelOverrides())),
- Arguments.of(
+ arguments(
"values from prom properties have precedence over builder and otel",
new TestCase()
.expectedProperties(
@@ -177,7 +177,7 @@ class OtelAutoConfigTest {
"io.prometheus.exporter.opentelemetry.resourceAttributes",
"key=prom-value")
.build())),
- Arguments.of(
+ arguments(
"values from prom properties builder have precedence over builder and otel",
new TestCase()
.expectedProperties(
@@ -250,8 +250,8 @@ class OtelAutoConfigTest {
.resourceAttribute("key", "builder-value");
}
- @ParameterizedTest(name = "{0}")
@MethodSource("testCases")
+ @ParameterizedTest(name = "{0}")
void properties(String name, TestCase testCase) {
testCase.systemProperties.forEach(System::setProperty);
@@ -272,9 +272,10 @@ class OtelAutoConfigTest {
OtelAutoConfig.getResourceField(sdk).getAttributes().asMap();
testCase.expectedProperties.forEach(
(key, value) -> {
- AbstractStringAssert<?> o = assertThat(config.getString(key)).describedAs("key=" + key);
+ AbstractStringAssert<?> o =
+ assertThat(config.getString(key)).describedAs("key=%s", key);
if (value.isPresent()) {
- o.isEqualTo(value.get());
+ o.isEqualTo(value.orElseThrow());
} else {
o.isNull();
}
@@ -282,7 +283,7 @@ class OtelAutoConfigTest {
testCase.expectedResourceAttributes.forEach(
(key, value) ->
assertThat(map.get(AttributeKey.stringKey(key)))
- .describedAs("key=" + key)
+ .describedAs("key=%s", key)
.hasToString(value));
} finally {
testCase.systemProperties.keySet().forEach(System::clearProperty);
--- a/prometheus-metrics-exporter-opentelemetry/src/test/java/io/prometheus/metrics/exporter/opentelemetry/PrometheusInstrumentationScopeTest.java
+++ b/prometheus-metrics-exporter-opentelemetry/src/test/java/io/prometheus/metrics/exporter/opentelemetry/PrometheusInstrumentationScopeTest.java
@@ -1,38 +1,38 @@
package io.prometheus.metrics.exporter.opentelemetry;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
-class PrometheusInstrumentationScopeTest {
+final class PrometheusInstrumentationScopeTest {
@Test
void loadInstrumentationScopeInfo() {
- assertThatExceptionOfType(IllegalStateException.class)
- .isThrownBy(
+ assertThatThrownBy(
() ->
PrometheusInstrumentationScope.loadInstrumentationScopeInfo(
"path", "name", "version"))
- .withMessage(
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessage(
"Prometheus metrics library initialization error: Failed to read path from classpath.");
- assertThatExceptionOfType(IllegalStateException.class)
- .isThrownBy(
+ assertThatThrownBy(
() ->
PrometheusInstrumentationScope.loadInstrumentationScopeInfo(
"instrumentationScope.properties", "name", "version"))
- .havingRootCause()
- .withMessage(
+ .isInstanceOf(IllegalStateException.class)
+ .rootCause()
+ .hasMessage(
"Prometheus metrics library initialization error: name not found in instrumentationScope.properties in classpath.");
- assertThatExceptionOfType(IllegalStateException.class)
- .isThrownBy(
+ assertThatThrownBy(
() ->
PrometheusInstrumentationScope.loadInstrumentationScopeInfo(
"instrumentationScope.properties", "instrumentationScope.name", "version"))
- .havingRootCause()
- .withMessage(
+ .isInstanceOf(IllegalStateException.class)
+ .rootCause()
+ .hasMessage(
"Prometheus metrics library initialization error: version not found in instrumentationScope.properties in classpath.");
}
}
--- a/prometheus-metrics-exporter-opentelemetry/src/test/java/io/prometheus/metrics/exporter/opentelemetry/otelmodel/PrometheusMetricDataTest.java
+++ b/prometheus-metrics-exporter-opentelemetry/src/test/java/io/prometheus/metrics/exporter/opentelemetry/otelmodel/PrometheusMetricDataTest.java
@@ -7,7 +7,7 @@ import io.prometheus.metrics.model.snapshots.Unit;
import java.util.Map;
import org.junit.jupiter.api.Test;
-class PrometheusMetricDataTest {
+final class PrometheusMetricDataTest {
Map<Object, Object> translations =
Map.ofEntries(
--- a/prometheus-metrics-exporter-pushgateway/src/main/java/io/prometheus/metrics/exporter/pushgateway/DefaultJobLabelDetector.java
+++ b/prometheus-metrics-exporter-pushgateway/src/main/java/io/prometheus/metrics/exporter/pushgateway/DefaultJobLabelDetector.java
@@ -3,7 +3,6 @@ package io.prometheus.metrics.exporter.pushgateway;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
-import java.nio.file.Paths;
/**
* The default {@code job} label is the name of the JAR file being executed.
@@ -51,7 +50,7 @@ class DefaultJobLabelDetector {
private static Path pathIfExists(String programArguments) {
Path candidate;
try {
- candidate = Paths.get(programArguments);
+ candidate = Path.of(programArguments);
} catch (InvalidPathException e) {
return null;
}
--- a/prometheus-metrics-exporter-pushgateway/src/main/java/io/prometheus/metrics/exporter/pushgateway/PushGateway.java
+++ b/prometheus-metrics-exporter-pushgateway/src/main/java/io/prometheus/metrics/exporter/pushgateway/PushGateway.java
@@ -1,6 +1,9 @@
package io.prometheus.metrics.exporter.pushgateway;
import static io.prometheus.metrics.exporter.pushgateway.Scheme.HTTP;
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static java.util.Collections.unmodifiableMap;
+import static java.util.Objects.requireNonNull;
import io.prometheus.metrics.config.ExporterPushgatewayProperties;
import io.prometheus.metrics.config.PrometheusProperties;
@@ -23,9 +26,7 @@ import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import java.net.UnknownHostException;
-import java.nio.charset.StandardCharsets;
import java.util.Base64;
-import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
@@ -92,7 +93,7 @@ public class PushGateway {
Map<String, String> requestHeaders) {
this.registry = registry;
this.url = url;
- this.requestHeaders = Collections.unmodifiableMap(new HashMap<>(requestHeaders));
+ this.requestHeaders = unmodifiableMap(new HashMap<>(requestHeaders));
this.connectionFactory = connectionFactory;
writer = getWriter(format);
if (!writer.isAvailable()) {
@@ -275,9 +276,7 @@ public class PushGateway {
/** Default is {@link Format#PROMETHEUS_PROTOBUF}. */
public Builder format(Format format) {
- if (format == null) {
- throw new NullPointerException();
- }
+ requireNonNull(format);
this.format = format;
return this;
}
@@ -288,9 +287,7 @@ public class PushGateway {
* property.
*/
public Builder address(String address) {
- if (address == null) {
- throw new NullPointerException();
- }
+ requireNonNull(address);
this.address = address;
return this;
}
@@ -300,7 +297,7 @@ public class PushGateway {
if (user == null || password == null) {
throw new NullPointerException();
}
- byte[] credentialsBytes = (user + ":" + password).getBytes(StandardCharsets.UTF_8);
+ byte[] credentialsBytes = (user + ":" + password).getBytes(UTF_8);
String encoded = Base64.getEncoder().encodeToString(credentialsBytes);
requestHeaders.put("Authorization", String.format("Basic %s", encoded));
return this;
@@ -308,9 +305,7 @@ public class PushGateway {
/** Bearer token authorization when pushing to the Pushgateway. */
public Builder bearerToken(String token) {
- if (token == null) {
- throw new NullPointerException();
- }
+ requireNonNull(token);
requestHeaders.put("Authorization", String.format("Bearer %s", token));
return this;
}
@@ -320,9 +315,7 @@ public class PushGateway {
* at runtime with the {@code io.prometheus.exporter.pushgateway.scheme} property.
*/
public Builder scheme(Scheme scheme) {
- if (scheme == null) {
- throw new NullPointerException();
- }
+ requireNonNull(scheme);
this.scheme = scheme;
return this;
}
@@ -334,9 +327,7 @@ public class PushGateway {
* of a custom connection factory that skips SSL certificate validation for HTTPS connections.
*/
public Builder connectionFactory(HttpConnectionFactory connectionFactory) {
- if (connectionFactory == null) {
- throw new NullPointerException();
- }
+ requireNonNull(connectionFactory);
this.connectionFactory = connectionFactory;
return this;
}
@@ -347,9 +338,7 @@ public class PushGateway {
* io.prometheus.exporter.pushgateway.job} property.
*/
public Builder job(String job) {
- if (job == null) {
- throw new NullPointerException();
- }
+ requireNonNull(job);
this.job = job;
return this;
}
@@ -373,9 +362,7 @@ public class PushGateway {
/** Push metrics from this registry instead of {@link PrometheusRegistry#defaultRegistry}. */
public Builder registry(PrometheusRegistry registry) {
- if (registry == null) {
- throw new NullPointerException();
- }
+ requireNonNull(registry);
this.registry = registry;
return this;
}
@@ -443,7 +430,7 @@ public class PushGateway {
private String base64url(String v) {
return Base64.getEncoder()
- .encodeToString(v.getBytes(StandardCharsets.UTF_8))
+ .encodeToString(v.getBytes(UTF_8))
.replace("+", "-")
.replace("/", "_");
}
--- a/prometheus-metrics-exporter-pushgateway/src/test/java/io/prometheus/metrics/exporter/pushgateway/BasicAuthPushGatewayTest.java
+++ b/prometheus-metrics-exporter-pushgateway/src/test/java/io/prometheus/metrics/exporter/pushgateway/BasicAuthPushGatewayTest.java
@@ -12,7 +12,7 @@ import org.junit.jupiter.api.Test;
import org.mockserver.client.MockServerClient;
import org.mockserver.integration.ClientAndServer;
-class BasicAuthPushGatewayTest {
+final class BasicAuthPushGatewayTest {
private MockServerClient mockServerClient;
PrometheusRegistry registry;
@@ -20,7 +20,7 @@ class BasicAuthPushGatewayTest {
PushGateway pushGateway;
@BeforeEach
- public void setUp() {
+ void setUp() {
mockServerClient = ClientAndServer.startClientAndServer(0);
registry = new PrometheusRegistry();
gauge = Gauge.builder().name("g").help("help").build();
@@ -39,7 +39,7 @@ class BasicAuthPushGatewayTest {
}
@Test
- public void testAuthorizedPush() throws IOException {
+ void authorizedPush() throws IOException {
mockServerClient
.when(
request()
--- a/prometheus-metrics-exporter-pushgateway/src/test/java/io/prometheus/metrics/exporter/pushgateway/BearerTokenPushGatewayTest.java
+++ b/prometheus-metrics-exporter-pushgateway/src/test/java/io/prometheus/metrics/exporter/pushgateway/BearerTokenPushGatewayTest.java
@@ -12,7 +12,7 @@ import org.junit.jupiter.api.Test;
import org.mockserver.client.MockServerClient;
import org.mockserver.integration.ClientAndServer;
-class BearerTokenPushGatewayTest {
+final class BearerTokenPushGatewayTest {
private MockServerClient mockServerClient;
@@ -21,7 +21,7 @@ class BearerTokenPushGatewayTest {
PushGateway pushGateway;
@BeforeEach
- public void setUp() {
+ void setUp() {
mockServerClient = ClientAndServer.startClientAndServer(0);
registry = new PrometheusRegistry();
gauge = Gauge.builder().name("g").help("help").build();
@@ -40,7 +40,7 @@ class BearerTokenPushGatewayTest {
}
@Test
- public void testAuthorizedPush() throws IOException {
+ void authorizedPush() throws IOException {
mockServerClient
.when(
request()
--- a/prometheus-metrics-exporter-pushgateway/src/test/java/io/prometheus/metrics/exporter/pushgateway/PushGatewayTest.java
+++ b/prometheus-metrics-exporter-pushgateway/src/test/java/io/prometheus/metrics/exporter/pushgateway/PushGatewayTest.java
@@ -2,6 +2,7 @@ package io.prometheus.metrics.exporter.pushgateway;
import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response;
@@ -17,7 +18,7 @@ import org.junit.jupiter.api.Test;
import org.mockserver.client.MockServerClient;
import org.mockserver.integration.ClientAndServer;
-class PushGatewayTest {
+final class PushGatewayTest {
private MockServerClient mockServerClient;
@@ -25,7 +26,7 @@ class PushGatewayTest {
Gauge gauge;
@BeforeEach
- public void setUp() {
+ void setUp() {
mockServerClient = ClientAndServer.startClientAndServer(0);
registry = new PrometheusRegistry();
gauge = Gauge.builder().name("g").help("help").build();
@@ -37,7 +38,7 @@ class PushGatewayTest {
}
@Test
- public void testInvalidURLThrowsRuntimeException() {
+ void invalidURLThrowsRuntimeException() {
assertThatExceptionOfType(RuntimeException.class)
.isThrownBy(
() -> {
@@ -48,8 +49,7 @@ class PushGatewayTest {
}
@Test
- public void testMultipleSlashesAreStrippedFromURL()
- throws NoSuchFieldException, IllegalAccessException {
+ void multipleSlashesAreStrippedFromURL() throws NoSuchFieldException, IllegalAccessException {
final PushGateway pushGateway =
PushGateway.builder().address("example.com:1234/context///path//").job("test").build();
assertThat(getUrl(pushGateway))
@@ -63,7 +63,7 @@ class PushGatewayTest {
}
@Test
- public void testPush() throws IOException {
+ void push() throws IOException {
mockServerClient
.when(request().withMethod("PUT").withPath("/metrics/job/j"))
.respond(response().withStatusCode(202));
@@ -77,7 +77,7 @@ class PushGatewayTest {
}
@Test
- public void testPush200Response() throws IOException {
+ void push200Response() throws IOException {
mockServerClient
.when(request().withMethod("PUT").withPath("/metrics/job/j"))
.respond(response().withStatusCode(200));
@@ -91,12 +91,11 @@ class PushGatewayTest {
}
@Test
- public void testNon202ResponseThrows() {
+ void non202ResponseThrows() {
mockServerClient
.when(request().withMethod("PUT").withPath("/metrics/job/j"))
.respond(response().withStatusCode(500));
- assertThatExceptionOfType(IOException.class)
- .isThrownBy(
+ assertThatThrownBy(
() -> {
PushGateway pg =
PushGateway.builder()
@@ -106,14 +105,14 @@ class PushGatewayTest {
.build();
pg.push();
})
- .withMessageContaining(
- "Response code from http://localhost:"
- + mockServerClient.getPort()
- + "/metrics/job/j was 500");
+ .isInstanceOf(IOException.class)
+ .hasMessageContaining(
+ "Response code from http://localhost:%s/metrics/job/j was 500",
+ mockServerClient.getPort());
}
@Test
- public void testPushCollector() throws IOException {
+ void pushCollector() throws IOException {
mockServerClient
.when(request().withMethod("PUT").withPath("/metrics/job/j"))
.respond(response().withStatusCode(202));
@@ -127,7 +126,7 @@ class PushGatewayTest {
}
@Test
- public void testPushWithGroupingKey() throws IOException {
+ void pushWithGroupingKey() throws IOException {
mockServerClient
.when(request().withMethod("PUT").withPath("/metrics/job/j/l/v"))
.respond(response().withStatusCode(202));
@@ -142,7 +141,7 @@ class PushGatewayTest {
}
@Test
- public void testPushWithMultiGroupingKey() throws IOException {
+ void pushWithMultiGroupingKey() throws IOException {
mockServerClient
.when(request().withMethod("PUT").withPath("/metrics/job/j/l/v/l2/v2"))
.respond(response().withStatusCode(202));
@@ -158,7 +157,7 @@ class PushGatewayTest {
}
@Test
- public void testPushWithEmptyLabelGroupingKey() throws IOException {
+ void pushWithEmptyLabelGroupingKey() throws IOException {
mockServerClient
.when(request().withMethod("PUT").withPath("/metrics/job/j/l/v/l2@base64/="))
.respond(response().withStatusCode(202));
@@ -174,7 +173,7 @@ class PushGatewayTest {
}
@Test
- public void testPushWithGroupingKeyWithSlashes() throws IOException {
+ void pushWithGroupingKeyWithSlashes() throws IOException {
mockServerClient
.when(
request().withMethod("PUT").withPath("/metrics/job@base64/YS9i/l/v/l2@base64/75-_Lw=="))
@@ -191,7 +190,7 @@ class PushGatewayTest {
}
@Test
- public void testPushCollectorWithGroupingKey() throws IOException {
+ void pushCollectorWithGroupingKey() throws IOException {
mockServerClient
.when(request().withMethod("PUT").withPath("/metrics/job/j/l/v"))
.respond(response().withStatusCode(202));
@@ -206,7 +205,7 @@ class PushGatewayTest {
}
@Test
- public void testPushAdd() throws IOException {
+ void pushAdd() throws IOException {
mockServerClient
.when(request().withMethod("POST").withPath("/metrics/job/j"))
.respond(response().withStatusCode(202));
@@ -220,7 +219,7 @@ class PushGatewayTest {
}
@Test
- public void testPushAddCollector() throws IOException {
+ void pushAddCollector() throws IOException {
mockServerClient
.when(request().withMethod("POST").withPath("/metrics/job/j"))
.respond(response().withStatusCode(202));
@@ -230,7 +229,7 @@ class PushGatewayTest {
}
@Test
- public void testPushAddWithGroupingKey() throws IOException {
+ void pushAddWithGroupingKey() throws IOException {
mockServerClient
.when(request().withMethod("POST").withPath("/metrics/job/j/l/v"))
.respond(response().withStatusCode(202));
@@ -245,7 +244,7 @@ class PushGatewayTest {
}
@Test
- public void testPushAddCollectorWithGroupingKey() throws IOException {
+ void pushAddCollectorWithGroupingKey() throws IOException {
mockServerClient
.when(request().withMethod("POST").withPath("/metrics/job/j/l/v"))
.respond(response().withStatusCode(202));
@@ -260,7 +259,7 @@ class PushGatewayTest {
}
@Test
- public void testDelete() throws IOException {
+ void delete() throws IOException {
mockServerClient
.when(request().withMethod("DELETE").withPath("/metrics/job/j"))
.respond(response().withStatusCode(202));
@@ -270,7 +269,7 @@ class PushGatewayTest {
}
@Test
- public void testDeleteWithGroupingKey() throws IOException {
+ void deleteWithGroupingKey() throws IOException {
mockServerClient
.when(request().withMethod("DELETE").withPath("/metrics/job/j/l/v"))
.respond(response().withStatusCode(202));
@@ -284,7 +283,7 @@ class PushGatewayTest {
}
@Test
- public void testInstanceIpGroupingKey() throws IOException {
+ void instanceIpGroupingKey() throws IOException {
String ip = InetAddress.getLocalHost().getHostAddress();
assertThat(ip).isNotEmpty();
mockServerClient
--- a/prometheus-metrics-exporter-pushgateway/src/test/java/io/prometheus/metrics/exporter/pushgateway/SchemeTest.java
+++ b/prometheus-metrics-exporter-pushgateway/src/test/java/io/prometheus/metrics/exporter/pushgateway/SchemeTest.java
@@ -1,18 +1,18 @@
package io.prometheus.metrics.exporter.pushgateway;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import org.junit.jupiter.api.Test;
-class SchemeTest {
+final class SchemeTest {
@Test
void fromString() {
assertThat(Scheme.HTTP).hasToString("http");
assertThat(Scheme.HTTPS).hasToString("https");
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> Scheme.fromString("ftp"))
- .withMessage("ftp: Unsupported scheme. Expecting 'http' or 'https'.");
+ assertThatThrownBy(() -> Scheme.fromString("ftp"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("ftp: Unsupported scheme. Expecting 'http' or 'https'.");
}
}
--- a/prometheus-metrics-exposition-formats/src/test/java/io/prometheus/metrics/expositionformats/ExpositionFormatsTest.java
+++ b/prometheus-metrics-exposition-formats/src/test/java/io/prometheus/metrics/expositionformats/ExpositionFormatsTest.java
@@ -30,7 +30,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.junit.jupiter.api.Test;
-class ExpositionFormatsTest {
+final class ExpositionFormatsTest {
private final String exemplar1String =
"{env=\"prod\",span_id=\"12345\",trace_id=\"abcde\"} 1.7 1672850685.829";
@@ -107,7 +107,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testCounterComplete() throws IOException {
+ void counterComplete() throws IOException {
String openMetricsText =
"# TYPE service_time_seconds counter\n"
+ "# UNIT service_time_seconds seconds\n"
@@ -235,7 +235,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testCounterMinimal() throws IOException {
+ void counterMinimal() throws IOException {
String openMetricsText = "# TYPE my_counter counter\n" + "my_counter_total 1.1\n" + "# EOF\n";
String prometheusText = "# TYPE my_counter_total counter\n" + "my_counter_total 1.1\n";
String prometheusProtobuf =
@@ -253,7 +253,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testCounterWithDots() throws IOException {
+ void counterWithDots() throws IOException {
String openMetricsText =
"# TYPE my_request_count counter\n"
+ "my_request_count_total{http_path=\"/hello\"} 3.0 # "
@@ -293,7 +293,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testGaugeComplete() throws IOException {
+ void gaugeComplete() throws IOException {
String openMetricsText =
"# TYPE disk_usage_ratio gauge\n"
+ "# UNIT disk_usage_ratio ratio\n"
@@ -374,7 +374,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testGaugeMinimal() throws IOException {
+ void gaugeMinimal() throws IOException {
String openMetricsText =
"# TYPE temperature_centigrade gauge\n" + "temperature_centigrade 22.3\n" + "# EOF\n";
String prometheusText =
@@ -394,7 +394,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testGaugeWithDots() throws IOException {
+ void gaugeWithDots() throws IOException {
String openMetricsText =
"# TYPE my_temperature_celsius gauge\n"
+ "# UNIT my_temperature_celsius celsius\n"
@@ -446,7 +446,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testSummaryComplete() throws IOException {
+ void summaryComplete() throws IOException {
String openMetricsText =
"# TYPE http_request_duration_seconds summary\n"
+ "# UNIT http_request_duration_seconds seconds\n"
@@ -739,7 +739,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testSummaryWithoutQuantiles() throws IOException {
+ void summaryWithoutQuantiles() throws IOException {
String openMetricsText =
"# TYPE latency_seconds summary\n"
+ "# UNIT latency_seconds seconds\n"
@@ -779,7 +779,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testSummaryNoCountAndSum() throws IOException {
+ void summaryNoCountAndSum() throws IOException {
String openMetricsText =
"# TYPE latency_seconds summary\n"
+ "latency_seconds{quantile=\"0.95\"} 200.0\n"
@@ -812,7 +812,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testSummaryJustCount() throws IOException {
+ void summaryJustCount() throws IOException {
String openMetricsText =
"# TYPE latency_seconds summary\n" + "latency_seconds_count 1\n" + "# EOF\n";
String prometheusText = "# TYPE latency_seconds summary\n" + "latency_seconds_count 1\n";
@@ -839,7 +839,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testSummaryJustSum() throws IOException {
+ void summaryJustSum() throws IOException {
String openMetricsText =
"# TYPE latency_seconds summary\n" + "latency_seconds_sum 12.3\n" + "# EOF\n";
String prometheusText = "# TYPE latency_seconds summary\n" + "latency_seconds_sum 12.3\n";
@@ -866,7 +866,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testSummaryEmptyData() throws IOException {
+ void summaryEmptyData() throws IOException {
// SummaryData can be present but empty (no count, no sum, no quantiles).
// This should be treated like no data is present.
SummarySnapshot summary =
@@ -884,7 +884,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testSummaryEmptyAndNonEmpty() throws IOException {
+ void summaryEmptyAndNonEmpty() throws IOException {
String openMetricsText =
"# TYPE latency_seconds summary\n"
+ "latency_seconds_count{path=\"/v2\"} 2\n"
@@ -926,7 +926,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testSummaryWithDots() throws IOException {
+ void summaryWithDots() throws IOException {
String openMetricsText =
"# TYPE my_request_duration_seconds summary\n"
+ "# UNIT my_request_duration_seconds seconds\n"
@@ -980,7 +980,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testClassicHistogramComplete() throws Exception {
+ void classicHistogramComplete() throws Exception {
String openMetricsText =
"# TYPE response_size_bytes histogram\n"
+ "# UNIT response_size_bytes bytes\n"
@@ -1286,7 +1286,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testClassicHistogramMinimal() throws Exception {
+ void classicHistogramMinimal() throws Exception {
// In OpenMetrics a histogram can have a _count if and only if it has a _sum.
// In Prometheus format, a histogram can have a _count without a _sum.
String openMetricsText =
@@ -1330,7 +1330,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testClassicHistogramCountAndSum() throws Exception {
+ void classicHistogramCountAndSum() throws Exception {
String openMetricsText =
"# TYPE request_latency_seconds histogram\n"
+ "request_latency_seconds_bucket{le=\"+Inf\"} 2\n"
@@ -1377,7 +1377,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testClassicGaugeHistogramComplete() throws IOException {
+ void classicGaugeHistogramComplete() throws IOException {
String openMetricsText =
"# TYPE cache_size_bytes gaugehistogram\n"
+ "# UNIT cache_size_bytes bytes\n"
@@ -1673,7 +1673,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testClassicGaugeHistogramMinimal() throws IOException {
+ void classicGaugeHistogramMinimal() throws IOException {
// In OpenMetrics a histogram can have a _count if and only if it has a _sum.
// In Prometheus format, a histogram can have a _count without a _sum.
String openMetricsText =
@@ -1719,7 +1719,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testClassicGaugeHistogramCountAndSum() throws IOException {
+ void classicGaugeHistogramCountAndSum() throws IOException {
String openMetricsText =
"# TYPE queue_size_bytes gaugehistogram\n"
+ "queue_size_bytes_bucket{le=\"+Inf\"} 130\n"
@@ -1769,7 +1769,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testClassicHistogramWithDots() throws IOException {
+ void classicHistogramWithDots() throws IOException {
String openMetricsText =
"# TYPE my_request_duration_seconds histogram\n"
+ "# UNIT my_request_duration_seconds seconds\n"
@@ -1839,7 +1839,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testNativeHistogramComplete() throws IOException {
+ void nativeHistogramComplete() throws IOException {
String openMetricsText =
"# TYPE response_size_bytes histogram\n"
+ "# UNIT response_size_bytes bytes\n"
@@ -2150,7 +2150,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testNativeHistogramMinimal() throws IOException {
+ void nativeHistogramMinimal() throws IOException {
String openMetricsText =
"# TYPE latency_seconds histogram\n"
+ "latency_seconds_bucket{le=\"+Inf\"} 0\n"
@@ -2184,7 +2184,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testNativeHistogramWithDots() throws IOException {
+ void nativeHistogramWithDots() throws IOException {
String openMetricsText =
"# TYPE my_request_duration_seconds histogram\n"
+ "# UNIT my_request_duration_seconds seconds\n"
@@ -2261,7 +2261,7 @@ class ExpositionFormatsTest {
// TODO: Gauge Native Histogram
@Test
- public void testInfo() throws IOException {
+ void info() throws IOException {
String openMetrics =
"# TYPE version info\n"
+ "# HELP version version information\n"
@@ -2287,7 +2287,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testInfoWithDots() throws IOException {
+ void infoWithDots() throws IOException {
String openMetricsText =
"# TYPE jvm_status info\n"
+ "# HELP jvm_status JVM status info\n"
@@ -2322,7 +2322,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testStateSetComplete() throws IOException {
+ void stateSetComplete() throws IOException {
String openMetrics =
"# TYPE state stateset\n"
+ "# HELP state complete state set example\n"
@@ -2380,7 +2380,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testStateSetMinimal() throws IOException {
+ void stateSetMinimal() throws IOException {
String openMetrics =
"# TYPE state stateset\n"
+ "state{state=\"a\"} 1\n"
@@ -2404,7 +2404,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testStateSetWithDots() throws IOException {
+ void stateSetWithDots() throws IOException {
String openMetricsText =
"# TYPE my_application_state stateset\n"
+ "# HELP my_application_state My application state\n"
@@ -2448,7 +2448,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testUnknownComplete() throws IOException {
+ void unknownComplete() throws IOException {
String openMetrics =
"# TYPE my_special_thing_bytes unknown\n"
+ "# UNIT my_special_thing_bytes bytes\n"
@@ -2513,7 +2513,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testUnknownMinimal() throws IOException {
+ void unknownMinimal() throws IOException {
String openMetrics = "# TYPE other unknown\n" + "other 22.3\n" + "# EOF\n";
String prometheus = "# TYPE other untyped\n" + "other 22.3\n";
UnknownSnapshot unknown =
@@ -2528,7 +2528,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testUnknownWithDots() throws IOException {
+ void unknownWithDots() throws IOException {
String openMetrics =
"# TYPE some_unknown_metric_bytes unknown\n"
+ "# UNIT some_unknown_metric_bytes bytes\n"
@@ -2577,7 +2577,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testHelpEscape() throws IOException {
+ void helpEscape() throws IOException {
String openMetrics =
"# TYPE test counter\n"
+ "# HELP test Some text and \\n some \\\" escaping\n"
@@ -2600,7 +2600,7 @@ class ExpositionFormatsTest {
}
@Test
- public void testLabelValueEscape() throws IOException {
+ void labelValueEscape() throws IOException {
String openMetrics =
"# TYPE test counter\n"
+ "test_total{a=\"x\",b=\"escaping\\\" example \\n \"} 1.0\n"
--- a/prometheus-metrics-exposition-textformats/src/main/java/io/prometheus/metrics/expositionformats/OpenMetricsTextFormatWriter.java
+++ b/prometheus-metrics-exposition-textformats/src/main/java/io/prometheus/metrics/expositionformats/OpenMetricsTextFormatWriter.java
@@ -5,7 +5,9 @@ import static io.prometheus.metrics.expositionformats.TextFormatUtil.writeEscape
import static io.prometheus.metrics.expositionformats.TextFormatUtil.writeLabels;
import static io.prometheus.metrics.expositionformats.TextFormatUtil.writeLong;
import static io.prometheus.metrics.expositionformats.TextFormatUtil.writeTimestamp;
+import static java.nio.charset.StandardCharsets.UTF_8;
+import com.google.common.base.Strings;
import io.prometheus.metrics.model.snapshots.ClassicHistogramBuckets;
import io.prometheus.metrics.model.snapshots.CounterSnapshot;
import io.prometheus.metrics.model.snapshots.DataPointSnapshot;
@@ -26,7 +28,6 @@ import io.prometheus.metrics.model.snapshots.UnknownSnapshot;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
-import java.nio.charset.StandardCharsets;
import java.util.List;
/**
@@ -65,7 +66,7 @@ public class OpenMetricsTextFormatWriter implements ExpositionFormatWriter {
@Override
public void write(OutputStream out, MetricSnapshots metricSnapshots) throws IOException {
- OutputStreamWriter writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
+ OutputStreamWriter writer = new OutputStreamWriter(out, UTF_8);
for (MetricSnapshot snapshot : metricSnapshots) {
if (!snapshot.getDataPoints().isEmpty()) {
if (snapshot instanceof CounterSnapshot) {
@@ -368,7 +369,7 @@ public class OpenMetricsTextFormatWriter implements ExpositionFormatWriter {
writeEscapedLabelValue(writer, metadata.getUnit().toString());
writer.write('\n');
}
- if (metadata.getHelp() != null && !metadata.getHelp().isEmpty()) {
+ if (!Strings.isNullOrEmpty(metadata.getHelp())) {
writer.write("# HELP ");
writer.write(metadata.getPrometheusName());
writer.write(' ');
--- a/prometheus-metrics-exposition-textformats/src/main/java/io/prometheus/metrics/expositionformats/PrometheusTextFormatWriter.java
+++ b/prometheus-metrics-exposition-textformats/src/main/java/io/prometheus/metrics/expositionformats/PrometheusTextFormatWriter.java
@@ -5,7 +5,9 @@ import static io.prometheus.metrics.expositionformats.TextFormatUtil.writeEscape
import static io.prometheus.metrics.expositionformats.TextFormatUtil.writeLabels;
import static io.prometheus.metrics.expositionformats.TextFormatUtil.writeLong;
import static io.prometheus.metrics.expositionformats.TextFormatUtil.writeTimestamp;
+import static java.nio.charset.StandardCharsets.UTF_8;
+import com.google.common.base.Strings;
import io.prometheus.metrics.model.snapshots.ClassicHistogramBuckets;
import io.prometheus.metrics.model.snapshots.CounterSnapshot;
import io.prometheus.metrics.model.snapshots.DataPointSnapshot;
@@ -24,7 +26,6 @@ import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
-import java.nio.charset.StandardCharsets;
/**
* Write the Prometheus text format. This is the default if you view a Prometheus endpoint with your
@@ -59,9 +60,9 @@ public class PrometheusTextFormatWriter implements ExpositionFormatWriter {
// See https://prometheus.io/docs/instrumenting/exposition_formats/
// "unknown", "gauge", "counter", "stateset", "info", "histogram", "gaugehistogram", and
// "summary".
- OutputStreamWriter writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
+ OutputStreamWriter writer = new OutputStreamWriter(out, UTF_8);
for (MetricSnapshot snapshot : metricSnapshots) {
- if (snapshot.getDataPoints().size() > 0) {
+ if (!snapshot.getDataPoints().isEmpty()) {
if (snapshot instanceof CounterSnapshot) {
writeCounter(writer, (CounterSnapshot) snapshot);
} else if (snapshot instanceof GaugeSnapshot) {
@@ -81,7 +82,7 @@ public class PrometheusTextFormatWriter implements ExpositionFormatWriter {
}
if (writeCreatedTimestamps) {
for (MetricSnapshot snapshot : metricSnapshots) {
- if (snapshot.getDataPoints().size() > 0) {
+ if (!snapshot.getDataPoints().isEmpty()) {
if (snapshot instanceof CounterSnapshot) {
writeCreated(writer, snapshot);
} else if (snapshot instanceof HistogramSnapshot) {
@@ -113,7 +114,7 @@ public class PrometheusTextFormatWriter implements ExpositionFormatWriter {
private void writeCounter(OutputStreamWriter writer, CounterSnapshot snapshot)
throws IOException {
- if (snapshot.getDataPoints().size() > 0) {
+ if (!snapshot.getDataPoints().isEmpty()) {
MetricMetadata metadata = snapshot.getMetadata();
writeMetadata(writer, "_total", "counter", metadata);
for (CounterSnapshot.CounterDataPointSnapshot data : snapshot.getDataPoints()) {
@@ -329,7 +330,7 @@ public class PrometheusTextFormatWriter implements ExpositionFormatWriter {
private void writeMetadata(
OutputStreamWriter writer, String suffix, String typeString, MetricMetadata metadata)
throws IOException {
- if (metadata.getHelp() != null && !metadata.getHelp().isEmpty()) {
+ if (!Strings.isNullOrEmpty(metadata.getHelp())) {
writer.write("# HELP ");
writer.write(metadata.getPrometheusName());
if (suffix != null) {
--- a/prometheus-metrics-instrumentation-caffeine/src/main/java/io/prometheus/metrics/instrumentation/caffeine/CacheMetricsCollector.java
+++ b/prometheus-metrics-instrumentation-caffeine/src/main/java/io/prometheus/metrics/instrumentation/caffeine/CacheMetricsCollector.java
@@ -1,9 +1,12 @@
package io.prometheus.metrics.instrumentation.caffeine;
+import static java.util.Collections.unmodifiableList;
+
import com.github.benmanes.caffeine.cache.AsyncCache;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.github.benmanes.caffeine.cache.stats.CacheStats;
+import com.google.common.collect.ImmutableList;
import io.prometheus.metrics.model.registry.MultiCollector;
import io.prometheus.metrics.model.snapshots.CounterSnapshot;
import io.prometheus.metrics.model.snapshots.GaugeSnapshot;
@@ -11,7 +14,6 @@ import io.prometheus.metrics.model.snapshots.Labels;
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
import io.prometheus.metrics.model.snapshots.SummarySnapshot;
import java.util.Arrays;
-import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -67,7 +69,7 @@ public class CacheMetricsCollector implements MultiCollector {
"caffeine_cache_load_duration_seconds";
private static final List<String> ALL_METRIC_NAMES =
- Collections.unmodifiableList(
+ unmodifiableList(
Arrays.asList(
METRIC_NAME_CACHE_HIT,
METRIC_NAME_CACHE_MISS,
@@ -168,7 +170,7 @@ public class CacheMetricsCollector implements MultiCollector {
.help("Cache load duration: both success and failures");
for (final Map.Entry<String, Cache<?, ?>> c : children.entrySet()) {
- final List<String> cacheName = Collections.singletonList(c.getKey());
+ final List<String> cacheName = ImmutableList.of(c.getKey());
final Labels labels = Labels.of(labelNames, cacheName);
final CacheStats stats = c.getValue().stats();
--- a/prometheus-metrics-instrumentation-caffeine/src/test/java/io/prometheus/metrics/instrumentation/caffeine/CacheMetricsCollectorTest.java
+++ b/prometheus-metrics-instrumentation-caffeine/src/test/java/io/prometheus/metrics/instrumentation/caffeine/CacheMetricsCollectorTest.java
@@ -24,10 +24,10 @@ import java.util.List;
import org.junit.jupiter.api.Test;
@SuppressWarnings("CheckReturnValue")
-class CacheMetricsCollectorTest {
+final class CacheMetricsCollectorTest {
@Test
- public void cacheExposesMetricsForHitMissAndEviction() {
+ void cacheExposesMetricsForHitMissAndEviction() {
// Run cleanup in same thread, to remove async behavior with evictions
final Cache<String, String> cache =
Caffeine.newBuilder().maximumSize(2).recordStats().executor(Runnable::run).build();
@@ -79,8 +79,8 @@ class CacheMetricsCollectorTest {
@SuppressWarnings("unchecked")
@Test
- public void loadingCacheExposesMetricsForLoadsAndExceptions() throws Exception {
- final CacheLoader<String, String> loader = mock(CacheLoader.class);
+ void loadingCacheExposesMetricsForLoadsAndExceptions() throws Exception {
+ final CacheLoader<String, String> loader = mock();
when(loader.load(anyString()))
.thenReturn("First User")
.thenThrow(new RuntimeException("Seconds time fails"))
@@ -114,11 +114,11 @@ class CacheMetricsCollectorTest {
getDataPointSnapshot(registry, "caffeine_cache_load_duration_seconds", "loadingusers");
assertThat(loadDuration.getCount()).isEqualTo(3);
- assertThat(loadDuration.getSum()).isGreaterThan(0);
+ assertThat(loadDuration.getSum()).isPositive();
}
@Test
- public void getPrometheusNamesHasSameSizeAsMetricSizeWhenScraping() {
+ void getPrometheusNamesHasSameSizeAsMetricSizeWhenScraping() {
final CacheMetricsCollector collector = new CacheMetricsCollector();
final PrometheusRegistry registry = new PrometheusRegistry();
@@ -131,7 +131,7 @@ class CacheMetricsCollectorTest {
}
@Test
- public void collectedMetricNamesAreKnownPrometheusNames() {
+ void collectedMetricNamesAreKnownPrometheusNames() {
final CacheMetricsCollector collector = new CacheMetricsCollector();
final PrometheusRegistry registry = new PrometheusRegistry();
--- a/prometheus-metrics-instrumentation-dropwizard/src/main/java/io/prometheus/metrics/instrumentation/dropwizard/DropwizardExports.java
+++ b/prometheus-metrics-instrumentation-dropwizard/src/main/java/io/prometheus/metrics/instrumentation/dropwizard/DropwizardExports.java
@@ -1,5 +1,7 @@
package io.prometheus.metrics.instrumentation.dropwizard;
+import static com.google.common.base.Preconditions.checkArgument;
+
import com.codahale.metrics.Counter;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Histogram;
@@ -9,6 +11,7 @@ import com.codahale.metrics.MetricFilter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Snapshot;
import com.codahale.metrics.Timer;
+import com.google.common.collect.ImmutableList;
import io.prometheus.metrics.instrumentation.dropwizard5.labels.CustomLabelMapper;
import io.prometheus.metrics.model.registry.MultiCollector;
import io.prometheus.metrics.model.registry.PrometheusRegistry;
@@ -20,7 +23,6 @@ import io.prometheus.metrics.model.snapshots.MetricSnapshots;
import io.prometheus.metrics.model.snapshots.PrometheusNaming;
import io.prometheus.metrics.model.snapshots.Quantiles;
import io.prometheus.metrics.model.snapshots.SummarySnapshot;
-import java.util.Collections;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
@@ -28,7 +30,7 @@ import java.util.logging.Logger;
/** Collect Dropwizard metrics from a MetricRegistry. */
public class DropwizardExports implements MultiCollector {
- private static final Logger logger = Logger.getLogger(DropwizardExports.class.getName());
+ private static final Logger LOGGER = Logger.getLogger(DropwizardExports.class.getName());
private final MetricRegistry registry;
private final MetricFilter metricFilter;
private final Optional<CustomLabelMapper> labelMapper;
@@ -76,7 +78,8 @@ public class DropwizardExports implements MultiCollector {
}
private MetricMetadata getMetricMetaData(String metricName, Metric metric) {
- String name = labelMapper.isPresent() ? labelMapper.get().getName(metricName) : metricName;
+ String name =
+ labelMapper.isPresent() ? labelMapper.orElseThrow().getName(metricName) : metricName;
return new MetricMetadata(
PrometheusNaming.sanitizeMetricName(name), getHelpMessage(metricName, metric));
}
@@ -93,9 +96,8 @@ public class DropwizardExports implements MultiCollector {
labelMapper.ifPresent(
mapper ->
dataPointBuilder.labels(
- mapper.getLabels(
- dropwizardName, Collections.emptyList(), Collections.emptyList())));
- return new CounterSnapshot(metadata, Collections.singletonList(dataPointBuilder.build()));
+ mapper.getLabels(dropwizardName, ImmutableList.of(), ImmutableList.of())));
+ return new CounterSnapshot(metadata, ImmutableList.of(dataPointBuilder.build()));
}
/** Export gauge as a prometheus gauge. */
@@ -107,7 +109,7 @@ public class DropwizardExports implements MultiCollector {
} else if (obj instanceof Boolean) {
value = ((Boolean) obj) ? 1 : 0;
} else {
- logger.log(
+ LOGGER.log(
Level.FINE,
String.format(
"Invalid type for Gauge %s: %s",
@@ -121,9 +123,8 @@ public class DropwizardExports implements MultiCollector {
labelMapper.ifPresent(
mapper ->
dataPointBuilder.labels(
- mapper.getLabels(
- dropwizardName, Collections.emptyList(), Collections.emptyList())));
- return new GaugeSnapshot(metadata, Collections.singletonList(dataPointBuilder.build()));
+ mapper.getLabels(dropwizardName, ImmutableList.of(), ImmutableList.of())));
+ return new GaugeSnapshot(metadata, ImmutableList.of(dataPointBuilder.build()));
}
/**
@@ -153,9 +154,8 @@ public class DropwizardExports implements MultiCollector {
labelMapper.ifPresent(
mapper ->
dataPointBuilder.labels(
- mapper.getLabels(
- dropwizardName, Collections.emptyList(), Collections.emptyList())));
- return new SummarySnapshot(metadata, Collections.singletonList(dataPointBuilder.build()));
+ mapper.getLabels(dropwizardName, ImmutableList.of(), ImmutableList.of())));
+ return new SummarySnapshot(metadata, ImmutableList.of(dataPointBuilder.build()));
}
/** Convert histogram snapshot. */
@@ -186,9 +186,8 @@ public class DropwizardExports implements MultiCollector {
labelMapper.ifPresent(
mapper ->
dataPointBuilder.labels(
- mapper.getLabels(
- dropwizardName, Collections.emptyList(), Collections.emptyList())));
- return new CounterSnapshot(metadata, Collections.singletonList(dataPointBuilder.build()));
+ mapper.getLabels(dropwizardName, ImmutableList.of(), ImmutableList.of())));
+ return new CounterSnapshot(metadata, ImmutableList.of(dataPointBuilder.build()));
}
@Override
@@ -252,9 +251,7 @@ public class DropwizardExports implements MultiCollector {
}
DropwizardExports build() {
- if (registry == null) {
- throw new IllegalArgumentException("MetricRegistry must be set");
- }
+ checkArgument(registry != null, "MetricRegistry must be set");
if (labelMapper == null) {
return new DropwizardExports(registry, metricFilter);
} else {
--- a/prometheus-metrics-instrumentation-dropwizard/src/test/java/io/prometheus/metrics/instrumentation/dropwizard/DropwizardExportsTest.java
+++ b/prometheus-metrics-instrumentation-dropwizard/src/test/java/io/prometheus/metrics/instrumentation/dropwizard/DropwizardExportsTest.java
@@ -1,5 +1,6 @@
package io.prometheus.metrics.instrumentation.dropwizard;
+import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@@ -10,18 +11,17 @@ import io.prometheus.metrics.model.registry.PrometheusRegistry;
import io.prometheus.metrics.model.snapshots.SummarySnapshot;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
-import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-class DropwizardExportsTest {
+final class DropwizardExportsTest {
private final PrometheusRegistry registry = new PrometheusRegistry();
private MetricRegistry metricRegistry;
@BeforeEach
- public void setUp() {
+ void setUp() {
metricRegistry = new MetricRegistry();
DropwizardExports.builder()
.dropwizardRegistry(metricRegistry)
@@ -30,21 +30,21 @@ class DropwizardExportsTest {
}
@Test
- public void testBuilderThrowsErrorOnNullRegistry() {
+ void builderThrowsErrorOnNullRegistry() {
assertThatThrownBy(
() -> DropwizardExports.builder().dropwizardRegistry(null).register(registry))
.isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testBuilderCreatesOkay() {
+ void builderCreatesOkay() {
assertThatCode(
() -> DropwizardExports.builder().dropwizardRegistry(metricRegistry).register(registry))
.doesNotThrowAnyException();
}
@Test
- public void testCounter() {
+ void counter() {
metricRegistry.counter("foo.bar").inc(1);
String expected =
"""
@@ -58,7 +58,7 @@ class DropwizardExportsTest {
}
@Test
- public void testGauge() {
+ void gauge() {
// don't convert to lambda, as we need to test the type
Gauge<Integer> integerGauge =
new Gauge<Integer>() {
@@ -126,7 +126,7 @@ class DropwizardExportsTest {
}
@Test
- public void testInvalidGaugeType() {
+ void invalidGaugeType() {
Gauge<String> invalidGauge = () -> "foobar";
metricRegistry.register("invalid_gauge", invalidGauge);
@@ -136,7 +136,7 @@ class DropwizardExportsTest {
}
@Test
- public void testGaugeReturningNullValue() {
+ void gaugeReturningNullValue() {
Gauge<String> invalidGauge = () -> null;
metricRegistry.register("invalid_gauge", invalidGauge);
String expected = "# EOF\n";
@@ -144,7 +144,7 @@ class DropwizardExportsTest {
}
@Test
- public void testHistogram() {
+ void histogram() {
// just test the standard mapper
final MetricRegistry metricRegistry = new MetricRegistry();
PrometheusRegistry pmRegistry = new PrometheusRegistry();
@@ -197,7 +197,7 @@ class DropwizardExportsTest {
}
@Test
- public void testMeter() {
+ void meter() {
Meter meter = metricRegistry.meter("meter");
meter.mark();
meter.mark();
@@ -213,7 +213,7 @@ class DropwizardExportsTest {
}
@Test
- public void testTimer() throws InterruptedException {
+ void timer() throws InterruptedException {
final MetricRegistry metricRegistry = new MetricRegistry();
DropwizardExports exports = new DropwizardExports(metricRegistry);
Timer t = metricRegistry.timer("timer");
@@ -232,12 +232,12 @@ class DropwizardExportsTest {
dataPointSnapshot
.getQuantiles()
.forEach(i -> assertThat(i.getValue()).isGreaterThan(timeSpentMillis / 1000d));
- assertThat(dataPointSnapshot.getCount()).isOne();
+ assertThat(dataPointSnapshot.getCount()).isEqualTo(1);
});
}
@Test
- public void testThatMetricHelpUsesOriginalDropwizardName() {
+ void thatMetricHelpUsesOriginalDropwizardName() {
metricRegistry.timer("my.application.namedTimer1");
metricRegistry.counter("my.application.namedCounter1");
metricRegistry.meter("my.application.namedMeter1");
@@ -290,7 +290,7 @@ class DropwizardExportsTest {
OpenMetricsTextFormatWriter writer = new OpenMetricsTextFormatWriter(true, true);
try {
writer.write(out, _registry.scrape());
- return out.toString(StandardCharsets.UTF_8);
+ return out.toString(UTF_8);
} catch (IOException e) {
throw new RuntimeException(e);
}
--- a/prometheus-metrics-instrumentation-dropwizard5/src/main/java/io/prometheus/metrics/instrumentation/dropwizard5/DropwizardExports.java
+++ b/prometheus-metrics-instrumentation-dropwizard5/src/main/java/io/prometheus/metrics/instrumentation/dropwizard5/DropwizardExports.java
@@ -1,5 +1,8 @@
package io.prometheus.metrics.instrumentation.dropwizard5;
+import static com.google.common.base.Preconditions.checkArgument;
+
+import com.google.common.collect.ImmutableList;
import io.dropwizard.metrics5.Counter;
import io.dropwizard.metrics5.Gauge;
import io.dropwizard.metrics5.Histogram;
@@ -21,7 +24,6 @@ import io.prometheus.metrics.model.snapshots.MetricSnapshots;
import io.prometheus.metrics.model.snapshots.PrometheusNaming;
import io.prometheus.metrics.model.snapshots.Quantiles;
import io.prometheus.metrics.model.snapshots.SummarySnapshot;
-import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
@@ -30,7 +32,7 @@ import java.util.logging.Logger;
/** Collect Dropwizard metrics from a MetricRegistry. */
public class DropwizardExports implements MultiCollector {
- private static final Logger logger = Logger.getLogger(DropwizardExports.class.getName());
+ private static final Logger LOGGER = Logger.getLogger(DropwizardExports.class.getName());
private final MetricRegistry registry;
private final MetricFilter metricFilter;
private final Optional<CustomLabelMapper> labelMapper;
@@ -78,7 +80,8 @@ public class DropwizardExports implements MultiCollector {
}
private MetricMetadata getMetricMetaData(String metricName, Metric metric) {
- String name = labelMapper.isPresent() ? labelMapper.get().getName(metricName) : metricName;
+ String name =
+ labelMapper.isPresent() ? labelMapper.orElseThrow().getName(metricName) : metricName;
return new MetricMetadata(
PrometheusNaming.sanitizeMetricName(name), getHelpMessage(metricName, metric));
}
@@ -95,9 +98,8 @@ public class DropwizardExports implements MultiCollector {
labelMapper.ifPresent(
mapper ->
dataPointBuilder.labels(
- mapper.getLabels(
- dropwizardName, Collections.emptyList(), Collections.emptyList())));
- return new CounterSnapshot(metadata, Collections.singletonList(dataPointBuilder.build()));
+ mapper.getLabels(dropwizardName, ImmutableList.of(), ImmutableList.of())));
+ return new CounterSnapshot(metadata, ImmutableList.of(dataPointBuilder.build()));
}
/** Export gauge as a prometheus gauge. */
@@ -109,7 +111,7 @@ public class DropwizardExports implements MultiCollector {
} else if (obj instanceof Boolean) {
value = ((Boolean) obj) ? 1 : 0;
} else {
- logger.log(
+ LOGGER.log(
Level.FINE,
String.format(
"Invalid type for Gauge %s: %s",
@@ -123,9 +125,8 @@ public class DropwizardExports implements MultiCollector {
labelMapper.ifPresent(
mapper ->
dataPointBuilder.labels(
- mapper.getLabels(
- dropwizardName, Collections.emptyList(), Collections.emptyList())));
- return new GaugeSnapshot(metadata, Collections.singletonList(dataPointBuilder.build()));
+ mapper.getLabels(dropwizardName, ImmutableList.of(), ImmutableList.of())));
+ return new GaugeSnapshot(metadata, ImmutableList.of(dataPointBuilder.build()));
}
/**
@@ -155,9 +156,8 @@ public class DropwizardExports implements MultiCollector {
labelMapper.ifPresent(
mapper ->
dataPointBuilder.labels(
- mapper.getLabels(
- dropwizardName, Collections.emptyList(), Collections.emptyList())));
- return new SummarySnapshot(metadata, Collections.singletonList(dataPointBuilder.build()));
+ mapper.getLabels(dropwizardName, ImmutableList.of(), ImmutableList.of())));
+ return new SummarySnapshot(metadata, ImmutableList.of(dataPointBuilder.build()));
}
/** Convert histogram snapshot. */
@@ -188,9 +188,8 @@ public class DropwizardExports implements MultiCollector {
labelMapper.ifPresent(
mapper ->
dataPointBuilder.labels(
- mapper.getLabels(
- dropwizardName, Collections.emptyList(), Collections.emptyList())));
- return new CounterSnapshot(metadata, Collections.singletonList(dataPointBuilder.build()));
+ mapper.getLabels(dropwizardName, ImmutableList.of(), ImmutableList.of())));
+ return new CounterSnapshot(metadata, ImmutableList.of(dataPointBuilder.build()));
}
@Override
@@ -246,9 +245,7 @@ public class DropwizardExports implements MultiCollector {
}
DropwizardExports build() {
- if (registry == null) {
- throw new IllegalArgumentException("MetricRegistry must be set");
- }
+ checkArgument(registry != null, "MetricRegistry must be set");
if (labelMapper == null) {
return new DropwizardExports(registry, metricFilter);
} else {
--- a/prometheus-metrics-instrumentation-dropwizard5/src/main/java/io/prometheus/metrics/instrumentation/dropwizard5/labels/CustomLabelMapper.java
+++ b/prometheus-metrics-instrumentation-dropwizard5/src/main/java/io/prometheus/metrics/instrumentation/dropwizard5/labels/CustomLabelMapper.java
@@ -1,5 +1,7 @@
package io.prometheus.metrics.instrumentation.dropwizard5.labels;
+import static com.google.common.base.Preconditions.checkArgument;
+
import io.prometheus.metrics.model.snapshots.Labels;
import java.util.ArrayList;
import java.util.List;
@@ -14,9 +16,9 @@ public class CustomLabelMapper {
private final List<CompiledMapperConfig> compiledMapperConfigs;
public CustomLabelMapper(final List<MapperConfig> mapperConfigs) {
- if (mapperConfigs == null || mapperConfigs.isEmpty()) {
- throw new IllegalArgumentException("CustomLabelMapper needs some mapper configs!");
- }
+ checkArgument(
+ mapperConfigs != null && !mapperConfigs.isEmpty(),
+ "CustomLabelMapper needs some mapper configs!");
this.compiledMapperConfigs = new ArrayList<CompiledMapperConfig>(mapperConfigs.size());
for (MapperConfig config : mapperConfigs) {
@@ -25,9 +27,7 @@ public class CustomLabelMapper {
}
public String getName(final String dropwizardName) {
- if (dropwizardName == null) {
- throw new IllegalArgumentException("Dropwizard metric name cannot be null");
- }
+ checkArgument(dropwizardName != null, "Dropwizard metric name cannot be null");
CompiledMapperConfig matchingConfig = null;
for (CompiledMapperConfig config : this.compiledMapperConfigs) {
@@ -50,9 +50,7 @@ public class CustomLabelMapper {
final String dropwizardName,
final List<String> additionalLabelNames,
final List<String> additionalLabelValues) {
- if (dropwizardName == null) {
- throw new IllegalArgumentException("Dropwizard metric name cannot be null");
- }
+ checkArgument(dropwizardName != null, "Dropwizard metric name cannot be null");
CompiledMapperConfig matchingConfig = null;
for (CompiledMapperConfig config : this.compiledMapperConfigs) {
--- a/prometheus-metrics-instrumentation-dropwizard5/src/main/java/io/prometheus/metrics/instrumentation/dropwizard5/labels/GraphiteNamePattern.java
+++ b/prometheus-metrics-instrumentation-dropwizard5/src/main/java/io/prometheus/metrics/instrumentation/dropwizard5/labels/GraphiteNamePattern.java
@@ -1,5 +1,6 @@
package io.prometheus.metrics.instrumentation.dropwizard5.labels;
+import static com.google.common.base.Preconditions.checkArgument;
import static io.prometheus.metrics.instrumentation.dropwizard5.labels.MapperConfig.METRIC_GLOB_REGEX;
import java.util.HashMap;
@@ -32,10 +33,9 @@ class GraphiteNamePattern {
* @param pattern The glob style pattern to be used.
*/
GraphiteNamePattern(final String pattern) throws IllegalArgumentException {
- if (!VALIDATION_PATTERN.matcher(pattern).matches()) {
- throw new IllegalArgumentException(
- String.format("Provided pattern [%s] does not matches [%s]", pattern, METRIC_GLOB_REGEX));
- }
+ checkArgument(
+ VALIDATION_PATTERN.matcher(pattern).matches(),
+ String.format("Provided pattern [%s] does not matches [%s]", pattern, METRIC_GLOB_REGEX));
initializePattern(pattern);
}
@@ -84,7 +84,7 @@ class GraphiteNamePattern {
escapedPattern.append("([^.]*)").append(quoted);
}
- final String regex = "^" + escapedPattern.toString() + "$";
+ final String regex = "^" + escapedPattern + "$";
this.patternStr = regex;
this.pattern = Pattern.compile(regex);
}
--- a/prometheus-metrics-instrumentation-dropwizard5/src/main/java/io/prometheus/metrics/instrumentation/dropwizard5/labels/MapperConfig.java
+++ b/prometheus-metrics-instrumentation-dropwizard5/src/main/java/io/prometheus/metrics/instrumentation/dropwizard5/labels/MapperConfig.java
@@ -1,5 +1,7 @@
package io.prometheus.metrics.instrumentation.dropwizard5.labels;
+import static com.google.common.base.Preconditions.checkArgument;
+
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
@@ -106,21 +108,19 @@ public final class MapperConfig {
}
private void validateMatch(final String match) {
- if (!MATCH_EXPRESSION_PATTERN.matcher(match).matches()) {
- throw new IllegalArgumentException(
- String.format(
- "Match expression [%s] does not match required pattern %s",
- match, MATCH_EXPRESSION_PATTERN));
- }
+ checkArgument(
+ MATCH_EXPRESSION_PATTERN.matcher(match).matches(),
+ String.format(
+ "Match expression [%s] does not match required pattern %s",
+ match, MATCH_EXPRESSION_PATTERN));
}
private void validateLabels(final Map<String, String> labels) {
if (labels != null) {
for (final String key : labels.keySet()) {
- if (!LABEL_PATTERN.matcher(key).matches()) {
- throw new IllegalArgumentException(
- String.format("Label [%s] does not match required pattern %s", match, LABEL_PATTERN));
- }
+ checkArgument(
+ LABEL_PATTERN.matcher(key).matches(),
+ String.format("Label [%s] does not match required pattern %s", match, LABEL_PATTERN));
}
}
}
@@ -149,7 +149,6 @@ public final class MapperConfig {
public int hashCode() {
int result = match != null ? match.hashCode() : 0;
result = 31 * result + (name != null ? name.hashCode() : 0);
- result = 31 * result + (labels != null ? labels.hashCode() : 0);
- return result;
+ return 31 * result + (labels != null ? labels.hashCode() : 0);
}
}
--- a/prometheus-metrics-instrumentation-dropwizard5/src/test/java/io/prometheus/metrics/instrumentation/dropwizard5/DropwizardExportsTest.java
+++ b/prometheus-metrics-instrumentation-dropwizard5/src/test/java/io/prometheus/metrics/instrumentation/dropwizard5/DropwizardExportsTest.java
@@ -16,19 +16,19 @@ import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-class DropwizardExportsTest {
+final class DropwizardExportsTest {
private PrometheusRegistry registry = new PrometheusRegistry();
private MetricRegistry metricRegistry;
@BeforeEach
- public void setUp() {
+ void setUp() {
metricRegistry = new MetricRegistry();
DropwizardExports.builder().dropwizardRegistry(metricRegistry).register(registry);
}
@Test
- public void testCounter() {
+ void counter() {
metricRegistry.counter("foo.bar").inc(1);
String expected =
"# TYPE foo_bar counter\n"
@@ -40,7 +40,7 @@ class DropwizardExportsTest {
}
@Test
- public void testGauge() {
+ void gauge() {
// don't convert to lambda, as we need to test the type
Gauge<Integer> integerGauge =
new Gauge<Integer>() {
@@ -106,7 +106,7 @@ class DropwizardExportsTest {
}
@Test
- public void testInvalidGaugeType() {
+ void invalidGaugeType() {
Gauge<String> invalidGauge = () -> "foobar";
metricRegistry.register("invalid_gauge", invalidGauge);
@@ -116,7 +116,7 @@ class DropwizardExportsTest {
}
@Test
- public void testGaugeReturningNullValue() {
+ void gaugeReturningNullValue() {
Gauge<String> invalidGauge = () -> null;
metricRegistry.register("invalid_gauge", invalidGauge);
String expected = "# EOF\n";
@@ -124,7 +124,7 @@ class DropwizardExportsTest {
}
@Test
- public void testHistogram() {
+ void histogram() {
// just test the standard mapper
final MetricRegistry metricRegistry = new MetricRegistry();
PrometheusRegistry pmRegistry = new PrometheusRegistry();
@@ -169,13 +169,13 @@ class DropwizardExportsTest {
// The following asserts the values, but allows an error of 1.0 for quantile values.
MetricSnapshots snapshots = pmRegistry.scrape(name -> name.equals("hist"));
- assertThat(snapshots.size()).isOne();
+ assertThat(snapshots.size()).isEqualTo(1);
SummarySnapshot snapshot = (SummarySnapshot) snapshots.get(0);
assertThat(snapshot.getMetadata().getName()).isEqualTo("hist");
assertThat(snapshot.getMetadata().getHelp())
.isEqualTo(
"Generated from Dropwizard metric import (metric=hist, type=io.dropwizard.metrics5.Histogram)");
- assertThat(snapshot.getDataPoints().size()).isOne();
+ assertThat(snapshot.getDataPoints()).hasSize(1);
SummarySnapshot.SummaryDataPointSnapshot dataPoint = snapshot.getDataPoints().get(0);
assertThat(dataPoint.hasCount()).isTrue();
assertThat(dataPoint.getCount()).isEqualTo(100);
@@ -197,7 +197,7 @@ class DropwizardExportsTest {
}
@Test
- public void testMeter() {
+ void meter() {
Meter meter = metricRegistry.meter("meter");
meter.mark();
meter.mark();
@@ -211,7 +211,7 @@ class DropwizardExportsTest {
}
@Test
- public void testTimer() throws InterruptedException {
+ void timer() throws InterruptedException {
final MetricRegistry metricRegistry = new MetricRegistry();
DropwizardExports exports = new DropwizardExports(metricRegistry);
Timer t = metricRegistry.timer("timer");
@@ -233,11 +233,11 @@ class DropwizardExportsTest {
System.out.println(i.getQuantile() + " : " + i.getValue());
assertThat(i.getValue()).isGreaterThan(timeSpentMillis / 1000d);
});
- assertThat(dataPointSnapshot.getCount()).isOne();
+ assertThat(dataPointSnapshot.getCount()).isEqualTo(1);
}
@Test
- public void testThatMetricHelpUsesOriginalDropwizardName() {
+ void thatMetricHelpUsesOriginalDropwizardName() {
metricRegistry.timer("my.application.namedTimer1");
metricRegistry.counter("my.application.namedCounter1");
--- a/prometheus-metrics-instrumentation-dropwizard5/src/test/java/io/prometheus/metrics/instrumentation/dropwizard5/labels/CustomLabelMapperTest.java
+++ b/prometheus-metrics-instrumentation-dropwizard5/src/test/java/io/prometheus/metrics/instrumentation/dropwizard5/labels/CustomLabelMapperTest.java
@@ -1,8 +1,9 @@
package io.prometheus.metrics.instrumentation.dropwizard5.labels;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import com.google.common.collect.ImmutableList;
import io.dropwizard.metrics5.MetricFilter;
import io.dropwizard.metrics5.MetricRegistry;
import io.prometheus.metrics.expositionformats.OpenMetricsTextFormatWriter;
@@ -15,22 +16,22 @@ import java.util.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-class CustomLabelMapperTest {
+final class CustomLabelMapperTest {
private MetricRegistry metricRegistry;
@BeforeEach
- public void setUp() {
+ void setUp() {
metricRegistry = new MetricRegistry();
}
@Test
- public void test_WHEN_EmptyConfig_THEN_Fail() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> new CustomLabelMapper(Collections.emptyList()));
+ void _WHEN_EmptyConfig_THEN_Fail() {
+ assertThatThrownBy(() -> new CustomLabelMapper(ImmutableList.of()))
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void test_WHEN_NoMatches_THEN_ShouldReturnDefaultSample() {
+ void _WHEN_NoMatches_THEN_ShouldReturnDefaultSample() {
final List<MapperConfig> mapperConfigs =
Arrays.asList(
new MapperConfig("client-nope.*.*.*"),
@@ -53,7 +54,7 @@ class CustomLabelMapperTest {
}
@Test
- public void test_WHEN_OneMatch_THEN_ShouldReturnConverted() {
+ void _WHEN_OneMatch_THEN_ShouldReturnConverted() {
final Map<String, String> labels = new HashMap<String, String>();
labels.put("service", "${0}");
final MapperConfig mapperConfig =
@@ -81,7 +82,7 @@ class CustomLabelMapperTest {
}
@Test
- public void test_WHEN_MoreMatches_THEN_ShouldReturnFirstOne() {
+ void _WHEN_MoreMatches_THEN_ShouldReturnFirstOne() {
final Map<String, String> labels = new HashMap<>();
labels.put("service", "${0}");
final MapperConfig mapperConfig =
@@ -110,7 +111,7 @@ class CustomLabelMapperTest {
}
@Test
- public void test_WHEN_MoreMatchesReverseOrder_THEN_ShouldReturnFirstOne() {
+ void _WHEN_MoreMatchesReverseOrder_THEN_ShouldReturnFirstOne() {
final Map<String, String> labels = new LinkedHashMap<>();
labels.put("service", "${0}");
labels.put("status", "${1}");
@@ -145,7 +146,7 @@ class CustomLabelMapperTest {
}
@Test
- public void test_WHEN_MoreToFormatInLabelsAndName_THEN_ShouldReturnCorrectSample() {
+ void _WHEN_MoreToFormatInLabelsAndName_THEN_ShouldReturnCorrectSample() {
final Map<String, String> labels = new LinkedHashMap<>();
labels.put("service", "${0}_${1}");
labels.put("status", "s_${1}");
@@ -176,7 +177,7 @@ class CustomLabelMapperTest {
}
@Test
- public void test_WHEN_AdditionalLabels_THEN_ShouldReturnCorrectSample() {
+ void _WHEN_AdditionalLabels_THEN_ShouldReturnCorrectSample() {
final Map<String, String> labels = new LinkedHashMap<>();
labels.put("service", "${0}");
labels.put("status", "s_${1}");
--- a/prometheus-metrics-instrumentation-dropwizard5/src/test/java/io/prometheus/metrics/instrumentation/dropwizard5/labels/GraphiteNamePatternTest.java
+++ b/prometheus-metrics-instrumentation-dropwizard5/src/test/java/io/prometheus/metrics/instrumentation/dropwizard5/labels/GraphiteNamePatternTest.java
@@ -1,19 +1,18 @@
package io.prometheus.metrics.instrumentation.dropwizard5.labels;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.util.Arrays;
-import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
-class GraphiteNamePatternTest {
+final class GraphiteNamePatternTest {
@Test
- public void createNew_WHEN_InvalidPattern_THEN_ShouldThrowException() {
+ void createNew_WHEN_InvalidPattern_THEN_ShouldThrowException() {
List<String> invalidPatterns =
Arrays.asList(
"",
@@ -32,14 +31,14 @@ class GraphiteNamePatternTest {
"org.test.contr.oller.gather.status..400",
"org.test.controller.gather.status..400");
for (String pattern : invalidPatterns) {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> new GraphiteNamePattern(pattern))
- .withMessageContaining(pattern);
+ assertThatThrownBy(() -> new GraphiteNamePattern(pattern))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining(pattern);
}
}
@Test
- public void createNew_WHEN_ValidPattern_THEN_ShouldCreateThePatternSuccessfully() {
+ void createNew_WHEN_ValidPattern_THEN_ShouldCreateThePatternSuccessfully() {
final List<String> validPatterns =
Arrays.asList(
"org.test.controller.gather.status.400",
@@ -54,7 +53,7 @@ class GraphiteNamePatternTest {
}
@Test
- public void createNew_WHEN_ValidPattern_THEN_ShouldInitInternalPatternSuccessfully() {
+ void createNew_WHEN_ValidPattern_THEN_ShouldInitInternalPatternSuccessfully() {
final Map<String, String> validPatterns = new HashMap<String, String>();
validPatterns.put(
"org.test.controller.gather.status.400", "^\\Qorg.test.controller.gather.status.400\\E$");
@@ -69,25 +68,25 @@ class GraphiteNamePatternTest {
for (Map.Entry<String, String> expected : validPatterns.entrySet()) {
final GraphiteNamePattern pattern = new GraphiteNamePattern(expected.getKey());
- Assertions.assertThat(pattern.getPatternString()).isEqualTo(expected.getValue());
+ assertThat(pattern.getPatternString()).isEqualTo(expected.getValue());
}
}
@Test
- public void match_WHEN_NotMatchingMetricNameProvided_THEN_ShouldNotMatch() {
+ void match_WHEN_NotMatchingMetricNameProvided_THEN_ShouldNotMatch() {
final GraphiteNamePattern pattern = new GraphiteNamePattern("org.test.controller.*.status.*");
final List<String> notMatchingMetricNamed =
Arrays.asList("org.test.controller.status.400", "", null);
for (String metricName : notMatchingMetricNamed) {
- Assertions.assertThat(pattern.matches(metricName))
+ assertThat(pattern.matches(metricName))
.as("Matching [%s] against [%s]", metricName, pattern.getPatternString())
.isFalse();
}
}
@Test
- public void match_WHEN_MatchingMetricNameProvided_THEN_ShouldMatch() {
+ void match_WHEN_MatchingMetricNameProvided_THEN_ShouldMatch() {
final GraphiteNamePattern pattern = new GraphiteNamePattern("org.test.controller.*.status.*");
final List<String> matchingMetricNamed =
Arrays.asList(
@@ -98,47 +97,46 @@ class GraphiteNamePatternTest {
"org.test.controller..status.*");
for (String metricName : matchingMetricNamed) {
- Assertions.assertThat(pattern.matches(metricName))
+ assertThat(pattern.matches(metricName))
.as("Matching [%s] against [%s]", metricName, pattern.getPatternString())
.isTrue();
}
}
@Test
- public void extractParameters() {
+ void extractParameters() {
GraphiteNamePattern pattern;
Map<String, String> expected = new HashMap<String, String>();
expected.put("${0}", "gather");
expected.put("${1}", "400");
pattern = new GraphiteNamePattern("org.test.controller.*.status.*");
- Assertions.assertThat(pattern.extractParameters("org.test.controller.gather.status.400"))
- .isEqualTo(expected);
+ assertThat(pattern.extractParameters("org.test.controller.gather.status.400"))
+ .containsExactlyInAnyOrderEntriesOf(expected);
expected = new HashMap<String, String>();
expected.put("${0}", "org");
expected.put("${1}", "gather");
expected.put("${2}", "400");
pattern = new GraphiteNamePattern("*.test.controller.*.status.*");
- Assertions.assertThat(pattern.extractParameters("org.test.controller.gather.status.400"))
- .isEqualTo(expected);
+ assertThat(pattern.extractParameters("org.test.controller.gather.status.400"))
+ .containsExactlyInAnyOrderEntriesOf(expected);
}
@Test
- public void extractParameters_WHEN_emptyStringInDottedMetricsName_THEN_ShouldReturnEmptyString() {
+ void extractParameters_WHEN_emptyStringInDottedMetricsName_THEN_ShouldReturnEmptyString() {
GraphiteNamePattern pattern;
Map<String, String> expected = new HashMap<String, String>();
expected.put("${0}", "");
expected.put("${1}", "400");
pattern = new GraphiteNamePattern("org.test.controller.*.status.*");
- Assertions.assertThat(pattern.extractParameters("org.test.controller..status.400"))
- .isEqualTo(expected);
+ assertThat(pattern.extractParameters("org.test.controller..status.400"))
+ .containsExactlyInAnyOrderEntriesOf(expected);
}
@Test
- public void extractParameters_WHEN_moreDots_THEN_ShouldReturnNoMatches() {
+ void extractParameters_WHEN_moreDots_THEN_ShouldReturnNoMatches() {
GraphiteNamePattern pattern;
pattern = new GraphiteNamePattern("org.test.controller.*.status.*");
- Assertions.assertThat(pattern.extractParameters("org.test.controller...status.400"))
- .isEqualTo(Collections.emptyMap());
+ assertThat(pattern.extractParameters("org.test.controller...status.400")).isEmpty();
}
}
--- a/prometheus-metrics-instrumentation-dropwizard5/src/test/java/io/prometheus/metrics/instrumentation/dropwizard5/labels/MapperConfigTest.java
+++ b/prometheus-metrics-instrumentation-dropwizard5/src/test/java/io/prometheus/metrics/instrumentation/dropwizard5/labels/MapperConfigTest.java
@@ -1,58 +1,58 @@
package io.prometheus.metrics.instrumentation.dropwizard5.labels;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
-import java.util.Collections;
+import com.google.common.collect.ImmutableMap;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;
-class MapperConfigTest {
+final class MapperConfigTest {
@Test
- public void setMatch_WHEN_ExpressionMatchesPattern_AllGood() {
+ void setMatch_WHEN_ExpressionMatchesPattern_AllGood() {
final MapperConfig mapperConfig = new MapperConfig();
mapperConfig.setMatch("com.company.meter.*");
assertThat(mapperConfig.getMatch()).isEqualTo("com.company.meter.*");
}
@Test
- public void setMatch_WHEN_ExpressionDoesnNotMatchPattern_ThrowException() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> new MapperConfig().setMatch("com.company.meter.**.yay"));
+ void setMatch_WHEN_ExpressionDoesnNotMatchPattern_ThrowException() {
+ assertThatThrownBy(() -> new MapperConfig().setMatch("com.company.meter.**.yay"))
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void setLabels_WHEN_ExpressionMatchesPattern_AllGood() {
+ void setLabels_WHEN_ExpressionMatchesPattern_AllGood() {
final MapperConfig mapperConfig = new MapperConfig();
final Map<String, String> labels = new HashMap<>();
labels.put("valid", "${0}");
mapperConfig.setLabels(labels);
- assertThat(mapperConfig.getLabels()).isEqualTo(labels);
+ assertThat(mapperConfig.getLabels()).containsExactlyInAnyOrderEntriesOf(labels);
}
@Test
- public void setLabels_WHEN_ExpressionDoesnNotMatchPattern_ThrowException() {
+ void setLabels_WHEN_ExpressionDoesnNotMatchPattern_ThrowException() {
final MapperConfig mapperConfig = new MapperConfig();
final Map<String, String> labels = new HashMap<>();
labels.put("valid", "${0}");
labels.put("not valid", "${0}");
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> mapperConfig.setLabels(labels));
+ assertThatThrownBy(() -> mapperConfig.setLabels(labels))
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void toString_WHEN_EmptyConfig_AllGood() {
+ void toString_WHEN_EmptyConfig_AllGood() {
final MapperConfig mapperConfig = new MapperConfig();
assertThat(mapperConfig).hasToString("MapperConfig{match=null, name=null, labels={}}");
}
@Test
- public void toString_WHEN_FullyConfigured_AllGood() {
+ void toString_WHEN_FullyConfigured_AllGood() {
final MapperConfig mapperConfig = new MapperConfig();
mapperConfig.setMatch("com.company.meter.*.foo");
mapperConfig.setName("foo");
- mapperConfig.setLabels(Collections.singletonMap("type", "${0}"));
+ mapperConfig.setLabels(ImmutableMap.of("type", "${0}"));
assertThat(mapperConfig)
.hasToString("MapperConfig{match=com.company.meter.*.foo, name=foo, labels={type=${0}}}");
}
--- a/prometheus-metrics-instrumentation-guava/src/main/java/io/prometheus/metrics/instrumentation/guava/CacheMetricsCollector.java
+++ b/prometheus-metrics-instrumentation-guava/src/main/java/io/prometheus/metrics/instrumentation/guava/CacheMetricsCollector.java
@@ -1,8 +1,11 @@
package io.prometheus.metrics.instrumentation.guava;
+import static java.util.Collections.unmodifiableList;
+
import com.google.common.cache.Cache;
import com.google.common.cache.CacheStats;
import com.google.common.cache.LoadingCache;
+import com.google.common.collect.ImmutableList;
import io.prometheus.metrics.model.registry.MultiCollector;
import io.prometheus.metrics.model.snapshots.CounterSnapshot;
import io.prometheus.metrics.model.snapshots.GaugeSnapshot;
@@ -10,7 +13,6 @@ import io.prometheus.metrics.model.snapshots.Labels;
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
import io.prometheus.metrics.model.snapshots.SummarySnapshot;
import java.util.Arrays;
-import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -66,7 +68,7 @@ public class CacheMetricsCollector implements MultiCollector {
"guava_cache_load_duration_seconds";
private static final List<String> ALL_METRIC_NAMES =
- Collections.unmodifiableList(
+ unmodifiableList(
Arrays.asList(
METRIC_NAME_CACHE_HIT,
METRIC_NAME_CACHE_MISS,
@@ -114,7 +116,7 @@ public class CacheMetricsCollector implements MultiCollector {
@Override
public MetricSnapshots collect() {
final MetricSnapshots.Builder metricSnapshotsBuilder = MetricSnapshots.builder();
- final List<String> labelNames = Collections.singletonList("cache");
+ final List<String> labelNames = ImmutableList.of("cache");
final CounterSnapshot.Builder cacheHitTotal =
CounterSnapshot.builder().name(METRIC_NAME_CACHE_HIT).help("Cache hit totals");
@@ -147,7 +149,7 @@ public class CacheMetricsCollector implements MultiCollector {
.help("Cache load duration: both success and failures");
for (final Map.Entry<String, Cache<?, ?>> c : children.entrySet()) {
- final List<String> cacheName = Collections.singletonList(c.getKey());
+ final List<String> cacheName = ImmutableList.of(c.getKey());
final Labels labels = Labels.of(labelNames, cacheName);
final CacheStats stats = c.getValue().stats();
--- a/prometheus-metrics-instrumentation-guava/src/test/java/io/prometheus/metrics/instrumentation/guava/CacheMetricsCollectorTest.java
+++ b/prometheus-metrics-instrumentation-guava/src/test/java/io/prometheus/metrics/instrumentation/guava/CacheMetricsCollectorTest.java
@@ -23,10 +23,10 @@ import java.nio.charset.StandardCharsets;
import java.util.List;
import org.junit.jupiter.api.Test;
-class CacheMetricsCollectorTest {
+final class CacheMetricsCollectorTest {
@Test
- public void cacheExposesMetricsForHitMissAndEviction() {
+ void cacheExposesMetricsForHitMissAndEviction() {
final Cache<String, String> cache =
CacheBuilder.newBuilder().maximumSize(2).recordStats().build();
@@ -74,8 +74,8 @@ class CacheMetricsCollectorTest {
@SuppressWarnings("unchecked")
@Test
- public void loadingCacheExposesMetricsForLoadsAndExceptions() throws Exception {
- final CacheLoader<String, String> loader = mock(CacheLoader.class);
+ void loadingCacheExposesMetricsForLoadsAndExceptions() throws Exception {
+ final CacheLoader<String, String> loader = mock();
when(loader.load(anyString()))
.thenReturn("First User")
.thenThrow(new RuntimeException("Seconds time fails"))
@@ -109,11 +109,11 @@ class CacheMetricsCollectorTest {
getDataPointSnapshot(registry, "guava_cache_load_duration_seconds", "loadingusers");
assertThat(loadDuration.getCount()).isEqualTo(3);
- assertThat(loadDuration.getSum()).isGreaterThan(0);
+ assertThat(loadDuration.getSum()).isPositive();
}
@Test
- public void getPrometheusNamesHasSameSizeAsMetricSizeWhenScraping() {
+ void getPrometheusNamesHasSameSizeAsMetricSizeWhenScraping() {
final CacheMetricsCollector collector = new CacheMetricsCollector();
final PrometheusRegistry registry = new PrometheusRegistry();
@@ -126,7 +126,7 @@ class CacheMetricsCollectorTest {
}
@Test
- public void collectedMetricNamesAreKnownPrometheusNames() {
+ void collectedMetricNamesAreKnownPrometheusNames() {
final CacheMetricsCollector collector = new CacheMetricsCollector();
final PrometheusRegistry registry = new PrometheusRegistry();
--- a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmNativeMemoryMetrics.java
+++ b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmNativeMemoryMetrics.java
@@ -89,7 +89,7 @@ public class JvmNativeMemoryMetrics {
private static final String JVM_NATIVE_MEMORY_COMMITTED_BYTES =
"jvm_native_memory_committed_bytes";
- private static final Pattern pattern =
+ private static final Pattern PATTERN =
Pattern.compile("\\s*([A-Z][A-Za-z\\s]*[A-Za-z]+).*reserved=(\\d+), committed=(\\d+)");
/** Package private. For testing only. */
@@ -133,7 +133,7 @@ public class JvmNativeMemoryMetrics {
return callback -> {
String summary = vmNativeMemorySummaryInBytesOrEmpty();
if (!summary.isEmpty()) {
- Matcher matcher = pattern.matcher(summary);
+ Matcher matcher = PATTERN.matcher(summary);
while (matcher.find()) {
String category = matcher.group(1);
long value =
--- a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmThreadsMetrics.java
+++ b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmThreadsMetrics.java
@@ -176,7 +176,7 @@ public class JvmThreadsMetrics {
}
private double nullSafeArrayLength(long[] array) {
- return null == array ? 0 : array.length;
+ return array == null ? 0 : array.length;
}
public static Builder builder() {
--- a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/ProcessMetrics.java
+++ b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/ProcessMetrics.java
@@ -1,5 +1,7 @@
package io.prometheus.metrics.instrumentation.jvm;
+import static java.nio.charset.StandardCharsets.UTF_8;
+
import io.prometheus.metrics.config.PrometheusProperties;
import io.prometheus.metrics.core.metrics.CounterWithCallback;
import io.prometheus.metrics.core.metrics.GaugeWithCallback;
@@ -14,7 +16,6 @@ import java.lang.management.OperatingSystemMXBean;
import java.lang.management.RuntimeMXBean;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
-import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
/**
@@ -243,8 +244,7 @@ public class ProcessMetrics {
@Override
public String lineStartingWith(File file, String prefix) throws IOException {
try (BufferedReader reader =
- new BufferedReader(
- new InputStreamReader(Files.newInputStream(file.toPath()), StandardCharsets.UTF_8))) {
+ new BufferedReader(new InputStreamReader(Files.newInputStream(file.toPath()), UTF_8))) {
String line = reader.readLine();
while (line != null) {
if (line.startsWith(prefix)) {
--- a/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmBufferPoolMetricsTest.java
+++ b/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmBufferPoolMetricsTest.java
@@ -2,7 +2,8 @@ package io.prometheus.metrics.instrumentation.jvm;
import static io.prometheus.metrics.instrumentation.jvm.TestUtil.convertToOpenMetricsFormat;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -14,15 +15,14 @@ import java.lang.management.BufferPoolMXBean;
import java.util.Arrays;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-import org.mockito.Mockito;
-class JvmBufferPoolMetricsTest {
+final class JvmBufferPoolMetricsTest {
- private final BufferPoolMXBean directBuffer = Mockito.mock(BufferPoolMXBean.class);
- private final BufferPoolMXBean mappedBuffer = Mockito.mock(BufferPoolMXBean.class);
+ private final BufferPoolMXBean directBuffer = mock();
+ private final BufferPoolMXBean mappedBuffer = mock();
@BeforeEach
- public void setUp() {
+ void setUp() {
when(directBuffer.getName()).thenReturn("direct");
when(directBuffer.getCount()).thenReturn(2L);
when(directBuffer.getMemoryUsed()).thenReturn(1234L);
@@ -34,7 +34,7 @@ class JvmBufferPoolMetricsTest {
}
@Test
- public void testGoodCase() throws IOException {
+ void goodCase() throws IOException {
PrometheusRegistry registry = new PrometheusRegistry();
JvmBufferPoolMetrics.builder()
.bufferPoolBeans(Arrays.asList(mappedBuffer, directBuffer))
@@ -62,7 +62,7 @@ class JvmBufferPoolMetricsTest {
}
@Test
- public void testIgnoredMetricNotScraped() {
+ void ignoredMetricNotScraped() {
MetricNameFilter filter =
MetricNameFilter.builder().nameMustNotBeEqualTo("jvm_buffer_pool_used_bytes").build();
@@ -72,9 +72,9 @@ class JvmBufferPoolMetricsTest {
.register(registry);
registry.scrape(filter);
- verify(directBuffer, times(0)).getMemoryUsed();
- verify(mappedBuffer, times(0)).getMemoryUsed();
- verify(directBuffer, times(1)).getTotalCapacity();
- verify(mappedBuffer, times(1)).getTotalCapacity();
+ verify(directBuffer, never()).getMemoryUsed();
+ verify(mappedBuffer, never()).getMemoryUsed();
+ verify(directBuffer).getTotalCapacity();
+ verify(mappedBuffer).getTotalCapacity();
}
}
--- a/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmClassLoadingMetricsTest.java
+++ b/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmClassLoadingMetricsTest.java
@@ -2,7 +2,8 @@ package io.prometheus.metrics.instrumentation.jvm;
import static io.prometheus.metrics.instrumentation.jvm.TestUtil.convertToOpenMetricsFormat;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -13,21 +14,20 @@ import java.io.IOException;
import java.lang.management.ClassLoadingMXBean;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-import org.mockito.Mockito;
-class JvmClassLoadingMetricsTest {
+final class JvmClassLoadingMetricsTest {
- private final ClassLoadingMXBean mockClassLoadingBean = Mockito.mock(ClassLoadingMXBean.class);
+ private final ClassLoadingMXBean mockClassLoadingBean = mock();
@BeforeEach
- public void setUp() {
+ void setUp() {
when(mockClassLoadingBean.getLoadedClassCount()).thenReturn(1000);
when(mockClassLoadingBean.getTotalLoadedClassCount()).thenReturn(2000L);
when(mockClassLoadingBean.getUnloadedClassCount()).thenReturn(500L);
}
@Test
- public void testGoodCase() throws IOException {
+ void goodCase() throws IOException {
PrometheusRegistry registry = new PrometheusRegistry();
JvmClassLoadingMetrics.builder().classLoadingBean(mockClassLoadingBean).register(registry);
MetricSnapshots snapshots = registry.scrape();
@@ -48,7 +48,7 @@ class JvmClassLoadingMetricsTest {
}
@Test
- public void testIgnoredMetricNotScraped() {
+ void ignoredMetricNotScraped() {
MetricNameFilter filter =
MetricNameFilter.builder().nameMustNotBeEqualTo("jvm_classes_currently_loaded").build();
@@ -56,7 +56,7 @@ class JvmClassLoadingMetricsTest {
JvmClassLoadingMetrics.builder().classLoadingBean(mockClassLoadingBean).register(registry);
registry.scrape(filter);
- verify(mockClassLoadingBean, times(0)).getLoadedClassCount();
- verify(mockClassLoadingBean, times(1)).getTotalLoadedClassCount();
+ verify(mockClassLoadingBean, never()).getLoadedClassCount();
+ verify(mockClassLoadingBean).getTotalLoadedClassCount();
}
}
--- a/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmCompilationMetricsTest.java
+++ b/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmCompilationMetricsTest.java
@@ -2,6 +2,7 @@ package io.prometheus.metrics.instrumentation.jvm;
import static io.prometheus.metrics.instrumentation.jvm.TestUtil.convertToOpenMetricsFormat;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.internal.verification.VerificationModeFactory.times;
@@ -13,20 +14,19 @@ import java.io.IOException;
import java.lang.management.CompilationMXBean;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-import org.mockito.Mockito;
-class JvmCompilationMetricsTest {
+final class JvmCompilationMetricsTest {
- private final CompilationMXBean mockCompilationBean = Mockito.mock(CompilationMXBean.class);
+ private final CompilationMXBean mockCompilationBean = mock();
@BeforeEach
- public void setUp() {
+ void setUp() {
when(mockCompilationBean.getTotalCompilationTime()).thenReturn(10000L);
when(mockCompilationBean.isCompilationTimeMonitoringSupported()).thenReturn(true);
}
@Test
- public void testGoodCase() throws IOException {
+ void goodCase() throws IOException {
PrometheusRegistry registry = new PrometheusRegistry();
JvmCompilationMetrics.builder().compilationBean(mockCompilationBean).register(registry);
MetricSnapshots snapshots = registry.scrape();
@@ -42,7 +42,7 @@ class JvmCompilationMetricsTest {
}
@Test
- public void testIgnoredMetricNotScraped() {
+ void ignoredMetricNotScraped() {
MetricNameFilter filter =
MetricNameFilter.builder()
.nameMustNotBeEqualTo("jvm_compilation_time_seconds_total")
@@ -53,6 +53,6 @@ class JvmCompilationMetricsTest {
MetricSnapshots snapshots = registry.scrape(filter);
verify(mockCompilationBean, times(0)).getTotalCompilationTime();
- assertThat(snapshots.size()).isZero();
+ assertThat(snapshots.size()).isEqualTo(0);
}
}
--- a/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmGarbageCollectorMetricsTest.java
+++ b/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmGarbageCollectorMetricsTest.java
@@ -2,7 +2,8 @@ package io.prometheus.metrics.instrumentation.jvm;
import static io.prometheus.metrics.instrumentation.jvm.TestUtil.convertToOpenMetricsFormat;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -15,15 +16,14 @@ import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-import org.mockito.Mockito;
-class JvmGarbageCollectorMetricsTest {
+final class JvmGarbageCollectorMetricsTest {
- private final GarbageCollectorMXBean mockGcBean1 = Mockito.mock(GarbageCollectorMXBean.class);
- private final GarbageCollectorMXBean mockGcBean2 = Mockito.mock(GarbageCollectorMXBean.class);
+ private final GarbageCollectorMXBean mockGcBean1 = mock();
+ private final GarbageCollectorMXBean mockGcBean2 = mock();
@BeforeEach
- public void setUp() {
+ void setUp() {
when(mockGcBean1.getName()).thenReturn("MyGC1");
when(mockGcBean1.getCollectionCount()).thenReturn(100L);
when(mockGcBean1.getCollectionTime()).thenReturn(TimeUnit.SECONDS.toMillis(10));
@@ -33,7 +33,7 @@ class JvmGarbageCollectorMetricsTest {
}
@Test
- public void testGoodCase() throws IOException {
+ void goodCase() throws IOException {
PrometheusRegistry registry = new PrometheusRegistry();
JvmGarbageCollectorMetrics.builder()
.garbageCollectorBeans(Arrays.asList(mockGcBean1, mockGcBean2))
@@ -54,7 +54,7 @@ class JvmGarbageCollectorMetricsTest {
}
@Test
- public void testIgnoredMetricNotScraped() {
+ void ignoredMetricNotScraped() {
MetricNameFilter filter =
MetricNameFilter.builder().nameMustNotBeEqualTo("jvm_gc_collection_seconds").build();
@@ -64,8 +64,8 @@ class JvmGarbageCollectorMetricsTest {
.register(registry);
MetricSnapshots snapshots = registry.scrape(filter);
- verify(mockGcBean1, times(0)).getCollectionTime();
- verify(mockGcBean1, times(0)).getCollectionCount();
- assertThat(snapshots.size()).isZero();
+ verify(mockGcBean1, never()).getCollectionTime();
+ verify(mockGcBean1, never()).getCollectionCount();
+ assertThat(snapshots.size()).isEqualTo(0);
}
}
--- a/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmMemoryMetricsTest.java
+++ b/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmMemoryMetricsTest.java
@@ -2,7 +2,8 @@ package io.prometheus.metrics.instrumentation.jvm;
import static io.prometheus.metrics.instrumentation.jvm.TestUtil.convertToOpenMetricsFormat;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -16,22 +17,21 @@ import java.lang.management.MemoryUsage;
import java.util.Arrays;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-import org.mockito.Mockito;
-class JvmMemoryMetricsTest {
+final class JvmMemoryMetricsTest {
- private final MemoryMXBean mockMemoryBean = Mockito.mock(MemoryMXBean.class);
- private final MemoryPoolMXBean mockPoolsBeanEdenSpace = Mockito.mock(MemoryPoolMXBean.class);
- private final MemoryPoolMXBean mockPoolsBeanOldGen = Mockito.mock(MemoryPoolMXBean.class);
- private final MemoryUsage memoryUsageHeap = Mockito.mock(MemoryUsage.class);
- private final MemoryUsage memoryUsageNonHeap = Mockito.mock(MemoryUsage.class);
- private final MemoryUsage memoryUsagePoolEdenSpace = Mockito.mock(MemoryUsage.class);
- private final MemoryUsage memoryUsagePoolOldGen = Mockito.mock(MemoryUsage.class);
- private final MemoryUsage memoryUsagePoolCollectionEdenSpace = Mockito.mock(MemoryUsage.class);
- private final MemoryUsage memoryUsagePoolCollectionOldGen = Mockito.mock(MemoryUsage.class);
+ private final MemoryMXBean mockMemoryBean = mock();
+ private final MemoryPoolMXBean mockPoolsBeanEdenSpace = mock();
+ private final MemoryPoolMXBean mockPoolsBeanOldGen = mock();
+ private final MemoryUsage memoryUsageHeap = mock();
+ private final MemoryUsage memoryUsageNonHeap = mock();
+ private final MemoryUsage memoryUsagePoolEdenSpace = mock();
+ private final MemoryUsage memoryUsagePoolOldGen = mock();
+ private final MemoryUsage memoryUsagePoolCollectionEdenSpace = mock();
+ private final MemoryUsage memoryUsagePoolCollectionOldGen = mock();
@BeforeEach
- public void setUp() {
+ void setUp() {
when(mockMemoryBean.getHeapMemoryUsage()).thenReturn(memoryUsageHeap);
when(mockMemoryBean.getNonHeapMemoryUsage()).thenReturn(memoryUsageNonHeap);
@@ -79,7 +79,7 @@ class JvmMemoryMetricsTest {
}
@Test
- public void testGoodCase() throws IOException {
+ void goodCase() throws IOException {
PrometheusRegistry registry = new PrometheusRegistry();
JvmMemoryMetrics.builder()
.withMemoryBean(mockMemoryBean)
@@ -157,7 +157,7 @@ class JvmMemoryMetricsTest {
}
@Test
- public void testIgnoredMetricNotScraped() {
+ void ignoredMetricNotScraped() {
MetricNameFilter filter =
MetricNameFilter.builder().nameMustNotBeEqualTo("jvm_memory_pool_used_bytes").build();
@@ -168,9 +168,9 @@ class JvmMemoryMetricsTest {
.register(registry);
registry.scrape(filter);
- verify(memoryUsagePoolEdenSpace, times(0)).getUsed();
- verify(memoryUsagePoolOldGen, times(0)).getUsed();
- verify(memoryUsagePoolEdenSpace, times(1)).getMax();
- verify(memoryUsagePoolOldGen, times(1)).getMax();
+ verify(memoryUsagePoolEdenSpace, never()).getUsed();
+ verify(memoryUsagePoolOldGen, never()).getUsed();
+ verify(memoryUsagePoolEdenSpace).getMax();
+ verify(memoryUsagePoolOldGen).getMax();
}
}
--- a/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmMemoryPoolAllocationMetricsTest.java
+++ b/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmMemoryPoolAllocationMetricsTest.java
@@ -11,10 +11,10 @@ import io.prometheus.metrics.model.snapshots.MetricSnapshot;
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
import org.junit.jupiter.api.Test;
-class JvmMemoryPoolAllocationMetricsTest {
+final class JvmMemoryPoolAllocationMetricsTest {
@Test
- public void testListenerLogic() {
+ void listenerLogic() {
PrometheusRegistry registry = new PrometheusRegistry();
Counter counter = Counter.builder().name("test").labelNames("pool").register(registry);
AllocationCountingNotificationListener listener =
@@ -60,7 +60,7 @@ class JvmMemoryPoolAllocationMetricsTest {
}
}
}
- fail("pool " + poolName + " not found.");
+ fail("pool %s not found.", poolName);
return 0.0;
}
}
--- a/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmMetricsTest.java
+++ b/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmMetricsTest.java
@@ -8,7 +8,7 @@ import java.lang.management.ManagementFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-class JvmMetricsTest {
+final class JvmMetricsTest {
@BeforeEach
void setUp() {
@@ -16,11 +16,11 @@ class JvmMetricsTest {
}
@Test
- public void testRegisterIdempotent() {
+ void registerIdempotent() {
PrometheusRegistry registry = new PrometheusRegistry();
- assertThat(registry.scrape().size()).isZero();
+ assertThat(registry.scrape().size()).isEqualTo(0);
JvmMetrics.builder().register(registry);
- assertThat(registry.scrape().size()).isGreaterThan(0);
+ assertThat(registry.scrape().size()).isPositive();
JvmMetrics.builder().register(registry);
}
@@ -33,7 +33,7 @@ class JvmMetricsTest {
}
@Test
- void testJvmMetrics() {
+ void jvmMetrics() {
JvmMetrics.builder(PrometheusProperties.get()).register();
JvmMetrics.builder().register();
}
--- a/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmNativeMemoryMetricsTest.java
+++ b/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmNativeMemoryMetricsTest.java
@@ -2,6 +2,7 @@ package io.prometheus.metrics.instrumentation.jvm;
import static io.prometheus.metrics.instrumentation.jvm.TestUtil.convertToOpenMetricsFormat;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import io.prometheus.metrics.config.PrometheusProperties;
@@ -9,16 +10,14 @@ import io.prometheus.metrics.model.registry.PrometheusRegistry;
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
import java.io.IOException;
import org.junit.jupiter.api.Test;
-import org.mockito.Mockito;
-class JvmNativeMemoryMetricsTest {
+final class JvmNativeMemoryMetricsTest {
@Test
- public void testNativeMemoryTrackingFail() throws IOException {
+ void nativeMemoryTrackingFail() throws IOException {
JvmNativeMemoryMetrics.isEnabled.set(true);
- JvmNativeMemoryMetrics.PlatformMBeanServerAdapter adapter =
- Mockito.mock(JvmNativeMemoryMetrics.PlatformMBeanServerAdapter.class);
+ JvmNativeMemoryMetrics.PlatformMBeanServerAdapter adapter = mock();
when(adapter.vmNativeMemorySummaryInBytes()).thenThrow(new RuntimeException("mock"));
PrometheusRegistry registry = new PrometheusRegistry();
@@ -39,11 +38,10 @@ class JvmNativeMemoryMetricsTest {
}
@Test
- public void testNativeMemoryTrackingEmpty() throws IOException {
+ void nativeMemoryTrackingEmpty() throws IOException {
JvmNativeMemoryMetrics.isEnabled.set(true);
- JvmNativeMemoryMetrics.PlatformMBeanServerAdapter adapter =
- Mockito.mock(JvmNativeMemoryMetrics.PlatformMBeanServerAdapter.class);
+ JvmNativeMemoryMetrics.PlatformMBeanServerAdapter adapter = mock();
when(adapter.vmNativeMemorySummaryInBytes()).thenReturn("");
PrometheusRegistry registry = new PrometheusRegistry();
@@ -56,11 +54,10 @@ class JvmNativeMemoryMetricsTest {
}
@Test
- public void testNativeMemoryTrackingDisabled() throws IOException {
+ void nativeMemoryTrackingDisabled() throws IOException {
JvmNativeMemoryMetrics.isEnabled.set(true);
- JvmNativeMemoryMetrics.PlatformMBeanServerAdapter adapter =
- Mockito.mock(JvmNativeMemoryMetrics.PlatformMBeanServerAdapter.class);
+ JvmNativeMemoryMetrics.PlatformMBeanServerAdapter adapter = mock();
when(adapter.vmNativeMemorySummaryInBytes())
.thenReturn("Native memory tracking is not enabled");
@@ -74,11 +71,10 @@ class JvmNativeMemoryMetricsTest {
}
@Test
- public void testNativeMemoryTrackingEnabled() throws IOException {
+ void nativeMemoryTrackingEnabled() throws IOException {
JvmNativeMemoryMetrics.isEnabled.set(true);
- JvmNativeMemoryMetrics.PlatformMBeanServerAdapter adapter =
- Mockito.mock(JvmNativeMemoryMetrics.PlatformMBeanServerAdapter.class);
+ JvmNativeMemoryMetrics.PlatformMBeanServerAdapter adapter = mock();
when(adapter.vmNativeMemorySummaryInBytes())
.thenReturn(
"Native Memory Tracking:\n"
--- a/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmRuntimeInfoMetricTest.java
+++ b/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmRuntimeInfoMetricTest.java
@@ -8,10 +8,10 @@ import io.prometheus.metrics.model.snapshots.MetricSnapshots;
import java.io.IOException;
import org.junit.jupiter.api.Test;
-class JvmRuntimeInfoMetricTest {
+final class JvmRuntimeInfoMetricTest {
@Test
- public void testGoodCase() throws IOException {
+ void goodCase() throws IOException {
PrometheusRegistry registry = new PrometheusRegistry();
JvmRuntimeInfoMetric.builder()
.version("1.8.0_382-b05")
--- a/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmThreadsMetricsTest.java
+++ b/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmThreadsMetricsTest.java
@@ -2,7 +2,8 @@ package io.prometheus.metrics.instrumentation.jvm;
import static io.prometheus.metrics.instrumentation.jvm.TestUtil.convertToOpenMetricsFormat;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -19,17 +20,16 @@ import java.util.Map;
import java.util.concurrent.CountDownLatch;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-import org.mockito.Mockito;
-class JvmThreadsMetricsTest {
+final class JvmThreadsMetricsTest {
- private final ThreadMXBean mockThreadsBean = Mockito.mock(ThreadMXBean.class);
- private final ThreadInfo mockThreadInfoBlocked = Mockito.mock(ThreadInfo.class);
- private final ThreadInfo mockThreadInfoRunnable1 = Mockito.mock(ThreadInfo.class);
- private final ThreadInfo mockThreadInfoRunnable2 = Mockito.mock(ThreadInfo.class);
+ private final ThreadMXBean mockThreadsBean = mock();
+ private final ThreadInfo mockThreadInfoBlocked = mock();
+ private final ThreadInfo mockThreadInfoRunnable1 = mock();
+ private final ThreadInfo mockThreadInfoRunnable2 = mock();
@BeforeEach
- public void setUp() {
+ void setUp() {
when(mockThreadsBean.getThreadCount()).thenReturn(300);
when(mockThreadsBean.getDaemonThreadCount()).thenReturn(200);
when(mockThreadsBean.getPeakThreadCount()).thenReturn(301);
@@ -48,7 +48,7 @@ class JvmThreadsMetricsTest {
}
@Test
- public void testGoodCase() throws IOException {
+ void goodCase() throws IOException {
PrometheusRegistry registry = new PrometheusRegistry();
JvmThreadsMetrics.builder().threadBean(mockThreadsBean).isNativeImage(false).register(registry);
MetricSnapshots snapshots = registry.scrape();
@@ -87,7 +87,7 @@ class JvmThreadsMetricsTest {
}
@Test
- public void testIgnoredMetricNotScraped() {
+ void ignoredMetricNotScraped() {
MetricNameFilter filter =
MetricNameFilter.builder().nameMustNotBeEqualTo("jvm_threads_deadlocked").build();
@@ -95,12 +95,12 @@ class JvmThreadsMetricsTest {
JvmThreadsMetrics.builder().threadBean(mockThreadsBean).isNativeImage(false).register(registry);
registry.scrape(filter);
- verify(mockThreadsBean, times(0)).findDeadlockedThreads();
- verify(mockThreadsBean, times(1)).getThreadCount();
+ verify(mockThreadsBean, never()).findDeadlockedThreads();
+ verify(mockThreadsBean).getThreadCount();
}
@Test
- public void testInvalidThreadIds() {
+ void invalidThreadIds() {
try {
String javaVersion = System.getProperty("java.version"); // Example: "21.0.2"
String majorJavaVersion = javaVersion.replaceAll("\\..*", ""); // Example: "21"
--- a/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/ProcessMetricsTest.java
+++ b/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/ProcessMetricsTest.java
@@ -4,7 +4,8 @@ import static io.prometheus.metrics.instrumentation.jvm.TestUtil.convertToOpenMe
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
-import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -17,20 +18,17 @@ import java.lang.management.RuntimeMXBean;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-import org.mockito.Mockito;
-class ProcessMetricsTest {
+final class ProcessMetricsTest {
- private final com.sun.management.UnixOperatingSystemMXBean sunOsBean =
- Mockito.mock(com.sun.management.UnixOperatingSystemMXBean.class);
- private final java.lang.management.OperatingSystemMXBean javaOsBean =
- Mockito.mock(java.lang.management.OperatingSystemMXBean.class);
- private final ProcessMetrics.Grepper linuxGrepper = Mockito.mock(ProcessMetrics.Grepper.class);
- private final ProcessMetrics.Grepper windowsGrepper = Mockito.mock(ProcessMetrics.Grepper.class);
- private final RuntimeMXBean runtimeBean = Mockito.mock(RuntimeMXBean.class);
+ private final com.sun.management.UnixOperatingSystemMXBean sunOsBean = mock();
+ private final java.lang.management.OperatingSystemMXBean javaOsBean = mock();
+ private final ProcessMetrics.Grepper linuxGrepper = mock();
+ private final ProcessMetrics.Grepper windowsGrepper = mock();
+ private final RuntimeMXBean runtimeBean = mock();
@BeforeEach
- public void setUp() throws IOException {
+ void setUp() throws IOException {
when(sunOsBean.getProcessCpuTime()).thenReturn(TimeUnit.MILLISECONDS.toNanos(72));
when(sunOsBean.getOpenFileDescriptorCount()).thenReturn(127L);
when(sunOsBean.getMaxFileDescriptorCount()).thenReturn(244L);
@@ -42,7 +40,7 @@ class ProcessMetricsTest {
}
@Test
- public void testGoodCase() throws IOException {
+ void goodCase() throws IOException {
PrometheusRegistry registry = new PrometheusRegistry();
ProcessMetrics.builder()
.osBean(sunOsBean)
@@ -89,7 +87,7 @@ class ProcessMetricsTest {
}
@Test
- public void testMinimal() throws IOException {
+ void minimal() throws IOException {
PrometheusRegistry registry = new PrometheusRegistry();
ProcessMetrics.builder()
.osBean(javaOsBean)
@@ -109,7 +107,7 @@ class ProcessMetricsTest {
}
@Test
- public void testIgnoredMetricNotScraped() {
+ void ignoredMetricNotScraped() {
MetricNameFilter filter =
MetricNameFilter.builder().nameMustNotBeEqualTo("process_max_fds").build();
@@ -121,7 +119,7 @@ class ProcessMetricsTest {
.register(registry);
registry.scrape(filter);
- verify(sunOsBean, times(0)).getMaxFileDescriptorCount();
- verify(sunOsBean, times(1)).getOpenFileDescriptorCount();
+ verify(sunOsBean, never()).getMaxFileDescriptorCount();
+ verify(sunOsBean).getOpenFileDescriptorCount();
}
}
--- a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/registry/MultiCollector.java
+++ b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/registry/MultiCollector.java
@@ -1,8 +1,8 @@
package io.prometheus.metrics.model.registry;
+import com.google.common.collect.ImmutableList;
import io.prometheus.metrics.model.snapshots.MetricSnapshot;
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
-import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
@@ -67,6 +67,6 @@ public interface MultiCollector {
* runtime it is a good idea to overwrite this and return the names.
*/
default List<String> getPrometheusNames() {
- return Collections.emptyList();
+ return ImmutableList.of();
}
}
--- a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/registry/PrometheusRegistry.java
+++ b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/registry/PrometheusRegistry.java
@@ -1,5 +1,6 @@
package io.prometheus.metrics.model.registry;
+import static com.google.common.base.Preconditions.checkState;
import static io.prometheus.metrics.model.snapshots.PrometheusNaming.prometheusName;
import io.prometheus.metrics.model.snapshots.MetricSnapshot;
@@ -21,22 +22,20 @@ public class PrometheusRegistry {
public void register(Collector collector) {
String prometheusName = collector.getPrometheusName();
if (prometheusName != null) {
- if (!prometheusNames.add(prometheusName)) {
- throw new IllegalStateException(
- "Can't register "
- + prometheusName
- + " because a metric with that name is already registered.");
- }
+ checkState(
+ prometheusNames.add(prometheusName),
+ "Can't register %s because a metric with that name is already registered.",
+ prometheusName);
}
collectors.add(collector);
}
public void register(MultiCollector collector) {
for (String prometheusName : collector.getPrometheusNames()) {
- if (!prometheusNames.add(prometheusName)) {
- throw new IllegalStateException(
- "Can't register " + prometheusName + " because that name is already registered.");
- }
+ checkState(
+ prometheusNames.add(prometheusName),
+ "Can't register %s because that name is already registered.",
+ prometheusName);
}
multiCollectors.add(collector);
}
@@ -72,10 +71,10 @@ public class PrometheusRegistry {
MetricSnapshot snapshot =
scrapeRequest == null ? collector.collect() : collector.collect(scrapeRequest);
if (snapshot != null) {
- if (result.containsMetricName(snapshot.getMetadata().getName())) {
- throw new IllegalStateException(
- snapshot.getMetadata().getPrometheusName() + ": duplicate metric name.");
- }
+ checkState(
+ !result.containsMetricName(snapshot.getMetadata().getName()),
+ "%s: duplicate metric name.",
+ snapshot.getMetadata().getPrometheusName());
result.metricSnapshot(snapshot);
}
}
@@ -83,10 +82,10 @@ public class PrometheusRegistry {
MetricSnapshots snapshots =
scrapeRequest == null ? collector.collect() : collector.collect(scrapeRequest);
for (MetricSnapshot snapshot : snapshots) {
- if (result.containsMetricName(snapshot.getMetadata().getName())) {
- throw new IllegalStateException(
- snapshot.getMetadata().getPrometheusName() + ": duplicate metric name.");
- }
+ checkState(
+ !result.containsMetricName(snapshot.getMetadata().getName()),
+ "%s: duplicate metric name.",
+ snapshot.getMetadata().getPrometheusName());
result.metricSnapshot(snapshot);
}
}
--- a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/ClassicHistogramBucket.java
+++ b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/ClassicHistogramBucket.java
@@ -1,5 +1,7 @@
package io.prometheus.metrics.model.snapshots;
+import static com.google.common.base.Preconditions.checkArgument;
+
/**
* Helper class for iterating over {@link ClassicHistogramBuckets}. Note that the {@code count} is
* <i>not</i> cumulative.
@@ -12,16 +14,13 @@ public class ClassicHistogramBucket implements Comparable<ClassicHistogramBucket
public ClassicHistogramBucket(double upperBound, long count) {
this.count = count;
this.upperBound = upperBound;
- if (Double.isNaN(upperBound)) {
- throw new IllegalArgumentException("Cannot use NaN as an upper bound for a histogram bucket");
- }
- if (count < 0) {
- throw new IllegalArgumentException(
- count
- + ": "
- + ClassicHistogramBuckets.class.getSimpleName()
- + " cannot have a negative count");
- }
+ checkArgument(
+ !Double.isNaN(upperBound), "Cannot use NaN as an upper bound for a histogram bucket");
+ checkArgument(
+ count >= 0,
+ "%s: %s cannot have a negative count",
+ count,
+ ClassicHistogramBuckets.class.getSimpleName());
}
public long getCount() {
--- a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/ClassicHistogramBuckets.java
+++ b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/ClassicHistogramBuckets.java
@@ -1,8 +1,10 @@
package io.prometheus.metrics.model.snapshots;
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Collections.unmodifiableList;
+
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Stream;
@@ -98,14 +100,11 @@ public class ClassicHistogramBuckets implements Iterable<ClassicHistogramBucket>
}
private static void sortAndValidate(double[] upperBounds, long[] counts) {
- if (upperBounds.length != counts.length) {
- throw new IllegalArgumentException(
- "upperBounds.length == "
- + upperBounds.length
- + " but counts.length == "
- + counts.length
- + ". Expected the same length.");
- }
+ checkArgument(
+ upperBounds.length == counts.length,
+ "upperBounds.length == %s but counts.length == %s. Expected the same length.",
+ upperBounds.length,
+ counts.length);
sort(upperBounds, counts);
validate(upperBounds, counts);
}
@@ -135,28 +134,26 @@ public class ClassicHistogramBuckets implements Iterable<ClassicHistogramBucket>
// Preconditions:
// * upperBounds sorted
// * upperBounds and counts have the same length
- if (upperBounds.length == 0) {
- throw new IllegalArgumentException(
- ClassicHistogramBuckets.class.getSimpleName()
- + " cannot be empty. They must contain at least the +Inf bucket.");
- }
- if (upperBounds[upperBounds.length - 1] != Double.POSITIVE_INFINITY) {
- throw new IllegalArgumentException(
- ClassicHistogramBuckets.class.getSimpleName() + " must contain the +Inf bucket.");
- }
+ checkArgument(
+ upperBounds.length != 0,
+ "%s cannot be empty. They must contain at least the +Inf bucket.",
+ ClassicHistogramBuckets.class.getSimpleName());
+ checkArgument(
+ upperBounds[upperBounds.length - 1] == Double.POSITIVE_INFINITY,
+ "%s must contain the +Inf bucket.",
+ ClassicHistogramBuckets.class.getSimpleName());
for (int i = 0; i < upperBounds.length; i++) {
- if (Double.isNaN(upperBounds[i])) {
- throw new IllegalArgumentException(
- "Cannot use NaN as an upper bound in " + ClassicHistogramBuckets.class.getSimpleName());
- }
- if (counts[i] < 0) {
- throw new IllegalArgumentException(
- "Counts in " + ClassicHistogramBuckets.class.getSimpleName() + " cannot be negative.");
- }
+ checkArgument(
+ !Double.isNaN(upperBounds[i]),
+ "Cannot use NaN as an upper bound in %s",
+ ClassicHistogramBuckets.class.getSimpleName());
+ checkArgument(
+ counts[i] >= 0,
+ "Counts in %s cannot be negative.",
+ ClassicHistogramBuckets.class.getSimpleName());
if (i > 0) {
- if (upperBounds[i - 1] == upperBounds[i]) {
- throw new IllegalArgumentException("Duplicate upper bound " + upperBounds[i]);
- }
+ checkArgument(
+ upperBounds[i - 1] != upperBounds[i], "Duplicate upper bound %s", upperBounds[i]);
}
}
}
@@ -183,7 +180,7 @@ public class ClassicHistogramBuckets implements Iterable<ClassicHistogramBucket>
for (int i = 0; i < upperBounds.length; i++) {
result.add(new ClassicHistogramBucket(upperBounds[i], counts[i]));
}
- return Collections.unmodifiableList(result);
+ return unmodifiableList(result);
}
@Override
--- a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/CounterSnapshot.java
+++ b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/CounterSnapshot.java
@@ -1,5 +1,7 @@
package io.prometheus.metrics.model.snapshots;
+import static com.google.common.base.Preconditions.checkArgument;
+
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@@ -19,8 +21,8 @@ public class CounterSnapshot extends MetricSnapshot {
super(metadata, dataPoints);
}
- @SuppressWarnings("unchecked")
@Override
+ @SuppressWarnings("unchecked")
public List<CounterDataPointSnapshot> getDataPoints() {
return (List<CounterDataPointSnapshot>) dataPoints;
}
@@ -74,9 +76,7 @@ public class CounterSnapshot extends MetricSnapshot {
}
protected void validate() {
- if (value < 0.0) {
- throw new IllegalArgumentException(value + ": counters cannot have a negative value");
- }
+ checkArgument(value >= 0.0, "%s: counters cannot have a negative value", value);
}
public static Builder builder() {
@@ -108,9 +108,7 @@ public class CounterSnapshot extends MetricSnapshot {
}
public CounterDataPointSnapshot build() {
- if (value == null) {
- throw new IllegalArgumentException("Missing required field: value is null.");
- }
+ checkArgument(value != null, "Missing required field: value is null.");
return new CounterDataPointSnapshot(
value, labels, exemplar, createdTimestampMillis, scrapeTimestampMillis);
}
--- a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/DataPointSnapshot.java
+++ b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/DataPointSnapshot.java
@@ -1,5 +1,7 @@
package io.prometheus.metrics.model.snapshots;
+import static com.google.common.base.Preconditions.checkArgument;
+
@SuppressWarnings("this-escape")
public abstract class DataPointSnapshot {
private final Labels labels;
@@ -15,25 +17,20 @@ public abstract class DataPointSnapshot {
}
private void validate() {
- if (labels == null) {
- throw new IllegalArgumentException(
- "Labels cannot be null. Use Labels.EMPTY if there are no labels.");
- }
- if (createdTimestampMillis < 0) {
- throw new IllegalArgumentException(
- "Created timestamp cannot be negative. "
- + "Use 0 if the metric doesn't have a created timestamp.");
- }
- if (scrapeTimestampMillis < 0) {
- throw new IllegalArgumentException(
- "Scrape timestamp cannot be negative. "
- + "Use 0 to indicate that the Prometheus server should set the scrape timestamp.");
- }
+ checkArgument(
+ labels != null, "Labels cannot be null. Use Labels.EMPTY if there are no labels.");
+ checkArgument(
+ createdTimestampMillis >= 0,
+ "Created timestamp cannot be negative. "
+ + "Use 0 if the metric doesn't have a created timestamp.");
+ checkArgument(
+ scrapeTimestampMillis >= 0,
+ "Scrape timestamp cannot be negative. "
+ + "Use 0 to indicate that the Prometheus server should set the scrape timestamp.");
if (hasCreatedTimestamp() && hasScrapeTimestamp()) {
- if (scrapeTimestampMillis < createdTimestampMillis) {
- throw new IllegalArgumentException(
- "The scrape timestamp cannot be before the created timestamp");
- }
+ checkArgument(
+ scrapeTimestampMillis >= createdTimestampMillis,
+ "The scrape timestamp cannot be before the created timestamp");
}
}
--- a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/Exemplar.java
+++ b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/Exemplar.java
@@ -1,5 +1,8 @@
package io.prometheus.metrics.model.snapshots;
+import static com.google.common.base.Preconditions.checkState;
+import static java.util.Objects.requireNonNull;
+
/** Immutable representation of an Exemplar. */
public class Exemplar {
@@ -24,9 +27,7 @@ public class Exemplar {
* available.
*/
public Exemplar(double value, Labels labels, long timestampMillis) {
- if (labels == null) {
- throw new NullPointerException("Labels cannot be null. Use Labels.EMPTY.");
- }
+ requireNonNull(labels, "Labels cannot be null. Use Labels.EMPTY.");
this.value = value;
this.labels = labels;
this.timestampMillis = timestampMillis;
@@ -83,9 +84,7 @@ public class Exemplar {
}
public Builder labels(Labels labels) {
- if (labels == null) {
- throw new NullPointerException();
- }
+ requireNonNull(labels);
this.labels = labels;
return this;
}
@@ -99,9 +98,7 @@ public class Exemplar {
* @throws IllegalStateException if {@link #value(double)} wasn't called.
*/
public Exemplar build() {
- if (value == null) {
- throw new IllegalStateException("cannot build an Exemplar without a value");
- }
+ checkState(value != null, "cannot build an Exemplar without a value");
Labels allLabels;
if (traceId != null && spanId != null) {
allLabels = Labels.of(TRACE_ID, traceId, SPAN_ID, spanId);
--- a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/Exemplars.java
+++ b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/Exemplars.java
@@ -1,9 +1,12 @@
package io.prometheus.metrics.model.snapshots;
+import static java.util.Collections.unmodifiableList;
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
-import java.util.Collections;
import java.util.Iterator;
import java.util.List;
@@ -16,19 +19,17 @@ import java.util.List;
public class Exemplars implements Iterable<Exemplar> {
/** EMPTY means no Exemplars. */
- public static final Exemplars EMPTY = new Exemplars(Collections.emptyList());
+ public static final Exemplars EMPTY = new Exemplars(ImmutableList.of());
private final List<Exemplar> exemplars;
private Exemplars(Collection<Exemplar> exemplars) {
List<Exemplar> copy = new ArrayList<>(exemplars.size());
for (Exemplar exemplar : exemplars) {
- if (exemplar == null) {
- throw new NullPointerException("Illegal null value in Exemplars");
- }
+ requireNonNull(exemplar, "Illegal null value in Exemplars");
copy.add(exemplar);
}
- this.exemplars = Collections.unmodifiableList(copy);
+ this.exemplars = unmodifiableList(copy);
}
/**
--- a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/GaugeSnapshot.java
+++ b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/GaugeSnapshot.java
@@ -1,5 +1,7 @@
package io.prometheus.metrics.model.snapshots;
+import static com.google.common.base.Preconditions.checkArgument;
+
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@@ -18,8 +20,8 @@ public final class GaugeSnapshot extends MetricSnapshot {
super(metadata, data);
}
- @SuppressWarnings("unchecked")
@Override
+ @SuppressWarnings("unchecked")
public List<GaugeDataPointSnapshot> getDataPoints() {
return (List<GaugeDataPointSnapshot>) dataPoints;
}
@@ -86,9 +88,7 @@ public final class GaugeSnapshot extends MetricSnapshot {
}
public GaugeDataPointSnapshot build() {
- if (value == null) {
- throw new IllegalArgumentException("Missing required field: value is null.");
- }
+ checkArgument(value != null, "Missing required field: value is null.");
return new GaugeDataPointSnapshot(value, labels, exemplar, scrapeTimestampMillis);
}
--- a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/HistogramSnapshot.java
+++ b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/HistogramSnapshot.java
@@ -1,5 +1,7 @@
package io.prometheus.metrics.model.snapshots;
+import static com.google.common.base.Preconditions.checkArgument;
+
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@@ -39,8 +41,8 @@ public final class HistogramSnapshot extends MetricSnapshot {
return gaugeHistogram;
}
- @SuppressWarnings("unchecked")
@Override
+ @SuppressWarnings("unchecked")
public List<HistogramDataPointSnapshot> getDataPoints() {
return (List<HistogramDataPointSnapshot>) dataPoints;
}
@@ -268,15 +270,11 @@ public final class HistogramSnapshot extends MetricSnapshot {
long nativeCount =
calculateNativeCount(
nativeZeroCount, nativeBucketsForPositiveValues, nativeBucketsForNegativeValues);
- if (classicCount != nativeCount) {
- throw new IllegalArgumentException(
- "Inconsistent observation count: If a histogram has both classic and native "
- + "data the observation count must be the same. Classic count is "
- + classicCount
- + " but native count is "
- + nativeCount
- + ".");
- }
+ checkArgument(
+ classicCount == nativeCount,
+ "Inconsistent observation count: If a histogram has both classic and native data the observation count must be the same. Classic count is %s but native count is %s.",
+ classicCount,
+ nativeCount);
return classicCount;
}
}
@@ -354,27 +352,22 @@ public final class HistogramSnapshot extends MetricSnapshot {
private void validate() {
for (Label label : getLabels()) {
- if (label.getName().equals("le")) {
- throw new IllegalArgumentException("le is a reserved label name for histograms");
- }
- }
- if (nativeSchema == CLASSIC_HISTOGRAM && classicBuckets.isEmpty()) {
- throw new IllegalArgumentException(
- "Histogram buckets cannot be empty, must at least have the +Inf bucket.");
+ checkArgument(!label.getName().equals("le"), "le is a reserved label name for histograms");
}
+ checkArgument(
+ nativeSchema != CLASSIC_HISTOGRAM || !classicBuckets.isEmpty(),
+ "Histogram buckets cannot be empty, must at least have the +Inf bucket.");
if (nativeSchema != CLASSIC_HISTOGRAM) {
- if (nativeSchema < -4 || nativeSchema > 8) {
- throw new IllegalArgumentException(
- nativeSchema + ": illegal schema. Expecting number in [-4, 8].");
- }
- if (nativeZeroCount < 0) {
- throw new IllegalArgumentException(
- nativeZeroCount + ": nativeZeroCount cannot be negative");
- }
- if (Double.isNaN(nativeZeroThreshold) || nativeZeroThreshold < 0) {
- throw new IllegalArgumentException(
- nativeZeroThreshold + ": illegal nativeZeroThreshold. Must be >= 0.");
- }
+ checkArgument(
+ nativeSchema >= -4 && nativeSchema <= 8,
+ "%s: illegal schema. Expecting number in [-4, 8].",
+ nativeSchema);
+ checkArgument(
+ nativeZeroCount >= 0, "%s: nativeZeroCount cannot be negative", nativeZeroCount);
+ checkArgument(
+ !Double.isNaN(nativeZeroThreshold) && nativeZeroThreshold >= 0,
+ "%s: illegal nativeZeroThreshold. Must be >= 0.",
+ nativeZeroThreshold);
}
}
@@ -431,10 +424,9 @@ public final class HistogramSnapshot extends MetricSnapshot {
}
public HistogramDataPointSnapshot build() {
- if (nativeSchema == CLASSIC_HISTOGRAM && classicHistogramBuckets.isEmpty()) {
- throw new IllegalArgumentException(
- "One of nativeSchema and classicHistogramBuckets is required.");
- }
+ checkArgument(
+ nativeSchema != CLASSIC_HISTOGRAM || !classicHistogramBuckets.isEmpty(),
+ "One of nativeSchema and classicHistogramBuckets is required.");
return new HistogramDataPointSnapshot(
classicHistogramBuckets,
nativeSchema,
--- a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/InfoSnapshot.java
+++ b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/InfoSnapshot.java
@@ -1,5 +1,7 @@
package io.prometheus.metrics.model.snapshots;
+import static com.google.common.base.Preconditions.checkArgument;
+
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@@ -17,13 +19,11 @@ public final class InfoSnapshot extends MetricSnapshot {
*/
public InfoSnapshot(MetricMetadata metadata, Collection<InfoDataPointSnapshot> data) {
super(metadata, data);
- if (metadata.hasUnit()) {
- throw new IllegalArgumentException("An Info metric cannot have a unit.");
- }
+ checkArgument(!metadata.hasUnit(), "An Info metric cannot have a unit.");
}
- @SuppressWarnings("unchecked")
@Override
+ @SuppressWarnings("unchecked")
public List<InfoDataPointSnapshot> getDataPoints() {
return (List<InfoDataPointSnapshot>) dataPoints;
}
--- a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/Labels.java
+++ b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/Labels.java
@@ -1,11 +1,12 @@
package io.prometheus.metrics.model.snapshots;
+import static com.google.common.base.Preconditions.checkArgument;
import static io.prometheus.metrics.model.snapshots.PrometheusNaming.isValidLabelName;
import static io.prometheus.metrics.model.snapshots.PrometheusNaming.prometheusName;
+import static java.util.Collections.unmodifiableList;
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Stream;
@@ -52,9 +53,7 @@ public final class Labels implements Comparable<Labels>, Iterable<Label> {
* names. Label names must be unique (no duplicate label names).
*/
public static Labels of(String... keyValuePairs) {
- if (keyValuePairs.length % 2 != 0) {
- throw new IllegalArgumentException("Key/value pairs must have an even length");
- }
+ checkArgument(keyValuePairs.length % 2 == 0, "Key/value pairs must have an even length");
if (keyValuePairs.length == 0) {
return EMPTY;
}
@@ -80,9 +79,7 @@ public final class Labels implements Comparable<Labels>, Iterable<Label> {
* @param values label values. {@code names.size()} must be equal to {@code values.size()}.
*/
public static Labels of(List<String> names, List<String> values) {
- if (names.size() != values.size()) {
- throw new IllegalArgumentException("Names and values must have the same size.");
- }
+ checkArgument(names.size() == values.size(), "Names and values must have the same size.");
if (names.isEmpty()) {
return EMPTY;
}
@@ -103,9 +100,7 @@ public final class Labels implements Comparable<Labels>, Iterable<Label> {
* @param values label values. {@code names.length} must be equal to {@code values.length}.
*/
public static Labels of(String[] names, String[] values) {
- if (names.length != values.length) {
- throw new IllegalArgumentException("Names and values must have the same length.");
- }
+ checkArgument(names.length == values.length, "Names and values must have the same length.");
if (names.length == 0) {
return EMPTY;
}
@@ -164,13 +159,12 @@ public final class Labels implements Comparable<Labels>, Iterable<Label> {
private static void validateNames(String[] names, String[] prometheusNames) {
for (int i = 0; i < names.length; i++) {
- if (!isValidLabelName(names[i])) {
- throw new IllegalArgumentException("'" + names[i] + "' is an illegal label name");
- }
+ checkArgument(isValidLabelName(names[i]), "'%s' is an illegal label name", names[i]);
// The arrays are sorted, so duplicates are next to each other
- if (i > 0 && prometheusNames[i - 1].equals(prometheusNames[i])) {
- throw new IllegalArgumentException(names[i] + ": duplicate label name");
- }
+ checkArgument(
+ i <= 0 || !prometheusNames[i - 1].equals(prometheusNames[i]),
+ "%s: duplicate label name",
+ names[i]);
}
}
@@ -353,7 +347,7 @@ public final class Labels implements Comparable<Labels>, Iterable<Label> {
for (int i = 0; i < names.length; i++) {
result.add(new Label(names[i], values[i]));
}
- return Collections.unmodifiableList(result);
+ return unmodifiableList(result);
}
/**
@@ -413,8 +407,7 @@ public final class Labels implements Comparable<Labels>, Iterable<Label> {
@Override
public int hashCode() {
int result = Arrays.hashCode(prometheusNames);
- result = 31 * result + Arrays.hashCode(values);
- return result;
+ return 31 * result + Arrays.hashCode(values);
}
public static Builder builder() {
--- a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/MetricMetadata.java
+++ b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/MetricMetadata.java
@@ -1,5 +1,7 @@
package io.prometheus.metrics.model.snapshots;
+import static com.google.common.base.Preconditions.checkArgument;
+
/** Immutable container for metric metadata: name, help, unit. */
public final class MetricMetadata {
@@ -89,33 +91,21 @@ public final class MetricMetadata {
}
private void validate() {
- if (name == null) {
- throw new IllegalArgumentException("Missing required field: name is null");
- }
+ checkArgument(name != null, "Missing required field: name is null");
String error = PrometheusNaming.validateMetricName(name);
- if (error != null) {
- throw new IllegalArgumentException(
- "'"
- + name
- + "': Illegal metric name. "
- + error
- + " Call "
- + PrometheusNaming.class.getSimpleName()
- + ".sanitizeMetricName(name) to avoid this error.");
- }
+ checkArgument(
+ error == null,
+ "'%s': Illegal metric name. %s Call %s.sanitizeMetricName(name) to avoid this error.",
+ name,
+ error,
+ PrometheusNaming.class.getSimpleName());
if (hasUnit()) {
- if (!name.endsWith("_" + unit) && !name.endsWith("." + unit)) {
- throw new IllegalArgumentException(
- "'"
- + name
- + "': Illegal metric name. If the unit is non-null, "
- + "the name must end with the unit: _"
- + unit
- + "."
- + " Call "
- + PrometheusNaming.class.getSimpleName()
- + ".sanitizeMetricName(name, unit) to avoid this error.");
- }
+ checkArgument(
+ name.endsWith("_" + unit) || name.endsWith("." + unit),
+ "'%s': Illegal metric name. If the unit is non-null, the name must end with the unit: _%s. Call %s.sanitizeMetricName(name, unit) to avoid this error.",
+ name,
+ unit,
+ PrometheusNaming.class.getSimpleName());
}
}
}
--- a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/MetricSnapshot.java
+++ b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/MetricSnapshot.java
@@ -1,9 +1,11 @@
package io.prometheus.metrics.model.snapshots;
+import static java.util.Collections.unmodifiableList;
+import static java.util.Comparator.comparing;
+import static java.util.Objects.requireNonNull;
+
import java.util.ArrayList;
import java.util.Collection;
-import java.util.Collections;
-import java.util.Comparator;
import java.util.List;
/** Base class for metric snapshots. */
@@ -14,16 +16,12 @@ public abstract class MetricSnapshot {
protected MetricSnapshot(
MetricMetadata metadata, Collection<? extends DataPointSnapshot> dataPoints) {
- if (metadata == null) {
- throw new NullPointerException("metadata");
- }
- if (dataPoints == null) {
- throw new NullPointerException("dataPoints");
- }
+ requireNonNull(metadata, "metadata");
+ requireNonNull(dataPoints, "dataPoints");
this.metadata = metadata;
List<? extends DataPointSnapshot> dataCopy = new ArrayList<>(dataPoints);
- dataCopy.sort(Comparator.comparing(DataPointSnapshot::getLabels));
- this.dataPoints = Collections.unmodifiableList(dataCopy);
+ dataCopy.sort(comparing(DataPointSnapshot::getLabels));
+ this.dataPoints = unmodifiableList(dataCopy);
validateLabels(this.dataPoints, metadata);
}
--- a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/MetricSnapshots.java
+++ b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/MetricSnapshots.java
@@ -1,5 +1,6 @@
package io.prometheus.metrics.model.snapshots;
+import static com.google.common.base.Preconditions.checkArgument;
import static io.prometheus.metrics.model.snapshots.PrometheusNaming.prometheusName;
import static java.util.Collections.unmodifiableList;
import static java.util.Comparator.comparing;
@@ -37,13 +38,13 @@ public class MetricSnapshots implements Iterable<MetricSnapshot> {
List<MetricSnapshot> list = new ArrayList<>(snapshots);
list.sort(comparing(s -> s.getMetadata().getPrometheusName()));
for (int i = 0; i < snapshots.size() - 1; i++) {
- if (list.get(i)
- .getMetadata()
- .getPrometheusName()
- .equals(list.get(i + 1).getMetadata().getPrometheusName())) {
- throw new IllegalArgumentException(
- list.get(i).getMetadata().getPrometheusName() + ": duplicate metric name");
- }
+ checkArgument(
+ !list.get(i)
+ .getMetadata()
+ .getPrometheusName()
+ .equals(list.get(i + 1).getMetadata().getPrometheusName()),
+ "%s: duplicate metric name",
+ list.get(i).getMetadata().getPrometheusName());
}
this.snapshots = unmodifiableList(list);
}
--- a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/NativeHistogramBuckets.java
+++ b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/NativeHistogramBuckets.java
@@ -1,8 +1,10 @@
package io.prometheus.metrics.model.snapshots;
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Collections.unmodifiableList;
+
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Stream;
@@ -74,7 +76,7 @@ public class NativeHistogramBuckets implements Iterable<NativeHistogramBucket> {
for (int i = 0; i < bucketIndexes.length; i++) {
result.add(new NativeHistogramBucket(bucketIndexes[i], counts[i]));
}
- return Collections.unmodifiableList(result);
+ return unmodifiableList(result);
}
@Override
@@ -95,14 +97,11 @@ public class NativeHistogramBuckets implements Iterable<NativeHistogramBucket> {
}
private static void sortAndValidate(int[] bucketIndexes, long[] counts) {
- if (bucketIndexes.length != counts.length) {
- throw new IllegalArgumentException(
- "bucketIndexes.length == "
- + bucketIndexes.length
- + " but counts.length == "
- + counts.length
- + ". Expected the same length.");
- }
+ checkArgument(
+ bucketIndexes.length == counts.length,
+ "bucketIndexes.length == %s but counts.length == %s. Expected the same length.",
+ bucketIndexes.length,
+ counts.length);
sort(bucketIndexes, counts);
validate(bucketIndexes, counts);
}
@@ -133,13 +132,12 @@ public class NativeHistogramBuckets implements Iterable<NativeHistogramBucket> {
// * bucketIndexes sorted
// * bucketIndexes and counts have the same length
for (int i = 0; i < bucketIndexes.length; i++) {
- if (counts[i] < 0) {
- throw new IllegalArgumentException("Bucket counts cannot be negative.");
- }
+ checkArgument(counts[i] >= 0, "Bucket counts cannot be negative.");
if (i > 0) {
- if (bucketIndexes[i - 1] == bucketIndexes[i]) {
- throw new IllegalArgumentException("Duplicate bucket index " + bucketIndexes[i]);
- }
+ checkArgument(
+ bucketIndexes[i - 1] != bucketIndexes[i],
+ "Duplicate bucket index %s",
+ bucketIndexes[i]);
}
}
}
--- a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/PrometheusNaming.java
+++ b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/PrometheusNaming.java
@@ -1,5 +1,7 @@
package io.prometheus.metrics.model.snapshots;
+import static com.google.common.base.Preconditions.checkArgument;
+
import java.util.regex.Pattern;
/**
@@ -132,9 +134,7 @@ public class PrometheusNaming {
* isValidMetricName(name)} is true.
*/
public static String sanitizeMetricName(String metricName) {
- if (metricName.isEmpty()) {
- throw new IllegalArgumentException("Cannot convert an empty string to a valid metric name.");
- }
+ checkArgument(!metricName.isEmpty(), "Cannot convert an empty string to a valid metric name.");
String sanitizedName = replaceIllegalCharsInMetricName(metricName);
boolean modified = true;
while (modified) {
@@ -174,9 +174,7 @@ public class PrometheusNaming {
* isValidLabelName(name)} is true.
*/
public static String sanitizeLabelName(String labelName) {
- if (labelName.isEmpty()) {
- throw new IllegalArgumentException("Cannot convert an empty string to a valid label name.");
- }
+ checkArgument(!labelName.isEmpty(), "Cannot convert an empty string to a valid label name.");
String sanitizedName = replaceIllegalCharsInLabelName(labelName);
while (sanitizedName.startsWith("__")
|| sanitizedName.startsWith("_.")
@@ -196,9 +194,7 @@ public class PrometheusNaming {
* @throws NullPointerException if {@code unitName} is null.
*/
public static String sanitizeUnitName(String unitName) {
- if (unitName.isEmpty()) {
- throw new IllegalArgumentException("Cannot convert an empty string to a valid unit name.");
- }
+ checkArgument(!unitName.isEmpty(), "Cannot convert an empty string to a valid unit name.");
String sanitizedName = replaceIllegalCharsInUnitName(unitName);
boolean modified = true;
while (modified) {
@@ -219,10 +215,8 @@ public class PrometheusNaming {
}
}
}
- if (sanitizedName.isEmpty()) {
- throw new IllegalArgumentException(
- "Cannot convert '" + unitName + "' into a valid unit name.");
- }
+ checkArgument(
+ !sanitizedName.isEmpty(), "Cannot convert '%s' into a valid unit name.", unitName);
return sanitizedName;
}
--- a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/Quantile.java
+++ b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/Quantile.java
@@ -1,5 +1,7 @@
package io.prometheus.metrics.model.snapshots;
+import static com.google.common.base.Preconditions.checkArgument;
+
/** Immutable representation of a Quantile. */
public class Quantile {
@@ -26,9 +28,9 @@ public class Quantile {
}
private void validate() {
- if (quantile < 0.0 || quantile > 1.0) {
- throw new IllegalArgumentException(
- quantile + ": Illegal quantile. Expecting 0 <= quantile <= 1");
- }
+ checkArgument(
+ quantile >= 0.0 && quantile <= 1.0,
+ "%s: Illegal quantile. Expecting 0 <= quantile <= 1",
+ quantile);
}
}
--- a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/Quantiles.java
+++ b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/Quantiles.java
@@ -1,9 +1,12 @@
package io.prometheus.metrics.model.snapshots;
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Collections.unmodifiableList;
+import static java.util.Comparator.comparingDouble;
+
+import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Collections;
-import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
@@ -11,21 +14,21 @@ import java.util.List;
public class Quantiles implements Iterable<Quantile> {
private final List<Quantile> quantiles;
- public static final Quantiles EMPTY = new Quantiles(Collections.emptyList());
+ public static final Quantiles EMPTY = new Quantiles(ImmutableList.of());
private Quantiles(List<Quantile> quantiles) {
quantiles = new ArrayList<>(quantiles);
- quantiles.sort(Comparator.comparing(Quantile::getQuantile));
- this.quantiles = Collections.unmodifiableList(quantiles);
+ quantiles.sort(comparingDouble(Quantile::getQuantile));
+ this.quantiles = unmodifiableList(quantiles);
validate();
}
private void validate() {
for (int i = 0; i < quantiles.size() - 1; i++) {
- if (quantiles.get(i).getQuantile() == quantiles.get(i + 1).getQuantile()) {
- throw new IllegalArgumentException(
- "Duplicate " + quantiles.get(i).getQuantile() + " quantile.");
- }
+ checkArgument(
+ quantiles.get(i).getQuantile() != quantiles.get(i + 1).getQuantile(),
+ "Duplicate %s quantile.",
+ quantiles.get(i).getQuantile());
}
}
--- a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/StateSetSnapshot.java
+++ b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/StateSetSnapshot.java
@@ -1,9 +1,11 @@
package io.prometheus.metrics.model.snapshots;
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Collections.unmodifiableList;
+
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
-import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Stream;
@@ -24,19 +26,17 @@ public final class StateSetSnapshot extends MetricSnapshot {
}
private void validate() {
- if (getMetadata().hasUnit()) {
- throw new IllegalArgumentException("An state set metric cannot have a unit.");
- }
+ checkArgument(!getMetadata().hasUnit(), "An state set metric cannot have a unit.");
for (StateSetDataPointSnapshot entry : getDataPoints()) {
- if (entry.getLabels().contains(getMetadata().getPrometheusName())) {
- throw new IllegalArgumentException(
- "Label name " + getMetadata().getPrometheusName() + " is reserved.");
- }
+ checkArgument(
+ !entry.getLabels().contains(getMetadata().getPrometheusName()),
+ "Label name %s is reserved.",
+ getMetadata().getPrometheusName());
}
}
- @SuppressWarnings("unchecked")
@Override
+ @SuppressWarnings("unchecked")
public List<StateSetDataPointSnapshot> getDataPoints() {
return (List<StateSetDataPointSnapshot>) dataPoints;
}
@@ -68,12 +68,9 @@ public final class StateSetSnapshot extends MetricSnapshot {
public StateSetDataPointSnapshot(
String[] names, boolean[] values, Labels labels, long scrapeTimestampMillis) {
super(labels, 0L, scrapeTimestampMillis);
- if (names.length == 0) {
- throw new IllegalArgumentException("StateSet must have at least one state.");
- }
- if (names.length != values.length) {
- throw new IllegalArgumentException("names[] and values[] must have the same length");
- }
+ checkArgument(names.length != 0, "StateSet must have at least one state.");
+ checkArgument(
+ names.length == values.length, "names[] and values[] must have the same length");
String[] namesCopy = Arrays.copyOf(names, names.length);
boolean[] valuesCopy = Arrays.copyOf(values, names.length);
sort(namesCopy, valuesCopy);
@@ -96,12 +93,9 @@ public final class StateSetSnapshot extends MetricSnapshot {
private void validate() {
for (int i = 0; i < names.length; i++) {
- if (names[i].isEmpty()) {
- throw new IllegalArgumentException("Empty string as state name");
- }
- if (i > 0 && names[i - 1].equals(names[i])) {
- throw new IllegalArgumentException(names[i] + " duplicate state name");
- }
+ checkArgument(!names[i].isEmpty(), "Empty string as state name");
+ checkArgument(
+ i <= 0 || !names[i - 1].equals(names[i]), "%s duplicate state name", names[i]);
}
}
@@ -110,7 +104,7 @@ public final class StateSetSnapshot extends MetricSnapshot {
for (int i = 0; i < names.length; i++) {
result.add(new State(names[i], values[i]));
}
- return Collections.unmodifiableList(result);
+ return unmodifiableList(result);
}
@Override
--- a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/SummarySnapshot.java
+++ b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/SummarySnapshot.java
@@ -1,5 +1,8 @@
package io.prometheus.metrics.model.snapshots;
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Objects.requireNonNull;
+
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@@ -18,8 +21,8 @@ public final class SummarySnapshot extends MetricSnapshot {
super(metadata, data);
}
- @SuppressWarnings("unchecked")
@Override
+ @SuppressWarnings("unchecked")
public List<SummaryDataPointSnapshot> getDataPoints() {
return (List<SummaryDataPointSnapshot>) dataPoints;
}
@@ -78,13 +81,10 @@ public final class SummarySnapshot extends MetricSnapshot {
private void validate() {
for (Label label : getLabels()) {
- if (label.getName().equals("quantile")) {
- throw new IllegalArgumentException("quantile is a reserved label name for summaries");
- }
- }
- if (quantiles == null) {
- throw new NullPointerException();
+ checkArgument(
+ !label.getName().equals("quantile"), "quantile is a reserved label name for summaries");
}
+ requireNonNull(quantiles);
}
public static Builder builder() {
--- a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/Unit.java
+++ b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/Unit.java
@@ -1,5 +1,8 @@
package io.prometheus.metrics.model.snapshots;
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Objects.requireNonNull;
+
import java.util.Objects;
/**
@@ -27,14 +30,10 @@ public final class Unit {
public static final Unit AMPERES = new Unit("amperes");
public Unit(String name) {
- if (name == null) {
- throw new NullPointerException("Unit name cannot be null.");
- }
+ requireNonNull(name, "Unit name cannot be null.");
name = name.trim();
String error = PrometheusNaming.validateUnitName(name);
- if (error != null) {
- throw new IllegalArgumentException(name + ": Illegal unit name: " + error);
- }
+ checkArgument(error == null, "%s: Illegal unit name: %s", name, error);
this.name = name;
}
--- a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/UnknownSnapshot.java
+++ b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/UnknownSnapshot.java
@@ -1,5 +1,7 @@
package io.prometheus.metrics.model.snapshots;
+import static com.google.common.base.Preconditions.checkArgument;
+
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@@ -19,8 +21,8 @@ public final class UnknownSnapshot extends MetricSnapshot {
super(metadata, data);
}
- @SuppressWarnings("unchecked")
@Override
+ @SuppressWarnings("unchecked")
public List<UnknownDataPointSnapshot> getDataPoints() {
return (List<UnknownDataPointSnapshot>) dataPoints;
}
@@ -87,9 +89,7 @@ public final class UnknownSnapshot extends MetricSnapshot {
}
public UnknownDataPointSnapshot build() {
- if (value == null) {
- throw new IllegalArgumentException("Missing required field: value is null.");
- }
+ checkArgument(value != null, "Missing required field: value is null.");
return new UnknownDataPointSnapshot(value, labels, exemplar, scrapeTimestampMillis);
}
--- a/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/registry/CollectorTest.java
+++ b/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/registry/CollectorTest.java
@@ -5,7 +5,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import io.prometheus.metrics.model.snapshots.CounterSnapshot;
import org.junit.jupiter.api.Test;
-class CollectorTest {
+final class CollectorTest {
Collector collector = () -> CounterSnapshot.builder().name("counter_a").build();
--- a/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/registry/MetricNameFilterTest.java
+++ b/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/registry/MetricNameFilterTest.java
@@ -8,12 +8,12 @@ import io.prometheus.metrics.model.snapshots.Labels;
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
import org.junit.jupiter.api.Test;
-class MetricNameFilterTest {
+final class MetricNameFilterTest {
private final PrometheusRegistry registry = new PrometheusRegistry();
@Test
- public void testCounter() {
+ void counter() {
registry.register(
() ->
CounterSnapshot.builder()
@@ -43,12 +43,12 @@ class MetricNameFilterTest {
filter = MetricNameFilter.builder().nameMustStartWith("counter1").build();
MetricSnapshots snapshots = registry.scrape(filter);
- assertThat(snapshots.size()).isOne();
+ assertThat(snapshots.size()).isEqualTo(1);
assertThat(snapshots.get(0).getMetadata().getName()).isEqualTo("counter1");
filter = MetricNameFilter.builder().nameMustNotStartWith("counter1").build();
snapshots = registry.scrape(filter);
- assertThat(snapshots.size()).isOne();
+ assertThat(snapshots.size()).isEqualTo(1);
assertThat(snapshots.get(0).getMetadata().getName()).isEqualTo("counter2");
filter =
@@ -61,6 +61,6 @@ class MetricNameFilterTest {
.nameMustBeEqualTo("counter1_total")
.nameMustNotBeEqualTo("counter1_total")
.build();
- assertThat(registry.scrape(filter).size()).isZero();
+ assertThat(registry.scrape(filter).size()).isEqualTo(0);
}
}
--- a/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/registry/MultiCollectorNameFilterTest.java
+++ b/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/registry/MultiCollectorNameFilterTest.java
@@ -2,18 +2,18 @@ package io.prometheus.metrics.model.registry;
import static org.assertj.core.api.Assertions.assertThat;
+import com.google.common.collect.ImmutableList;
import io.prometheus.metrics.model.snapshots.CounterSnapshot;
import io.prometheus.metrics.model.snapshots.CounterSnapshot.CounterDataPointSnapshot;
import io.prometheus.metrics.model.snapshots.GaugeSnapshot;
import io.prometheus.metrics.model.snapshots.GaugeSnapshot.GaugeDataPointSnapshot;
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
import java.util.Arrays;
-import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.jupiter.api.Test;
-class MultiCollectorNameFilterTest {
+final class MultiCollectorNameFilterTest {
private static class Registry extends PrometheusRegistry {
private final AtomicBoolean collectCalled = new AtomicBoolean();
@@ -51,39 +51,39 @@ class MultiCollectorNameFilterTest {
}
@Test
- public void testPartialFilter() {
- Registry registry = new Registry(Collections.emptyList());
+ void partialFilter() {
+ Registry registry = new Registry(ImmutableList.of());
MetricSnapshots snapshots = registry.scrape(name -> name.equals("counter_1"));
assertThat(registry.collectCalled()).isTrue();
- assertThat(snapshots.size()).isOne();
+ assertThat(snapshots.size()).isEqualTo(1);
assertThat(snapshots.get(0).getMetadata().getName()).isEqualTo("counter_1");
}
@Test
- public void testPartialFilterWithPrometheusNames() {
+ void partialFilterWithPrometheusNames() {
Registry registry = new Registry(Arrays.asList("counter_1", "gauge_2"));
MetricSnapshots snapshots = registry.scrape(name -> name.equals("counter_1"));
assertThat(registry.collectCalled()).isTrue();
- assertThat(snapshots.size()).isOne();
+ assertThat(snapshots.size()).isEqualTo(1);
assertThat(snapshots.get(0).getMetadata().getName()).isEqualTo("counter_1");
}
@Test
- public void testCompleteFilter_CollectCalled() {
- Registry registry = new Registry(Collections.emptyList());
+ void completeFilter_CollectCalled() {
+ Registry registry = new Registry(ImmutableList.of());
MetricSnapshots snapshots =
registry.scrape(name -> !name.equals("counter_1") && !name.equals("gauge_2"));
assertThat(registry.collectCalled()).isTrue();
- assertThat(snapshots.size()).isZero();
+ assertThat(snapshots.size()).isEqualTo(0);
}
@Test
- public void testCompleteFilter_CollectNotCalled() {
+ void completeFilter_CollectNotCalled() {
Registry registry = new Registry(Arrays.asList("counter_1", "gauge_2"));
MetricSnapshots snapshots =
registry.scrape(name -> !name.equals("counter_1") && !name.equals("gauge_2"));
assertThat(registry.collectCalled()).isFalse();
- assertThat(snapshots.size()).isZero();
+ assertThat(snapshots.size()).isEqualTo(0);
}
}
--- a/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/registry/PrometheusRegistryTest.java
+++ b/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/registry/PrometheusRegistryTest.java
@@ -1,7 +1,7 @@
package io.prometheus.metrics.model.registry;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.fail;
import io.prometheus.metrics.model.snapshots.CounterSnapshot;
@@ -12,7 +12,7 @@ import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
-class PrometheusRegistryTest {
+final class PrometheusRegistryTest {
Collector noName = () -> GaugeSnapshot.builder().name("no_name_gauge").build();
@@ -82,7 +82,7 @@ class PrometheusRegistryTest {
};
@Test
- public void registerNoName() {
+ void registerNoName() {
PrometheusRegistry registry = new PrometheusRegistry();
// If the collector does not have a name at registration time, there is no conflict during
// registration.
@@ -101,15 +101,15 @@ class PrometheusRegistryTest {
}
@Test
- public void registerDuplicateName() {
+ void registerDuplicateName() {
PrometheusRegistry registry = new PrometheusRegistry();
registry.register(counterA1);
- assertThatExceptionOfType(IllegalStateException.class)
- .isThrownBy(() -> registry.register(counterA2));
+ assertThatThrownBy(() -> registry.register(counterA2))
+ .isInstanceOf(IllegalStateException.class);
}
@Test
- public void registerOk() {
+ void registerOk() {
PrometheusRegistry registry = new PrometheusRegistry();
registry.register(counterA1);
registry.register(counterB);
@@ -127,15 +127,15 @@ class PrometheusRegistryTest {
}
@Test
- public void registerDuplicateMultiCollector() {
+ void registerDuplicateMultiCollector() {
PrometheusRegistry registry = new PrometheusRegistry();
registry.register(multiCollector);
- assertThatExceptionOfType(IllegalStateException.class)
- .isThrownBy(() -> registry.register(multiCollector));
+ assertThatThrownBy(() -> registry.register(multiCollector))
+ .isInstanceOf(IllegalStateException.class);
}
@Test
- public void registerOkMultiCollector() {
+ void registerOkMultiCollector() {
PrometheusRegistry registry = new PrometheusRegistry();
registry.register(multiCollector);
MetricSnapshots snapshots = registry.scrape();
@@ -143,11 +143,11 @@ class PrometheusRegistryTest {
registry.unregister(multiCollector);
snapshots = registry.scrape();
- assertThat(snapshots.size()).isZero();
+ assertThat(snapshots.size()).isEqualTo(0);
}
@Test
- public void clearOk() {
+ void clearOk() {
PrometheusRegistry registry = new PrometheusRegistry();
registry.register(counterA1);
registry.register(counterB);
@@ -155,6 +155,6 @@ class PrometheusRegistryTest {
assertThat(registry.scrape().size()).isEqualTo(3);
registry.clear();
- assertThat(registry.scrape().size()).isZero();
+ assertThat(registry.scrape().size()).isEqualTo(0);
}
}
--- a/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/ClassicHistogramBucketsTest.java
+++ b/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/ClassicHistogramBucketsTest.java
@@ -1,17 +1,17 @@
package io.prometheus.metrics.model.snapshots;
+import static com.google.common.collect.ImmutableList.toImmutableList;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.util.Iterator;
import java.util.List;
-import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
-class ClassicHistogramBucketsTest {
+final class ClassicHistogramBucketsTest {
@Test
- public void testGoodCase() {
+ void goodCase() {
ClassicHistogramBuckets buckets =
ClassicHistogramBuckets.builder()
.bucket(Double.NEGATIVE_INFINITY, 0)
@@ -23,7 +23,7 @@ class ClassicHistogramBucketsTest {
}
@Test
- public void testSort() {
+ void sort() {
ClassicHistogramBuckets buckets =
ClassicHistogramBuckets.builder()
.bucket(7, 2)
@@ -34,77 +34,77 @@ class ClassicHistogramBucketsTest {
assertThat(buckets.getUpperBound(0)).isEqualTo(2);
assertThat(buckets.getUpperBound(1)).isEqualTo(7);
assertThat(buckets.getUpperBound(2)).isEqualTo(Double.POSITIVE_INFINITY);
- assertThat(buckets.getCount(0)).isZero();
+ assertThat(buckets.getCount(0)).isEqualTo(0);
assertThat(buckets.getCount(1)).isEqualTo(2);
assertThat(buckets.getCount(2)).isEqualTo(3);
}
@Test
- public void testMinimalBuckets() {
+ void minimalBuckets() {
ClassicHistogramBuckets buckets =
ClassicHistogramBuckets.builder().bucket(Double.POSITIVE_INFINITY, 0).build();
- assertThat(buckets.size()).isOne();
+ assertThat(buckets.size()).isEqualTo(1);
}
@Test
- public void testInfBucketMissing() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(
- () -> ClassicHistogramBuckets.builder().bucket(Double.NEGATIVE_INFINITY, 0).build());
+ void infBucketMissing() {
+ assertThatThrownBy(
+ () -> ClassicHistogramBuckets.builder().bucket(Double.NEGATIVE_INFINITY, 0).build())
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testNegativeCount() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(
+ void negativeCount() {
+ assertThatThrownBy(
() ->
ClassicHistogramBuckets.builder()
.bucket(0.0, 10)
.bucket(Double.POSITIVE_INFINITY, -1)
- .build());
+ .build())
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testNaNBoundary() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(
+ void naNBoundary() {
+ assertThatThrownBy(
() ->
ClassicHistogramBuckets.builder()
.bucket(0.0, 1)
.bucket(Double.NaN, 2)
.bucket(Double.POSITIVE_INFINITY, 0)
- .build());
+ .build())
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testDuplicateBoundary() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(
+ void duplicateBoundary() {
+ assertThatThrownBy(
() ->
ClassicHistogramBuckets.builder()
.bucket(1.0, 1)
.bucket(2.0, 2)
.bucket(1.0, 2)
.bucket(Double.POSITIVE_INFINITY, 0)
- .build());
+ .build())
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testEmptyBuckets() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> ClassicHistogramBuckets.builder().build());
+ void emptyBuckets() {
+ assertThatThrownBy(() -> ClassicHistogramBuckets.builder().build())
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testDifferentLength() {
+ void differentLength() {
double[] upperBounds = new double[] {0.7, 1.3, Double.POSITIVE_INFINITY};
long[] counts = new long[] {13, 178, 1024, 3000};
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> ClassicHistogramBuckets.of(upperBounds, counts));
+ assertThatThrownBy(() -> ClassicHistogramBuckets.of(upperBounds, counts))
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testImmutable() {
+ void immutable() {
ClassicHistogramBuckets buckets =
ClassicHistogramBuckets.builder()
.bucket(1.0, 7)
@@ -113,18 +113,18 @@ class ClassicHistogramBucketsTest {
.build();
Iterator<ClassicHistogramBucket> iterator = buckets.iterator();
iterator.next();
- assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(iterator::remove);
+ assertThatThrownBy(iterator::remove).isInstanceOf(UnsupportedOperationException.class);
}
@Test
- public void compare() {
+ void compare() {
ClassicHistogramBuckets buckets =
ClassicHistogramBuckets.builder()
.bucket(1.0, 7)
.bucket(2.0, 8)
.bucket(Double.POSITIVE_INFINITY, 0)
.build();
- List<ClassicHistogramBucket> list = buckets.stream().collect(Collectors.toList());
+ List<ClassicHistogramBucket> list = buckets.stream().collect(toImmutableList());
assertThat(list.get(0)).isNotEqualByComparingTo(list.get(1));
}
}
--- a/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/CounterSnapshotTest.java
+++ b/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/CounterSnapshotTest.java
@@ -1,17 +1,17 @@
package io.prometheus.metrics.model.snapshots;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import io.prometheus.metrics.model.snapshots.CounterSnapshot.CounterDataPointSnapshot;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;
-class CounterSnapshotTest {
+final class CounterSnapshotTest {
@Test
- public void testCompleteGoodCase() {
+ void completeGoodCase() {
long createdTimestamp1 = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(1);
long createdTimestamp2 = System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(2);
long exemplarTimestamp = System.currentTimeMillis();
@@ -68,7 +68,7 @@ class CounterSnapshotTest {
}
@Test
- public void testMinimalGoodCase() {
+ void minimalGoodCase() {
CounterSnapshot snapshot =
CounterSnapshot.builder()
.name("events")
@@ -85,25 +85,25 @@ class CounterSnapshotTest {
}
@Test
- public void testEmptyCounter() {
+ void emptyCounter() {
CounterSnapshot snapshot = CounterSnapshot.builder().name("events").build();
assertThat(snapshot.getDataPoints()).isEmpty();
}
@Test
- public void testTotalSuffixPresent() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> CounterSnapshot.builder().name("test_total").build());
+ void totalSuffixPresent() {
+ assertThatThrownBy(() -> CounterSnapshot.builder().name("test_total").build())
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testValueMissing() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> CounterDataPointSnapshot.builder().build());
+ void valueMissing() {
+ assertThatThrownBy(() -> CounterDataPointSnapshot.builder().build())
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testDataImmutable() {
+ void dataImmutable() {
CounterSnapshot snapshot =
CounterSnapshot.builder()
.name("events")
@@ -114,6 +114,6 @@ class CounterSnapshotTest {
.build();
Iterator<CounterDataPointSnapshot> iterator = snapshot.getDataPoints().iterator();
iterator.next();
- assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(iterator::remove);
+ assertThatThrownBy(iterator::remove).isInstanceOf(UnsupportedOperationException.class);
}
}
--- a/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/ExemplarTest.java
+++ b/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/ExemplarTest.java
@@ -1,15 +1,15 @@
package io.prometheus.metrics.model.snapshots;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import org.assertj.core.api.IterableAssert;
import org.junit.jupiter.api.Test;
-class ExemplarTest {
+final class ExemplarTest {
@Test
- public void testGoodCaseComplete() {
+ void goodCaseComplete() {
long timestamp = System.currentTimeMillis();
Exemplar exemplar =
Exemplar.builder()
@@ -36,13 +36,12 @@ class ExemplarTest {
}
@Test
- public void testValueMissing() {
- assertThatExceptionOfType(IllegalStateException.class)
- .isThrownBy(() -> Exemplar.builder().build());
+ void valueMissing() {
+ assertThatThrownBy(() -> Exemplar.builder().build()).isInstanceOf(IllegalStateException.class);
}
@Test
- public void testMinimal() {
+ void minimal() {
Exemplar exemplar = Exemplar.builder().value(0.0).build();
assertThat(exemplar.getValue()).isEqualTo(0.0);
assertLabels(exemplar.getLabels()).isEqualTo(Labels.EMPTY);
@@ -50,7 +49,7 @@ class ExemplarTest {
}
@Test
- public void testLabelsMergeTraceId() {
+ void labelsMergeTraceId() {
Exemplar exemplar =
Exemplar.builder().value(0.0).labels(Labels.of("a", "b")).traceId("abc").build();
assertLabels(exemplar.getLabels()).isEqualTo(Labels.of("a", "b", "trace_id", "abc"));
@@ -61,14 +60,14 @@ class ExemplarTest {
}
@Test
- public void testLabelsMergeSpanId() {
+ void labelsMergeSpanId() {
Exemplar exemplar =
Exemplar.builder().value(0.0).labels(Labels.of("a", "b")).spanId("abc").build();
assertLabels(exemplar.getLabels()).isEqualTo(Labels.of("a", "b", "span_id", "abc"));
}
@Test
- public void testLabelsMergeTraceIdAndSpanId() {
+ void labelsMergeTraceIdAndSpanId() {
Exemplar exemplar =
Exemplar.builder()
.value(0.0)
@@ -81,7 +80,7 @@ class ExemplarTest {
}
@Test
- public void testLabelsMergeNone() {
+ void labelsMergeNone() {
Exemplar exemplar = Exemplar.builder().value(0.0).labels(Labels.of("a", "b")).build();
assertLabels(exemplar.getLabels()).isEqualTo(Labels.of("a", "b"));
}
--- a/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/ExemplarsTest.java
+++ b/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/ExemplarsTest.java
@@ -1,15 +1,15 @@
package io.prometheus.metrics.model.snapshots;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.util.Iterator;
import org.junit.jupiter.api.Test;
-class ExemplarsTest {
+final class ExemplarsTest {
@Test
- public void testUpperBound() {
+ void upperBound() {
Exemplars exemplars =
Exemplars.of(
Exemplar.builder().value(1.0).build(),
@@ -28,7 +28,7 @@ class ExemplarsTest {
}
@Test
- public void testImmutable() {
+ void immutable() {
Exemplars exemplars =
Exemplars.of(
Exemplar.builder().value(1.0).build(),
@@ -36,11 +36,11 @@ class ExemplarsTest {
Exemplar.builder().value(2.0).build());
Iterator<Exemplar> iterator = exemplars.iterator();
iterator.next();
- assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(iterator::remove);
+ assertThatThrownBy(iterator::remove).isInstanceOf(UnsupportedOperationException.class);
}
@Test
- public void testGet() {
+ void get() {
Exemplar oldest =
Exemplar.builder().timestampMillis(System.currentTimeMillis() - 100).value(1.8).build();
Exemplar middle =
--- a/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/GaugeSnapshotTest.java
+++ b/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/GaugeSnapshotTest.java
@@ -1,17 +1,17 @@
package io.prometheus.metrics.model.snapshots;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import io.prometheus.metrics.model.snapshots.CounterSnapshot.CounterDataPointSnapshot;
import io.prometheus.metrics.model.snapshots.GaugeSnapshot.GaugeDataPointSnapshot;
import java.util.Iterator;
import org.junit.jupiter.api.Test;
-class GaugeSnapshotTest {
+final class GaugeSnapshotTest {
@Test
- public void testCompleteGoodCase() {
+ void completeGoodCase() {
long exemplarTimestamp = System.currentTimeMillis();
GaugeSnapshot snapshot =
GaugeSnapshot.builder()
@@ -63,14 +63,14 @@ class GaugeSnapshotTest {
}
@Test
- public void testMinimalGoodCase() {
+ void minimalGoodCase() {
GaugeSnapshot snapshot =
GaugeSnapshot.builder()
.name("temperature")
.dataPoint(GaugeDataPointSnapshot.builder().value(23.0).build())
.build();
SnapshotTestUtil.assertMetadata(snapshot, "temperature", null, null);
- assertThat(snapshot.getDataPoints().size()).isOne();
+ assertThat(snapshot.getDataPoints()).hasSize(1);
GaugeDataPointSnapshot data = snapshot.getDataPoints().get(0);
assertThat((Iterable<? extends Label>) data.getLabels()).isEmpty();
assertThat(data.getValue()).isEqualTo(23.0);
@@ -80,31 +80,31 @@ class GaugeSnapshotTest {
}
@Test
- public void testEmptyGauge() {
+ void emptyGauge() {
GaugeSnapshot snapshot = GaugeSnapshot.builder().name("temperature").build();
- assertThat(snapshot.getDataPoints().size()).isZero();
+ assertThat(snapshot.getDataPoints()).isEmpty();
}
@Test
- public void testTotalSuffixPresent() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> CounterSnapshot.builder().name("test_total").build());
+ void totalSuffixPresent() {
+ assertThatThrownBy(() -> CounterSnapshot.builder().name("test_total").build())
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testTotalSuffixPresentDot() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> CounterSnapshot.builder().name("test.total").build());
+ void totalSuffixPresentDot() {
+ assertThatThrownBy(() -> CounterSnapshot.builder().name("test.total").build())
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testValueMissing() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> CounterDataPointSnapshot.builder().build());
+ void valueMissing() {
+ assertThatThrownBy(() -> CounterDataPointSnapshot.builder().build())
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testDataImmutable() {
+ void dataImmutable() {
GaugeSnapshot snapshot =
GaugeSnapshot.builder()
.name("gauge")
@@ -115,6 +115,6 @@ class GaugeSnapshotTest {
.build();
Iterator<GaugeDataPointSnapshot> iterator = snapshot.getDataPoints().iterator();
iterator.next();
- assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(iterator::remove);
+ assertThatThrownBy(iterator::remove).isInstanceOf(UnsupportedOperationException.class);
}
}
--- a/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/HistogramSnapshotTest.java
+++ b/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/HistogramSnapshotTest.java
@@ -1,19 +1,19 @@
package io.prometheus.metrics.model.snapshots;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.data.Offset.offset;
+import com.google.common.collect.ImmutableList;
import io.prometheus.metrics.model.snapshots.HistogramSnapshot.HistogramDataPointSnapshot;
-import java.util.Collections;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;
-class HistogramSnapshotTest {
+final class HistogramSnapshotTest {
@Test
- public void testGoodCaseComplete() {
+ void goodCaseComplete() {
long createdTimestamp = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(1);
long scrapeTimestamp = System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(2);
long exemplarTimestamp = System.currentTimeMillis();
@@ -115,7 +115,7 @@ class HistogramSnapshotTest {
break;
case 2:
assertThat(bucket.getUpperBound()).isEqualTo(Double.POSITIVE_INFINITY);
- assertThat(bucket.getCount()).isZero();
+ assertThat(bucket.getCount()).isEqualTo(0);
break;
}
}
@@ -129,7 +129,7 @@ class HistogramSnapshotTest {
for (NativeHistogramBucket bucket : data.getNativeBucketsForPositiveValues()) {
switch (i++) {
case 0:
- assertThat(bucket.getBucketIndex()).isOne();
+ assertThat(bucket.getBucketIndex()).isEqualTo(1);
assertThat(bucket.getCount()).isEqualTo(12);
break;
case 1:
@@ -149,10 +149,10 @@ class HistogramSnapshotTest {
switch (i++) {
case 0:
assertThat(bucket.getBucketIndex()).isEqualTo(-1);
- assertThat(bucket.getCount()).isOne();
+ assertThat(bucket.getCount()).isEqualTo(1);
break;
case 1:
- assertThat(bucket.getBucketIndex()).isZero();
+ assertThat(bucket.getBucketIndex()).isEqualTo(0);
assertThat(bucket.getCount()).isEqualTo(2);
break;
}
@@ -165,23 +165,23 @@ class HistogramSnapshotTest {
// native histogram 2 (it's ok that this is incomplete, because we covered it with the other
// tests)
assertThat(data.getNativeSchema()).isEqualTo(5);
- assertThat(data.getNativeZeroCount()).isZero();
- assertThat(data.getNativeZeroThreshold()).isZero();
+ assertThat(data.getNativeZeroCount()).isEqualTo(0);
+ assertThat(data.getNativeZeroThreshold()).isEqualTo(0);
}
@Test
- public void testEmptyHistogram() {
+ void emptyHistogram() {
assertThat(HistogramSnapshot.builder().name("empty_histogram").build().getDataPoints())
.isEmpty();
HistogramSnapshot snapshot =
new HistogramSnapshot(
- new MetricMetadata("empty_bytes", "help", Unit.BYTES), Collections.emptyList());
+ new MetricMetadata("empty_bytes", "help", Unit.BYTES), ImmutableList.of());
assertThat(snapshot.getDataPoints()).isEmpty();
assertThat(snapshot.isGaugeHistogram()).isFalse();
}
@Test
- public void testMinimalClassicHistogram() {
+ void minimalClassicHistogram() {
HistogramSnapshot snapshot =
HistogramSnapshot.builder()
.name("minimal_histogram")
@@ -194,11 +194,11 @@ class HistogramSnapshotTest {
.build();
HistogramDataPointSnapshot data = snapshot.getDataPoints().get(0);
assertThat(data.hasSum()).isFalse();
- assertThat(snapshot.getDataPoints().get(0).getClassicBuckets().size()).isOne();
+ assertThat(snapshot.getDataPoints().get(0).getClassicBuckets().size()).isEqualTo(1);
}
@Test
- public void testMinimalNativeHistogram() {
+ void minimalNativeHistogram() {
HistogramSnapshot snapshot =
HistogramSnapshot.builder()
.name("hist")
@@ -206,19 +206,19 @@ class HistogramSnapshotTest {
.build();
assertThat(snapshot.getMetadata().getName()).isEqualTo("hist");
assertThat(snapshot.getMetadata().hasUnit()).isFalse();
- assertThat(snapshot.getDataPoints().size()).isOne();
+ assertThat(snapshot.getDataPoints()).hasSize(1);
HistogramDataPointSnapshot data = snapshot.getDataPoints().get(0);
assertThat(data.hasCreatedTimestamp()).isFalse();
assertThat(data.hasScrapeTimestamp()).isFalse();
assertThat(data.hasCount()).isTrue();
- assertThat(data.getCount()).isZero();
+ assertThat(data.getCount()).isEqualTo(0);
assertThat(data.hasSum()).isFalse();
- assertThat(data.getNativeBucketsForNegativeValues().size()).isZero();
- assertThat(data.getNativeBucketsForPositiveValues().size()).isZero();
+ assertThat(data.getNativeBucketsForNegativeValues().size()).isEqualTo(0);
+ assertThat(data.getNativeBucketsForPositiveValues().size()).isEqualTo(0);
}
@Test
- public void testClassicCount() {
+ void classicCount() {
HistogramSnapshot snapshot =
HistogramSnapshot.builder()
.name("test_histogram")
@@ -239,21 +239,21 @@ class HistogramSnapshotTest {
}
@Test
- public void testEmptyData() {
+ void emptyData() {
// This will fail because one of nativeSchema and classicHistogramBuckets is required
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> HistogramDataPointSnapshot.builder().build());
+ assertThatThrownBy(() -> HistogramDataPointSnapshot.builder().build())
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testEmptyNativeData() {
+ void emptyNativeData() {
HistogramDataPointSnapshot data = HistogramDataPointSnapshot.builder().nativeSchema(5).build();
- assertThat(data.getNativeBucketsForNegativeValues().size()).isZero();
- assertThat(data.getNativeBucketsForPositiveValues().size()).isZero();
+ assertThat(data.getNativeBucketsForNegativeValues().size()).isEqualTo(0);
+ assertThat(data.getNativeBucketsForPositiveValues().size()).isEqualTo(0);
}
@Test
- public void testDataImmutable() {
+ void dataImmutable() {
HistogramSnapshot snapshot =
HistogramSnapshot.builder()
.name("test_histogram")
@@ -274,20 +274,20 @@ class HistogramSnapshotTest {
.build();
Iterator<HistogramDataPointSnapshot> iterator = snapshot.getDataPoints().iterator();
iterator.next();
- assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(iterator::remove);
+ assertThatThrownBy(iterator::remove).isInstanceOf(UnsupportedOperationException.class);
}
@Test
- public void testEmptyClassicBuckets() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(
+ void emptyClassicBuckets() {
+ assertThatThrownBy(
() ->
new HistogramDataPointSnapshot(
- ClassicHistogramBuckets.EMPTY, Double.NaN, Labels.EMPTY, Exemplars.EMPTY, 0L));
+ ClassicHistogramBuckets.EMPTY, Double.NaN, Labels.EMPTY, Exemplars.EMPTY, 0L))
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testMinimalNativeData() {
+ void minimalNativeData() {
new HistogramDataPointSnapshot(
ClassicHistogramBuckets.EMPTY,
0,
@@ -302,7 +302,7 @@ class HistogramSnapshotTest {
}
@Test
- public void testMinimalClassicData() {
+ void minimalClassicData() {
ClassicHistogramBuckets buckets =
ClassicHistogramBuckets.builder().bucket(Double.POSITIVE_INFINITY, 0).build();
new HistogramDataPointSnapshot(
--- a/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/InfoSnapshotTest.java
+++ b/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/InfoSnapshotTest.java
@@ -1,15 +1,15 @@
package io.prometheus.metrics.model.snapshots;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.util.Iterator;
import org.junit.jupiter.api.Test;
-class InfoSnapshotTest {
+final class InfoSnapshotTest {
@Test
- public void testCompleteGoodCase() {
+ void completeGoodCase() {
InfoSnapshot snapshot =
InfoSnapshot.builder()
.name("target")
@@ -22,24 +22,24 @@ class InfoSnapshotTest {
assertThat(snapshot.getMetadata().getName()).isEqualTo("target");
assertThat(snapshot.getMetadata().getHelp()).isEqualTo("Target info");
assertThat(snapshot.getMetadata().hasUnit()).isFalse();
- assertThat(snapshot.getDataPoints().size()).isOne();
+ assertThat(snapshot.getDataPoints()).hasSize(1);
}
@Test
void create() {
InfoSnapshot.InfoDataPointSnapshot snapshot =
new InfoSnapshot.InfoDataPointSnapshot(Labels.EMPTY);
- assertThat(snapshot.getScrapeTimestampMillis()).isZero();
+ assertThat(snapshot.getScrapeTimestampMillis()).isEqualTo(0);
}
@Test
- public void testEmptyInfo() {
+ void emptyInfo() {
InfoSnapshot snapshot = InfoSnapshot.builder().name("target").build();
assertThat(snapshot.getDataPoints()).isEmpty();
}
@Test
- public void testDataImmutable() {
+ void dataImmutable() {
InfoSnapshot snapshot =
InfoSnapshot.builder()
.name("target")
@@ -56,18 +56,18 @@ class InfoSnapshotTest {
.build();
Iterator<InfoSnapshot.InfoDataPointSnapshot> iterator = snapshot.getDataPoints().iterator();
iterator.next();
- assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(iterator::remove);
+ assertThatThrownBy(iterator::remove).isInstanceOf(UnsupportedOperationException.class);
}
@Test
- public void testNameMustNotIncludeSuffix() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> InfoSnapshot.builder().name("jvm_info").build());
+ void nameMustNotIncludeSuffix() {
+ assertThatThrownBy(() -> InfoSnapshot.builder().name("jvm_info").build())
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testNameMustNotIncludeSuffixDot() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> InfoSnapshot.builder().name("jvm.info").build());
+ void nameMustNotIncludeSuffixDot() {
+ assertThatThrownBy(() -> InfoSnapshot.builder().name("jvm.info").build())
+ .isInstanceOf(IllegalArgumentException.class);
}
}
--- a/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/LabelTest.java
+++ b/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/LabelTest.java
@@ -4,7 +4,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
-class LabelTest {
+final class LabelTest {
private static final Label LABEL = new Label("name", "value");
private static final Label SAME = new Label("name", "value");
--- a/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/LabelsTest.java
+++ b/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/LabelsTest.java
@@ -1,12 +1,12 @@
package io.prometheus.metrics.model.snapshots;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import org.assertj.core.api.IterableAssert;
import org.junit.jupiter.api.Test;
-class LabelsTest {
+final class LabelsTest {
private <T extends Comparable<T>> void assertLessThan(T a, T b) {
assertThat(a).isLessThan(b);
@@ -17,7 +17,7 @@ class LabelsTest {
}
@Test
- public void testCompareDifferentLabelNames() {
+ void compareDifferentLabelNames() {
Labels labels1 = Labels.of("env", "prod", "status2", "200");
Labels labels2 = Labels.of("env", "prod", "status1", "200");
assertGreaterThan(labels1, labels2);
@@ -31,7 +31,7 @@ class LabelsTest {
}
@Test
- public void testCompareSameLabelNames() {
+ void compareSameLabelNames() {
// If all label names are the same, labels should be sorted by label value.
Labels labels1 = Labels.of("env", "prod", "status", "200");
Labels labels2 = Labels.of("env", "prod", "status", "500");
@@ -42,7 +42,7 @@ class LabelsTest {
}
@Test
- public void testCompareDifferentNumberOfLabels() {
+ void compareDifferentNumberOfLabels() {
Labels labels1 = Labels.of("env", "prod", "status", "200");
Labels labels2 = Labels.of("env", "prod", "status", "200", "x_code", "none");
assertLessThan(labels1, labels2);
@@ -52,22 +52,22 @@ class LabelsTest {
}
@Test
- public void testComparePrometheusNames() {
+ void comparePrometheusNames() {
Labels labels1 = Labels.of("my_a", "val");
Labels labels2 = Labels.of("my.b", "val");
assertLessThan(labels1, labels2); // this is true because it compares "my_a" to "my_b".
}
@Test
- public void testEqualsHashcodeDots() {
+ void equalsHashcodeDots() {
Labels labels1 = Labels.of("my_a", "val");
Labels labels2 = Labels.of("my.a", "val");
assertLabels(labels2).isEqualTo(labels1).hasSameHashCodeAs(labels1);
}
- @SuppressWarnings({"unchecked", "rawtypes"})
+ @SuppressWarnings({"rawtypes", "unchecked"})
@Test
- public void testCompareEquals() {
+ void compareEquals() {
Labels labels1 = Labels.of("env", "prod", "status", "200");
Labels labels2 = Labels.of("env", "prod", "status", "200");
assertThat((Comparable) labels1).isEqualByComparingTo(labels2);
@@ -77,25 +77,25 @@ class LabelsTest {
}
@Test
- public void testIllegalLabelName() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> Labels.of("my_service/status", "200"));
+ void illegalLabelName() {
+ assertThatThrownBy(() -> Labels.of("my_service/status", "200"))
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testReservedLabelName() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> Labels.of("__name__", "requests_total"));
+ void reservedLabelName() {
+ assertThatThrownBy(() -> Labels.of("__name__", "requests_total"))
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testDuplicateLabelName() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> Labels.of("name1", "value1", "name2", "value2", "name1", "value3"));
+ void duplicateLabelName() {
+ assertThatThrownBy(() -> Labels.of("name1", "value1", "name2", "value2", "name1", "value3"))
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testMakePrometheusNames() {
+ void makePrometheusNames() {
String[] names = new String[] {};
String[] prometheusNames = Labels.makePrometheusNames(names);
assertThat(prometheusNames).isSameAs(names);
@@ -113,7 +113,7 @@ class LabelsTest {
}
@Test
- public void testMerge() {
+ void merge() {
Labels labels1 = Labels.of("key.1", "value 1", "key.3", "value 3");
Labels labels2 = Labels.of("key_2", "value 2");
Labels merged = labels2.merge(labels1);
@@ -123,16 +123,15 @@ class LabelsTest {
}
@Test
- public void testMergeDuplicateName() {
+ void mergeDuplicateName() {
Labels labels1 = Labels.of("key_one", "v1");
Labels labels2 = Labels.of("key.one", "v2");
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> labels2.merge(labels1));
+ assertThatThrownBy(() -> labels2.merge(labels1)).isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testDuplicateName() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> Labels.of("key_one", "v1", "key.one", "v2"));
+ void duplicateName() {
+ assertThatThrownBy(() -> Labels.of("key_one", "v1", "key.one", "v2"))
+ .isInstanceOf(IllegalArgumentException.class);
}
}
--- a/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/MetricMetadataTest.java
+++ b/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/MetricMetadataTest.java
@@ -2,36 +2,31 @@ package io.prometheus.metrics.model.snapshots;
import static io.prometheus.metrics.model.snapshots.PrometheusNaming.sanitizeMetricName;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import org.junit.jupiter.api.Test;
-class MetricMetadataTest {
+final class MetricMetadataTest {
@Test
- public void testEmptyName() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> new MetricMetadata(""));
+ void emptyName() {
+ assertThatThrownBy(() -> new MetricMetadata("")).isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testNullName() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> new MetricMetadata(null));
+ void nullName() {
+ assertThatThrownBy(() -> new MetricMetadata(null)).isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testIllegalName() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(
- () ->
- new MetricMetadata(
- "my_namespace/http_server_duration")); // let's see when we decide to allow
+ void illegalName() {
+ assertThatThrownBy(() -> new MetricMetadata("my_namespace/http_server_duration"))
+ .isInstanceOf(IllegalArgumentException.class); // let's see when we decide to allow
// slashes :)
}
@Test
- public void testSanitizationIllegalCharacters() {
+ void sanitizationIllegalCharacters() {
MetricMetadata metadata =
new MetricMetadata(
sanitizeMetricName("my_namespace/http.server.duration", Unit.SECONDS),
@@ -40,46 +35,45 @@ class MetricMetadataTest {
assertThat(metadata.getName()).isEqualTo("my_namespace_http.server.duration_seconds");
assertThat(metadata.getPrometheusName()).isEqualTo("my_namespace_http_server_duration_seconds");
assertThat(metadata.getHelp()).isEqualTo("help string");
- assertThat(metadata.getUnit().toString()).isEqualTo("seconds");
+ assertThat(metadata.getUnit()).hasToString("seconds");
}
@Test
- public void testSanitizationCounter() {
+ void sanitizationCounter() {
MetricMetadata metadata = new MetricMetadata(sanitizeMetricName("my_events_total"));
assertThat(metadata.getName()).isEqualTo("my_events");
}
@Test
- public void testSanitizationInfo() {
+ void sanitizationInfo() {
MetricMetadata metadata = new MetricMetadata(sanitizeMetricName("target_info"));
assertThat(metadata.getName()).isEqualTo("target");
}
@Test
- public void testSanitizationWeirdCornerCase() {
+ void sanitizationWeirdCornerCase() {
MetricMetadata metadata = new MetricMetadata(sanitizeMetricName("_total_created"));
assertThat(metadata.getName()).isEqualTo("total");
}
@Test
- public void testSanitizeEmptyString() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> sanitizeMetricName(""));
+ void sanitizeEmptyString() {
+ assertThatThrownBy(() -> sanitizeMetricName("")).isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testUnitSuffixRequired() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> new MetricMetadata("my_counter", "help", Unit.SECONDS));
+ void unitSuffixRequired() {
+ assertThatThrownBy(() -> new MetricMetadata("my_counter", "help", Unit.SECONDS))
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testUnitSuffixAdded() {
+ void unitSuffixAdded() {
new MetricMetadata(sanitizeMetricName("my_counter", Unit.SECONDS), "help", Unit.SECONDS);
}
@Test
- public void testUnitNotDuplicated() {
+ void unitNotDuplicated() {
assertThat(sanitizeMetricName("my_counter_bytes", Unit.BYTES)).isEqualTo("my_counter_bytes");
}
}
--- a/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/MetricSnapshotTest.java
+++ b/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/MetricSnapshotTest.java
@@ -1,16 +1,16 @@
package io.prometheus.metrics.model.snapshots;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.assertj.core.api.InstanceOfAssertFactories.throwable;
import org.junit.jupiter.api.Test;
-class MetricSnapshotTest {
+final class MetricSnapshotTest {
@Test
- public void testDuplicateLabels() {
- assertThatExceptionOfType(DuplicateLabelsException.class)
- .isThrownBy(
+ void duplicateLabels() {
+ assertThatThrownBy(
() ->
CounterSnapshot.builder()
.name("events")
@@ -30,6 +30,7 @@ class MetricSnapshotTest {
.value(3.0)
.build())
.build())
+ .asInstanceOf(throwable(DuplicateLabelsException.class))
.satisfies(
e -> {
assertThat(e.getMetadata().getName()).isEqualTo("events");
@@ -39,14 +40,14 @@ class MetricSnapshotTest {
}
@Test
- public void testNoData() {
+ void noData() {
MetricSnapshot snapshot = CounterSnapshot.builder().name("test").build();
- assertThat(snapshot.getDataPoints().size()).isEqualTo(0);
+ assertThat(snapshot.getDataPoints()).isEmpty();
}
@Test
- public void testNullData() {
- assertThatExceptionOfType(NullPointerException.class)
- .isThrownBy(() -> new CounterSnapshot(new MetricMetadata("test"), null));
+ void nullData() {
+ assertThatThrownBy(() -> new CounterSnapshot(new MetricMetadata("test"), null))
+ .isInstanceOf(NullPointerException.class);
}
}
--- a/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/MetricSnapshotsTest.java
+++ b/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/MetricSnapshotsTest.java
@@ -1,21 +1,21 @@
package io.prometheus.metrics.model.snapshots;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.util.Iterator;
import org.junit.jupiter.api.Test;
-class MetricSnapshotsTest {
+final class MetricSnapshotsTest {
@Test
- public void testEmpty() {
+ void empty() {
MetricSnapshots snapshots = MetricSnapshots.builder().build();
- assertThat(snapshots.stream().findAny().isPresent()).isFalse();
+ assertThat(snapshots.stream().findAny()).isEmpty();
}
@Test
- public void testSort() {
+ void sort() {
CounterSnapshot c1 =
CounterSnapshot.builder()
.name("counter1")
@@ -39,7 +39,7 @@ class MetricSnapshotsTest {
}
@Test
- public void testDuplicateName() {
+ void duplicateName() {
// Q: What if you have a counter named "foo" and a gauge named "foo"?
// A: Great question. You might think this is a valid scenario, because the counter will produce
// the values "foo_total" and "foo_created" while the gauge will produce the value "foo".
@@ -57,12 +57,12 @@ class MetricSnapshotsTest {
.name("my_metric")
.dataPoint(GaugeSnapshot.GaugeDataPointSnapshot.builder().value(1.0).build())
.build();
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> new MetricSnapshots(c, g));
+ assertThatThrownBy(() -> new MetricSnapshots(c, g))
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testBuilder() {
+ void builder() {
CounterSnapshot counter =
CounterSnapshot.builder()
.name("my_metric")
@@ -75,7 +75,7 @@ class MetricSnapshotsTest {
}
@Test
- public void testImmutable() {
+ void immutable() {
CounterSnapshot c1 =
CounterSnapshot.builder()
.name("counter1")
@@ -94,6 +94,6 @@ class MetricSnapshotsTest {
MetricSnapshots snapshots = new MetricSnapshots(c2, c3, c1);
Iterator<MetricSnapshot> iterator = snapshots.iterator();
iterator.next();
- assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(iterator::remove);
+ assertThatThrownBy(iterator::remove).isInstanceOf(UnsupportedOperationException.class);
}
}
--- a/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/NativeHistogramBucketsTest.java
+++ b/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/NativeHistogramBucketsTest.java
@@ -1,15 +1,15 @@
package io.prometheus.metrics.model.snapshots;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.util.Iterator;
import org.junit.jupiter.api.Test;
-class NativeHistogramBucketsTest {
+final class NativeHistogramBucketsTest {
@Test
- public void testGoodCase() {
+ void goodCase() {
NativeHistogramBuckets buckets =
NativeHistogramBuckets.builder().bucket(-10, 12).bucket(120, 17).build();
assertThat(buckets.size()).isEqualTo(2);
@@ -20,38 +20,38 @@ class NativeHistogramBucketsTest {
}
@Test
- public void testEmpty() {
+ void empty() {
NativeHistogramBuckets buckets = NativeHistogramBuckets.builder().build();
- assertThat(buckets.size()).isZero();
+ assertThat(buckets.size()).isEqualTo(0);
}
@Test
- public void testSort() {
+ void sort() {
NativeHistogramBuckets buckets =
NativeHistogramBuckets.builder().bucket(7, 4).bucket(2, 0).bucket(5, 3).build();
assertThat(buckets.size()).isEqualTo(3);
assertThat(buckets.getBucketIndex(0)).isEqualTo(2);
assertThat(buckets.getBucketIndex(1)).isEqualTo(5);
assertThat(buckets.getBucketIndex(2)).isEqualTo(7);
- assertThat(buckets.getCount(0)).isZero();
+ assertThat(buckets.getCount(0)).isEqualTo(0);
assertThat(buckets.getCount(1)).isEqualTo(3);
assertThat(buckets.getCount(2)).isEqualTo(4);
}
@Test
- public void testDifferentLength() {
+ void differentLength() {
int[] bucketIndexes = new int[] {0, 1, 2};
long[] cumulativeCounts = new long[] {13, 178, 1024, 3000};
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> NativeHistogramBuckets.of(bucketIndexes, cumulativeCounts));
+ assertThatThrownBy(() -> NativeHistogramBuckets.of(bucketIndexes, cumulativeCounts))
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testImmutable() {
+ void immutable() {
NativeHistogramBuckets buckets =
NativeHistogramBuckets.builder().bucket(1, 1).bucket(2, 1).build();
Iterator<NativeHistogramBucket> iterator = buckets.iterator();
iterator.next();
- assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(iterator::remove);
+ assertThatThrownBy(iterator::remove).isInstanceOf(UnsupportedOperationException.class);
}
}
--- a/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/PrometheusNamingTest.java
+++ b/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/PrometheusNamingTest.java
@@ -2,14 +2,14 @@ package io.prometheus.metrics.model.snapshots;
import static io.prometheus.metrics.model.snapshots.PrometheusNaming.*;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import org.junit.jupiter.api.Test;
-class PrometheusNamingTest {
+final class PrometheusNamingTest {
@Test
- public void testSanitizeMetricName() {
+ void testSanitizeMetricName() {
assertThat(prometheusName(sanitizeMetricName("0abc.def"))).isEqualTo("_abc_def");
assertThat(prometheusName(sanitizeMetricName("___ab.:c0"))).isEqualTo("___ab__c0");
assertThat(sanitizeMetricName("my_prefix/my_metric")).isEqualTo("my_prefix_my_metric");
@@ -23,25 +23,25 @@ class PrometheusNamingTest {
}
@Test
- public void testSanitizeMetricNameWithUnit() {
+ void sanitizeMetricNameWithUnit() {
assertThat(prometheusName(sanitizeMetricName("0abc.def", Unit.RATIO)))
- .isEqualTo("_abc_def_" + Unit.RATIO);
+ .isEqualTo("_abc_def_%s", Unit.RATIO);
assertThat(prometheusName(sanitizeMetricName("___ab.:c0", Unit.RATIO)))
- .isEqualTo("___ab__c0_" + Unit.RATIO);
+ .isEqualTo("___ab__c0_%s", Unit.RATIO);
assertThat(sanitizeMetricName("my_prefix/my_metric", Unit.RATIO))
- .isEqualTo("my_prefix_my_metric_" + Unit.RATIO);
+ .isEqualTo("my_prefix_my_metric_%s", Unit.RATIO);
assertThat(prometheusName(sanitizeMetricName("my_counter_total", Unit.RATIO)))
- .isEqualTo("my_counter_" + Unit.RATIO);
- assertThat(sanitizeMetricName("jvm.info", Unit.RATIO)).isEqualTo("jvm_" + Unit.RATIO);
- assertThat(sanitizeMetricName("jvm_info", Unit.RATIO)).isEqualTo("jvm_" + Unit.RATIO);
- assertThat(sanitizeMetricName("jvm.info", Unit.RATIO)).isEqualTo("jvm_" + Unit.RATIO);
- assertThat(sanitizeMetricName("a.b", Unit.RATIO)).isEqualTo("a.b_" + Unit.RATIO);
- assertThat(sanitizeMetricName("_total", Unit.RATIO)).isEqualTo("total_" + Unit.RATIO);
- assertThat(sanitizeMetricName("total", Unit.RATIO)).isEqualTo("total_" + Unit.RATIO);
+ .isEqualTo("my_counter_%s", Unit.RATIO);
+ assertThat(sanitizeMetricName("jvm.info", Unit.RATIO)).isEqualTo("jvm_%s", Unit.RATIO);
+ assertThat(sanitizeMetricName("jvm_info", Unit.RATIO)).isEqualTo("jvm_%s", Unit.RATIO);
+ assertThat(sanitizeMetricName("jvm.info", Unit.RATIO)).isEqualTo("jvm_%s", Unit.RATIO);
+ assertThat(sanitizeMetricName("a.b", Unit.RATIO)).isEqualTo("a.b_%s", Unit.RATIO);
+ assertThat(sanitizeMetricName("_total", Unit.RATIO)).isEqualTo("total_%s", Unit.RATIO);
+ assertThat(sanitizeMetricName("total", Unit.RATIO)).isEqualTo("total_%s", Unit.RATIO);
}
@Test
- public void testSanitizeLabelName() {
+ void testSanitizeLabelName() {
assertThat(prometheusName(sanitizeLabelName("0abc.def"))).isEqualTo("_abc_def");
assertThat(prometheusName(sanitizeLabelName("_abc"))).isEqualTo("_abc");
assertThat(prometheusName(sanitizeLabelName("__abc"))).isEqualTo("_abc");
@@ -52,7 +52,7 @@ class PrometheusNamingTest {
}
@Test
- public void testValidateUnitName() {
+ void testValidateUnitName() {
assertThat(validateUnitName("secondstotal")).isNotNull();
assertThat(validateUnitName("total")).isNotNull();
assertThat(validateUnitName("seconds_total")).isNotNull();
@@ -64,7 +64,7 @@ class PrometheusNamingTest {
}
@Test
- public void testSanitizeUnitName() {
+ void testSanitizeUnitName() {
assertThat(sanitizeUnitName("seconds")).isEqualTo("seconds");
assertThat(sanitizeUnitName("seconds_total")).isEqualTo("seconds");
assertThat(sanitizeUnitName("seconds_total_total")).isEqualTo("seconds");
@@ -74,26 +74,24 @@ class PrometheusNamingTest {
}
@Test
- public void testInvalidUnitName1() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> sanitizeUnitName("total"));
+ void invalidUnitName1() {
+ assertThatThrownBy(() -> sanitizeUnitName("total"))
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testInvalidUnitName2() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> sanitizeUnitName("_total"));
+ void invalidUnitName2() {
+ assertThatThrownBy(() -> sanitizeUnitName("_total"))
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testInvalidUnitName3() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> sanitizeUnitName("%"));
+ void invalidUnitName3() {
+ assertThatThrownBy(() -> sanitizeUnitName("%")).isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testEmptyUnitName() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> sanitizeUnitName(""));
+ void emptyUnitName() {
+ assertThatThrownBy(() -> sanitizeUnitName("")).isInstanceOf(IllegalArgumentException.class);
}
}
--- a/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/QuantilesTest.java
+++ b/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/QuantilesTest.java
@@ -1,15 +1,15 @@
package io.prometheus.metrics.model.snapshots;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.util.Iterator;
import org.junit.jupiter.api.Test;
-class QuantilesTest {
+final class QuantilesTest {
@Test
- public void testSort() {
+ void sort() {
Quantiles quantiles =
Quantiles.builder().quantile(0.99, 0.23).quantile(0.5, 0.2).quantile(0.95, 0.22).build();
assertThat(quantiles.size()).isEqualTo(3);
@@ -22,28 +22,28 @@ class QuantilesTest {
}
@Test
- public void testImmutable() {
+ void immutable() {
Quantiles quantiles =
Quantiles.builder().quantile(0.99, 0.23).quantile(0.5, 0.2).quantile(0.95, 0.22).build();
Iterator<Quantile> iterator = quantiles.iterator();
iterator.next();
- assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(iterator::remove);
+ assertThatThrownBy(iterator::remove).isInstanceOf(UnsupportedOperationException.class);
}
@Test
- public void testEmpty() {
- assertThat(Quantiles.EMPTY.size()).isZero();
+ void empty() {
+ assertThat(Quantiles.EMPTY.size()).isEqualTo(0);
}
@Test
- public void testDuplicate() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(
+ void duplicate() {
+ assertThatThrownBy(
() ->
Quantiles.builder()
.quantile(0.95, 0.23)
.quantile(0.5, 0.2)
.quantile(0.95, 0.22)
- .build());
+ .build())
+ .isInstanceOf(IllegalArgumentException.class);
}
}
--- a/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/StateSetSnapshotTest.java
+++ b/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/StateSetSnapshotTest.java
@@ -1,16 +1,16 @@
package io.prometheus.metrics.model.snapshots;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;
-class StateSetSnapshotTest {
+final class StateSetSnapshotTest {
@Test
- public void testCompleteGoodCase() {
+ void completeGoodCase() {
long scrapeTimestamp = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(1);
StateSetSnapshot snapshot =
StateSetSnapshot.builder()
@@ -49,7 +49,7 @@ class StateSetSnapshotTest {
}
@Test
- public void testStateSetDataSorted() {
+ void stateSetDataSorted() {
StateSetSnapshot.StateSetDataPointSnapshot data =
StateSetSnapshot.StateSetDataPointSnapshot.builder()
.state("b", true)
@@ -69,31 +69,31 @@ class StateSetSnapshotTest {
}
@Test
- public void testMustHaveState() {
+ void mustHaveState() {
// Must have at least one state.
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> StateSetSnapshot.StateSetDataPointSnapshot.builder().build());
+ assertThatThrownBy(() -> StateSetSnapshot.StateSetDataPointSnapshot.builder().build())
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testMinimal() {
+ void minimal() {
StateSetSnapshot snapshot =
StateSetSnapshot.builder()
.name("my_flag")
.dataPoint(
StateSetSnapshot.StateSetDataPointSnapshot.builder().state("flag", true).build())
.build();
- assertThat(snapshot.dataPoints.size()).isOne();
+ assertThat(snapshot.dataPoints).hasSize(1);
}
@Test
- public void testEmpty() {
+ void empty() {
StateSetSnapshot snapshot = StateSetSnapshot.builder().name("my_flag").build();
assertThat(snapshot.dataPoints).isEmpty();
}
@Test
- public void testDataImmutable() {
+ void dataImmutable() {
StateSetSnapshot.StateSetDataPointSnapshot data =
StateSetSnapshot.StateSetDataPointSnapshot.builder()
.state("a", true)
@@ -107,29 +107,29 @@ class StateSetSnapshotTest {
StateSetSnapshot.State state = iterator.next();
assertThat(state.getName()).isEqualTo("a");
assertThat(state.isTrue()).isTrue();
- assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(iterator::remove);
+ assertThatThrownBy(iterator::remove).isInstanceOf(UnsupportedOperationException.class);
}
@Test
- public void testDuplicateState() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(
+ void duplicateState() {
+ assertThatThrownBy(
() ->
StateSetSnapshot.StateSetDataPointSnapshot.builder()
.state("a", true)
.state("b", true)
.state("a", true)
- .build());
+ .build())
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void noUnit() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> StateSetSnapshot.builder().name("flags").unit(Unit.BYTES).build());
+ void noUnit() {
+ assertThatThrownBy(() -> StateSetSnapshot.builder().name("flags").unit(Unit.BYTES).build())
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testStateSetImmutable() {
+ void stateSetImmutable() {
StateSetSnapshot snapshot =
StateSetSnapshot.builder()
.name("flags")
@@ -147,13 +147,12 @@ class StateSetSnapshotTest {
Iterator<StateSetSnapshot.StateSetDataPointSnapshot> iterator =
snapshot.getDataPoints().iterator();
iterator.next();
- assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(iterator::remove);
+ assertThatThrownBy(iterator::remove).isInstanceOf(UnsupportedOperationException.class);
}
@Test
- public void testLabelsUnique() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(
+ void labelsUnique() {
+ assertThatThrownBy(
() ->
StateSetSnapshot.builder()
.name("flags")
@@ -165,6 +164,7 @@ class StateSetSnapshotTest {
StateSetSnapshot.StateSetDataPointSnapshot.builder()
.state("feature", true)
.build())
- .build());
+ .build())
+ .isInstanceOf(IllegalArgumentException.class);
}
}
--- a/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/SummarySnapshotTest.java
+++ b/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/SummarySnapshotTest.java
@@ -5,10 +5,10 @@ import static org.assertj.core.api.Assertions.assertThat;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;
-class SummarySnapshotTest {
+final class SummarySnapshotTest {
@Test
- public void testCompleteGoodCase() {
+ void completeGoodCase() {
long createdTimestamp = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(1);
long scrapeTimestamp = System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(2);
long exemplarTimestamp = System.currentTimeMillis();
@@ -81,7 +81,7 @@ class SummarySnapshotTest {
}
@Test
- public void testMinimal() {
+ void minimal() {
SummarySnapshot snapshot =
SummarySnapshot.builder()
.name("size_bytes")
@@ -94,20 +94,20 @@ class SummarySnapshotTest {
}
@Test
- public void testEmptySnapshot() {
+ void emptySnapshot() {
SummarySnapshot snapshot = SummarySnapshot.builder().name("empty_summary").build();
assertThat(snapshot.getDataPoints()).isEmpty();
}
@Test
- public void testEmptyData() {
+ void emptyData() {
SummarySnapshot.SummaryDataPointSnapshot data =
SummarySnapshot.SummaryDataPointSnapshot.builder().build();
- assertThat(data.getQuantiles().size()).isZero();
+ assertThat(data.getQuantiles().size()).isEqualTo(0);
assertThat(data.hasCount()).isFalse();
assertThat(data.hasSum()).isFalse();
assertThat(data.hasCreatedTimestamp()).isFalse();
assertThat(data.hasScrapeTimestamp()).isFalse();
- assertThat(data.getExemplars().size()).isZero();
+ assertThat(data.getExemplars().size()).isEqualTo(0);
}
}
--- a/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/UnitTest.java
+++ b/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/UnitTest.java
@@ -1,19 +1,19 @@
package io.prometheus.metrics.model.snapshots;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import org.junit.jupiter.api.Test;
-class UnitTest {
+final class UnitTest {
@Test
- public void testEmpty() {
- assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> new Unit(" "));
+ void empty() {
+ assertThatThrownBy(() -> new Unit(" ")).isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testEquals1() {
+ void equals1() {
Unit unit1 = Unit.BYTES;
Unit unit2 = new Unit("bytes");
@@ -21,7 +21,7 @@ class UnitTest {
}
@Test
- public void testEquals2() {
+ void equals2() {
Unit unit1 = new Unit("bytes ");
Unit unit2 = new Unit("bytes");
@@ -29,7 +29,7 @@ class UnitTest {
}
@Test
- public void testEquals3() {
+ void equals3() {
Unit unit1 = new Unit(" bytes");
Unit unit2 = new Unit("bytes");
@@ -37,7 +37,7 @@ class UnitTest {
}
@Test
- public void testEquals4() {
+ void equals4() {
Unit unit1 = new Unit(" bytes ");
Unit unit2 = new Unit("bytes");
--- a/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/UnknownSnapshotTest.java
+++ b/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/UnknownSnapshotTest.java
@@ -1,15 +1,15 @@
package io.prometheus.metrics.model.snapshots;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.data.Offset.offset;
import org.junit.jupiter.api.Test;
-class UnknownSnapshotTest {
+final class UnknownSnapshotTest {
@Test
- public void testCompleteGoodCase() {
+ void completeGoodCase() {
long exemplarTimestamp = System.currentTimeMillis();
UnknownSnapshot snapshot =
UnknownSnapshot.builder()
@@ -46,7 +46,7 @@ class UnknownSnapshotTest {
}
@Test
- public void testMinimal() {
+ void minimal() {
UnknownSnapshot snapshot =
UnknownSnapshot.builder()
.name("test")
@@ -56,25 +56,25 @@ class UnknownSnapshotTest {
}
@Test
- public void testEmpty() {
+ void empty() {
UnknownSnapshot snapshot = UnknownSnapshot.builder().name("test").build();
assertThat(snapshot.getDataPoints()).isEmpty();
}
@Test
- public void testNameMissing() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> UnknownSnapshot.builder().build());
+ void nameMissing() {
+ assertThatThrownBy(() -> UnknownSnapshot.builder().build())
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testValueMissing() {
- assertThatExceptionOfType(IllegalArgumentException.class)
- .isThrownBy(() -> UnknownSnapshot.UnknownDataPointSnapshot.builder().build());
+ void valueMissing() {
+ assertThatThrownBy(() -> UnknownSnapshot.UnknownDataPointSnapshot.builder().build())
+ .isInstanceOf(IllegalArgumentException.class);
}
@Test
- public void testUnknownDataPointSnapshot() {
+ void unknownDataPointSnapshot() {
Labels labels = Labels.of("k1", "v1");
Exemplar exemplar = Exemplar.builder().value(2.0).build();
--- a/prometheus-metrics-simpleclient-bridge/src/main/java/io/prometheus/metrics/simpleclient/bridge/SimpleclientCollector.java
+++ b/prometheus-metrics-simpleclient-bridge/src/main/java/io/prometheus/metrics/simpleclient/bridge/SimpleclientCollector.java
@@ -1,7 +1,9 @@
package io.prometheus.metrics.simpleclient.bridge;
+import static com.google.common.base.Preconditions.checkState;
import static io.prometheus.metrics.model.snapshots.PrometheusNaming.sanitizeMetricName;
+import com.google.common.base.Strings;
import io.prometheus.client.Collector;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.metrics.config.PrometheusProperties;
@@ -257,9 +259,7 @@ public class SimpleclientCollector implements MultiCollector {
break;
}
}
- if (stateName == null) {
- throw new IllegalStateException("Invalid StateSet metric: No state name found.");
- }
+ checkState(stateName != null, "Invalid StateSet metric: No state name found.");
dataPoints.get(labels).state(stateName, sample.value == 1.0);
if (sample.timestampMs != null) {
dataPoints.get(labels).scrapeTimestampMillis(sample.timestampMs);
@@ -292,7 +292,7 @@ public class SimpleclientCollector implements MultiCollector {
}
private Unit convertUnit(Collector.MetricFamilySamples samples) {
- if (samples.unit != null && !samples.unit.isEmpty()) {
+ if (!Strings.isNullOrEmpty(samples.unit)) {
return new Unit(samples.unit);
} else {
return null;
--- a/prometheus-metrics-simpleclient-bridge/src/test/java/io/prometheus/metrics/simpleclient/bridge/SimpleclientCollectorTest.java
+++ b/prometheus-metrics-simpleclient-bridge/src/test/java/io/prometheus/metrics/simpleclient/bridge/SimpleclientCollectorTest.java
@@ -2,6 +2,7 @@ package io.prometheus.metrics.simpleclient.bridge;
import static org.assertj.core.api.Assertions.assertThat;
+import com.google.common.collect.ImmutableList;
import io.prometheus.client.Collector;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.Counter;
@@ -18,25 +19,24 @@ import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-class SimpleclientCollectorTest {
+final class SimpleclientCollectorTest {
private CollectorRegistry origRegistry;
private PrometheusRegistry newRegistry;
@BeforeEach
- public void setUp() {
+ void setUp() {
origRegistry = new CollectorRegistry();
newRegistry = new PrometheusRegistry();
SimpleclientCollector.builder().collectorRegistry(origRegistry).register(newRegistry);
}
@Test
- public void testCounterComplete() throws IOException, InterruptedException {
+ void counterComplete() throws IOException, InterruptedException {
Counter counter =
Counter.build()
.name("service_time_seconds_total")
@@ -51,14 +51,14 @@ class SimpleclientCollectorTest {
}
@Test
- public void testCounterMinimal() throws IOException {
+ void counterMinimal() throws IOException {
Counter.build().name("events").help("total number of events").register(origRegistry);
assertThat(sort(newOpenMetrics())).isEqualTo(fixTimestamps(sort(origOpenMetrics())));
}
@Test
- public void testGaugeComplete() throws IOException, InterruptedException {
+ void gaugeComplete() throws IOException, InterruptedException {
Gauge gauge =
Gauge.build()
.name("disk_usage_ratio")
@@ -74,7 +74,7 @@ class SimpleclientCollectorTest {
}
@Test
- public void testGaugeMinimal() throws IOException {
+ void gaugeMinimal() throws IOException {
Gauge gauge =
Gauge.build()
.name("temperature_centigrade")
@@ -87,7 +87,7 @@ class SimpleclientCollectorTest {
}
@Test
- public void testHistogramComplete() throws IOException, InterruptedException {
+ void histogramComplete() throws IOException, InterruptedException {
Histogram histogram =
Histogram.build()
.name("response_size_bytes")
@@ -107,14 +107,14 @@ class SimpleclientCollectorTest {
}
@Test
- public void testHistogramMinimal() throws IOException {
+ void histogramMinimal() throws IOException {
Histogram.build().name("request_latency").help("request latency").register(origRegistry);
assertThat(sort(newOpenMetrics())).isEqualTo(fixCounts(fixTimestamps(sort(origOpenMetrics()))));
}
@Test
- public void testSummaryComplete() throws IOException, InterruptedException {
+ void summaryComplete() throws IOException, InterruptedException {
Summary summary =
Summary.build()
.name("http_request_duration_seconds")
@@ -138,14 +138,14 @@ class SimpleclientCollectorTest {
}
@Test
- public void testSummaryMinimal() throws IOException {
+ void summaryMinimal() throws IOException {
Summary.build().name("request_size").help("request size").register(origRegistry);
assertThat(sort(newOpenMetrics())).isEqualTo(fixCounts(fixTimestamps(sort(origOpenMetrics()))));
}
@Test
- public void testInfoComplete() throws IOException, InterruptedException {
+ void infoComplete() throws IOException, InterruptedException {
Info info =
Info.build()
.name("version")
@@ -160,7 +160,7 @@ class SimpleclientCollectorTest {
}
@Test
- public void testInfoMinimal() throws IOException {
+ void infoMinimal() throws IOException {
Info info = Info.build().name("jvm").help("JVM info").register(origRegistry);
info.info("version", "17");
@@ -168,7 +168,7 @@ class SimpleclientCollectorTest {
}
@Test
- public void testStateSetComplete() throws IOException {
+ void stateSetComplete() throws IOException {
Collector stateSet =
new Collector() {
@Override
@@ -180,7 +180,7 @@ class SimpleclientCollectorTest {
samples.add(
new Collector.MetricFamilySamples.Sample(
"state", Arrays.asList("env", "state"), Arrays.asList("dev", "state2"), 0.0));
- return Collections.singletonList(
+ return ImmutableList.of(
new Collector.MetricFamilySamples(
"state", Collector.Type.STATE_SET, "my state", samples));
}
@@ -191,7 +191,7 @@ class SimpleclientCollectorTest {
}
@Test
- public void testUnknownComplete() throws IOException {
+ void unknownComplete() throws IOException {
Collector unknown =
new Collector() {
@Override
@@ -209,7 +209,7 @@ class SimpleclientCollectorTest {
Arrays.asList("env", "status"),
Arrays.asList("prod", "error"),
0.0));
- return Collections.singletonList(
+ return ImmutableList.of(
new Collector.MetricFamilySamples(
"my_unknown_metric_seconds",
"seconds",
--- a/prometheus-metrics-tracer/prometheus-metrics-tracer-initializer/src/main/java/io/prometheus/metrics/tracer/initializer/SpanContextSupplier.java
+++ b/prometheus-metrics-tracer/prometheus-metrics-tracer-initializer/src/main/java/io/prometheus/metrics/tracer/initializer/SpanContextSupplier.java
@@ -7,11 +7,11 @@ import java.util.concurrent.atomic.AtomicReference;
public class SpanContextSupplier {
- private static final AtomicReference<SpanContext> spanContextRef =
+ private static final AtomicReference<SpanContext> SPAN_CONTEXT_REF =
new AtomicReference<SpanContext>();
public static void setSpanContext(SpanContext spanContext) {
- spanContextRef.set(spanContext);
+ SPAN_CONTEXT_REF.set(spanContext);
}
public static boolean hasSpanContext() {
@@ -19,13 +19,13 @@ public class SpanContextSupplier {
}
public static SpanContext getSpanContext() {
- return spanContextRef.get();
+ return SPAN_CONTEXT_REF.get();
}
static {
try {
if (OpenTelemetrySpanContext.isAvailable()) {
- spanContextRef.set(new OpenTelemetrySpanContext());
+ SPAN_CONTEXT_REF.set(new OpenTelemetrySpanContext());
}
} catch (NoClassDefFoundError ignored) {
// tracer_otel dependency not found
@@ -34,7 +34,7 @@ public class SpanContextSupplier {
}
try {
if (OpenTelemetryAgentSpanContext.isAvailable()) {
- spanContextRef.set(new OpenTelemetryAgentSpanContext());
+ SPAN_CONTEXT_REF.set(new OpenTelemetryAgentSpanContext());
}
} catch (NoClassDefFoundError ignored) {
// tracer_otel_agent dependency not found