Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Registering JMX Exporter build info #279

Merged
merged 2 commits into from
Jun 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions collector/src/main/java/io/prometheus/jmx/BuildInfoCollector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.prometheus.jmx;

import io.prometheus.client.Collector;
import io.prometheus.client.GaugeMetricFamily;

import java.util.ArrayList;
import java.util.List;

import static java.util.Arrays.asList;

/**
* Collects jmx_exporter build version info.
* <p>
* Example usage:
* <pre>
* {@code
* new BuildInfoCollector().register();
* }
* </pre>
* Metrics being exported:
* <pre>
* jmx_exporter_build_info{version="3.2.0",name="jmx_prometheus_httpserver",} 1.0
* </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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.prometheus.jmx;

import io.prometheus.client.CollectorRegistry;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class BuildInfoCollectorTest {

private CollectorRegistry registry = new CollectorRegistry();

@Before
public void setUp() {
new BuildInfoCollector().register(registry);
}

@Test
public void testBuildInfo() {
String version = this.getClass().getPackage().getImplementationVersion();
String name = this.getClass().getPackage().getImplementationTitle();

assertEquals(
1L,
registry.getSampleValue(
"jmx_exporter_build_info", new String[]{"version", "name"}, new String[]{
version != null ? version : "unknown",
name != null ? name : "unknown"
}),
.0000001);
}
}
1 change: 1 addition & 0 deletions jmx_prometheus_httpserver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<archive>
<manifest>
<mainClass>io.prometheus.jmx.WebServer</mainClass>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
<descriptorRefs>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static void main(String[] args) throws Exception {
socket = new InetSocketAddress(port);
}

new BuildInfoCollector().register();
new JmxCollector(new File(args[1])).register();
new HTTPServer(socket, CollectorRegistry.defaultRegistry);
}
Expand Down
2 changes: 2 additions & 0 deletions jmx_prometheus_javaagent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Premain-Class>io.prometheus.jmx.shaded.io.prometheus.jmx.JavaAgent</Premain-Class>
<Implementation-Version>${project.version}</Implementation-Version>
<Implementation-Title>${project.artifactId}</Implementation-Title>
</manifestEntries>
</transformer>
</transformers>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public static void premain(String agentArgument, Instrumentation instrumentation
file = args[1];
}

new BuildInfoCollector().register();
new JmxCollector(new File(file)).register();
DefaultExports.initialize();
server = new HTTPServer(socket, CollectorRegistry.defaultRegistry, true);
Expand Down