From d3326736f3bde9a7182052fa320cfa70a5342a88 Mon Sep 17 00:00:00 2001 From: TROMEL LOUIS <58844429+Ash84@users.noreply.github.com> Date: Fri, 5 Jul 2024 11:51:11 +0200 Subject: [PATCH 1/3] v1.0.0 --- sava-core/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sava-core/pom.xml b/sava-core/pom.xml index 0b17110..fde17d4 100644 --- a/sava-core/pom.xml +++ b/sava-core/pom.xml @@ -5,7 +5,7 @@ <parent> <groupId>fr.inrae.agroclim</groupId> <artifactId>sava</artifactId> - <version>0.0.1-SNAPSHOT</version> + <version>1.0.0</version> </parent> <artifactId>sava-core</artifactId> <name>SAVA library</name> -- GitLab From 6857d8a35746e64103f02d9158369fdbfe6aab15 Mon Sep 17 00:00:00 2001 From: TROMEL LOUIS <58844429+Ash84@users.noreply.github.com> Date: Fri, 5 Jul 2024 12:01:23 +0200 Subject: [PATCH 2/3] v1.0 --- pom.xml | 17 +++- sava-core-jakarta/pom.xml | 10 +-- .../sava/core/MetricsBasicAuthServlet.java | 82 +++++++++++++------ .../java/fr/agroclim/sava/core/SavaUtils.java | 23 +++++- sava-core/pom.xml | 19 ----- sava-example/pom.xml | 2 +- 6 files changed, 99 insertions(+), 54 deletions(-) diff --git a/pom.xml b/pom.xml index 70101fc..ad77cc2 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ <modelVersion>4.0.0</modelVersion> <groupId>fr.inrae.agroclim</groupId> <artifactId>sava</artifactId> - <version>0.0.1-SNAPSHOT</version> + <version>1.0.0</version> <packaging>pom</packaging> <name>SAVA</name> <description>Suivi des Applications jaVa d'Agroclim</description> @@ -246,6 +246,21 @@ <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> </plugin> + + <!-- Attach source and javadoc artifacts --> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-source-plugin</artifactId> + <version>3.3.1</version> + <executions> + <execution> + <id>attach-sources</id> + <goals> + <goal>jar-no-fork</goal> + </goals> + </execution> + </executions> + </plugin> </plugins> <pluginManagement> diff --git a/sava-core-jakarta/pom.xml b/sava-core-jakarta/pom.xml index 4262e65..e9bd9da 100644 --- a/sava-core-jakarta/pom.xml +++ b/sava-core-jakarta/pom.xml @@ -5,11 +5,12 @@ <parent> <groupId>fr.inrae.agroclim</groupId> <artifactId>sava</artifactId> - <version>0.0.1-SNAPSHOT</version> + <version>1.0.0</version> </parent> <artifactId>sava-core-jakarta</artifactId> <name>SAVA library for Jakarta</name> <packaging>jar</packaging> + <properties> <client_java.version>0.16.0</client_java.version> <jaxrs.version>3.1.0</jaxrs.version> @@ -18,14 +19,13 @@ <servlet-api.version>5.0.0</servlet-api.version> <checkstyle.config.location>file://${basedir}/../config/sun_checks.xml</checkstyle.config.location> </properties> - <dependencies> + <dependencies> <!-- Log4J --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> </dependency> - <dependency> <groupId>jakarta.servlet</groupId> <artifactId>jakarta.servlet-api</artifactId> @@ -38,14 +38,12 @@ <version>${junit.version}</version> <scope>test</scope> </dependency> - <!-- Lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </dependency> - <!-- The client --> <dependency> <groupId>io.prometheus</groupId> @@ -68,13 +66,11 @@ <artifactId>simpleclient_servlet_jakarta</artifactId> <version>${client_java.version}</version> </dependency> - </dependencies> <reporting> <!-- Those plugins are used by `mvn site` --> <plugins> - <!-- Generates API JavaDoc. --> <!-- mvn javadoc:javadoc --> <plugin> 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 5dda781..3f07a9e 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 @@ -1,20 +1,27 @@ package fr.agroclim.sava.core; +import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.time.LocalDateTime; import java.util.Base64; 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 java.nio.charset.StandardCharsets; +import lombok.extern.log4j.Log4j2; /** * SAVA metrics servlet, using basic_auth with context parameters. * * @author ltromel */ +@Log4j2 public class MetricsBasicAuthServlet extends MetricsServlet { /** @@ -34,6 +41,9 @@ 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 */ @Override public void init() throws ServletException { @@ -60,46 +70,70 @@ 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 (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", br.readLine()); + } catch (final Exception e) { + LOGGER.trace("Could not init host_distribution."); + } + SavaUtils.addCounter("run_since", "Server run date", "", "instant"); + SavaUtils.incrementCounter("run_since", LocalDateTime.now().toString()); + } /** * doGet with simple authentification. + * + * @param req an {@link HttpServletRequest} object that contains the request the client has made of the servlet + * @param resp an {@link HttpServletResponse} object that contains the response the servlet sends to the client + * @exception IOException if an input or output error is detected when the servlet handles the GET request or if + * the request for the GET could not be handled */ @Override protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws IOException { // basic_auth associate to the request a header "Authorization" if (req.getHeader("Authorization") != null) { - // credentials are encoded in Base64, prefixed with "Basic " - // removing the prefix - final String trimmed = req.getHeader("Authorization").replace("Basic ", ""); - // decoding the sentence - final byte[] decodedBytes = Base64.getDecoder().decode(trimmed); - final String decoded = new String(decodedBytes, StandardCharsets.UTF_8); - // the credentials are given in the form username:password - // splitting the sentence - final String[] decodedSplitted = decoded.split(":"); - // making the checks - if (!key.equals(decodedSplitted[0]) - || !pass.equals(decodedSplitted[1])) { - resp.sendError(HttpServletResponse.SC_FORBIDDEN); - } else { - // update JVM values - final Runtime runtime = Runtime.getRuntime(); - SavaUtils.setGaugeValue("jvm_max_memory", (double) runtime.maxMemory()); - SavaUtils.setGaugeValue("jvm_used_memory", (double) (runtime.totalMemory() - runtime.freeMemory())); - SavaUtils.setGaugeValue("jvm_total_memory", (double) runtime.totalMemory()); - SavaUtils.setGaugeValue("jvm_free_memory", (double) runtime.freeMemory()); - // continue with the servlet - super.doGet(req, resp); + try { + // credentials are encoded in Base64, prefixed with "Basic " + // removing the prefix + final String trimmed = req.getHeader("Authorization").replace("Basic ", ""); + // decoding the sentence + final byte[] decodedBytes = Base64.getDecoder().decode(trimmed); + final String decoded = new String(decodedBytes, StandardCharsets.UTF_8); + // the credentials are given in the form username:password + // splitting the sentence + final String[] decodedSplitted = decoded.split(":"); + // making the checks + if (decodedSplitted.length == 2 && key.equals(decodedSplitted[0]) + && pass.equals(decodedSplitted[1])) { + // update JVM values + final Runtime runtime = Runtime.getRuntime(); + SavaUtils.setGaugeValue("jvm_max_memory", (double) runtime.maxMemory()); + SavaUtils.setGaugeValue("jvm_used_memory", (double) (runtime.totalMemory() - runtime.freeMemory())); + SavaUtils.setGaugeValue("jvm_total_memory", (double) runtime.totalMemory()); + SavaUtils.setGaugeValue("jvm_free_memory", (double) runtime.freeMemory()); + // continue with the servlet + super.doGet(req, resp); + } + } catch (final Exception e) { + LOGGER.warn("Received a bad request"); + LOGGER.catching(Level.WARN, e); } } + resp.sendError(HttpServletResponse.SC_FORBIDDEN); } /** * doPost with simple authentification. + * + * @param req an {@link HttpServletRequest} object that contains the request the client has made of the servlet + * @param resp an {@link HttpServletResponse} object that contains the response the servlet sends to the client + * @exception IOException if an input or output error is detected when the servlet handles the GET request or if + * the request for the GET could not be handled */ @Override protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws IOException { - // Do nothing + resp.sendError(HttpServletResponse.SC_FORBIDDEN); } } 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 b979e33..9ac84b4 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 @@ -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); diff --git a/sava-core/pom.xml b/sava-core/pom.xml index fde17d4..d1ade6c 100644 --- a/sava-core/pom.xml +++ b/sava-core/pom.xml @@ -68,25 +68,6 @@ </dependency> </dependencies> - <build> - <plugins> - <!-- Attach source and javadoc artifacts --> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-source-plugin</artifactId> - <version>3.3.1</version> - <executions> - <execution> - <id>attach-sources</id> - <goals> - <goal>jar-no-fork</goal> - </goals> - </execution> - </executions> - </plugin> - </plugins> - </build> - <reporting> <!-- Those plugins are used by `mvn site` --> <plugins> diff --git a/sava-example/pom.xml b/sava-example/pom.xml index 45c747e..c0b7ad3 100644 --- a/sava-example/pom.xml +++ b/sava-example/pom.xml @@ -5,7 +5,7 @@ <parent> <groupId>fr.inrae.agroclim</groupId> <artifactId>sava</artifactId> - <version>0.0.1-SNAPSHOT</version> + <version>1.0.0</version> </parent> <artifactId>sava-example</artifactId> <name>SAVA Example</name> -- GitLab From 4ddf6a9cfe3a5a2defed1c3d1e7e4238473ee372 Mon Sep 17 00:00:00 2001 From: TROMEL LOUIS <58844429+Ash84@users.noreply.github.com> Date: Fri, 5 Jul 2024 12:02:40 +0200 Subject: [PATCH 3/3] metadata 1.0 --- codemeta.json | 2 +- publiccode.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/codemeta.json b/codemeta.json index 9c832a4..51a3ccd 100644 --- a/codemeta.json +++ b/codemeta.json @@ -43,5 +43,5 @@ "Java", "Java 11" ], - "version": "0.0.1-SNAPSHOT" + "version": "1.0.0" } diff --git a/publiccode.yml b/publiccode.yml index 4930760..30c4171 100644 --- a/publiccode.yml +++ b/publiccode.yml @@ -26,7 +26,7 @@ legal: name: SAVA releaseDate: 2023-12-07 softwareType: library -softwareVersion: 0.0.1-SNAPSHOT +softwareVersion: 1.0.0 url: https://forgemia.inra.fr/agroclim/sava.git usedBy: - Agroclim -- GitLab