001package io.prometheus.jmx;
002
003import io.prometheus.client.Collector;
004import io.prometheus.client.GaugeMetricFamily;
005
006import java.util.ArrayList;
007import java.util.List;
008
009import static java.util.Arrays.asList;
010
011/**
012 * Collects jmx_exporter build version info.
013 * <p>
014 * Example usage:
015 * <pre>
016 * {@code
017 *   new BuildInfoCollector().register();
018 * }
019 * </pre>
020 * Metrics being exported:
021 * <pre>
022 *   jmx_exporter_build_info{version="3.2.0",name="jmx_prometheus_httpserver",} 1.0
023 * </pre>
024 */
025public class BuildInfoCollector extends Collector {
026  public List<Collector.MetricFamilySamples> collect() {
027    List<Collector.MetricFamilySamples> mfs = new ArrayList<Collector.MetricFamilySamples>();
028
029    GaugeMetricFamily artifactInfo = new GaugeMetricFamily(
030            "jmx_exporter_build_info",
031            "A metric with a constant '1' value labeled with the version of the JMX exporter.",
032            asList("version", "name"));
033
034    Package pkg = this.getClass().getPackage();
035    String version = pkg.getImplementationVersion();
036    String name = pkg.getImplementationTitle();
037
038    artifactInfo.addMetric(asList(
039            version != null ? version : "unknown",
040            name != null ? name : "unknown"
041    ), 1L);
042    mfs.add(artifactInfo);
043
044    return mfs;
045  }
046}