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