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