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