Browse Source

ZOOKEEPER-3474: Enable BookKeeper checkstyle configuration on zookeeper-prometheus-metrics

Subtasks of ZOOKEEPER-3431. Enable BookKeeper checkstyle configuration on zookeeper-promethus-metrics.

Author: tison <wander4096@gmail.com>

Reviewers: Enrico Olivelli <eolivelli@apache.org>, Norbert Kalmar <nkalmar@apache.org>

Closes #1028 from TisonKun/ZOOKEEPER-3474
tison 5 years ago
parent
commit
49354f581f

+ 1 - 1
pom.xml

@@ -574,7 +574,7 @@
         <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-checkstyle-plugin</artifactId>
-          <version>3.0.0</version>
+          <version>3.1.0</version>
           <dependencies>
             <dependency>
               <groupId>com.puppycrawl.tools</groupId>

+ 1 - 1
zookeeper-jute/pom.xml

@@ -157,7 +157,7 @@
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-checkstyle-plugin</artifactId>
-        <version>3.0.0</version>
+        <version>3.1.0</version>
         <dependencies>
           <dependency>
             <groupId>com.puppycrawl.tools</groupId>

+ 37 - 0
zookeeper-metrics-providers/pom.xml

@@ -37,4 +37,41 @@
     <module>zookeeper-prometheus-metrics</module>
   </modules>
 
+  <build>
+    <pluginManagement>
+      <plugins>
+        <plugin>
+          <groupId>org.apache.maven.plugins</groupId>
+          <artifactId>maven-checkstyle-plugin</artifactId>
+          <version>3.1.0</version>
+          <dependencies>
+            <dependency>
+              <groupId>com.puppycrawl.tools</groupId>
+              <artifactId>checkstyle</artifactId>
+              <version>${checkstyle.version}</version>
+            </dependency>
+          </dependencies>
+          <configuration>
+            <configLocation>checkstyle-strict.xml</configLocation>
+            <suppressionsLocation>checkstyleSuppressions.xml</suppressionsLocation>
+            <encoding>UTF-8</encoding>
+            <consoleOutput>true</consoleOutput>
+            <failOnViolation>true</failOnViolation>
+            <includeResources>false</includeResources>
+            <includeTestSourceDirectory>true</includeTestSourceDirectory>
+          </configuration>
+          <executions>
+            <execution>
+              <id>checkstyle</id>
+              <phase>validate</phase>
+              <goals>
+                <goal>check</goal>
+              </goals>
+            </execution>
+          </executions>
+        </plugin>
+      </plugins>
+    </pluginManagement>
+  </build>
+
 </project>

+ 10 - 13
zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProvider.java

@@ -1,4 +1,4 @@
-/**
+/*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
  * distributed with this work for additional information
@@ -15,6 +15,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
 package org.apache.zookeeper.metrics.prometheus;
 
 import io.prometheus.client.Collector;
@@ -29,7 +30,6 @@ import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.function.BiConsumer;
 import javax.servlet.ServletException;
-import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import org.apache.zookeeper.metrics.Counter;
@@ -60,7 +60,7 @@ public class PrometheusMetricsProvider implements MetricsProvider {
      * <p>
      * When you are running ZooKeeper (server or client) together with other
      * libraries every metrics will be expected as a single view.
-     *
+     * </p>
      */
     private final CollectorRegistry collectorRegistry = CollectorRegistry.defaultRegistry;
     private int port = 7000;
@@ -133,6 +133,7 @@ public class PrometheusMetricsProvider implements MetricsProvider {
      * This method is not expected to be used to serve metrics to Prometheus. We
      * are using the MetricsServlet provided by Prometheus for that, leaving the
      * real representation to the Prometheus Java client.
+     * </p>
      *
      * @param sink the receiver of data (4lw interface, Admin server or tests)
      */
@@ -199,25 +200,21 @@ public class PrometheusMetricsProvider implements MetricsProvider {
 
         @Override
         public Counter getCounter(String name) {
-            return counters.computeIfAbsent(name, (n) -> {
-                return new PrometheusCounter(n);
-            });
+            return counters.computeIfAbsent(name, PrometheusCounter::new);
         }
 
         /**
          * Gauges may go up and down, in ZooKeeper they are a way to export
          * internal values with a callback.
          *
-         * @param name the name of the gauge
+         * @param name  the name of the gauge
          * @param gauge the callback
          */
         @Override
         public void registerGauge(String name, Gauge gauge) {
             Objects.requireNonNull(name);
-            gauges.compute(name, (id, prev) -> {
-                return new PrometheusGaugeWrapper(id, gauge,
-                        prev != null ? prev.inner : null);
-            });
+            gauges.compute(name, (id, prev) ->
+                    new PrometheusGaugeWrapper(id, gauge, prev != null ? prev.inner : null));
         }
 
         @Override
@@ -311,8 +308,8 @@ public class PrometheusMetricsProvider implements MetricsProvider {
             this.gauge = gauge;
             this.inner = prev != null ? prev
                     : io.prometheus.client.Gauge
-                            .build(name, name)
-                            .register(collectorRegistry);
+                    .build(name, name)
+                    .register(collectorRegistry);
         }
 
         /**

+ 3 - 2
zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/test/java/org/apache/zookeeper/metrics/prometheus/ExportJvmInfoTest.java

@@ -1,4 +1,4 @@
-/**
+/*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
  * distributed with this work for additional information
@@ -15,11 +15,12 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
 package org.apache.zookeeper.metrics.prometheus;
 
+import static org.junit.Assert.assertEquals;
 import io.prometheus.client.CollectorRegistry;
 import java.util.Properties;
-import static org.junit.Assert.assertEquals;
 import org.junit.Test;
 
 /**

+ 10 - 9
zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/test/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProviderTest.java

@@ -1,4 +1,4 @@
-/**
+/*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
  * distributed with this work for additional information
@@ -15,8 +15,17 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
 package org.apache.zookeeper.metrics.prometheus;
 
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 import io.prometheus.client.CollectorRegistry;
 import java.io.IOException;
 import java.io.PrintWriter;
@@ -30,17 +39,9 @@ import org.apache.zookeeper.metrics.Gauge;
 import org.apache.zookeeper.metrics.MetricsContext;
 import org.apache.zookeeper.metrics.Summary;
 import org.hamcrest.CoreMatchers;
-import static org.hamcrest.CoreMatchers.containsString;
 import org.junit.After;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertSame;
-import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
 import org.junit.Before;
 import org.junit.Test;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
 
 /**
  * Tests about Prometheus Metrics Provider. Please note that we are not testing

+ 2 - 2
zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/test/resources/log4j.properties

@@ -1,5 +1,5 @@
 # Copyright 2012 The Apache Software Foundation
-# 
+#
 # Licensed to the Apache Software Foundation (ASF) under one
 # or more contributor license agreements.  See the NOTICE file
 # distributed with this work for additional information
@@ -34,7 +34,7 @@ log4j.rootLogger=${zookeeper.root.logger}
 
 #
 # console
-# Add "console" to rootlogger above if you want to use this 
+# Add "console" to rootlogger above if you want to use this
 #
 log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
 log4j.appender.CONSOLE.Threshold=${zookeeper.console.threshold}