From 14c09083e75b807837d5b1da80688c01d0ff1b73 Mon Sep 17 00:00:00 2001 From: Mickael Maison Date: Mon, 30 Sep 2024 16:37:07 +0200 Subject: [PATCH 1/2] Make JvmMetrics.register idempotent with the default registry (#987) * Make JvmMetrics.register idempotent with the default registry Signed-off-by: Mickael Maison * Make JvmMetrics.register() methods idempotent Signed-off-by: Mickael Maison --------- Signed-off-by: Mickael Maison --- .../instrumentation/jvm/JvmMetrics.java | 42 +++++++++---------- .../instrumentation/jvm/JvmMetricsTest.java | 19 +++++++++ 2 files changed, 40 insertions(+), 21 deletions(-) create mode 100644 prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmMetricsTest.java diff --git a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmMetrics.java b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmMetrics.java index fa909f629..54d937688 100644 --- a/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmMetrics.java +++ b/prometheus-metrics-instrumentation-jvm/src/main/java/io/prometheus/metrics/instrumentation/jvm/JvmMetrics.java @@ -3,7 +3,8 @@ import io.prometheus.metrics.config.PrometheusProperties; import io.prometheus.metrics.model.registry.PrometheusRegistry; -import java.util.concurrent.atomic.AtomicBoolean; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; /** * Registers all JVM metrics. Example usage: @@ -13,15 +14,14 @@ */ public class JvmMetrics { - private static AtomicBoolean registeredWithTheDefaultRegistry = new AtomicBoolean(false); - + private static final Set REGISTERED = ConcurrentHashMap.newKeySet(); public static Builder builder() { return new Builder(PrometheusProperties.get()); } // Note: Currently there is no configuration for JVM metrics, so it doesn't matter whether you pass a config or not. // However, we will add config options in the future, like whether you want to use Prometheus naming conventions - //'or OpenTelemetry semantic conventions for JVM metrics. + // or OpenTelemetry semantic conventions for JVM metrics. public static Builder builder(PrometheusProperties config) { return new Builder(config); } @@ -37,32 +37,32 @@ private Builder(PrometheusProperties config) { /** * Register all JVM metrics with the default registry. *

- * It's safe to call this multiple times: - * Only the first call will register the metrics, all subsequent calls will be ignored. + * It's safe to call this multiple times, only the first call will register the metrics, all subsequent calls + * will be ignored. */ public void register() { - if (!registeredWithTheDefaultRegistry.getAndSet(true)) { - register(PrometheusRegistry.defaultRegistry); - } + register(PrometheusRegistry.defaultRegistry); } /** * Register all JVM metrics with the {@code registry}. *

- * You must make sure to call this only once per {@code registry}, otherwise it will - * throw an Exception because you are trying to register duplicate metrics. + * It's safe to call this multiple times, only the first call will register the metrics, all subsequent calls + * will be ignored. */ public void register(PrometheusRegistry registry) { - JvmThreadsMetrics.builder(config).register(registry); - JvmBufferPoolMetrics.builder(config).register(registry); - JvmClassLoadingMetrics.builder(config).register(registry); - JvmCompilationMetrics.builder(config).register(registry); - JvmGarbageCollectorMetrics.builder(config).register(registry); - JvmMemoryPoolAllocationMetrics.builder(config).register(registry); - JvmMemoryMetrics.builder(config).register(registry); - JvmNativeMemoryMetrics.builder(config).register(registry); - JvmRuntimeInfoMetric.builder(config).register(registry); - ProcessMetrics.builder(config).register(registry); + if (REGISTERED.add(registry)) { + JvmThreadsMetrics.builder(config).register(registry); + JvmBufferPoolMetrics.builder(config).register(registry); + JvmClassLoadingMetrics.builder(config).register(registry); + JvmCompilationMetrics.builder(config).register(registry); + JvmGarbageCollectorMetrics.builder(config).register(registry); + JvmMemoryPoolAllocationMetrics.builder(config).register(registry); + JvmMemoryMetrics.builder(config).register(registry); + JvmNativeMemoryMetrics.builder(config).register(registry); + JvmRuntimeInfoMetric.builder(config).register(registry); + ProcessMetrics.builder(config).register(registry); + } } } } diff --git a/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmMetricsTest.java b/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmMetricsTest.java new file mode 100644 index 000000000..d93b1682d --- /dev/null +++ b/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmMetricsTest.java @@ -0,0 +1,19 @@ +package io.prometheus.metrics.instrumentation.jvm; + +import io.prometheus.metrics.model.registry.PrometheusRegistry; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class JvmMetricsTest { + + @Test + public void testRegisterIdempotent() { + PrometheusRegistry registry = new PrometheusRegistry(); + assertEquals(0, registry.scrape().size()); + JvmMetrics.builder().register(registry); + assertTrue(registry.scrape().size() > 0); + JvmMetrics.builder().register(registry); + } +} From bddcb487947375c6980f34fb7fce91ac62ee39d9 Mon Sep 17 00:00:00 2001 From: Gregor Zeitlinger Date: Tue, 1 Oct 2024 13:26:27 +0200 Subject: [PATCH 2/2] move to github actions / require google java format for changed files (#988) * move to github actions Signed-off-by: Gregor Zeitlinger * apply spotless for all changed files Signed-off-by: Gregor Zeitlinger * apply spotless for all changed files Signed-off-by: Gregor Zeitlinger --------- Signed-off-by: Gregor Zeitlinger --- .circleci/config.yml | 27 --------------------------- .github/workflows/build.yml | 25 +++++++++++++++++++++++++ pom.xml | 30 ++++++++++++++++++++++++++++-- 3 files changed, 53 insertions(+), 29 deletions(-) delete mode 100644 .circleci/config.yml create mode 100644 .github/workflows/build.yml diff --git a/.circleci/config.yml b/.circleci/config.yml deleted file mode 100644 index 55ce02110..000000000 --- a/.circleci/config.yml +++ /dev/null @@ -1,27 +0,0 @@ -version: 2 -machine: true -jobs: - build: - machine: - image: ubuntu-2204:current - working_directory: ~/circleci-java - steps: - - run: - name: Install OpenJDK 17 - command: | - sudo apt-get update && sudo apt-get install -y openjdk-17-jdk - sudo update-alternatives --set java /usr/lib/jvm/java-17-openjdk-amd64/bin/java - sudo update-alternatives --set javac /usr/lib/jvm/java-17-openjdk-amd64/bin/javac - java -version - - checkout - - restore_cache: - key: maven-dependencies-{{ checksum "pom.xml" }} - - run: cd prometheus-metrics-shaded-dependencies && ../mvnw clean install && cd .. - - run: ./mvnw clean install - - run: ./mvnw javadoc:jar - - save_cache: - paths: - - ~/.m2 - key: maven-dependencies-{{ checksum "pom.xml" }} -orbs: - prometheus: prometheus/prometheus@0.16.0 diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 000000000..ed5d2605f --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,25 @@ +name: Build + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Set up JDK + uses: actions/setup-java@v4 + with: + java-version: 17 + distribution: temurin + cache: 'maven' + - name: Shaded dependencies + run: | + cd prometheus-metrics-shaded-dependencies + ../mvnw clean install + - name: Run the Maven verify phase + run: | + ./mvnw clean install + ./mvnw javadoc:jar diff --git a/pom.xml b/pom.xml index 52953fbc9..0f0e27d08 100644 --- a/pom.xml +++ b/pom.xml @@ -1,5 +1,6 @@ - + pom 4.0.0 @@ -156,9 +157,32 @@ maven-enforcer-plugin 1.4.1 + + com.diffplug.spotless + spotless-maven-plugin + 2.43.0 + + + com.diffplug.spotless + spotless-maven-plugin + + origin/main + + + + + + + verify + + check + + + + org.apache.maven.plugins maven-enforcer-plugin @@ -217,7 +241,9 @@ true all public - benchmarks,examples,integration-tests,integration_tests,,simpleclient,simpleclient_bom,simpleclient_caffeine,simpleclient_common,simpleclient_dropwizard,simpleclient_graphite_bridge,simpleclient_guava,simpleclient_hibernate,simpleclient_hotspot,simpleclient_httpserver,simpleclient_jetty,simpleclient_jetty_jdk8,simpleclient_log4j,simpleclient_log4j2,simpleclient_logback,simpleclient_pushgateway,simpleclient_servlet,simpleclient_servlet_common,simpleclient_servlet_jakarta,simpleclient_spring_boot,simpleclient_spring_web,simpleclient_tracer,simpleclient_vertx,simpleclient_vertx4 + + benchmarks,examples,integration-tests,integration_tests,,simpleclient,simpleclient_bom,simpleclient_caffeine,simpleclient_common,simpleclient_dropwizard,simpleclient_graphite_bridge,simpleclient_guava,simpleclient_hibernate,simpleclient_hotspot,simpleclient_httpserver,simpleclient_jetty,simpleclient_jetty_jdk8,simpleclient_log4j,simpleclient_log4j2,simpleclient_logback,simpleclient_pushgateway,simpleclient_servlet,simpleclient_servlet_common,simpleclient_servlet_jakarta,simpleclient_spring_boot,simpleclient_spring_web,simpleclient_tracer,simpleclient_vertx,simpleclient_vertx4 + io.prometheus.metrics.expositionformats.generated.*