Skip to content

Commit

Permalink
rework JmxScraper creation
Browse files Browse the repository at this point in the history
  • Loading branch information
SylvainJuge committed Sep 25, 2024
1 parent 140ac17 commit 4c61c22
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
import java.util.logging.Logger;
import javax.management.MBeanServerConnection;
Expand All @@ -38,11 +37,12 @@ public class JmxScraper {
*/
@SuppressWarnings({"SystemOut", "SystemExitOutsideMain"})
public static void main(String[] args) {
JmxScraperConfig config;
JmxScraper jmxScraper = null;
try {
config = JmxScraper.createConfigFromArgs(Arrays.asList(args));
jmxScraper = new JmxScraper(config);
JmxScraperConfig config = JmxScraper.createConfigFromArgs(Arrays.asList(args));
// TODO: depend on instrumentation 2.9.0 snapshot
// service = JmxMetricInsight.createService(GlobalOpenTelemetry.get(), config.getIntervalMilliseconds());
JmxScraper jmxScraper = new JmxScraper(JmxRemoteClient.createNew(config.getServiceUrl()));
jmxScraper.start();

} catch (ArgumentsParsingException e) {
System.err.println("ERROR: " + e.getMessage());
Expand All @@ -53,14 +53,11 @@ public static void main(String[] args) {
} catch (ConfigurationException e) {
System.err.println(e.getMessage());
System.exit(1);
}

try {
Objects.requireNonNull(jmxScraper).start();
} catch (IOException e) {
System.err.println("Unable to connect " + e.getMessage());
System.exit(2);
}

}

/**
Expand Down Expand Up @@ -112,15 +109,8 @@ private static Properties loadPropertiesFromPath(String path)
}
}

JmxScraper(JmxScraperConfig config) throws ConfigurationException {
String serviceUrl = config.getServiceUrl();
int interval = config.getIntervalMilliseconds();
if (interval < 0) {
throw new ConfigurationException("interval must be positive");
}
this.client = JmxRemoteClient.createNew(serviceUrl);
// TODO: depend on instrumentation 2.9.0 snapshot
// this.service = JmxMetricInsight.createService(GlobalOpenTelemetry.get(), interval);
JmxScraper(JmxRemoteClient client) {
this.client = client;
}

private void start() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ public JmxScraperConfig createConfig(Properties props) throws ConfigurationExcep
int interval = getProperty(INTERVAL_MILLISECONDS, 0);
config.intervalMilliseconds = (interval == 0 ? 10000 : interval);
getAndSetPropertyIfUndefined(EXPORTER_INTERVAL, config.intervalMilliseconds);
if (config.intervalMilliseconds < 0) {
throw new ConfigurationException(
"interval must be positive, got " + config.intervalMilliseconds);
}

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

0 comments on commit 4c61c22

Please sign in to comment.