From 2b5ea261aef316ffcd710a79d8de0dd6743a8385 Mon Sep 17 00:00:00 2001 From: Olivier Maury <Olivier.Maury@inrae.fr> Date: Mon, 3 Feb 2025 11:55:08 +0100 Subject: [PATCH] style: checkstyle pmd --- .../sava/core/MetricsBasicAuthServlet.java | 15 ++++---- .../agroclim/sava/core/MetricsScheduler.java | 3 +- .../fr/agroclim/sava/core/SavaFilter.java | 36 +++++++++++-------- .../java/fr/agroclim/sava/core/SavaUtils.java | 32 +---------------- .../sava/core/MetricsBasicAuthServlet.java | 12 +++---- .../agroclim/sava/core/MetricsScheduler.java | 3 +- .../fr/agroclim/sava/core/SavaFilter.java | 36 +++++++++++-------- .../java/fr/agroclim/sava/core/SavaUtils.java | 30 ---------------- 8 files changed, 60 insertions(+), 107 deletions(-) diff --git a/sava-core-jakarta/src/main/java/fr/agroclim/sava/core/MetricsBasicAuthServlet.java b/sava-core-jakarta/src/main/java/fr/agroclim/sava/core/MetricsBasicAuthServlet.java index 3f07a9e..35df181 100644 --- a/sava-core-jakarta/src/main/java/fr/agroclim/sava/core/MetricsBasicAuthServlet.java +++ b/sava-core-jakarta/src/main/java/fr/agroclim/sava/core/MetricsBasicAuthServlet.java @@ -7,14 +7,12 @@ import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; import java.util.Base64; +import io.prometheus.client.servlet.jakarta.exporter.MetricsServlet; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; - -import org.apache.logging.log4j.Level; - -import io.prometheus.client.servlet.jakarta.exporter.MetricsServlet; import lombok.extern.log4j.Log4j2; +import org.apache.logging.log4j.Level; /** * SAVA metrics servlet, using basic_auth with context parameters. @@ -41,9 +39,7 @@ public class MetricsBasicAuthServlet extends MetricsServlet { /** * Initialize values to key and pass, coming from context init parameters. - * @exception ServletException if an exception occurs that - * interrupts the servlet's - * normal operation + * @exception ServletException if an exception occurs that interrupts the servlet's normal operation */ @Override public void init() throws ServletException { @@ -115,10 +111,13 @@ public class MetricsBasicAuthServlet extends MetricsServlet { SavaUtils.setGaugeValue("jvm_free_memory", (double) runtime.freeMemory()); // continue with the servlet super.doGet(req, resp); + return; } - } catch (final Exception e) { + } catch (final IOException e) { LOGGER.warn("Received a bad request"); LOGGER.catching(Level.WARN, e); + resp.sendError(HttpServletResponse.SC_BAD_REQUEST); + return; } } resp.sendError(HttpServletResponse.SC_FORBIDDEN); diff --git a/sava-core-jakarta/src/main/java/fr/agroclim/sava/core/MetricsScheduler.java b/sava-core-jakarta/src/main/java/fr/agroclim/sava/core/MetricsScheduler.java index 78c5e0c..a742362 100644 --- a/sava-core-jakarta/src/main/java/fr/agroclim/sava/core/MetricsScheduler.java +++ b/sava-core-jakarta/src/main/java/fr/agroclim/sava/core/MetricsScheduler.java @@ -56,7 +56,8 @@ public class MetricsScheduler { * @param minute the minute of schedule * @param second the second of schedule */ - protected static void scheduleEveryDayAt(final Runnable runnable, final int hour, final int minute, final int second) { + protected static void scheduleEveryDayAt(final Runnable runnable, final int hour, final int minute, + final int second) { final long initDelay = initialDelay(hour, minute, second); SCHEDULER.scheduleAtFixedRate(runnable, initDelay, DAY_IN_SECONDS, TimeUnit.SECONDS); } diff --git a/sava-core-jakarta/src/main/java/fr/agroclim/sava/core/SavaFilter.java b/sava-core-jakarta/src/main/java/fr/agroclim/sava/core/SavaFilter.java index 77dbfc2..ccde254 100644 --- a/sava-core-jakarta/src/main/java/fr/agroclim/sava/core/SavaFilter.java +++ b/sava-core-jakarta/src/main/java/fr/agroclim/sava/core/SavaFilter.java @@ -2,6 +2,7 @@ package fr.agroclim.sava.core; import java.io.IOException; +import io.prometheus.client.Histogram; import jakarta.servlet.Filter; import jakarta.servlet.FilterChain; import jakarta.servlet.FilterConfig; @@ -11,8 +12,6 @@ import jakarta.servlet.ServletResponse; import jakarta.servlet.annotation.WebFilter; import jakarta.servlet.annotation.WebInitParam; import jakarta.servlet.http.HttpServletRequest; - -import io.prometheus.client.Histogram; import lombok.extern.log4j.Log4j2; /** @@ -29,13 +28,14 @@ public class SavaFilter implements Filter { */ private Histogram histogram; - private PathPrecision pathPrecision = PathPrecision.MAX; - /** - * Nothing to do. + * Path precision. */ + private PathPrecision pathPrecision = PathPrecision.MAX; + @Override public void destroy() { + // Nothing to do. } /** @@ -94,18 +94,26 @@ public class SavaFilter implements Filter { "method"); } + /** + * Precision of stored path. + */ private enum PathPrecision { - MIN("min"), MAX("max"); + /** + * To get servlet path. + */ + MIN, + /** + * To get request URI, without servlet context. + */ + MAX; - private final String value; - - PathPrecision(final String precision) { - value = precision; + /** + * @return String value. + */ + private String getValue() { + return name().toLowerCase(); } - public final String getValue() { - return value; - } } -} \ No newline at end of file +} diff --git a/sava-core-jakarta/src/main/java/fr/agroclim/sava/core/SavaUtils.java b/sava-core-jakarta/src/main/java/fr/agroclim/sava/core/SavaUtils.java index 9ac84b4..b74ef3a 100644 --- a/sava-core-jakarta/src/main/java/fr/agroclim/sava/core/SavaUtils.java +++ b/sava-core-jakarta/src/main/java/fr/agroclim/sava/core/SavaUtils.java @@ -149,7 +149,7 @@ public class SavaUtils { } catch (final Exception e) { LOGGER.warn("Gauge value MUST be a double. Otherwise getMetric.call() has failed." + " Please read stackstrace :"); - e.printStackTrace(); + LOGGER.catching(e); } }; @@ -313,36 +313,6 @@ public class SavaUtils { return histogram; } - /** - * TODO this method is not working, it's difficult to get the real metric to - * unregister it, as the scope of this method is to override automatic Histogram - * registration. - * This method will force add an {@link Histogram} to registered - * metrics. This will unregister every histogram with the same metricName. - * - * @param metricName Set the name of the metric. Required. - * @param metricDescription Set the help string of the metric. Required. - * @param buckets Set the upper bounds of buckets for the histogram. - * The default buckets are intended to cover a typical - * web/rpc request from milliseconds to seconds. They - * can be overridden by setting values to this - * parameter. - * @param labelNames Label(s) of the metric. Optional. (ex: method) - * @return created Histogram. - */ - // public static Histogram forceAddHistogram(@NonNull final String metricName, @NonNull final String metricDescription, - // final double[] buckets, final String... labelNames) { - // final Set<String> metricNameSet = new HashSet<String>(); - // metricNameSet.add(metricName + "_count"); - // metricNameSet.add(metricName + "_sum"); - // metricNameSet.add(metricName + "_bucket"); - // final Enumeration<MetricFamilySamples> toBeRemoved = - // CollectorRegistry.defaultRegistry.filteredMetricFamilySamples(metricNameSet); - // toBeRemoved.asIterator().forEachRemaining( - // o -> CollectorRegistry.defaultRegistry.unregister(Histogram.build(o.name, o.help).create())); - // return addHistogram(metricName, metricDescription, buckets, labelNames); - // } - /** * Time a runnable within the named {@link Histogram}. * diff --git a/sava-core/src/main/java/fr/agroclim/sava/core/MetricsBasicAuthServlet.java b/sava-core/src/main/java/fr/agroclim/sava/core/MetricsBasicAuthServlet.java index 4703060..5672962 100644 --- a/sava-core/src/main/java/fr/agroclim/sava/core/MetricsBasicAuthServlet.java +++ b/sava-core/src/main/java/fr/agroclim/sava/core/MetricsBasicAuthServlet.java @@ -7,14 +7,12 @@ import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; import java.util.Base64; +import io.prometheus.client.exporter.MetricsServlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; - -import org.apache.logging.log4j.Level; - -import io.prometheus.client.exporter.MetricsServlet; import lombok.extern.log4j.Log4j2; +import org.apache.logging.log4j.Level; /** * SAVA metrics servlet, using basic_auth with context parameters. @@ -41,9 +39,7 @@ public class MetricsBasicAuthServlet extends MetricsServlet { /** * Initialize values to key and pass, coming from context init parameters. - * @exception ServletException if an exception occurs that - * interrupts the servlet's - * normal operation + * @exception ServletException if an exception occurs that interrupts the servlet's normal operation */ @Override public void init() throws ServletException { @@ -117,7 +113,7 @@ public class MetricsBasicAuthServlet extends MetricsServlet { super.doGet(req, resp); return; } - } catch (final Exception e) { + } catch (final IOException e) { LOGGER.warn("Received a bad request"); LOGGER.catching(Level.WARN, e); resp.sendError(HttpServletResponse.SC_BAD_REQUEST); diff --git a/sava-core/src/main/java/fr/agroclim/sava/core/MetricsScheduler.java b/sava-core/src/main/java/fr/agroclim/sava/core/MetricsScheduler.java index 78c5e0c..a742362 100644 --- a/sava-core/src/main/java/fr/agroclim/sava/core/MetricsScheduler.java +++ b/sava-core/src/main/java/fr/agroclim/sava/core/MetricsScheduler.java @@ -56,7 +56,8 @@ public class MetricsScheduler { * @param minute the minute of schedule * @param second the second of schedule */ - protected static void scheduleEveryDayAt(final Runnable runnable, final int hour, final int minute, final int second) { + protected static void scheduleEveryDayAt(final Runnable runnable, final int hour, final int minute, + final int second) { final long initDelay = initialDelay(hour, minute, second); SCHEDULER.scheduleAtFixedRate(runnable, initDelay, DAY_IN_SECONDS, TimeUnit.SECONDS); } diff --git a/sava-core/src/main/java/fr/agroclim/sava/core/SavaFilter.java b/sava-core/src/main/java/fr/agroclim/sava/core/SavaFilter.java index 4b5cb35..f931104 100644 --- a/sava-core/src/main/java/fr/agroclim/sava/core/SavaFilter.java +++ b/sava-core/src/main/java/fr/agroclim/sava/core/SavaFilter.java @@ -2,6 +2,7 @@ package fr.agroclim.sava.core; import java.io.IOException; +import io.prometheus.client.Histogram; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; @@ -11,8 +12,6 @@ import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; import javax.servlet.annotation.WebInitParam; import javax.servlet.http.HttpServletRequest; - -import io.prometheus.client.Histogram; import lombok.extern.log4j.Log4j2; /** @@ -29,13 +28,14 @@ public class SavaFilter implements Filter { */ private Histogram histogram; - private PathPrecision pathPrecision = PathPrecision.MAX; - /** - * Nothing to do. + * Path precision. */ + private PathPrecision pathPrecision = PathPrecision.MAX; + @Override public void destroy() { + // Nothing to do. } /** @@ -94,18 +94,26 @@ public class SavaFilter implements Filter { "method"); } + /** + * Precision of stored path. + */ private enum PathPrecision { - MIN("min"), MAX("max"); + /** + * To get servlet path. + */ + MIN, + /** + * To get request URI, without servlet context. + */ + MAX; - private final String value; - - PathPrecision(final String precision) { - value = precision; + /** + * @return String value. + */ + private String getValue() { + return name().toLowerCase(); } - public final String getValue() { - return value; - } } -} \ No newline at end of file +} diff --git a/sava-core/src/main/java/fr/agroclim/sava/core/SavaUtils.java b/sava-core/src/main/java/fr/agroclim/sava/core/SavaUtils.java index 14968ff..b74ef3a 100644 --- a/sava-core/src/main/java/fr/agroclim/sava/core/SavaUtils.java +++ b/sava-core/src/main/java/fr/agroclim/sava/core/SavaUtils.java @@ -313,36 +313,6 @@ public class SavaUtils { return histogram; } - /** - * TODO this method is not working, it's difficult to get the real metric to - * unregister it, as the scope of this method is to override automatic Histogram - * registration. - * This method will force add an {@link Histogram} to registered - * metrics. This will unregister every histogram with the same metricName. - * - * @param metricName Set the name of the metric. Required. - * @param metricDescription Set the help string of the metric. Required. - * @param buckets Set the upper bounds of buckets for the histogram. - * The default buckets are intended to cover a typical - * web/rpc request from milliseconds to seconds. They - * can be overridden by setting values to this - * parameter. - * @param labelNames Label(s) of the metric. Optional. (ex: method) - * @return created Histogram. - */ - // public static Histogram forceAddHistogram(@NonNull final String metricName, @NonNull final String metricDescription, - // final double[] buckets, final String... labelNames) { - // final Set<String> metricNameSet = new HashSet<String>(); - // metricNameSet.add(metricName + "_count"); - // metricNameSet.add(metricName + "_sum"); - // metricNameSet.add(metricName + "_bucket"); - // final Enumeration<MetricFamilySamples> toBeRemoved = - // CollectorRegistry.defaultRegistry.filteredMetricFamilySamples(metricNameSet); - // toBeRemoved.asIterator().forEachRemaining( - // o -> CollectorRegistry.defaultRegistry.unregister(Histogram.build(o.name, o.help).create())); - // return addHistogram(metricName, metricDescription, buckets, labelNames); - // } - /** * Time a runnable within the named {@link Histogram}. * -- GitLab