001/*
002 * Copyright (C) 2018-present 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 io.prometheus.metrics.core.metrics.Info;
020import io.prometheus.metrics.model.registry.PrometheusRegistry;
021
022/**
023 * Collects jmx_exporter build version info.
024 *
025 * <p>Example usage:
026 *
027 * <pre>{@code
028 * new BuildInfoCollector()
029 * }</pre>
030 *
031 * Metrics being exported:
032 *
033 * <pre>
034 *   jmx_exporter_build_info{version="3.2.0",name="jmx_prometheus_httpserver",} 1.0
035 * </pre>
036 */
037public class BuildInfoMetrics {
038
039    /**
040     * Method to register BuildInfoMetrics
041     *
042     * @return this BuildInfoMetrics
043     */
044    public BuildInfoMetrics register() {
045        return register(PrometheusRegistry.defaultRegistry);
046    }
047
048    /**
049     * Method to register BuildInfoMetrics
050     *
051     * @param prometheusRegistry prometheusRegistry
052     * @return this BuildInfoMetrics
053     */
054    public BuildInfoMetrics register(PrometheusRegistry prometheusRegistry) {
055        Info info =
056                Info.builder()
057                        .name("jmx_exporter_build_info")
058                        .help("JMX Exporter build information")
059                        .labelNames("name", "version")
060                        .register(prometheusRegistry);
061
062        Package pkg = this.getClass().getPackage();
063        String name = pkg.getImplementationTitle();
064        String version = pkg.getImplementationVersion();
065
066        info.setLabelValues(name != null ? name : "unknown", version != null ? version : "unknown");
067
068        return this;
069    }
070}