diff --git a/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/HistogramTest.java b/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/HistogramTest.java index b021bf8ce..2e0df7b01 100644 --- a/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/HistogramTest.java +++ b/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/HistogramTest.java @@ -530,7 +530,7 @@ public void testGolangTests() throws NoSuchFieldException, IllegalAccessExceptio "sample_count: 2 " + "sample_sum: -7.0 " + "schema: 2 " + - "zero_threshold: 2.9387358770557188E-39 " + + "zero_threshold: " + Math.pow(2.0, -128.0) + " " + "zero_count: 0 " + "negative_span { offset: 7 length: 2 } " + "negative_delta: 1 " + @@ -564,7 +564,7 @@ public void testGolangTests() throws NoSuchFieldException, IllegalAccessExceptio "sample_count: 2 " + "sample_sum: 7.0 " + "schema: 2 " + - "zero_threshold: 2.9387358770557188E-39 " + + "zero_threshold: " + Math.pow(2.0, -128.0) + " " + "zero_count: 0 " + "positive_span { offset: 7 length: 2 } " + "positive_delta: 1 " + diff --git a/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmThreadsMetricsTest.java b/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmThreadsMetricsTest.java index e88e86811..7be7892a9 100644 --- a/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmThreadsMetricsTest.java +++ b/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmThreadsMetricsTest.java @@ -107,14 +107,21 @@ public void testIgnoredMetricNotScraped() { @Test public void testInvalidThreadIds() { + try { + int javaVersion = Integer.parseInt(System.getProperty("java.version")); + if (javaVersion >= 21) { + // With Java 21 and newer you can no longer have invalid thread ids. + return; + } + } catch (NumberFormatException ignored) { + } PrometheusRegistry registry = new PrometheusRegistry(); JvmThreadsMetrics.builder().register(registry); - MetricSnapshots snapshots = registry.scrape(); // Number of threads to create with invalid thread ids int numberOfInvalidThreadIds = 2; - Map expected = getCountByState(snapshots); + Map expected = getCountByState(registry.scrape()); expected.compute("UNKNOWN", (key, oldValue) -> oldValue == null ? numberOfInvalidThreadIds : oldValue + numberOfInvalidThreadIds); final CountDownLatch countDownLatch = new CountDownLatch(numberOfInvalidThreadIds); @@ -122,7 +129,7 @@ public void testInvalidThreadIds() { try { // Create and start threads with invalid thread ids (id=0, id=-1, etc.) for (int i = 0; i < numberOfInvalidThreadIds; i++) { - new TestThread(-i, new TestRunnable(countDownLatch)).start(); + new ThreadWithInvalidId(-i, new TestRunnable(countDownLatch)).start(); } Map actual = getCountByState(registry.scrape()); @@ -152,16 +159,21 @@ private Map getCountByState(MetricSnapshots snapshots) { return result; } - private static class TestThread extends Thread { + private static class ThreadWithInvalidId extends Thread { private final long id; - public TestThread(long id, Runnable runnable) { + public ThreadWithInvalidId(long id, Runnable runnable) { super(runnable); setDaemon(true); this.id = id; } + /** + * Note that only Java versions < 21 call this to get the thread id. + * With Java 21 and newer it's no longer possible to make an invalid thread id. + */ + @Override public long getId() { return this.id; }