Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.prometheus.metrics.instrumentation.jvm;

import com.sun.management.GarbageCollectionNotificationInfo;
import io.prometheus.metrics.config.PrometheusProperties;
import io.prometheus.metrics.core.metrics.Histogram;
import io.prometheus.metrics.core.metrics.SummaryWithCallback;
import io.prometheus.metrics.model.registry.PrometheusRegistry;
import io.prometheus.metrics.model.snapshots.Quantiles;
Expand All @@ -9,6 +11,8 @@
import java.lang.management.ManagementFactory;
import java.util.List;
import javax.annotation.Nullable;
import javax.management.NotificationEmitter;
import javax.management.openmbean.CompositeData;

/**
* JVM Garbage Collector metrics. The {@link JvmGarbageCollectorMetrics} are registered as part of
Expand Down Expand Up @@ -39,6 +43,7 @@
public class JvmGarbageCollectorMetrics {

private static final String JVM_GC_COLLECTION_SECONDS = "jvm_gc_collection_seconds";
private static final String JVM_GC_DURATION = "jvm_gc_duration";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to have an "otel" mode - rather than mixing


private final PrometheusProperties config;
private final List<GarbageCollectorMXBean> garbageCollectorBeans;
Expand Down Expand Up @@ -67,6 +72,46 @@ private void register(PrometheusRegistry registry) {
}
})
.register(registry);

registerGCDurationHistogram(registry);
}

private void registerGCDurationHistogram(PrometheusRegistry registry) {
double[] buckets = {0.01, 0.1, 1, 10};

Histogram gcDurationHistogram =
Histogram.builder(config)
.name(JVM_GC_DURATION)
.help("Duration of JVM garbage collection actions.")
Copy link

Copilot AI Jan 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The histogram should include .unit(Unit.SECONDS) to be consistent with other time-based metrics in this codebase. This is present in jvm_gc_collection_seconds (line 62) and other time-based metrics like process_cpu_seconds_total and jvm_compilation_time_seconds_total.

Suggested change
.help("Duration of JVM garbage collection actions.")
.help("Duration of JVM garbage collection actions.")
.unit(Unit.SECONDS)

Copilot uses AI. Check for mistakes.
.labelNames("jvm_gc_action", "jvm_gc_name", "jvm_gc_cause")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use . instead of _ - the conversion is done at scrape time (if needed)

.classicUpperBounds(buckets)
.register(registry);

for (GarbageCollectorMXBean gcBean : garbageCollectorBeans) {

if (!(gcBean instanceof NotificationEmitter)) {
continue;
}

((NotificationEmitter) gcBean)
.addNotificationListener(
(notification, handback) -> {
if (!GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION.equals(
notification.getType())) {
return;
}

GarbageCollectionNotificationInfo info =
GarbageCollectionNotificationInfo.from(
(CompositeData) notification.getUserData());

gcDurationHistogram
.labelValues(info.getGcAction(), info.getGcName(), info.getGcCause())
.observe(Unit.millisToSeconds(info.getGcInfo().getDuration()));
},
null,
null);
}
}

public static Builder builder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public void testGoodCase() throws IOException {
@Test
public void testIgnoredMetricNotScraped() {
MetricNameFilter filter =
MetricNameFilter.builder().nameMustNotBeEqualTo("jvm_gc_collection_seconds").build();
MetricNameFilter.builder()
.nameMustNotBeEqualTo("jvm_gc_collection_seconds", "jvm_gc_duration")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also test label values

.build();
Comment on lines +61 to +63
Copy link

Copilot AI Jan 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new histogram metric functionality lacks test coverage. While the test file was updated to filter out the new metric, there are no tests that verify the histogram is properly registered, records GC events, or produces expected output. Consider adding a test similar to the existing testGoodCase() that validates the histogram metric's behavior, especially since other metrics in this file have comprehensive test coverage.

Copilot uses AI. Check for mistakes.

PrometheusRegistry registry = new PrometheusRegistry();
JvmGarbageCollectorMetrics.builder()
Expand Down