001package io.prometheus.metrics.instrumentation.jvm; 002 003import io.prometheus.metrics.config.PrometheusProperties; 004import io.prometheus.metrics.model.registry.PrometheusRegistry; 005import java.util.Set; 006import java.util.concurrent.ConcurrentHashMap; 007 008/** 009 * Registers all JVM metrics. Example usage: 010 * 011 * <pre>{@code 012 * JvmMetrics.builder().register(); 013 * }</pre> 014 */ 015public class JvmMetrics { 016 017 private static final Set<PrometheusRegistry> REGISTERED = ConcurrentHashMap.newKeySet(); 018 019 public static Builder builder() { 020 return new Builder(PrometheusProperties.get()); 021 } 022 023 // Note: Currently there is no configuration for JVM metrics, so it doesn't matter whether you 024 // pass a config or not. 025 // However, we will add config options in the future, like whether you want to use Prometheus 026 // naming conventions 027 // or OpenTelemetry semantic conventions for JVM metrics. 028 public static Builder builder(PrometheusProperties config) { 029 return new Builder(config); 030 } 031 032 public static class Builder { 033 034 private final PrometheusProperties config; 035 036 private Builder(PrometheusProperties config) { 037 this.config = config; 038 } 039 040 /** 041 * Register all JVM metrics with the default registry. 042 * 043 * <p>It's safe to call this multiple times, only the first call will register the metrics, all 044 * subsequent calls will be ignored. 045 */ 046 public void register() { 047 register(PrometheusRegistry.defaultRegistry); 048 } 049 050 /** 051 * Register all JVM metrics with the {@code registry}. 052 * 053 * <p>It's safe to call this multiple times, only the first call will register the metrics, all 054 * subsequent calls will be ignored. 055 */ 056 public void register(PrometheusRegistry registry) { 057 if (REGISTERED.add(registry)) { 058 JvmThreadsMetrics.builder(config).register(registry); 059 JvmBufferPoolMetrics.builder(config).register(registry); 060 JvmClassLoadingMetrics.builder(config).register(registry); 061 JvmCompilationMetrics.builder(config).register(registry); 062 JvmGarbageCollectorMetrics.builder(config).register(registry); 063 JvmMemoryPoolAllocationMetrics.builder(config).register(registry); 064 JvmMemoryMetrics.builder(config).register(registry); 065 JvmNativeMemoryMetrics.builder(config).register(registry); 066 JvmRuntimeInfoMetric.builder(config).register(registry); 067 ProcessMetrics.builder(config).register(registry); 068 } 069 } 070 } 071}