001/* 002 * Copyright (C) 2018-2023 The Prometheus jmx_exporter Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package io.prometheus.jmx; 018 019import static java.util.Arrays.asList; 020 021import io.prometheus.client.Collector; 022import io.prometheus.client.GaugeMetricFamily; 023import java.util.ArrayList; 024import java.util.List; 025 026/** 027 * Collects jmx_exporter build version info. 028 * 029 * <p>Example usage: 030 * 031 * <pre>{@code 032 * new BuildInfoCollector().register(); 033 * }</pre> 034 * 035 * Metrics being exported: 036 * 037 * <pre> 038 * jmx_exporter_build_info{version="3.2.0",name="jmx_prometheus_httpserver",} 1.0 039 * </pre> 040 */ 041public class BuildInfoCollector extends Collector { 042 043 private final List<Collector.MetricFamilySamples> metricFamilySamples; 044 045 /** Constructor */ 046 public BuildInfoCollector() { 047 super(); 048 049 metricFamilySamples = new ArrayList<>(); 050 051 GaugeMetricFamily artifactInfo = 052 new GaugeMetricFamily( 053 "jmx_exporter_build_info", 054 "A metric with a constant '1' value labeled with the version of the JMX" 055 + " exporter.", 056 asList("version", "name")); 057 058 Package pkg = this.getClass().getPackage(); 059 String version = pkg.getImplementationVersion(); 060 String name = pkg.getImplementationTitle(); 061 062 artifactInfo.addMetric( 063 asList(version != null ? version : "unknown", name != null ? name : "unknown"), 1L); 064 065 metricFamilySamples.add(artifactInfo); 066 } 067 068 /** 069 * Method to get the List of MetricFamilySamples 070 * 071 * @return the return value 072 */ 073 @Override 074 public List<Collector.MetricFamilySamples> collect() { 075 return metricFamilySamples; 076 } 077}