Skip to content

Commit

Permalink
add Checkstyle (#1199)
Browse files Browse the repository at this point in the history
* add checkstyle

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

* add checkstyle

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

* add checkstyle

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

* add checkstyle

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

* add checkstyle

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

* add checkstyle

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

* add checkstyle

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

* add checkstyle

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

* add checkstyle

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

* add checkstyle

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

* add checkstyle

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

* add checkstyle

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

* add checkstyle

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

---------

Signed-off-by: Gregor Zeitlinger <[email protected]>
  • Loading branch information
zeitlinger authored Nov 12, 2024
1 parent d6572b7 commit c93e48c
Show file tree
Hide file tree
Showing 51 changed files with 764 additions and 238 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*]
max_line_length = 100
indent_size = 2

[pom.xml]
indent_size = 4
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ If you're getting errors when running tests:

- Use `-Dspotless.check.skip=true` to skip the formatting check during development.
- Use `-Dcoverage.skip=true` to skip the coverage check during development.
- Use `-Dcheckstyle.skip=true` to skip the checkstyle check during development.
- Use `-Dwarnings=-nowarn` to skip the warnings during development.

## Updating the Protobuf Java Classes
Expand Down
9 changes: 9 additions & 0 deletions checkstyle-suppressions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0"?>

<!DOCTYPE suppressions PUBLIC
"-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
"https://checkstyle.org/dtds/suppressions_1_2.dtd">

<suppressions>
<suppress checks="Javadoc" files="."/>
</suppressions>
375 changes: 375 additions & 0 deletions checkstyle.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>io.prometheus.metrics.examples.otel_exemplars.greeting.Main</mainClass>
<mainClass>io.prometheus.metrics.examples.otel.exemplars.greeting.Main</mainClass>
</transformer>
</transformers>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.prometheus.metrics.examples.otel_exemplars.greeting;
package io.prometheus.metrics.examples.otel.exemplars.greeting;

import static io.prometheus.metrics.model.snapshots.Unit.nanosToSeconds;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.prometheus.metrics.examples.otel_exemplars.greeting;
package io.prometheus.metrics.examples.otel.exemplars.greeting;

import io.prometheus.metrics.exporter.servlet.jakarta.PrometheusMetricsServlet;
import io.prometheus.metrics.instrumentation.jvm.JvmMetrics;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>io.prometheus.metrics.examples.otel_exemplars.app.Main</mainClass>
<mainClass>io.prometheus.metrics.examples.otel.exemplars.app.Main</mainClass>
</transformer>
</transformers>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.prometheus.metrics.examples.otel_exemplars.app;
package io.prometheus.metrics.examples.otel.exemplars.app;

import static io.prometheus.metrics.model.snapshots.Unit.nanosToSeconds;
import static java.net.http.HttpResponse.BodyHandlers.ofString;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.prometheus.metrics.examples.otel_exemplars.app;
package io.prometheus.metrics.examples.otel.exemplars.app;

import io.prometheus.metrics.exporter.servlet.jakarta.PrometheusMetricsServlet;
import io.prometheus.metrics.instrumentation.jvm.JvmMetrics;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public static void main(String[] args) throws Exception {
histogram.labelValues("200").observe(random.nextGaussian());
}
// Explicitly use a classic-only histogram to have an example of a classic histogram in OpenTelemetry
// Explicitly use a classic-only histogram to have an example of a classic
// histogram in OpenTelemetry
Histogram classicHistogram = Histogram.newBuilder()
.withName("request_size_bytes")
.withHelp("Request size in Bytes")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ public static void main(String[] args) throws IOException, InterruptedException

int port = parsePortOrExit(args[0]);
Mode mode = parseModeOrExit(args[1]);
run(mode, port);
}

private static void run(Mode mode, int port) throws IOException, InterruptedException {
Counter counter =
Counter.builder()
.name("uptime_seconds_total")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ public static void main(String[] args) throws Exception {

int port = parsePortOrExit(args[0]);
Mode mode = parseModeOrExit(args[1]);
run(mode, port);
}

private static void run(Mode mode, int port) throws Exception {
Counter counter =
Counter.builder()
.name("uptime_seconds_total")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ public static void main(String[] args) throws LifecycleException, IOException {

int port = parsePortOrExit(args[0]);
Mode mode = parseModeOrExit(args[1]);
run(mode, port);
}

private static void run(Mode mode, int port) throws IOException, LifecycleException {
Counter counter =
Counter.builder()
.name("uptime_seconds_total")
Expand Down
23 changes: 23 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<otel.instrumentation.version>2.9.0-alpha</otel.instrumentation.version>
<java.version>8</java.version>
<jacoco.line-coverage>0.70</jacoco.line-coverage>
<checkstyle.skip>false</checkstyle.skip>
<coverage.skip>false</coverage.skip>
<warnings>-Werror</warnings>
</properties>
Expand Down Expand Up @@ -224,6 +225,28 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<consoleOutput>true</consoleOutput>
<configLocation>google_checks.xml</configLocation>
<configLocation>checkstyle.xml</configLocation>
<violationSeverity>warning</violationSeverity>
<failOnViolation>true</failOnViolation>
<skip>${checkstyle.skip}</skip>
<suppressionsLocation>checkstyle-suppressions.xml</suppressionsLocation>
<excludes>**/generated/**,**/jmh_generated/*</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ public interface StateSetDataPoint extends DataPoint {
*/
void setTrue(String state);

/**
* {@code state} must be one of the states from when the {@code StateSet} was created with {@link
* io.prometheus.metrics.core.metrics.StateSet.Builder#states(String...)}.
*/
void setFalse(String state);

/**
* {@code state} must be one of the states from when the {@code StateSet} was created with {@link
* io.prometheus.metrics.core.metrics.StateSet.Builder#states(Class)}.
Expand All @@ -28,6 +22,12 @@ default void setTrue(Enum<?> state) {
setTrue(state.toString());
}

/**
* {@code state} must be one of the states from when the {@code StateSet} was created with {@link
* io.prometheus.metrics.core.metrics.StateSet.Builder#states(String...)}.
*/
void setFalse(String state);

/**
* {@code state} must be one of the states from when the {@code StateSet} was created with {@link
* io.prometheus.metrics.core.metrics.StateSet.Builder#states(Class)}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ private long doObserveSingleExemplar(double value) {
return 0;
}

private long doObserveSingleExemplar(double amount, Labels labels) {
long now = System.currentTimeMillis();
Exemplar current = customExemplars[0];
if (current == null
|| now - current.getTimestampMillis() > config.getMinRetentionPeriodMillis()) {
return updateCustomExemplar(0, amount, labels, now);
}
return 0;
}

private long doObserveWithUpperBounds(double value) {
long now = System.currentTimeMillis();
double[] upperBounds = config.getHistogramClassicUpperBounds();
Expand Down Expand Up @@ -212,16 +222,6 @@ private long doObserveWithExemplar(double amount, Labels labels) {
}
}

private long doObserveSingleExemplar(double amount, Labels labels) {
long now = System.currentTimeMillis();
Exemplar current = customExemplars[0];
if (current == null
|| now - current.getTimestampMillis() > config.getMinRetentionPeriodMillis()) {
return updateCustomExemplar(0, amount, labels, now);
}
return 0;
}

private long doObserveWithExemplarWithUpperBounds(double value, Labels labels) {
long now = System.currentTimeMillis();
double[] upperBounds = config.getHistogramClassicUpperBounds();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.util.LinkedList;
import java.util.ListIterator;

// CHECKSTYLE:OFF: checkstyle

/**
* Algorithm solving the "Targeted Quantile Problem" as described in "Effective Computation of
* Biased Quantiles over Data Streams" by Cormode, Korn, Muthukrishnan, and Srivastava.
Expand Down Expand Up @@ -273,3 +275,5 @@ public String toString() {
}
}
}

// CHECKSTYLE:ON: checkstyle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ protected Labels makeLabels(String... labelValues) {
if (labelValues == null) {
throw new IllegalArgumentException(
this.getClass().getSimpleName()
+ " was created with label names, but the callback was called without label values.");
+ " was created with label names, "
+ "but the callback was called without label values.");
}
if (labelValues.length != labelNames.length) {
throw new IllegalArgumentException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ public CounterSnapshot collect() {
return (CounterSnapshot) super.collect();
}

@Override
protected CounterSnapshot collect(List<Labels> labels, List<DataPoint> metricData) {
List<CounterSnapshot.CounterDataPointSnapshot> data = new ArrayList<>(labels.size());
for (int i = 0; i < labels.size(); i++) {
data.add(metricData.get(i).collect(labels.get(i)));
}
return new CounterSnapshot(getMetadata(), data);
}

@Override
protected boolean isExemplarsEnabled() {
return exemplarsEnabled;
Expand All @@ -96,15 +105,6 @@ protected DataPoint newDataPoint() {
}
}

@Override
protected CounterSnapshot collect(List<Labels> labels, List<DataPoint> metricData) {
List<CounterSnapshot.CounterDataPointSnapshot> data = new ArrayList<>(labels.size());
for (int i = 0; i < labels.size(); i++) {
data.add(metricData.get(i).collect(labels.get(i)));
}
return new CounterSnapshot(getMetadata(), data);
}

static String stripTotalSuffix(String name) {
if (name != null && (name.endsWith("_total") || name.endsWith(".total"))) {
name = name.substring(0, name.length() - 6);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
* CounterWithCallback.builder()
* .name("classes_loaded_total")
* .help("The total number of classes that have been loaded since the JVM has started execution")
* .help("The total number of classes since the JVM has started execution")
* .callback(callback -> callback.call(classLoadingMXBean.getLoadedClassCount()))
* .register();
* }</pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class Histogram extends StatefulMetric<DistributionDataPoint, Histogram.D
implements DistributionDataPoint {

// nativeSchema == CLASSIC_HISTOGRAM indicates that this is a classic histogram only.
private final int CLASSIC_HISTOGRAM = Integer.MIN_VALUE;
private static final int CLASSIC_HISTOGRAM = Integer.MIN_VALUE;

// NATIVE_BOUNDS is used to look up the native bucket index depending on the current schema.
private static final double[][] NATIVE_BOUNDS;
Expand Down Expand Up @@ -152,7 +152,7 @@ private Histogram(Histogram.Builder builder, PrometheusProperties prometheusProp
double min =
getConfigProperty(properties, MetricsProperties::getHistogramNativeMinZeroThreshold);
nativeMaxZeroThreshold =
max == builder.DEFAULT_NATIVE_MAX_ZERO_THRESHOLD && min > max ? min : max;
max == Builder.DEFAULT_NATIVE_MAX_ZERO_THRESHOLD && min > max ? min : max;
nativeMinZeroThreshold = Math.min(min, nativeMaxZeroThreshold);
nativeMaxBuckets =
getConfigProperty(properties, MetricsProperties::getHistogramNativeMaxNumberOfBuckets);
Expand Down Expand Up @@ -447,9 +447,9 @@ private void maybeScaleDown(AtomicBoolean wasReset) {
// Now we are in the synchronized block while new observations go into the buffer.
// Check again if we need to limit the bucket size, because another thread might
// have limited it in the meantime.
int nBuckets =
int numBuckets =
nativeBucketsForPositiveValues.size() + nativeBucketsForNegativeValues.size();
if (nBuckets <= nativeMaxBuckets || nativeSchema == -4) {
if (numBuckets <= nativeMaxBuckets || nativeSchema == -4) {
return null;
}
if (maybeReset()) {
Expand Down Expand Up @@ -668,11 +668,11 @@ public static class Builder extends StatefulMetric.Builder<Histogram.Builder, Hi
public static final double[] DEFAULT_CLASSIC_UPPER_BOUNDS =
new double[] {.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10};

private final double DEFAULT_NATIVE_MIN_ZERO_THRESHOLD = Math.pow(2.0, -128);
private final double DEFAULT_NATIVE_MAX_ZERO_THRESHOLD = Math.pow(2.0, -128);
private final int DEFAULT_NATIVE_INITIAL_SCHEMA = 5;
private final int DEFAULT_NATIVE_MAX_NUMBER_OF_BUCKETS = 160;
private final long DEFAULT_NATIVE_RESET_DURATION_SECONDS = 0; // 0 means no reset
private static final double DEFAULT_NATIVE_MIN_ZERO_THRESHOLD = Math.pow(2.0, -128);
private static final double DEFAULT_NATIVE_MAX_ZERO_THRESHOLD = Math.pow(2.0, -128);
private static final int DEFAULT_NATIVE_INITIAL_SCHEMA = 5;
private static final int DEFAULT_NATIVE_MAX_NUMBER_OF_BUCKETS = 160;
private static final long DEFAULT_NATIVE_RESET_DURATION_SECONDS = 0; // 0 means no reset

private Boolean nativeOnly;
private Boolean classicOnly;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,6 @@ public StateSetSnapshot collect() {
return (StateSetSnapshot) super.collect();
}

@Override
public void setTrue(String state) {
getNoLabels().setTrue(state);
}

@Override
public void setFalse(String state) {
getNoLabels().setFalse(state);
}

@Override
protected StateSetSnapshot collect(List<Labels> labels, List<DataPoint> metricDataList) {
List<StateSetSnapshot.StateSetDataPointSnapshot> data = new ArrayList<>(labels.size());
Expand All @@ -97,6 +87,16 @@ protected StateSetSnapshot collect(List<Labels> labels, List<DataPoint> metricDa
return new StateSetSnapshot(getMetadata(), data);
}

@Override
public void setTrue(String state) {
getNoLabels().setTrue(state);
}

@Override
public void setFalse(String state) {
getNoLabels().setFalse(state);
}

@Override
protected DataPoint newDataPoint() {
return new DataPoint();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ public D labelValues(String... labelValues) {
getClass().getSimpleName()
+ " "
+ getMetadata().getName()
+ " was created with label names, so you must call labelValues(...) when using it.");
+ " was created with label names, so you must call labelValues(...)"
+ " when using it.");
} else {
throw new IllegalArgumentException(
"Expected " + labelNames.length + " label values, but got " + labelValues.length + ".");
Expand Down
Loading

0 comments on commit c93e48c

Please sign in to comment.