001package io.prometheus.metrics.instrumentation.jvm; 002 003import io.prometheus.metrics.config.PrometheusProperties; 004import io.prometheus.metrics.core.metrics.Info; 005import io.prometheus.metrics.model.registry.PrometheusRegistry; 006 007/** 008 * JVM Runtime Info metric. The {@link JvmRuntimeInfoMetric} is registered as part of the {@link JvmMetrics} like this: 009 * <pre>{@code 010 * JvmMetrics.builder().register(); 011 * }</pre> 012 * However, if you want only the {@link JvmRuntimeInfoMetric} you can also register them directly: 013 * <pre>{@code 014 * JvmRuntimeInfoMetric.builder().register(); 015 * }</pre> 016 * 017 * <pre> 018 * # TYPE jvm_runtime info 019 * # HELP jvm_runtime JVM runtime info 020 * jvm_runtime_info{runtime="OpenJDK Runtime Environment",vendor="Oracle Corporation",version="1.8.0_382-b05"} 1 021 * </pre> 022 */ 023public class JvmRuntimeInfoMetric { 024 025 private static final String JVM_RUNTIME_INFO = "jvm_runtime_info"; 026 027 private final PrometheusProperties config; 028 private final String version; 029 private final String vendor; 030 private final String runtime; 031 032 private JvmRuntimeInfoMetric(String version, String vendor, String runtime, PrometheusProperties config) { 033 this.config = config; 034 this.version = version; 035 this.vendor = vendor; 036 this.runtime = runtime; 037 } 038 039 private void register(PrometheusRegistry registry) { 040 041 Info jvmInfo = Info.builder(config) 042 .name(JVM_RUNTIME_INFO) 043 .help("JVM runtime info") 044 .labelNames("version", "vendor", "runtime") 045 .register(registry); 046 047 jvmInfo.setLabelValues(version, vendor, runtime); 048 } 049 050 public static Builder builder() { 051 return new Builder(PrometheusProperties.get()); 052 } 053 054 public static Builder builder(PrometheusProperties config) { 055 return new Builder(config); 056 } 057 058 public static class Builder { 059 060 private final PrometheusProperties config; 061 private String version; 062 private String vendor; 063 private String runtime; 064 065 private Builder(PrometheusProperties config) { 066 this.config = config; 067 } 068 069 /** 070 * Package private. For testing only. 071 */ 072 Builder version(String version) { 073 this.version = version; 074 return this; 075 } 076 077 /** 078 * Package private. For testing only. 079 */ 080 Builder vendor(String vendor) { 081 this.vendor = vendor; 082 return this; 083 } 084 085 /** 086 * Package private. For testing only. 087 */ 088 Builder runtime(String runtime) { 089 this.runtime = runtime; 090 return this; 091 } 092 093 public void register() { 094 register(PrometheusRegistry.defaultRegistry); 095 } 096 097 public void register(PrometheusRegistry registry) { 098 String version = this.version != null ? this.version : System.getProperty("java.runtime.version", "unknown"); 099 String vendor = this.vendor != null ? this.vendor : System.getProperty("java.vm.vendor", "unknown"); 100 String runtime = this.runtime != null ? this.runtime : System.getProperty("java.runtime.name", "unknown"); 101 new JvmRuntimeInfoMetric(version, vendor, runtime, config).register(registry); 102 } 103 } 104}