Skip to content

Commit bad8cee

Browse files
authored
Added isolator-javaagent
Signed-off-by: dhoard <doug.hoard@gmail.com>
1 parent e0c3565 commit bad8cee

File tree

12 files changed

+1208
-0
lines changed

12 files changed

+1208
-0
lines changed

integration_test_suite/integration_tests/src/test/java/io/prometheus/jmx/test/core/BasicIsolatorTest.java

Lines changed: 443 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
* Copyright (C) 2024-present The Prometheus jmx_exporter Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.prometheus.jmx.test.support;
18+
19+
import java.time.Duration;
20+
import java.util.ArrayList;
21+
import java.util.Collection;
22+
import java.util.stream.Stream;
23+
import org.testcontainers.containers.BindMode;
24+
import org.testcontainers.containers.GenericContainer;
25+
import org.testcontainers.containers.Network;
26+
import org.testcontainers.containers.wait.strategy.Wait;
27+
import org.verifyica.api.Argument;
28+
29+
/** Class to implement MultiExporterTestEnvironment */
30+
public class IsolatorExporterTestEnvironment implements Argument<IsolatorExporterTestEnvironment> {
31+
32+
private static final String BASE_URL = "http://localhost";
33+
private static final int BASE_PORT = 8888;
34+
35+
private final String javaDockerImage;
36+
37+
private Class<?> testClass;
38+
private String baseUrl;
39+
private Network network;
40+
private GenericContainer<?> javaAgentApplicationContainer;
41+
42+
/**
43+
* Constructor
44+
*
45+
* @param javaDockerImage javaDockerImage
46+
*/
47+
public IsolatorExporterTestEnvironment(String javaDockerImage) {
48+
this.javaDockerImage = javaDockerImage;
49+
this.baseUrl = BASE_URL;
50+
}
51+
52+
@Override
53+
public String getName() {
54+
return javaDockerImage + " / IsolatorJavaAgent";
55+
}
56+
57+
@Override
58+
public IsolatorExporterTestEnvironment getPayload() {
59+
return this;
60+
}
61+
62+
/**
63+
* Method to set the base URL
64+
*
65+
* @param baseUrl baseUrl
66+
* @return the ExporterTestEnvironment
67+
*/
68+
public IsolatorExporterTestEnvironment setBaseUrl(String baseUrl) {
69+
this.baseUrl = baseUrl;
70+
return this;
71+
}
72+
73+
/**
74+
* Method to get the Java Docker image name
75+
*
76+
* @return the Java Docker image name
77+
*/
78+
public String getJavaDockerImage() {
79+
return javaDockerImage;
80+
}
81+
82+
/**
83+
* Method to initialize the test environment
84+
*
85+
* @param network network
86+
*/
87+
public void initialize(Class<?> testClass, Network network) {
88+
this.testClass = testClass;
89+
this.network = network;
90+
91+
javaAgentApplicationContainer = createJavaAgentApplicationContainer();
92+
javaAgentApplicationContainer.start();
93+
}
94+
95+
/**
96+
* Method to get a URL (base URL + path)
97+
*
98+
* @param index index
99+
* @param path path
100+
* @return the URL (base URL + path)
101+
*/
102+
public String getUrl(int index, String path) {
103+
return !path.startsWith("/") ? getBaseUrl(index) + "/" + path : getBaseUrl(index) + path;
104+
}
105+
106+
/**
107+
* Method to get the base URL
108+
*
109+
* @param index index
110+
* @return the base URL
111+
*/
112+
public String getBaseUrl(int index) {
113+
int port = javaAgentApplicationContainer.getMappedPort(BASE_PORT + index);
114+
return baseUrl + ":" + port;
115+
}
116+
117+
/** Method to destroy the test environment */
118+
public void destroy() {
119+
if (javaAgentApplicationContainer != null) {
120+
javaAgentApplicationContainer.stop();
121+
javaAgentApplicationContainer = null;
122+
}
123+
}
124+
125+
/**
126+
* Method to create an application container
127+
*
128+
* @return the return value
129+
*/
130+
private GenericContainer<?> createJavaAgentApplicationContainer() {
131+
return new GenericContainer<>(javaDockerImage)
132+
.waitingFor(Wait.forListeningPort())
133+
.withClasspathResourceMapping("common", "/temp", BindMode.READ_ONLY)
134+
.withClasspathResourceMapping(
135+
testClass.getName().replace(".", "/") + "/JavaAgent",
136+
"/temp",
137+
BindMode.READ_ONLY)
138+
.withCreateContainerCmdModifier(TestContainerConfigureCmd.getInstance())
139+
.withCommand("/bin/sh application.sh")
140+
.withExposedPorts(BASE_PORT, BASE_PORT + 1, BASE_PORT + 2)
141+
.withLogConsumer(TestContainerLogger.getInstance())
142+
.withNetwork(network)
143+
.withNetworkAliases("application")
144+
.waitingFor(Wait.forLogMessage(".*JmxExampleApplication \\| Running.*\\n", 1))
145+
.withStartupTimeout(Duration.ofMillis(60000))
146+
.withWorkingDirectory("/temp");
147+
}
148+
149+
/**
150+
* Create the MultiExporterTestEnvironment
151+
*
152+
* @return a Stream of MultiExporterTestEnvironments
153+
*/
154+
public static Stream<IsolatorExporterTestEnvironment> createMultiExporterTestEnvironments() {
155+
Collection<IsolatorExporterTestEnvironment> collection = new ArrayList<>();
156+
157+
JavaDockerImages.names()
158+
.forEach(
159+
dockerImageName -> {
160+
collection.add(new IsolatorExporterTestEnvironment(dockerImageName));
161+
});
162+
163+
return collection.stream();
164+
}
165+
}

integration_test_suite/integration_tests/src/test/java/io/prometheus/jmx/test/support/TestSupport.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,21 @@ public static void initializeExporterTestEnvironment(
7070
.initialize(testClass, network);
7171
}
7272

73+
/**
74+
* Initializes the IsolatorExporterTestEnvironment
75+
*
76+
* @param argumentContext argumentContext
77+
* @param network network
78+
* @param testClass testClass
79+
*/
80+
public static void initializeIsolatorExporterTestEnvironment(
81+
ArgumentContext argumentContext, Network network, Class<?> testClass) {
82+
argumentContext
83+
.testArgument(IsolatorExporterTestEnvironment.class)
84+
.payload()
85+
.initialize(testClass, network);
86+
}
87+
7388
/**
7489
* Initializes the OpenTelemetryTestEnvironment
7590
*
@@ -97,6 +112,18 @@ public static void destroyExporterTestEnvironment(ArgumentContext argumentContex
97112
exporterTestEnvironmentArgument.payload().destroy());
98113
}
99114

115+
/**
116+
* Destroys the IsolatorExporterTestEnvironment
117+
*
118+
* @param argumentContext argumentContext
119+
*/
120+
public static void destroyIsolatorExporterTestEnvironment(ArgumentContext argumentContext) {
121+
Optional.ofNullable(argumentContext.testArgument(IsolatorExporterTestEnvironment.class))
122+
.ifPresent(
123+
exporterTestEnvironmentArgument ->
124+
exporterTestEnvironmentArgument.payload().destroy());
125+
}
126+
100127
/**
101128
* Destroys the OpenTelemetryTestEnvironment
102129
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
java \
4+
-Xmx512M \
5+
-javaagent:jmx_prometheus_isolator_javaagent.jar=jmx_prometheus_javaagent.jar=8888:exporter.yaml,jmx_prometheus_javaagent.jar=8889:exporter2.yaml,jmx_prometheus_javaagent.jar=8890:exporter3.yaml \
6+
-jar jmx_example_application.jar
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
rules:
2+
- pattern: ".*"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
lowercaseOutputName: true
2+
rules:
3+
- pattern: ".*"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
httpServer:
2+
authentication:
3+
basic:
4+
username: Prometheus
5+
password: secret
6+
rules:
7+
- pattern: ".*"

0 commit comments

Comments
 (0)