1313import java .nio .file .Files ;
1414import java .nio .file .Paths ;
1515import java .util .Arrays ;
16- import java .util .LinkedHashSet ;
1716import java .util .List ;
1817import java .util .Locale ;
1918import java .util .Properties ;
19+ import java .util .stream .Collectors ;
2020
2121@ SuppressWarnings ({"SystemOut" , "SystemExitOutsideMain" })
2222public 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