Skip to content

Commit

Permalink
Refactored BuildInfoCollector for optimization (#800)
Browse files Browse the repository at this point in the history
Signed-off-by: dhoard <[email protected]>
Signed-off-by: Petr Dušák <[email protected]>
  • Loading branch information
dhoard authored and cspetrdusak committed May 12, 2023
1 parent c7254a7 commit eefed13
Showing 1 changed file with 53 additions and 20 deletions.
73 changes: 53 additions & 20 deletions collector/src/main/java/io/prometheus/jmx/BuildInfoCollector.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright (C) 2018-2023 The Prometheus jmx_exporter Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.prometheus.jmx;

import io.prometheus.client.Collector;
Expand All @@ -23,24 +39,41 @@
* </pre>
*/
public class BuildInfoCollector extends Collector {
public List<Collector.MetricFamilySamples> collect() {
List<Collector.MetricFamilySamples> mfs = new ArrayList<Collector.MetricFamilySamples>();

GaugeMetricFamily artifactInfo = new GaugeMetricFamily(
"jmx_exporter_build_info",
"A metric with a constant '1' value labeled with the version of the JMX exporter.",
asList("version", "name"));

Package pkg = this.getClass().getPackage();
String version = pkg.getImplementationVersion();
String name = pkg.getImplementationTitle();

artifactInfo.addMetric(asList(
version != null ? version : "unknown",
name != null ? name : "unknown"
), 1L);
mfs.add(artifactInfo);

return mfs;
}

private final List<Collector.MetricFamilySamples> metricFamilySamples;

/**
* Constructor
*/
public BuildInfoCollector() {
super();

metricFamilySamples = new ArrayList<Collector.MetricFamilySamples>();

GaugeMetricFamily artifactInfo =
new GaugeMetricFamily(
"jmx_exporter_build_info",
"A metric with a constant '1' value labeled with the version of the JMX exporter.",
asList("version", "name"));

Package pkg = this.getClass().getPackage();
String version = pkg.getImplementationVersion();
String name = pkg.getImplementationTitle();

artifactInfo.addMetric(
asList(version != null ? version : "unknown", name != null ? name : "unknown"),
1L);

metricFamilySamples.add(artifactInfo);
}

/**
* Method to get the List of MetricFamilySamples
*
* @return the return value
*/
@Override
public List<Collector.MetricFamilySamples> collect() {
return metricFamilySamples;
}
}

0 comments on commit eefed13

Please sign in to comment.