Skip to content

Commit

Permalink
Cleanup of config.
Browse files Browse the repository at this point in the history
Refactoring of JmxScraperConfigFactory.
Added first unit tests.
  • Loading branch information
robsunday committed Sep 11, 2024
1 parent 165fcb8 commit 164679f
Show file tree
Hide file tree
Showing 4 changed files with 364 additions and 59 deletions.
2 changes: 2 additions & 0 deletions jmx-scraper/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ dependencies {
implementation("io.opentelemetry:opentelemetry-sdk-testing")

implementation("io.opentelemetry.instrumentation:opentelemetry-jmx-metrics")

testImplementation("org.junit-pioneer:junit-pioneer")
}

tasks {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,13 @@
/** This class keeps application settings */
public class JmxScraperConfig {
String serviceUrl = "";
String customJmxScrapingConfig = "";
String targetSystem = "";
String customJmxScrapingConfigPath = "";
Set<String> targetSystems = Collections.emptySet();
int intervalMilliseconds;
String metricsExporterType = "";

String otlpExporterEndpoint = "";

String prometheusExporterHost = "";
int prometheusExporterPort;

String username = "";
String password = "";
String realm = "";
Expand All @@ -34,12 +30,8 @@ public String getServiceUrl() {
return serviceUrl;
}

public String getCustomJmxScrapingConfig() {
return customJmxScrapingConfig;
}

public String getTargetSystem() {
return targetSystem;
public String getCustomJmxScrapingConfigPath() {
return customJmxScrapingConfigPath;
}

public Set<String> getTargetSystems() {
Expand All @@ -58,14 +50,6 @@ public String getOtlpExporterEndpoint() {
return otlpExporterEndpoint;
}

public String getPrometheusExporterHost() {
return prometheusExporterHost;
}

public int getPrometheusExporterPort() {
return prometheusExporterPort;
}

public String getUsername() {
return username;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,32 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.stream.Collectors;

@SuppressWarnings({"SystemOut", "SystemExitOutsideMain"})
public class JmxScraperConfigFactory {
private static final String PREFIX = "otel.";
private static final String SERVICE_URL = PREFIX + "jmx.service.url";
private static final String CUSTOM_JMX_SCRAPING_CONFIG =
PREFIX + "jmx.custom.jmx.scraping.config";
private static final String TARGET_SYSTEM = PREFIX + "jmx.target.system";
private static final String INTERVAL_MILLISECONDS = PREFIX + "jmx.interval.milliseconds";
private static final String METRICS_EXPORTER_TYPE = PREFIX + "metrics.exporter";
private static final String EXPORTER = PREFIX + "exporter.";
private static final String REGISTRY_SSL = PREFIX + "jmx.remote.registry.ssl";
private static final String EXPORTER_INTERVAL = PREFIX + "metric.export.interval";

private static final String OTLP_ENDPOINT = EXPORTER + "otlp.endpoint";

private static final String PROMETHEUS_HOST = EXPORTER + "prometheus.host";
private static final String PROMETHEUS_PORT = EXPORTER + "prometheus.port";

private static final String JMX_USERNAME = PREFIX + "jmx.username";
private static final String JMX_PASSWORD = PREFIX + "jmx.password";
private static final String JMX_REMOTE_PROFILE = PREFIX + "jmx.remote.profile";
private static final String JMX_REALM = PREFIX + "jmx.realm";
static final String SERVICE_URL = PREFIX + "jmx.service.url";
static final String CUSTOM_JMX_SCRAPING_CONFIG = PREFIX + "jmx.custom.jmx.scraping.config";
static final String TARGET_SYSTEM = PREFIX + "jmx.target.system";
static final String INTERVAL_MILLISECONDS = PREFIX + "jmx.interval.milliseconds";
static final String METRICS_EXPORTER_TYPE = PREFIX + "metrics.exporter";
static final String EXPORTER_INTERVAL = PREFIX + "metric.export.interval";
static final String REGISTRY_SSL = PREFIX + "jmx.remote.registry.ssl";

static final String OTLP_ENDPOINT = PREFIX + "exporter.otlp.endpoint";

static final String JMX_USERNAME = PREFIX + "jmx.username";
static final String JMX_PASSWORD = PREFIX + "jmx.password";
static final String JMX_REMOTE_PROFILE = PREFIX + "jmx.remote.profile";
static final String JMX_REALM = PREFIX + "jmx.realm";

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

private static final List<String> AVAILABLE_TARGET_SYSTEMS =
static final List<String> AVAILABLE_TARGET_SYSTEMS =
Arrays.asList(
"activemq",
"cassandra",
Expand Down Expand Up @@ -94,6 +89,8 @@ public JmxScraperConfig createConfigFromArgs(List<String> args) {

JmxScraperConfig config = createConfig(loadedProperties);
validateConfig(config);
populateJmxSystemProperties();

return config;
}

Expand Down Expand Up @@ -128,26 +125,21 @@ JmxScraperConfig createConfig(Properties props) {
JmxScraperConfig config = new JmxScraperConfig();

config.serviceUrl = properties.getProperty(SERVICE_URL);
config.customJmxScrapingConfig = properties.getProperty(CUSTOM_JMX_SCRAPING_CONFIG);
config.targetSystem =
config.customJmxScrapingConfigPath = properties.getProperty(CUSTOM_JMX_SCRAPING_CONFIG);
String targetSystem =
properties.getProperty(TARGET_SYSTEM, "").toLowerCase(Locale.ENGLISH).trim();

List<String> targets =
Arrays.asList(
isBlank(config.targetSystem) ? new String[0] : config.targetSystem.split(","));
config.targetSystems = new LinkedHashSet<>(targets);
Arrays.asList(isBlank(targetSystem) ? new String[0] : targetSystem.split(","));
config.targetSystems = targets.stream().map(String::trim).collect(Collectors.toSet());

int interval = getProperty(INTERVAL_MILLISECONDS, 10000);
config.intervalMilliseconds = interval == 0 ? 10000 : interval;
// set for autoconfigure usage
getAndSetProperty(EXPORTER_INTERVAL, config.intervalMilliseconds);
int interval = getProperty(INTERVAL_MILLISECONDS, 0);
config.intervalMilliseconds = (interval == 0 ? 10000 : interval);
getAndSetPropertyIfUndefined(EXPORTER_INTERVAL, config.intervalMilliseconds);

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

config.prometheusExporterHost = getAndSetProperty(PROMETHEUS_HOST, "0.0.0.0");
config.prometheusExporterPort = getAndSetProperty(PROMETHEUS_PORT, 9464);

config.username = properties.getProperty(JMX_USERNAME);
config.password = properties.getProperty(JMX_PASSWORD);

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

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

return config;
}

private void populateJmxSystemProperties() {
// For the list of System Properties, if they have been set in the properties file
// they need to be set in Java System Properties.
JAVA_SYSTEM_PROPERTIES.forEach(
Expand All @@ -170,8 +166,6 @@ JmxScraperConfig createConfig(Properties props) {
System.setProperty(key, value);
}
});

return config;
}

private int getProperty(String key, int defaultValue) {
Expand All @@ -189,15 +183,15 @@ private int getProperty(String key, int defaultValue) {
/**
* Similar to getProperty(key, defaultValue) but sets the property to default if not in object.
*/
private String getAndSetProperty(String key, String defaultValue) {
private String getAndSetPropertyIfUndefined(String key, String defaultValue) {
String propVal = properties.getProperty(key, defaultValue);
if (propVal.equals(defaultValue)) {
properties.setProperty(key, defaultValue);
}
return propVal;
}

private int getAndSetProperty(String key, int defaultValue) {
private int getAndSetPropertyIfUndefined(String key, int defaultValue) {
int propVal = getProperty(key, defaultValue);
if (propVal == defaultValue) {
properties.setProperty(key, String.valueOf(defaultValue));
Expand All @@ -211,7 +205,7 @@ void validateConfig(JmxScraperConfig config) {
throw new ConfigurationException(SERVICE_URL + " must be specified.");
}

if (isBlank(config.customJmxScrapingConfig) && isBlank(config.targetSystem)) {
if (isBlank(config.customJmxScrapingConfigPath) && config.targetSystems.isEmpty()) {
throw new ConfigurationException(
CUSTOM_JMX_SCRAPING_CONFIG + " or " + TARGET_SYSTEM + " must be specified.");
}
Expand Down
Loading

0 comments on commit 164679f

Please sign in to comment.