forked from open-telemetry/opentelemetry-java-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJmxScraperContainer.java
168 lines (142 loc) · 4.81 KB
/
JmxScraperContainer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.contrib.jmxscraper;
import static org.assertj.core.api.Assertions.assertThat;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.utility.MountableFile;
/** Test container that allows to execute {@link JmxScraper} in an isolated container */
public class JmxScraperContainer extends GenericContainer<JmxScraperContainer> {
private final String endpoint;
private final Set<String> targetSystems;
private String serviceUrl;
private int intervalMillis;
private final Set<String> customYamlFiles;
private String user;
private String password;
private final List<String> extraJars;
public JmxScraperContainer(String otlpEndpoint, String baseImage) {
super(baseImage);
String scraperJarPath = System.getProperty("shadow.jar.path");
assertThat(scraperJarPath).isNotNull();
this.withCopyFileToContainer(MountableFile.forHostPath(scraperJarPath), "/scraper.jar")
.waitingFor(
Wait.forLogMessage(".*JMX scraping started.*", 1)
.withStartupTimeout(Duration.ofSeconds(10)));
this.endpoint = otlpEndpoint;
this.targetSystems = new HashSet<>();
this.customYamlFiles = new HashSet<>();
this.intervalMillis = 1000;
this.extraJars = new ArrayList<>();
}
@CanIgnoreReturnValue
public JmxScraperContainer withTargetSystem(String targetSystem) {
targetSystems.add(targetSystem);
return this;
}
@CanIgnoreReturnValue
public JmxScraperContainer withIntervalMillis(int intervalMillis) {
this.intervalMillis = intervalMillis;
return this;
}
@CanIgnoreReturnValue
public JmxScraperContainer withRmiServiceUrl(String host, int port) {
// TODO: adding a way to provide 'host:port' syntax would make this easier for end users
return withServiceUrl(
String.format(
Locale.getDefault(), "service:jmx:rmi:///jndi/rmi://%s:%d/jmxrmi", host, port));
}
@CanIgnoreReturnValue
public JmxScraperContainer withServiceUrl(String serviceUrl) {
this.serviceUrl = serviceUrl;
return this;
}
/**
* Sets JMX user login
*
* @param user user login
* @return this
*/
@CanIgnoreReturnValue
public JmxScraperContainer withUser(String user) {
this.user = user;
return this;
}
/**
* Sets JMX password
*
* @param password user password
* @return this
*/
@CanIgnoreReturnValue
public JmxScraperContainer withPassword(String password) {
this.password = password;
return this;
}
/**
* Adds path to an extra jar for classpath
*
* @param jarPath path to an extra jar that should be added to jmx scraper classpath
* @return this
*/
@CanIgnoreReturnValue
public JmxScraperContainer withExtraJar(String jarPath) {
this.extraJars.add(jarPath);
return this;
}
@CanIgnoreReturnValue
public JmxScraperContainer withCustomYaml(String yamlPath) {
this.customYamlFiles.add(yamlPath);
return this;
}
@Override
public void start() {
// for now only configure through JVM args
List<String> arguments = new ArrayList<>();
arguments.add("java");
arguments.add("-Dotel.metrics.exporter=otlp");
arguments.add("-Dotel.exporter.otlp.endpoint=" + endpoint);
if (!targetSystems.isEmpty()) {
arguments.add("-Dotel.jmx.target.system=" + String.join(",", targetSystems));
}
if (serviceUrl == null) {
throw new IllegalStateException("Missing service URL");
}
arguments.add("-Dotel.jmx.service.url=" + serviceUrl);
arguments.add("-Dotel.jmx.interval.milliseconds=" + intervalMillis);
if (user != null) {
arguments.add("-Dotel.jmx.username=" + user);
}
if (password != null) {
arguments.add("-Dotel.jmx.password=" + password);
}
if (!customYamlFiles.isEmpty()) {
for (String yaml : customYamlFiles) {
this.withCopyFileToContainer(MountableFile.forClasspathResource(yaml), yaml);
}
arguments.add("-Dotel.jmx.config=" + String.join(",", customYamlFiles));
}
if (extraJars.isEmpty()) {
// using "java -jar" to start
arguments.add("-jar");
arguments.add("/scraper.jar");
} else {
// using "java -cp" to start
arguments.add("-cp");
arguments.add("/scraper.jar:" + String.join(":", extraJars));
arguments.add("io.opentelemetry.contrib.jmxscraper.JmxScraper");
}
this.withCommand(arguments.toArray(new String[0]));
logger().info("Starting scraper with command: " + String.join(" ", arguments));
super.start();
}
}