Skip to content

Commit 4c61c22

Browse files
committed
rework JmxScraper creation
1 parent 140ac17 commit 4c61c22

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

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

+8-18
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import java.nio.file.Paths;
1717
import java.util.Arrays;
1818
import java.util.List;
19-
import java.util.Objects;
2019
import java.util.Properties;
2120
import java.util.logging.Logger;
2221
import javax.management.MBeanServerConnection;
@@ -38,11 +37,12 @@ public class JmxScraper {
3837
*/
3938
@SuppressWarnings({"SystemOut", "SystemExitOutsideMain"})
4039
public static void main(String[] args) {
41-
JmxScraperConfig config;
42-
JmxScraper jmxScraper = null;
4340
try {
44-
config = JmxScraper.createConfigFromArgs(Arrays.asList(args));
45-
jmxScraper = new JmxScraper(config);
41+
JmxScraperConfig config = JmxScraper.createConfigFromArgs(Arrays.asList(args));
42+
// TODO: depend on instrumentation 2.9.0 snapshot
43+
// service = JmxMetricInsight.createService(GlobalOpenTelemetry.get(), config.getIntervalMilliseconds());
44+
JmxScraper jmxScraper = new JmxScraper(JmxRemoteClient.createNew(config.getServiceUrl()));
45+
jmxScraper.start();
4646

4747
} catch (ArgumentsParsingException e) {
4848
System.err.println("ERROR: " + e.getMessage());
@@ -53,14 +53,11 @@ public static void main(String[] args) {
5353
} catch (ConfigurationException e) {
5454
System.err.println(e.getMessage());
5555
System.exit(1);
56-
}
57-
58-
try {
59-
Objects.requireNonNull(jmxScraper).start();
6056
} catch (IOException e) {
6157
System.err.println("Unable to connect " + e.getMessage());
6258
System.exit(2);
6359
}
60+
6461
}
6562

6663
/**
@@ -112,15 +109,8 @@ private static Properties loadPropertiesFromPath(String path)
112109
}
113110
}
114111

115-
JmxScraper(JmxScraperConfig config) throws ConfigurationException {
116-
String serviceUrl = config.getServiceUrl();
117-
int interval = config.getIntervalMilliseconds();
118-
if (interval < 0) {
119-
throw new ConfigurationException("interval must be positive");
120-
}
121-
this.client = JmxRemoteClient.createNew(serviceUrl);
122-
// TODO: depend on instrumentation 2.9.0 snapshot
123-
// this.service = JmxMetricInsight.createService(GlobalOpenTelemetry.get(), interval);
112+
JmxScraper(JmxRemoteClient client) {
113+
this.client = client;
124114
}
125115

126116
private void start() throws IOException {

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

+4
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ public JmxScraperConfig createConfig(Properties props) throws ConfigurationExcep
8181
int interval = getProperty(INTERVAL_MILLISECONDS, 0);
8282
config.intervalMilliseconds = (interval == 0 ? 10000 : interval);
8383
getAndSetPropertyIfUndefined(EXPORTER_INTERVAL, config.intervalMilliseconds);
84+
if (config.intervalMilliseconds < 0) {
85+
throw new ConfigurationException(
86+
"interval must be positive, got " + config.intervalMilliseconds);
87+
}
8488

8589
config.metricsExporterType = getAndSetPropertyIfUndefined(METRICS_EXPORTER_TYPE, "logging");
8690
config.otlpExporterEndpoint = properties.getProperty(OTLP_ENDPOINT);

0 commit comments

Comments
 (0)