diff --git a/examples/example-exemplars-tail-sampling/example-greeting-service/pom.xml b/examples/example-exemplars-tail-sampling/example-greeting-service/pom.xml index 530c74cea..fc329b061 100644 --- a/examples/example-exemplars-tail-sampling/example-greeting-service/pom.xml +++ b/examples/example-exemplars-tail-sampling/example-greeting-service/pom.xml @@ -41,6 +41,19 @@ ${project.artifactId} + + maven-compiler-plugin + + 17 + 17 + 17 + true + + -Xlint:all + -Werror + + + org.apache.maven.plugins maven-shade-plugin diff --git a/examples/example-exemplars-tail-sampling/example-greeting-service/src/main/java/io/prometheus/metrics/examples/otel_exemplars/greeting/GreetingServlet.java b/examples/example-exemplars-tail-sampling/example-greeting-service/src/main/java/io/prometheus/metrics/examples/otel_exemplars/greeting/GreetingServlet.java index af1652f76..45b2c8a79 100644 --- a/examples/example-exemplars-tail-sampling/example-greeting-service/src/main/java/io/prometheus/metrics/examples/otel_exemplars/greeting/GreetingServlet.java +++ b/examples/example-exemplars-tail-sampling/example-greeting-service/src/main/java/io/prometheus/metrics/examples/otel_exemplars/greeting/GreetingServlet.java @@ -13,6 +13,8 @@ /** Hello World REST servlet, with an example counter and an example histogram. */ public class GreetingServlet extends HttpServlet { + private static final long serialVersionUID = 0L; + private final Random random = new Random(0); private final Histogram histogram; diff --git a/examples/example-exemplars-tail-sampling/example-hello-world-app/pom.xml b/examples/example-exemplars-tail-sampling/example-hello-world-app/pom.xml index 88ad9c667..e7ab8cbd6 100644 --- a/examples/example-exemplars-tail-sampling/example-hello-world-app/pom.xml +++ b/examples/example-exemplars-tail-sampling/example-hello-world-app/pom.xml @@ -41,6 +41,19 @@ ${project.artifactId} + + maven-compiler-plugin + + 17 + 17 + 17 + true + + -Xlint:all + -Werror + + + org.apache.maven.plugins maven-shade-plugin diff --git a/examples/example-exemplars-tail-sampling/example-hello-world-app/src/main/java/io/prometheus/metrics/examples/otel_exemplars/app/HelloWorldServlet.java b/examples/example-exemplars-tail-sampling/example-hello-world-app/src/main/java/io/prometheus/metrics/examples/otel_exemplars/app/HelloWorldServlet.java index fbe293d50..f5a90d977 100644 --- a/examples/example-exemplars-tail-sampling/example-hello-world-app/src/main/java/io/prometheus/metrics/examples/otel_exemplars/app/HelloWorldServlet.java +++ b/examples/example-exemplars-tail-sampling/example-hello-world-app/src/main/java/io/prometheus/metrics/examples/otel_exemplars/app/HelloWorldServlet.java @@ -20,6 +20,8 @@ /** Hello World REST servlet, with an example counter and an example histogram. */ public class HelloWorldServlet extends HttpServlet { + private static final long serialVersionUID = 0L; + private final Random random = new Random(0); private final Histogram histogram; diff --git a/examples/example-exporter-multi-target/src/main/java/io/prometheus/metrics/examples/multitarget/SampleMultiCollector.java b/examples/example-exporter-multi-target/src/main/java/io/prometheus/metrics/examples/multitarget/SampleMultiCollector.java index 7a05f0a8b..f9cea3f87 100644 --- a/examples/example-exporter-multi-target/src/main/java/io/prometheus/metrics/examples/multitarget/SampleMultiCollector.java +++ b/examples/example-exporter-multi-target/src/main/java/io/prometheus/metrics/examples/multitarget/SampleMultiCollector.java @@ -71,11 +71,10 @@ protected MetricSnapshots collectMetricSnapshots(PrometheusScrapeRequest scrapeR gaugeBuilder.dataPoint(gaugeDataPointBuilder.build()); } } - Collection snaps = new ArrayList(); + Collection> snaps = new ArrayList<>(); snaps.add(counterBuilder.build()); snaps.add(gaugeBuilder.build()); - MetricSnapshots msnaps = new MetricSnapshots(snaps); - return msnaps; + return new MetricSnapshots(snaps); } public List getPrometheusNames() { diff --git a/examples/example-exporter-servlet-tomcat/pom.xml b/examples/example-exporter-servlet-tomcat/pom.xml index b253d7c50..357dd3118 100644 --- a/examples/example-exporter-servlet-tomcat/pom.xml +++ b/examples/example-exporter-servlet-tomcat/pom.xml @@ -41,6 +41,19 @@ ${project.artifactId} + + maven-compiler-plugin + + 17 + 17 + 17 + true + + -Xlint:all + -Werror + + + org.apache.maven.plugins maven-shade-plugin diff --git a/examples/example-exporter-servlet-tomcat/src/main/java/io/prometheus/metrics/examples/tomcat_servlet/HelloWorldServlet.java b/examples/example-exporter-servlet-tomcat/src/main/java/io/prometheus/metrics/examples/tomcat_servlet/HelloWorldServlet.java index eb2fa4f19..e2b654f34 100644 --- a/examples/example-exporter-servlet-tomcat/src/main/java/io/prometheus/metrics/examples/tomcat_servlet/HelloWorldServlet.java +++ b/examples/example-exporter-servlet-tomcat/src/main/java/io/prometheus/metrics/examples/tomcat_servlet/HelloWorldServlet.java @@ -14,6 +14,8 @@ /** Hello World REST servlet, with an example counter and an example histogram. */ public class HelloWorldServlet extends HttpServlet { + private static final long serialVersionUID = 0L; + private final Random random = new Random(0); // Note: The requests_total counter is not a great example, because the diff --git a/integration-tests/it-exporter/it-exporter-servlet-tomcat-sample/pom.xml b/integration-tests/it-exporter/it-exporter-servlet-tomcat-sample/pom.xml index 606be4c2a..e9ebcb583 100644 --- a/integration-tests/it-exporter/it-exporter-servlet-tomcat-sample/pom.xml +++ b/integration-tests/it-exporter/it-exporter-servlet-tomcat-sample/pom.xml @@ -36,6 +36,19 @@ exporter-servlet-tomcat-sample + + maven-compiler-plugin + + 17 + 17 + 17 + true + + -Xlint:all + -Werror + + + org.apache.maven.plugins maven-shade-plugin diff --git a/pom.xml b/pom.xml index 3e8a2809a..6ae53a26b 100644 --- a/pom.xml +++ b/pom.xml @@ -125,7 +125,7 @@ maven-compiler-plugin - 3.1 + 3.13.0 maven-surefire-plugin @@ -270,8 +270,14 @@ maven-compiler-plugin - 1.8 - 1.8 + 8 + 8 + 8 + true + + -Xlint:all + -Werror + diff --git a/prometheus-metrics-config/src/main/java/io/prometheus/metrics/config/PrometheusPropertiesException.java b/prometheus-metrics-config/src/main/java/io/prometheus/metrics/config/PrometheusPropertiesException.java index 5024cc45a..bdb024514 100644 --- a/prometheus-metrics-config/src/main/java/io/prometheus/metrics/config/PrometheusPropertiesException.java +++ b/prometheus-metrics-config/src/main/java/io/prometheus/metrics/config/PrometheusPropertiesException.java @@ -2,6 +2,8 @@ public class PrometheusPropertiesException extends RuntimeException { + private static final long serialVersionUID = 0L; + public PrometheusPropertiesException(String msg) { super(msg); } diff --git a/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/datapoints/TimerApi.java b/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/datapoints/TimerApi.java index d4266c04c..8267f7f78 100644 --- a/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/datapoints/TimerApi.java +++ b/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/datapoints/TimerApi.java @@ -57,22 +57,25 @@ public interface TimerApi { * must use base units (e.g. seconds, bytes) and leave converting them to something more readable * to graphing tools". */ + @SuppressWarnings("try") default void time(Runnable func) { - try (Timer timer = startTimer()) { + try (Timer ignored = startTimer()) { func.run(); } } /** Like {@link #time(Runnable)}, but returns the return value of {@code func}. */ + @SuppressWarnings("try") default T time(Supplier func) { - try (Timer timer = startTimer()) { + try (Timer ignored = startTimer()) { return func.get(); } } /** Like {@link #time(Supplier)}, but {@code func} may throw a checked {@code Exception}. */ + @SuppressWarnings("try") default T timeChecked(Callable func) throws Exception { - try (Timer timer = startTimer()) { + try (Timer ignored = startTimer()) { return func.call(); } } diff --git 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 index d3c00eca0..4821e93cd 100644 --- 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 @@ -19,7 +19,7 @@ protected Metric(Builder builder) { } @Override - public abstract MetricSnapshot collect(); + public abstract MetricSnapshot collect(); protected abstract static class Builder, M extends Metric> { diff --git a/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/SlidingWindow.java b/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/SlidingWindow.java index 93fa20d0e..c9586e179 100644 --- a/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/SlidingWindow.java +++ b/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/SlidingWindow.java @@ -39,6 +39,7 @@ public class SlidingWindow { * @param maxAgeSeconds after this amount of time an instance of T gets evicted. * @param ageBuckets number of age buckets. */ + @SuppressWarnings("unchecked") public SlidingWindow( Class clazz, Supplier constructor, diff --git a/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/StatefulMetric.java b/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/StatefulMetric.java index dc61b943d..1d2bfe02e 100644 --- a/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/StatefulMetric.java +++ b/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/StatefulMetric.java @@ -45,9 +45,9 @@ protected StatefulMetric(Builder builder) { /** * labels and metricData have the same size. labels.get(i) are the labels for metricData.get(i). */ - protected abstract MetricSnapshot collect(List labels, List metricData); + protected abstract MetricSnapshot collect(List labels, List metricData); - public MetricSnapshot collect() { + public MetricSnapshot collect() { if (labelNames.length == 0 && data.isEmpty()) { // This is a metric without labels that has not been used yet. Initialize the data on the fly. labelValues(); @@ -129,6 +129,7 @@ public void clear() { protected abstract T newDataPoint(); + @SuppressWarnings("unchecked") protected T getNoLabels() { if (noLabels == null) { // Note that this will throw an IllegalArgumentException if labelNames is not empty. diff --git 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 index 1e48d63c0..9aa0b04b3 100644 --- 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 @@ -80,8 +80,9 @@ public void testSet() { } @Test + @SuppressWarnings("try") public void testTimer() throws InterruptedException { - try (Timer timer = noLabels.startTimer()) { + try (Timer ignored = noLabels.startTimer()) { Thread.sleep(12); } assertThat(getValue(noLabels)) diff --git a/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/PrometheusMetricProducer.java b/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/PrometheusMetricProducer.java index 4b8b5591f..a3022289a 100644 --- a/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/PrometheusMetricProducer.java +++ b/prometheus-metrics-exporter-opentelemetry/src/main/java/io/prometheus/metrics/exporter/opentelemetry/PrometheusMetricProducer.java @@ -51,7 +51,7 @@ public Collection collectAllMetrics() { resourceWithTargetInfo, scopeFromInfo != null ? scopeFromInfo : instrumentationScopeInfo, System.currentTimeMillis()); - for (MetricSnapshot snapshot : snapshots) { + for (MetricSnapshot snapshot : snapshots) { if (snapshot instanceof CounterSnapshot) { addUnlessNull(result, factory.create((CounterSnapshot) snapshot)); } else if (snapshot instanceof GaugeSnapshot) { @@ -78,10 +78,10 @@ public Collection collectAllMetrics() { private Resource resourceFromTargetInfo(MetricSnapshots snapshots) { ResourceBuilder result = Resource.builder(); - for (MetricSnapshot snapshot : snapshots) { + for (MetricSnapshot snapshot : snapshots) { if (snapshot.getMetadata().getName().equals("target") && snapshot instanceof InfoSnapshot) { InfoSnapshot targetInfo = (InfoSnapshot) snapshot; - if (targetInfo.getDataPoints().size() > 0) { + if (!targetInfo.getDataPoints().isEmpty()) { InfoSnapshot.InfoDataPointSnapshot data = targetInfo.getDataPoints().get(0); Labels labels = data.getLabels(); for (int i = 0; i < labels.size(); i++) { @@ -95,11 +95,11 @@ private Resource resourceFromTargetInfo(MetricSnapshots snapshots) { private InstrumentationScopeInfo instrumentationScopeFromOTelScopeInfo( MetricSnapshots snapshots) { - for (MetricSnapshot snapshot : snapshots) { + for (MetricSnapshot snapshot : snapshots) { if (snapshot.getMetadata().getPrometheusName().equals("otel_scope") && snapshot instanceof InfoSnapshot) { InfoSnapshot scopeInfo = (InfoSnapshot) snapshot; - if (scopeInfo.getDataPoints().size() > 0) { + if (!scopeInfo.getDataPoints().isEmpty()) { Labels labels = scopeInfo.getDataPoints().get(0).getLabels(); String name = null; String version = null; diff --git 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 index def62834f..48054b192 100644 --- 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 @@ -70,13 +70,14 @@ public void tearDown() { } @Test + @SuppressWarnings("try") public void sampledExemplarIsForwarded() { try (SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder().setSampler(Sampler.alwaysOn()).build()) { Tracer test = sdkTracerProvider.get(INSTRUMENTATION_SCOPE_NAME); Span span = test.spanBuilder(SPAN_NAME).startSpan(); - try (Scope scope = span.makeCurrent()) { + try (Scope ignored = span.makeCurrent()) { testCounter.inc(2); } } @@ -95,13 +96,14 @@ public void sampledExemplarIsForwarded() { } @Test + @SuppressWarnings("try") public void notSampledExemplarIsNotForwarded() { try (SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder().setSampler(Sampler.alwaysOff()).build()) { Tracer test = sdkTracerProvider.get(INSTRUMENTATION_SCOPE_NAME); Span span = test.spanBuilder(SPAN_NAME).startSpan(); - try (Scope scope = span.makeCurrent()) { + try (Scope ignored = span.makeCurrent()) { testCounter.inc(2); } } diff --git a/prometheus-metrics-exporter-servlet-jakarta/pom.xml b/prometheus-metrics-exporter-servlet-jakarta/pom.xml index 02e225888..d135fb36d 100644 --- a/prometheus-metrics-exporter-servlet-jakarta/pom.xml +++ b/prometheus-metrics-exporter-servlet-jakarta/pom.xml @@ -1,5 +1,6 @@ - + 4.0.0 @@ -34,4 +35,22 @@ + + + + maven-compiler-plugin + + 17 + 17 + 17 + true + + -Xlint:all + -Werror + + + + + + diff --git a/prometheus-metrics-exporter-servlet-jakarta/src/main/java/io/prometheus/metrics/exporter/servlet/jakarta/PrometheusMetricsServlet.java b/prometheus-metrics-exporter-servlet-jakarta/src/main/java/io/prometheus/metrics/exporter/servlet/jakarta/PrometheusMetricsServlet.java index 28728dc94..5e22fc3fd 100644 --- a/prometheus-metrics-exporter-servlet-jakarta/src/main/java/io/prometheus/metrics/exporter/servlet/jakarta/PrometheusMetricsServlet.java +++ b/prometheus-metrics-exporter-servlet-jakarta/src/main/java/io/prometheus/metrics/exporter/servlet/jakarta/PrometheusMetricsServlet.java @@ -16,6 +16,8 @@ */ public class PrometheusMetricsServlet extends HttpServlet { + private static final long serialVersionUID = 0L; + private final PrometheusScrapeHandler handler; public PrometheusMetricsServlet() { diff --git a/prometheus-metrics-exporter-servlet-javax/src/main/java/io/prometheus/metrics/exporter/servlet/javax/PrometheusMetricsServlet.java b/prometheus-metrics-exporter-servlet-javax/src/main/java/io/prometheus/metrics/exporter/servlet/javax/PrometheusMetricsServlet.java index fe81fe96b..a486aec93 100644 --- a/prometheus-metrics-exporter-servlet-javax/src/main/java/io/prometheus/metrics/exporter/servlet/javax/PrometheusMetricsServlet.java +++ b/prometheus-metrics-exporter-servlet-javax/src/main/java/io/prometheus/metrics/exporter/servlet/javax/PrometheusMetricsServlet.java @@ -15,6 +15,8 @@ */ public class PrometheusMetricsServlet extends HttpServlet { + private static final long serialVersionUID = 0L; + private final PrometheusScrapeHandler handler; /** Default constructor. Uses the default PrometheusProperties and PrometheusRegistry. */ diff --git a/prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/OpenMetricsTextFormatWriter.java b/prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/OpenMetricsTextFormatWriter.java index 00ac6b9fc..10f54848b 100644 --- a/prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/OpenMetricsTextFormatWriter.java +++ b/prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/OpenMetricsTextFormatWriter.java @@ -65,7 +65,7 @@ public String getContentType() { public void write(OutputStream out, MetricSnapshots metricSnapshots) throws IOException { OutputStreamWriter writer = new OutputStreamWriter(out, StandardCharsets.UTF_8); - for (MetricSnapshot snapshot : metricSnapshots) { + for (MetricSnapshot snapshot : metricSnapshots) { if (snapshot.getDataPoints().size() > 0) { if (snapshot instanceof CounterSnapshot) { writeCounter(writer, (CounterSnapshot) snapshot); diff --git a/prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/PrometheusProtobufWriter.java b/prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/PrometheusProtobufWriter.java index 7e722b056..87e50ed8b 100644 --- a/prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/PrometheusProtobufWriter.java +++ b/prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/PrometheusProtobufWriter.java @@ -52,7 +52,7 @@ public String getContentType() { public String toDebugString(MetricSnapshots metricSnapshots) { StringBuilder stringBuilder = new StringBuilder(); - for (MetricSnapshot snapshot : metricSnapshots) { + for (MetricSnapshot snapshot : metricSnapshots) { if (snapshot.getDataPoints().size() > 0) { stringBuilder.append(TextFormat.printer().printToString(convert(snapshot))); } @@ -62,14 +62,14 @@ public String toDebugString(MetricSnapshots metricSnapshots) { @Override public void write(OutputStream out, MetricSnapshots metricSnapshots) throws IOException { - for (MetricSnapshot snapshot : metricSnapshots) { + for (MetricSnapshot snapshot : metricSnapshots) { if (snapshot.getDataPoints().size() > 0) { convert(snapshot).writeDelimitedTo(out); } } } - public Metrics.MetricFamily convert(MetricSnapshot snapshot) { + public Metrics.MetricFamily convert(MetricSnapshot snapshot) { Metrics.MetricFamily.Builder builder = Metrics.MetricFamily.newBuilder(); if (snapshot instanceof CounterSnapshot) { for (CounterDataPointSnapshot data : ((CounterSnapshot) snapshot).getDataPoints()) { diff --git a/prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/PrometheusTextFormatWriter.java b/prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/PrometheusTextFormatWriter.java index 28d049f25..786e1d9bd 100644 --- a/prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/PrometheusTextFormatWriter.java +++ b/prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/PrometheusTextFormatWriter.java @@ -59,7 +59,7 @@ public void write(OutputStream out, MetricSnapshots metricSnapshots) throws IOEx // "unknown", "gauge", "counter", "stateset", "info", "histogram", "gaugehistogram", and // "summary". OutputStreamWriter writer = new OutputStreamWriter(out, StandardCharsets.UTF_8); - for (MetricSnapshot snapshot : metricSnapshots) { + for (MetricSnapshot snapshot : metricSnapshots) { if (snapshot.getDataPoints().size() > 0) { if (snapshot instanceof CounterSnapshot) { writeCounter(writer, (CounterSnapshot) snapshot); @@ -79,7 +79,7 @@ public void write(OutputStream out, MetricSnapshots metricSnapshots) throws IOEx } } if (writeCreatedTimestamps) { - for (MetricSnapshot snapshot : metricSnapshots) { + for (MetricSnapshot snapshot : metricSnapshots) { if (snapshot.getDataPoints().size() > 0) { if (snapshot instanceof CounterSnapshot) { writeCreated(writer, snapshot); @@ -94,7 +94,8 @@ public void write(OutputStream out, MetricSnapshots metricSnapshots) throws IOEx writer.flush(); } - public void writeCreated(OutputStreamWriter writer, MetricSnapshot snapshot) throws IOException { + public void writeCreated(OutputStreamWriter writer, MetricSnapshot snapshot) + throws IOException { boolean metadataWritten = false; MetricMetadata metadata = snapshot.getMetadata(); for (DataPointSnapshot data : snapshot.getDataPoints()) { diff --git a/prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/TextFormatUtil.java b/prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/TextFormatUtil.java index da64f30bb..c61a8f1b6 100644 --- a/prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/TextFormatUtil.java +++ b/prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/TextFormatUtil.java @@ -85,6 +85,6 @@ static void writeLabels( } public static String shortDebugString(MessageOrBuilder protobufData) { - return TextFormat.printer().shortDebugString(protobufData); + return TextFormat.printer().emittingSingleLine(true).printToString(protobufData); } } diff --git 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 index 7a7a0ea7e..a5e427a89 100644 --- 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 @@ -2592,7 +2592,8 @@ public void testLabelValueEscape() throws IOException { assertPrometheusText(prometheus, counter); } - private void assertOpenMetricsText(String expected, MetricSnapshot snapshot) throws IOException { + private void assertOpenMetricsText(String expected, MetricSnapshot snapshot) + throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); OpenMetricsTextFormatWriter writer = new OpenMetricsTextFormatWriter(true, false); writer.write(out, MetricSnapshots.of(snapshot)); @@ -2600,14 +2601,14 @@ private void assertOpenMetricsText(String expected, MetricSnapshot snapshot) thr } private void assertOpenMetricsTextWithExemplarsOnAllTimeSeries( - String expected, MetricSnapshot snapshot) throws IOException { + String expected, MetricSnapshot snapshot) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); OpenMetricsTextFormatWriter writer = new OpenMetricsTextFormatWriter(true, true); writer.write(out, MetricSnapshots.of(snapshot)); assertThat(out).hasToString(expected); } - private void assertOpenMetricsTextWithoutCreated(String expected, MetricSnapshot snapshot) + private void assertOpenMetricsTextWithoutCreated(String expected, MetricSnapshot snapshot) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); OpenMetricsTextFormatWriter writer = new OpenMetricsTextFormatWriter(false, false); @@ -2615,14 +2616,15 @@ private void assertOpenMetricsTextWithoutCreated(String expected, MetricSnapshot assertThat(out).hasToString(expected); } - private void assertPrometheusText(String expected, MetricSnapshot snapshot) throws IOException { + private void assertPrometheusText(String expected, MetricSnapshot snapshot) + throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); PrometheusTextFormatWriter writer = new PrometheusTextFormatWriter(true); writer.write(out, MetricSnapshots.of(snapshot)); assertThat(out).hasToString(expected); } - private void assertPrometheusTextWithoutCreated(String expected, MetricSnapshot snapshot) + private void assertPrometheusTextWithoutCreated(String expected, MetricSnapshot snapshot) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); PrometheusTextFormatWriter writer = new PrometheusTextFormatWriter(false); @@ -2630,7 +2632,7 @@ private void assertPrometheusTextWithoutCreated(String expected, MetricSnapshot assertThat(out).hasToString(expected); } - private void assertPrometheusProtobuf(String expected, MetricSnapshot snapshot) { + private void assertPrometheusProtobuf(String expected, MetricSnapshot snapshot) { PrometheusProtobufWriter writer = new PrometheusProtobufWriter(); Metrics.MetricFamily protobufData = writer.convert(snapshot); String actual = TextFormatUtil.shortDebugString(protobufData); diff --git 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 index 69b852a41..c195ae77c 100644 --- 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 @@ -11,6 +11,7 @@ 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; @@ -53,7 +54,7 @@ public class CacheMetricsCollector implements MultiCollector { private static final double NANOSECONDS_PER_SECOND = 1_000_000_000.0; - protected final ConcurrentMap children = new ConcurrentHashMap(); + protected final ConcurrentMap> children = new ConcurrentHashMap<>(); /** * Add or replace the cache with the given name. @@ -63,7 +64,7 @@ public class CacheMetricsCollector implements MultiCollector { * @param cacheName The name of the cache, will be the metrics label value * @param cache The cache being monitored */ - public void addCache(String cacheName, Cache cache) { + public void addCache(String cacheName, Cache cache) { children.put(cacheName, cache); } @@ -75,7 +76,7 @@ public void addCache(String cacheName, Cache cache) { * @param cacheName The name of the cache, will be the metrics label value * @param cache The cache being monitored */ - public void addCache(String cacheName, AsyncCache cache) { + public void addCache(String cacheName, AsyncCache cache) { children.put(cacheName, cache.synchronous()); } @@ -86,7 +87,7 @@ public void addCache(String cacheName, AsyncCache cache) { * * @param cacheName cache to be removed */ - public Cache removeCache(String cacheName) { + public Cache nremoveCache(String cacheName) { return children.remove(cacheName); } @@ -141,8 +142,8 @@ public MetricSnapshots collect() { .name("caffeine_cache_load_duration_seconds") .help("Cache load duration: both success and failures"); - for (final Map.Entry c : children.entrySet()) { - final List cacheName = Arrays.asList(c.getKey()); + for (final Map.Entry> c : children.entrySet()) { + final List cacheName = Collections.singletonList(c.getKey()); final Labels labels = Labels.of(labelNames, cacheName); final CacheStats stats = c.getValue().stats(); diff --git 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 index c87f4aa90..6d99c176d 100644 --- 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 @@ -70,7 +70,7 @@ private MetricMetadata getMetricMetaData(String metricName, Metric metric) { * Export counter as Prometheus Gauge. */ - MetricSnapshot fromCounter(String dropwizardName, Counter counter) { + MetricSnapshot fromCounter(String dropwizardName, Counter counter) { MetricMetadata metadata = getMetricMetaData(dropwizardName, counter); CounterSnapshot.CounterDataPointSnapshot.Builder dataPointBuilder = CounterSnapshot.CounterDataPointSnapshot.builder() @@ -84,7 +84,7 @@ MetricSnapshot fromCounter(String dropwizardName, Counter counter) { } /** Export gauge as a prometheus gauge. */ - MetricSnapshot fromGauge(String dropwizardName, Gauge gauge) { + MetricSnapshot fromGauge(String dropwizardName, Gauge gauge) { Object obj = gauge.getValue(); double value; if (obj instanceof Number) { @@ -119,7 +119,7 @@ MetricSnapshot fromGauge(String dropwizardName, Gauge gauge) { * @param count the total sample count for this snapshot. * @param factor a factor to apply to histogram values. */ - MetricSnapshot fromSnapshotAndCount( + MetricSnapshot fromSnapshotAndCount( String dropwizardName, Snapshot snapshot, long count, double factor, String helpMessage) { Quantiles quantiles = Quantiles.builder() @@ -144,7 +144,7 @@ MetricSnapshot fromSnapshotAndCount( } /** Convert histogram snapshot. */ - MetricSnapshot fromHistogram(String dropwizardName, Histogram histogram) { + MetricSnapshot fromHistogram(String dropwizardName, Histogram histogram) { return fromSnapshotAndCount( dropwizardName, histogram.getSnapshot(), @@ -154,7 +154,7 @@ MetricSnapshot fromHistogram(String dropwizardName, Histogram histogram) { } /** Export Dropwizard Timer as a histogram. Use TIME_UNIT as time unit. */ - MetricSnapshot fromTimer(String dropwizardName, Timer timer) { + MetricSnapshot fromTimer(String dropwizardName, Timer timer) { return fromSnapshotAndCount( dropwizardName, timer.getSnapshot(), @@ -164,7 +164,7 @@ MetricSnapshot fromTimer(String dropwizardName, Timer timer) { } /** Export a Meter as a prometheus COUNTER. */ - MetricSnapshot fromMeter(String dropwizardName, Meter meter) { + MetricSnapshot fromMeter(String dropwizardName, Meter meter) { MetricMetadata metadata = getMetricMetaData(dropwizardName + "_total", meter); CounterSnapshot.CounterDataPointSnapshot.Builder dataPointBuilder = CounterSnapshot.CounterDataPointSnapshot.builder().value(meter.getCount()); @@ -179,7 +179,8 @@ MetricSnapshot fromMeter(String dropwizardName, Meter meter) { @Override public MetricSnapshots collect() { MetricSnapshots.Builder metricSnapshots = MetricSnapshots.builder(); - for (SortedMap.Entry entry : registry.getGauges(metricFilter).entrySet()) { + for (@SuppressWarnings("rawtypes") + SortedMap.Entry entry : registry.getGauges(metricFilter).entrySet()) { Optional.ofNullable(fromGauge(entry.getKey().getKey(), entry.getValue())) .map(metricSnapshots::metricSnapshot); } diff --git 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 index 5e17338b7..e1da20071 100644 --- 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 @@ -9,7 +9,7 @@ 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; @@ -53,7 +53,7 @@ public class CacheMetricsCollector implements MultiCollector { private static final double NANOSECONDS_PER_SECOND = 1_000_000_000.0; - protected final ConcurrentMap children = new ConcurrentHashMap<>(); + protected final ConcurrentMap> children = new ConcurrentHashMap<>(); /** * Add or replace the cache with the given name. @@ -63,7 +63,7 @@ public class CacheMetricsCollector implements MultiCollector { * @param cacheName The name of the cache, will be the metrics label value * @param cache The cache being monitored */ - public void addCache(String cacheName, Cache cache) { + public void addCache(String cacheName, Cache cache) { children.put(cacheName, cache); } @@ -74,7 +74,7 @@ public void addCache(String cacheName, Cache cache) { * * @param cacheName cache to be removed */ - public Cache removeCache(String cacheName) { + public Cache removeCache(String cacheName) { return children.remove(cacheName); } @@ -90,7 +90,7 @@ public void clear() { @Override public MetricSnapshots collect() { final MetricSnapshots.Builder metricSnapshotsBuilder = MetricSnapshots.builder(); - final List labelNames = Arrays.asList("cache"); + final List labelNames = Collections.singletonList("cache"); final CounterSnapshot.Builder cacheHitTotal = CounterSnapshot.builder().name("guava_cache_hit").help("Cache hit totals"); @@ -122,8 +122,8 @@ public MetricSnapshots collect() { .name("guava_cache_load_duration_seconds") .help("Cache load duration: both success and failures"); - for (final Map.Entry c : children.entrySet()) { - final List cacheName = Arrays.asList(c.getKey()); + for (final Map.Entry> c : children.entrySet()) { + final List cacheName = Collections.singletonList(c.getKey()); final Labels labels = Labels.of(labelNames, cacheName); final CacheStats stats = c.getValue().stats(); diff --git 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 index 67dd6f2e6..6aeacb13f 100644 --- 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 @@ -50,7 +50,7 @@ public void testListenerLogic() { } private double getCountByPool(String metricName, String poolName, MetricSnapshots snapshots) { - for (MetricSnapshot snapshot : snapshots) { + for (MetricSnapshot snapshot : snapshots) { if (snapshot.getMetadata().getPrometheusName().equals(metricName)) { for (CounterSnapshot.CounterDataPointSnapshot data : ((CounterSnapshot) snapshot).getDataPoints()) { diff --git 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 index 16e04b76c..fe97561aa 100644 --- 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 @@ -145,7 +145,7 @@ public void testInvalidThreadIds() { private Map getCountByState(MetricSnapshots snapshots) { Map result = new HashMap<>(); - for (MetricSnapshot snapshot : snapshots) { + for (MetricSnapshot snapshot : snapshots) { if (snapshot.getMetadata().getName().equals("jvm_threads_state")) { for (GaugeSnapshot.GaugeDataPointSnapshot data : ((GaugeSnapshot) snapshot).getDataPoints()) { diff --git a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/registry/Collector.java b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/registry/Collector.java index 57b2b640f..fad461548 100644 --- a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/registry/Collector.java +++ b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/registry/Collector.java @@ -11,13 +11,13 @@ public interface Collector { /** Called when the Prometheus server scrapes metrics. */ - MetricSnapshot collect(); + MetricSnapshot collect(); /** * Provides Collector with the details of the request issued by Prometheus to allow multi-target * pattern implementation Override to implement request dependent logic to provide MetricSnapshot */ - default MetricSnapshot collect(PrometheusScrapeRequest scrapeRequest) { + default MetricSnapshot collect(PrometheusScrapeRequest scrapeRequest) { return collect(); } @@ -28,8 +28,8 @@ default MetricSnapshot collect(PrometheusScrapeRequest scrapeRequest) { *

Override this if there is a more efficient way than first collecting the snapshot and then * discarding it. */ - default MetricSnapshot collect(Predicate includedNames) { - MetricSnapshot result = collect(); + default MetricSnapshot collect(Predicate includedNames) { + MetricSnapshot result = collect(); if (includedNames.test(result.getMetadata().getPrometheusName())) { return result; } else { @@ -43,9 +43,9 @@ default MetricSnapshot collect(Predicate includedNames) { *

Override this if there is a more efficient way than first collecting the snapshot and then * discarding it. */ - default MetricSnapshot collect( + default MetricSnapshot collect( Predicate includedNames, PrometheusScrapeRequest scrapeRequest) { - MetricSnapshot result = collect(scrapeRequest); + MetricSnapshot result = collect(scrapeRequest); if (includedNames.test(result.getMetadata().getPrometheusName())) { return result; } else { diff --git 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 index a4f52746e..8f44239fb 100644 --- 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 @@ -42,7 +42,7 @@ default MetricSnapshots collect( Predicate includedNames, PrometheusScrapeRequest scrapeRequest) { MetricSnapshots allSnapshots = scrapeRequest == null ? collect() : collect(scrapeRequest); MetricSnapshots.Builder result = MetricSnapshots.builder(); - for (MetricSnapshot snapshot : allSnapshots) { + for (MetricSnapshot snapshot : allSnapshots) { if (includedNames.test(snapshot.getMetadata().getPrometheusName())) { result.metricSnapshot(snapshot); } diff --git 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 index 00a19d9f6..026560bb9 100644 --- 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 @@ -63,7 +63,7 @@ public MetricSnapshots scrape() { public MetricSnapshots scrape(PrometheusScrapeRequest scrapeRequest) { MetricSnapshots.Builder result = MetricSnapshots.builder(); for (Collector collector : collectors) { - MetricSnapshot snapshot = + MetricSnapshot snapshot = scrapeRequest == null ? collector.collect() : collector.collect(scrapeRequest); if (snapshot != null) { if (result.containsMetricName(snapshot.getMetadata().getName())) { @@ -76,7 +76,7 @@ public MetricSnapshots scrape(PrometheusScrapeRequest scrapeRequest) { for (MultiCollector collector : multiCollectors) { MetricSnapshots snapshots = scrapeRequest == null ? collector.collect() : collector.collect(scrapeRequest); - for (MetricSnapshot snapshot : snapshots) { + for (MetricSnapshot snapshot : snapshots) { if (result.containsMetricName(snapshot.getMetadata().getName())) { throw new IllegalStateException( snapshot.getMetadata().getPrometheusName() + ": duplicate metric name."); @@ -105,7 +105,7 @@ public MetricSnapshots scrape( // prometheusName == null means the name is unknown, and we have to scrape to learn the name. // prometheusName != null means we can skip the scrape if the name is excluded. if (prometheusName == null || includedNames.test(prometheusName)) { - MetricSnapshot snapshot = + MetricSnapshot snapshot = scrapeRequest == null ? collector.collect(includedNames) : collector.collect(includedNames, scrapeRequest); @@ -132,7 +132,7 @@ public MetricSnapshots scrape( scrapeRequest == null ? collector.collect(includedNames) : collector.collect(includedNames, scrapeRequest); - for (MetricSnapshot snapshot : snapshots) { + for (MetricSnapshot snapshot : snapshots) { if (snapshot != null) { result.metricSnapshot(snapshot); } diff --git 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 index fa807af19..45be6f0f1 100644 --- 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 @@ -5,7 +5,7 @@ import java.util.List; /** Immutable snapshot of a Counter. */ -public class CounterSnapshot extends MetricSnapshot { +public class CounterSnapshot extends MetricSnapshot { /** * To create a new {@link CounterSnapshot}, you can either call the constructor directly or use @@ -21,7 +21,7 @@ public CounterSnapshot(MetricMetadata metadata, Collection getDataPoints() { - return (List) dataPoints; + return dataPoints; } public static class CounterDataPointSnapshot extends DataPointSnapshot { diff --git a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/DuplicateLabelsException.java b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/DuplicateLabelsException.java index d1f3f3414..721655105 100644 --- a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/DuplicateLabelsException.java +++ b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/DuplicateLabelsException.java @@ -6,6 +6,8 @@ */ public class DuplicateLabelsException extends IllegalArgumentException { + private static final long serialVersionUID = 0L; + private final MetricMetadata metadata; private final Labels labels; diff --git 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 index c94faf8dd..795323271 100644 --- 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 @@ -5,7 +5,7 @@ import java.util.List; /** Immutable snapshot of a Gauge. */ -public final class GaugeSnapshot extends MetricSnapshot { +public final class GaugeSnapshot extends MetricSnapshot { /** * To create a new {@link GaugeSnapshot}, you can either call the constructor directly or use the @@ -20,7 +20,7 @@ public GaugeSnapshot(MetricMetadata metadata, Collection @Override public List getDataPoints() { - return (List) dataPoints; + return dataPoints; } public static final class GaugeDataPointSnapshot extends DataPointSnapshot { diff --git 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 index 5988cce3d..6966552a7 100644 --- 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 @@ -5,7 +5,8 @@ import java.util.List; /** Immutable snapshot of a Histogram. */ -public final class HistogramSnapshot extends MetricSnapshot { +public final class HistogramSnapshot + extends MetricSnapshot { private final boolean gaugeHistogram; public static final int CLASSIC_HISTOGRAM = Integer.MIN_VALUE; @@ -41,7 +42,7 @@ public boolean isGaugeHistogram() { @Override public List getDataPoints() { - return (List) dataPoints; + return dataPoints; } public static final class HistogramDataPointSnapshot extends DistributionDataPointSnapshot { diff --git 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 index c7f180d9f..ec8ece23c 100644 --- 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 @@ -5,7 +5,7 @@ import java.util.List; /** Immutable snapshot of an Info metric. */ -public final class InfoSnapshot extends MetricSnapshot { +public final class InfoSnapshot extends MetricSnapshot { /** * To create a new {@link InfoSnapshot}, you can either call the constructor directly or use the @@ -24,7 +24,7 @@ public InfoSnapshot(MetricMetadata metadata, Collection d @Override public List getDataPoints() { - return (List) dataPoints; + return dataPoints; } public static class InfoDataPointSnapshot extends DataPointSnapshot { diff --git 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 index d44e54ff9..420feeced 100644 --- 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,24 +1,18 @@ package io.prometheus.metrics.model.snapshots; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.List; /** Base class for metric snapshots. */ -public abstract class MetricSnapshot { +public abstract class MetricSnapshot { private final MetricMetadata metadata; - protected final List dataPoints; + protected final List dataPoints; - protected MetricSnapshot(MetricMetadata metadata, DataPointSnapshot... dataPoints) { - this(metadata, Arrays.asList(dataPoints)); - } - - protected MetricSnapshot( - MetricMetadata metadata, Collection dataPoints) { + protected MetricSnapshot(MetricMetadata metadata, Collection dataPoints) { if (metadata == null) { throw new NullPointerException("metadata"); } @@ -26,7 +20,7 @@ protected MetricSnapshot( throw new NullPointerException("dataPoints"); } this.metadata = metadata; - List dataCopy = new ArrayList<>(dataPoints); + List dataCopy = new ArrayList<>(dataPoints); dataCopy.sort(Comparator.comparing(DataPointSnapshot::getLabels)); this.dataPoints = Collections.unmodifiableList(dataCopy); validateLabels(); @@ -36,7 +30,7 @@ public MetricMetadata getMetadata() { return metadata; } - public abstract List getDataPoints(); + public abstract List getDataPoints(); protected void validateLabels() { // Verify that labels are unique (the same set of names/values must not be used multiple times @@ -76,7 +70,7 @@ public T unit(Unit unit) { return self(); } - public abstract MetricSnapshot build(); + public abstract MetricSnapshot build(); protected MetricMetadata buildMetadata() { return new MetricMetadata(name, help, unit); diff --git 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 index ecee897e4..f56976e9e 100644 --- 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 @@ -14,12 +14,12 @@ import java.util.stream.Stream; /** Immutable list of metric snapshots. */ -public class MetricSnapshots implements Iterable { +public class MetricSnapshots implements Iterable> { - private final List snapshots; + private final List> snapshots; /** See {@link #MetricSnapshots(Collection)} */ - public MetricSnapshots(MetricSnapshot... snapshots) { + public MetricSnapshots(MetricSnapshot... snapshots) { this(Arrays.asList(snapshots)); } @@ -33,8 +33,8 @@ public MetricSnapshots(MetricSnapshot... snapshots) { * Builder#containsMetricName(String)} before calling {@link * Builder#metricSnapshot(MetricSnapshot)}. */ - public MetricSnapshots(Collection snapshots) { - List list = new ArrayList<>(snapshots); + public MetricSnapshots(Collection> snapshots) { + List> list = new ArrayList<>(snapshots); list.sort(comparing(s -> s.getMetadata().getPrometheusName())); for (int i = 0; i < snapshots.size() - 1; i++) { if (list.get(i) @@ -48,12 +48,12 @@ public MetricSnapshots(Collection snapshots) { this.snapshots = unmodifiableList(list); } - public static MetricSnapshots of(MetricSnapshot... snapshots) { + public static MetricSnapshots of(MetricSnapshot... snapshots) { return new MetricSnapshots(snapshots); } @Override - public Iterator iterator() { + public Iterator> iterator() { return snapshots.iterator(); } @@ -61,11 +61,11 @@ public int size() { return snapshots.size(); } - public MetricSnapshot get(int i) { + public MetricSnapshot get(int i) { return snapshots.get(i); } - public Stream stream() { + public Stream> stream() { return snapshots.stream(); } @@ -75,7 +75,7 @@ public static Builder builder() { public static class Builder { - private final List snapshots = new ArrayList<>(); + private final List> snapshots = new ArrayList<>(); private final Set prometheusNames = new HashSet<>(); private Builder() {} @@ -89,7 +89,7 @@ public boolean containsMetricName(String name) { } /** Add a metric snapshot. Call multiple times to add multiple metric snapshots. */ - public Builder metricSnapshot(MetricSnapshot snapshot) { + public Builder metricSnapshot(MetricSnapshot snapshot) { snapshots.add(snapshot); prometheusNames.add(snapshot.getMetadata().getPrometheusName()); return this; diff --git 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 index 591732137..7c6e49d19 100644 --- 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 @@ -9,7 +9,8 @@ import java.util.stream.Stream; /** Immutable snapshot of a StateSet metric. */ -public final class StateSetSnapshot extends MetricSnapshot { +public final class StateSetSnapshot + extends MetricSnapshot { /** * To create a new {@link StateSetSnapshot}, you can either call the constructor directly or use @@ -37,7 +38,7 @@ private void validate() { @Override public List getDataPoints() { - return (List) dataPoints; + return dataPoints; } public static class StateSetDataPointSnapshot extends DataPointSnapshot diff --git 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 index 7bc575ef8..26bd0f602 100644 --- 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 @@ -5,7 +5,8 @@ import java.util.List; /** Immutable snapshot of a Summary metric. */ -public final class SummarySnapshot extends MetricSnapshot { +public final class SummarySnapshot + extends MetricSnapshot { /** * To create a new {@link SummarySnapshot}, you can either call the constructor directly or use @@ -20,7 +21,7 @@ public SummarySnapshot(MetricMetadata metadata, Collection getDataPoints() { - return (List) dataPoints; + return dataPoints; } public static final class SummaryDataPointSnapshot extends DistributionDataPointSnapshot { diff --git 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 index 3fe01c9df..30ec564cb 100644 --- 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 @@ -5,7 +5,8 @@ import java.util.List; /** Immutable snapshot of an Unknown (Untyped) metric. */ -public final class UnknownSnapshot extends MetricSnapshot { +public final class UnknownSnapshot + extends MetricSnapshot { /** * To create a new {@link UnknownSnapshot}, you can either call the constructor directly or use @@ -21,7 +22,7 @@ public UnknownSnapshot(MetricMetadata metadata, Collection getDataPoints() { - return (List) dataPoints; + return dataPoints; } public static final class UnknownDataPointSnapshot extends DataPointSnapshot { diff --git 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 index 55e8b0be4..6557c0ecf 100644 --- 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 @@ -19,7 +19,7 @@ class PrometheusRegistryTest { Collector counterA1 = new Collector() { @Override - public MetricSnapshot collect() { + public MetricSnapshot collect() { return CounterSnapshot.builder().name("counter_a").build(); } @@ -32,7 +32,7 @@ public String getPrometheusName() { Collector counterA2 = new Collector() { @Override - public MetricSnapshot collect() { + public MetricSnapshot collect() { return CounterSnapshot.builder().name("counter.a").build(); } @@ -45,7 +45,7 @@ public String getPrometheusName() { Collector counterB = new Collector() { @Override - public MetricSnapshot collect() { + public MetricSnapshot collect() { return CounterSnapshot.builder().name("counter_b").build(); } @@ -58,7 +58,7 @@ public String getPrometheusName() { Collector gaugeA = new Collector() { @Override - public MetricSnapshot collect() { + public MetricSnapshot collect() { return GaugeSnapshot.builder().name("gauge_a").build(); } diff --git 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 index fc0c24bfc..25cd6a4ec 100644 --- 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 @@ -34,7 +34,7 @@ public void testDuplicateLabels() { @Test public void testNoData() { - MetricSnapshot snapshot = CounterSnapshot.builder().name("test").build(); + MetricSnapshot snapshot = CounterSnapshot.builder().name("test").build(); assertThat(snapshot.getDataPoints().size()).isEqualTo(0); } diff --git 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 index ed2f66fec..09d63d4d9 100644 --- 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 @@ -92,7 +92,7 @@ public void testImmutable() { .dataPoint(CounterSnapshot.CounterDataPointSnapshot.builder().value(1.0).build()) .build(); MetricSnapshots snapshots = new MetricSnapshots(c2, c3, c1); - Iterator iterator = snapshots.iterator(); + Iterator> iterator = snapshots.iterator(); iterator.next(); assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(iterator::remove); } diff --git a/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/SnapshotTestUtil.java b/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/SnapshotTestUtil.java index d8e8d33e4..a69bba499 100644 --- a/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/SnapshotTestUtil.java +++ b/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/SnapshotTestUtil.java @@ -5,7 +5,7 @@ class SnapshotTestUtil { public static void assertMetadata( - MetricSnapshot snapshot, String name, String help, String unit) { + MetricSnapshot snapshot, String name, String help, String unit) { assertThat(snapshot.getMetadata().getName()).isEqualTo(name); assertThat(snapshot.getMetadata().getHelp()).isEqualTo(help); if (unit != null) { diff --git 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 index f1b592eaa..e68ebd968 100644 --- 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 @@ -101,7 +101,7 @@ private MetricSnapshots convert(Enumeration sampl return result.build(); } - private MetricSnapshot convertCounter(Collector.MetricFamilySamples samples) { + private MetricSnapshot convertCounter(Collector.MetricFamilySamples samples) { CounterSnapshot.Builder counter = CounterSnapshot.builder() .name(sanitizeMetricName(samples.name)) @@ -128,7 +128,7 @@ private MetricSnapshot convertCounter(Collector.MetricFamilySamples samples) { return counter.build(); } - private MetricSnapshot convertGauge(Collector.MetricFamilySamples samples) { + private MetricSnapshot convertGauge(Collector.MetricFamilySamples samples) { GaugeSnapshot.Builder gauge = GaugeSnapshot.builder() .name(sanitizeMetricName(samples.name)) @@ -148,7 +148,7 @@ private MetricSnapshot convertGauge(Collector.MetricFamilySamples samples) { return gauge.build(); } - private MetricSnapshot convertHistogram( + private MetricSnapshot convertHistogram( Collector.MetricFamilySamples samples, boolean isGaugeHistogram) { HistogramSnapshot.Builder histogram = HistogramSnapshot.builder() @@ -192,7 +192,7 @@ private MetricSnapshot convertHistogram( return histogram.build(); } - private MetricSnapshot convertSummary(Collector.MetricFamilySamples samples) { + private MetricSnapshot convertSummary(Collector.MetricFamilySamples samples) { SummarySnapshot.Builder summary = SummarySnapshot.builder() .name(sanitizeMetricName(samples.name)) @@ -242,7 +242,7 @@ private MetricSnapshot convertSummary(Collector.MetricFamilySamples samples) { return summary.build(); } - private MetricSnapshot convertStateSet(Collector.MetricFamilySamples samples) { + private MetricSnapshot convertStateSet(Collector.MetricFamilySamples samples) { StateSetSnapshot.Builder stateSet = StateSetSnapshot.builder().name(sanitizeMetricName(samples.name)).help(samples.help); Map dataPoints = new HashMap<>(); @@ -271,7 +271,7 @@ private MetricSnapshot convertStateSet(Collector.MetricFamilySamples samples) { return stateSet.build(); } - private MetricSnapshot convertUnknown(Collector.MetricFamilySamples samples) { + private MetricSnapshot convertUnknown(Collector.MetricFamilySamples samples) { UnknownSnapshot.Builder unknown = UnknownSnapshot.builder() .name(sanitizeMetricName(samples.name)) @@ -353,7 +353,7 @@ private Labels labelsWithout( return labels.build(); } - private MetricSnapshot convertInfo(Collector.MetricFamilySamples samples) { + private MetricSnapshot convertInfo(Collector.MetricFamilySamples samples) { InfoSnapshot.Builder info = InfoSnapshot.builder().name(sanitizeMetricName(samples.name)).help(samples.help); for (Collector.MetricFamilySamples.Sample sample : samples.samples) {