Skip to content

Commit

Permalink
Fixed packaging to add version and name to BuildInfoMetric for the Ht…
Browse files Browse the repository at this point in the history
…tp server. Added tests to validate the correct name and version is not unknown

Signed-off-by: Doug Hoard <doug.hoard@gmail.com>
  • Loading branch information
dhoard committed Feb 8, 2023
1 parent b6b811b commit 39965b4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;

/**
* Runs the JmxExampleApplication on different Java Docker images with the jmx_exporter agent attached,
Expand All @@ -23,6 +25,7 @@
@RunWith(Parameterized.class)
public class AgentJavaVersionsIT {

private final String agentModule;
private final GenericContainer<?> javaContainer;
private final Volume volume;
private final Scraper scraper;
Expand Down Expand Up @@ -59,6 +62,7 @@ public static String[][] images() {
}

public AgentJavaVersionsIT(String baseImage, String agentModule) throws IOException, URISyntaxException {
this.agentModule = agentModule;
volume = Volume.create("agent-integration-test-");
volume.copyAgentJar(agentModule);
volume.copyConfigYaml("config.yml");
Expand Down Expand Up @@ -108,4 +112,32 @@ public void testTabularMetric() throws Exception {
.orElseThrow(() -> new AssertionError("Metric " + expectedMetric + " not found."));
}
}

@Test
public void testBuildInfoMetricName() {
AtomicReference expectedName = new AtomicReference("\"jmx_prometheus_javaagent\"");
if (agentModule.endsWith("_java6")) {
expectedName.set("\"jmx_prometheus_javaagent_java6\"");
}

List<String> metrics = scraper.scrape(10 * 1000);
metrics.stream()
.filter(line -> line.startsWith("jmx_exporter_build_info"))
.findAny()
.ifPresent(line -> {
Assert.assertTrue(line.contains((String) expectedName.get()));
});
}

@Test
public void testBuildVersionMetricNotUnknown() {
// No easy way to test the version, so make sure it's not "unknown"
List<String> metrics = scraper.scrape(10 * 1000);
metrics.stream()
.filter(line -> line.startsWith("jmx_exporter_build_info"))
.findAny()
.ifPresent(line -> {
Assert.assertTrue(!line.contains("\"unknown\""));
});
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.prometheus.jmx;

import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
Expand All @@ -10,13 +11,16 @@

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
* Simple test of the jmx_prometheus_httpserver getting metrics from the JmxExampleApplication.
*/
@RunWith(Parameterized.class)
public class HttpServerIT {

private final String httpServerModule;
private final Volume volume;
private final GenericContainer<?> javaContainer;
private final Scraper scraper;
Expand Down Expand Up @@ -53,6 +57,7 @@ public static String[][] images() {
}

public HttpServerIT(String baseImage, String httpServerModule) throws IOException, URISyntaxException {
this.httpServerModule = httpServerModule;
volume = Volume.create("http-server-integration-test-");
volume.copyHttpServer(httpServerModule);
volume.copyConfigYaml("config-httpserver.yml");
Expand Down Expand Up @@ -92,4 +97,32 @@ public void testExampleMetrics() throws Exception {
.orElseThrow(() -> new AssertionError("Metric " + metric + " not found."));
}
}

@Test
public void testBuildInfoMetricName() {
AtomicReference expectedName = new AtomicReference("\"jmx_prometheus_httpserver\"");
if (httpServerModule.endsWith("_java6")) {
expectedName.set("\"jmx_prometheus_httpserver_java6\"");
}

List<String> metrics = scraper.scrape(10 * 1000);
metrics.stream()
.filter(line -> line.startsWith("jmx_exporter_build_info"))
.findAny()
.ifPresent(line -> {
Assert.assertTrue(line.contains((String) expectedName.get()));
});
}

@Test
public void testBuildVersionMetricNotUnknown() {
// No easy way to test the version, so make sure it's not "unknown"
List<String> metrics = scraper.scrape(10 * 1000);
metrics.stream()
.filter(line -> line.startsWith("jmx_exporter_build_info"))
.findAny()
.ifPresent(line -> {
Assert.assertTrue(!line.contains("\"unknown\""));
});
}
}
2 changes: 2 additions & 0 deletions jmx_prometheus_httpserver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>io.prometheus.jmx.WebServer</Main-Class>
<Implementation-Version>${project.version}</Implementation-Version>
<Implementation-Title>${project.artifactId}</Implementation-Title>
</manifestEntries>
</transformer>
</transformers>
Expand Down
2 changes: 2 additions & 0 deletions jmx_prometheus_httpserver_java6/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>io.prometheus.jmx.WebServer</Main-Class>
<Implementation-Version>${project.version}</Implementation-Version>
<Implementation-Title>${project.artifactId}</Implementation-Title>
</manifestEntries>
</transformer>
</transformers>
Expand Down

0 comments on commit 39965b4

Please sign in to comment.