Skip to content

Commit 164679f

Browse files
committed
Cleanup of config.
Refactoring of JmxScraperConfigFactory. Added first unit tests.
1 parent 165fcb8 commit 164679f

File tree

4 files changed

+364
-59
lines changed

4 files changed

+364
-59
lines changed

jmx-scraper/build.gradle.kts

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ dependencies {
1919
implementation("io.opentelemetry:opentelemetry-sdk-testing")
2020

2121
implementation("io.opentelemetry.instrumentation:opentelemetry-jmx-metrics")
22+
23+
testImplementation("org.junit-pioneer:junit-pioneer")
2224
}
2325

2426
tasks {

jmx-scraper/src/main/java/io/opentelemetry/contrib/jmxscraper/config/JmxScraperConfig.java

+3-19
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,13 @@
1111
/** This class keeps application settings */
1212
public class JmxScraperConfig {
1313
String serviceUrl = "";
14-
String customJmxScrapingConfig = "";
15-
String targetSystem = "";
14+
String customJmxScrapingConfigPath = "";
1615
Set<String> targetSystems = Collections.emptySet();
1716
int intervalMilliseconds;
1817
String metricsExporterType = "";
1918

2019
String otlpExporterEndpoint = "";
2120

22-
String prometheusExporterHost = "";
23-
int prometheusExporterPort;
24-
2521
String username = "";
2622
String password = "";
2723
String realm = "";
@@ -34,12 +30,8 @@ public String getServiceUrl() {
3430
return serviceUrl;
3531
}
3632

37-
public String getCustomJmxScrapingConfig() {
38-
return customJmxScrapingConfig;
39-
}
40-
41-
public String getTargetSystem() {
42-
return targetSystem;
33+
public String getCustomJmxScrapingConfigPath() {
34+
return customJmxScrapingConfigPath;
4335
}
4436

4537
public Set<String> getTargetSystems() {
@@ -58,14 +50,6 @@ public String getOtlpExporterEndpoint() {
5850
return otlpExporterEndpoint;
5951
}
6052

61-
public String getPrometheusExporterHost() {
62-
return prometheusExporterHost;
63-
}
64-
65-
public int getPrometheusExporterPort() {
66-
return prometheusExporterPort;
67-
}
68-
6953
public String getUsername() {
7054
return username;
7155
}

jmx-scraper/src/main/java/io/opentelemetry/contrib/jmxscraper/config/JmxScraperConfigFactory.java

+34-40
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,32 @@
1313
import java.nio.file.Files;
1414
import java.nio.file.Paths;
1515
import java.util.Arrays;
16-
import java.util.LinkedHashSet;
1716
import java.util.List;
1817
import java.util.Locale;
1918
import java.util.Properties;
19+
import java.util.stream.Collectors;
2020

2121
@SuppressWarnings({"SystemOut", "SystemExitOutsideMain"})
2222
public class JmxScraperConfigFactory {
2323
private static final String PREFIX = "otel.";
24-
private static final String SERVICE_URL = PREFIX + "jmx.service.url";
25-
private static final String CUSTOM_JMX_SCRAPING_CONFIG =
26-
PREFIX + "jmx.custom.jmx.scraping.config";
27-
private static final String TARGET_SYSTEM = PREFIX + "jmx.target.system";
28-
private static final String INTERVAL_MILLISECONDS = PREFIX + "jmx.interval.milliseconds";
29-
private static final String METRICS_EXPORTER_TYPE = PREFIX + "metrics.exporter";
30-
private static final String EXPORTER = PREFIX + "exporter.";
31-
private static final String REGISTRY_SSL = PREFIX + "jmx.remote.registry.ssl";
32-
private static final String EXPORTER_INTERVAL = PREFIX + "metric.export.interval";
33-
34-
private static final String OTLP_ENDPOINT = EXPORTER + "otlp.endpoint";
35-
36-
private static final String PROMETHEUS_HOST = EXPORTER + "prometheus.host";
37-
private static final String PROMETHEUS_PORT = EXPORTER + "prometheus.port";
38-
39-
private static final String JMX_USERNAME = PREFIX + "jmx.username";
40-
private static final String JMX_PASSWORD = PREFIX + "jmx.password";
41-
private static final String JMX_REMOTE_PROFILE = PREFIX + "jmx.remote.profile";
42-
private static final String JMX_REALM = PREFIX + "jmx.realm";
24+
static final String SERVICE_URL = PREFIX + "jmx.service.url";
25+
static final String CUSTOM_JMX_SCRAPING_CONFIG = PREFIX + "jmx.custom.jmx.scraping.config";
26+
static final String TARGET_SYSTEM = PREFIX + "jmx.target.system";
27+
static final String INTERVAL_MILLISECONDS = PREFIX + "jmx.interval.milliseconds";
28+
static final String METRICS_EXPORTER_TYPE = PREFIX + "metrics.exporter";
29+
static final String EXPORTER_INTERVAL = PREFIX + "metric.export.interval";
30+
static final String REGISTRY_SSL = PREFIX + "jmx.remote.registry.ssl";
31+
32+
static final String OTLP_ENDPOINT = PREFIX + "exporter.otlp.endpoint";
33+
34+
static final String JMX_USERNAME = PREFIX + "jmx.username";
35+
static final String JMX_PASSWORD = PREFIX + "jmx.password";
36+
static final String JMX_REMOTE_PROFILE = PREFIX + "jmx.remote.profile";
37+
static final String JMX_REALM = PREFIX + "jmx.realm";
4338

4439
// These properties need to be copied into System Properties if provided via the property
4540
// file so that they are available to the JMX Connection builder
46-
private static final List<String> JAVA_SYSTEM_PROPERTIES =
41+
static final List<String> JAVA_SYSTEM_PROPERTIES =
4742
Arrays.asList(
4843
"javax.net.ssl.keyStore",
4944
"javax.net.ssl.keyStorePassword",
@@ -52,7 +47,7 @@ public class JmxScraperConfigFactory {
5247
"javax.net.ssl.trustStorePassword",
5348
"javax.net.ssl.trustStoreType");
5449

55-
private static final List<String> AVAILABLE_TARGET_SYSTEMS =
50+
static final List<String> AVAILABLE_TARGET_SYSTEMS =
5651
Arrays.asList(
5752
"activemq",
5853
"cassandra",
@@ -94,6 +89,8 @@ public JmxScraperConfig createConfigFromArgs(List<String> args) {
9489

9590
JmxScraperConfig config = createConfig(loadedProperties);
9691
validateConfig(config);
92+
populateJmxSystemProperties();
93+
9794
return config;
9895
}
9996

@@ -128,26 +125,21 @@ JmxScraperConfig createConfig(Properties props) {
128125
JmxScraperConfig config = new JmxScraperConfig();
129126

130127
config.serviceUrl = properties.getProperty(SERVICE_URL);
131-
config.customJmxScrapingConfig = properties.getProperty(CUSTOM_JMX_SCRAPING_CONFIG);
132-
config.targetSystem =
128+
config.customJmxScrapingConfigPath = properties.getProperty(CUSTOM_JMX_SCRAPING_CONFIG);
129+
String targetSystem =
133130
properties.getProperty(TARGET_SYSTEM, "").toLowerCase(Locale.ENGLISH).trim();
134131

135132
List<String> targets =
136-
Arrays.asList(
137-
isBlank(config.targetSystem) ? new String[0] : config.targetSystem.split(","));
138-
config.targetSystems = new LinkedHashSet<>(targets);
133+
Arrays.asList(isBlank(targetSystem) ? new String[0] : targetSystem.split(","));
134+
config.targetSystems = targets.stream().map(String::trim).collect(Collectors.toSet());
139135

140-
int interval = getProperty(INTERVAL_MILLISECONDS, 10000);
141-
config.intervalMilliseconds = interval == 0 ? 10000 : interval;
142-
// set for autoconfigure usage
143-
getAndSetProperty(EXPORTER_INTERVAL, config.intervalMilliseconds);
136+
int interval = getProperty(INTERVAL_MILLISECONDS, 0);
137+
config.intervalMilliseconds = (interval == 0 ? 10000 : interval);
138+
getAndSetPropertyIfUndefined(EXPORTER_INTERVAL, config.intervalMilliseconds);
144139

145-
config.metricsExporterType = getAndSetProperty(METRICS_EXPORTER_TYPE, "logging");
140+
config.metricsExporterType = getAndSetPropertyIfUndefined(METRICS_EXPORTER_TYPE, "logging");
146141
config.otlpExporterEndpoint = properties.getProperty(OTLP_ENDPOINT);
147142

148-
config.prometheusExporterHost = getAndSetProperty(PROMETHEUS_HOST, "0.0.0.0");
149-
config.prometheusExporterPort = getAndSetProperty(PROMETHEUS_PORT, 9464);
150-
151143
config.username = properties.getProperty(JMX_USERNAME);
152144
config.password = properties.getProperty(JMX_PASSWORD);
153145

@@ -156,6 +148,10 @@ JmxScraperConfig createConfig(Properties props) {
156148

157149
config.registrySsl = Boolean.parseBoolean(properties.getProperty(REGISTRY_SSL));
158150

151+
return config;
152+
}
153+
154+
private void populateJmxSystemProperties() {
159155
// For the list of System Properties, if they have been set in the properties file
160156
// they need to be set in Java System Properties.
161157
JAVA_SYSTEM_PROPERTIES.forEach(
@@ -170,8 +166,6 @@ JmxScraperConfig createConfig(Properties props) {
170166
System.setProperty(key, value);
171167
}
172168
});
173-
174-
return config;
175169
}
176170

177171
private int getProperty(String key, int defaultValue) {
@@ -189,15 +183,15 @@ private int getProperty(String key, int defaultValue) {
189183
/**
190184
* Similar to getProperty(key, defaultValue) but sets the property to default if not in object.
191185
*/
192-
private String getAndSetProperty(String key, String defaultValue) {
186+
private String getAndSetPropertyIfUndefined(String key, String defaultValue) {
193187
String propVal = properties.getProperty(key, defaultValue);
194188
if (propVal.equals(defaultValue)) {
195189
properties.setProperty(key, defaultValue);
196190
}
197191
return propVal;
198192
}
199193

200-
private int getAndSetProperty(String key, int defaultValue) {
194+
private int getAndSetPropertyIfUndefined(String key, int defaultValue) {
201195
int propVal = getProperty(key, defaultValue);
202196
if (propVal == defaultValue) {
203197
properties.setProperty(key, String.valueOf(defaultValue));
@@ -211,7 +205,7 @@ void validateConfig(JmxScraperConfig config) {
211205
throw new ConfigurationException(SERVICE_URL + " must be specified.");
212206
}
213207

214-
if (isBlank(config.customJmxScrapingConfig) && isBlank(config.targetSystem)) {
208+
if (isBlank(config.customJmxScrapingConfigPath) && config.targetSystems.isEmpty()) {
215209
throw new ConfigurationException(
216210
CUSTOM_JMX_SCRAPING_CONFIG + " or " + TARGET_SYSTEM + " must be specified.");
217211
}

0 commit comments

Comments
 (0)