From 5ee05dc6ca33276846f9f162a901bb87a85b6d0f Mon Sep 17 00:00:00 2001
From: TROMEL LOUIS <58844429+Ash84@users.noreply.github.com>
Date: Thu, 13 Jun 2024 12:28:26 +0200
Subject: [PATCH 1/2] resource leak fixed

---
 .../java/fr/agroclim/sava/core/MetricsBasicAuthServlet.java  | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

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 090f283..f0374e8 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
@@ -68,9 +68,10 @@ public class MetricsBasicAuthServlet extends MetricsServlet {
                 "The total amount of memory in the Java virtual machine, measured in bytes");
         SavaUtils.addGauge("jvm_free_memory",
                 "The amount of free memory in the Java Virtual Machine, measured in bytes");
-        try {
+        try (BufferedReader br = new BufferedReader(new InputStreamReader(
+                new ProcessBuilder("lsb_release", "-ds").start().getInputStream()))) {
             SavaUtils.addCounter("host_distribution", "Host machine distribution (linux only)", "", "version");
-            SavaUtils.incrementCounter("host_distribution", new BufferedReader(new InputStreamReader(new ProcessBuilder("lsb_release", "-ds").start().getInputStream())).readLine());
+            SavaUtils.incrementCounter("host_distribution", br.readLine());
         } catch (final Exception e) {
             LOGGER.trace("Could not init host_distribution.");
         }
-- 
GitLab


From a2fa43dc076a9a15469f68e303b3ea6fb6f5bd91 Mon Sep 17 00:00:00 2001
From: TROMEL LOUIS <58844429+Ash84@users.noreply.github.com>
Date: Thu, 20 Jun 2024 14:38:58 +0200
Subject: [PATCH 2/2] increment counter with custom amount

---
 .../java/fr/agroclim/sava/core/SavaUtils.java | 23 +++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

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 b979e33..9ac84b4 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
@@ -231,12 +231,31 @@ public class SavaUtils {
      * @param label      Label associated to the measure. Optional.
      */
     public static void incrementCounter(@NonNull final String metricName, final String label) {
+        incrementCounter(metricName, label, null);
+    }
+
+    /**
+     * Increments a {@link Counter} value, if metric's name is registered.
+     *
+     * @param metricName Set the name of the metric. Required.
+     * @param label      Label associated to the measure. Optional.
+     * @param amount     Increment by a custom amount. Optional.
+     */
+    public static void incrementCounter(@NonNull final String metricName, final String label, final Double amount) {
         if (METRICS_MAP.containsKey(metricName) && METRICS_MAP.get(metricName) instanceof Counter) {
             final Counter counter = (Counter) METRICS_MAP.get(metricName);
             if (label != null) {
-                counter.labels(label).inc();
+                if (amount != null) {
+                    counter.labels(label).inc(amount);
+                } else {
+                    counter.labels(label).inc();
+                }
             } else {
-                counter.inc();
+                if (amount != null) {
+                    counter.inc(amount);
+                } else {
+                    counter.inc();
+                }
             }
         } else {
             LOGGER.warn("incrementCounter() was not able to increment the Counter: " + metricName);
-- 
GitLab