Skip to content

Commit

Permalink
fix all warnings (prometheus#1138)
Browse files Browse the repository at this point in the history
* fix warnings

Signed-off-by: Gregor Zeitlinger <[email protected]>

* fix warnings

Signed-off-by: Gregor Zeitlinger <[email protected]>

* fix warnings

Signed-off-by: Gregor Zeitlinger <[email protected]>

* fix warnings

Signed-off-by: Gregor Zeitlinger <[email protected]>

---------

Signed-off-by: Gregor Zeitlinger <[email protected]>
  • Loading branch information
zeitlinger authored Oct 10, 2024
1 parent 68849a8 commit eb48f5d
Show file tree
Hide file tree
Showing 48 changed files with 220 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<release>17</release>
<source>17</source>
<target>17</target>
<showWarnings>true</showWarnings>
<compilerArgs>
<arg>-Xlint:all</arg>
<arg>-Werror</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<release>17</release>
<source>17</source>
<target>17</target>
<showWarnings>true</showWarnings>
<compilerArgs>
<arg>-Xlint:all</arg>
<arg>-Werror</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,10 @@ protected MetricSnapshots collectMetricSnapshots(PrometheusScrapeRequest scrapeR
gaugeBuilder.dataPoint(gaugeDataPointBuilder.build());
}
}
Collection<MetricSnapshot> snaps = new ArrayList<MetricSnapshot>();
Collection<MetricSnapshot<?>> snaps = new ArrayList<>();
snaps.add(counterBuilder.build());
snaps.add(gaugeBuilder.build());
MetricSnapshots msnaps = new MetricSnapshots(snaps);
return msnaps;
return new MetricSnapshots(snaps);
}

public List<String> getPrometheusNames() {
Expand Down
13 changes: 13 additions & 0 deletions examples/example-exporter-servlet-tomcat/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<release>17</release>
<source>17</source>
<target>17</target>
<showWarnings>true</showWarnings>
<compilerArgs>
<arg>-Xlint:all</arg>
<arg>-Werror</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@
<build>
<finalName>exporter-servlet-tomcat-sample</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<release>17</release>
<source>17</source>
<target>17</target>
<showWarnings>true</showWarnings>
<compilerArgs>
<arg>-Xlint:all</arg>
<arg>-Werror</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
Expand Down
12 changes: 9 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<version>3.13.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
Expand Down Expand Up @@ -270,8 +270,14 @@
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<release>8</release>
<source>8</source>
<target>8</target>
<showWarnings>true</showWarnings>
<compilerArgs>
<arg>-Xlint:all</arg>
<arg>-Werror</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

public class PrometheusPropertiesException extends RuntimeException {

private static final long serialVersionUID = 0L;

public PrometheusPropertiesException(String msg) {
super(msg);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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".</i>
*/
@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> T time(Supplier<T> 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> T timeChecked(Callable<T> func) throws Exception {
try (Timer timer = startTimer()) {
try (Timer ignored = startTimer()) {
return func.call();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected Metric(Builder<?, ?> builder) {
}

@Override
public abstract MetricSnapshot collect();
public abstract MetricSnapshot<?> collect();

protected abstract static class Builder<B extends Builder<B, M>, M extends Metric> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class SlidingWindow<T> {
* @param maxAgeSeconds after this amount of time an instance of T gets evicted.
* @param ageBuckets number of age buckets.
*/
@SuppressWarnings("unchecked")
public SlidingWindow(
Class<T> clazz,
Supplier<T> constructor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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> labels, List<T> metricData);
protected abstract MetricSnapshot<?> collect(List<Labels> labels, List<T> 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();
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public Collection<MetricData> 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) {
Expand All @@ -78,10 +78,10 @@ public Collection<MetricData> 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++) {
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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);
}
}
Expand Down
21 changes: 20 additions & 1 deletion prometheus-metrics-exporter-servlet-jakarta/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand Down Expand Up @@ -34,4 +35,22 @@
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<release>17</release>
<source>17</source>
<target>17</target>
<showWarnings>true</showWarnings>
<compilerArgs>
<arg>-Xlint:all</arg>
<arg>-Werror</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
public class PrometheusMetricsServlet extends HttpServlet {

private static final long serialVersionUID = 0L;

private final PrometheusScrapeHandler handler;

public PrometheusMetricsServlet() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
Expand All @@ -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()) {
Expand Down
Loading

0 comments on commit eb48f5d

Please sign in to comment.