13
13
import java .nio .file .Files ;
14
14
import java .nio .file .Paths ;
15
15
import java .util .Arrays ;
16
- import java .util .LinkedHashSet ;
17
16
import java .util .List ;
18
17
import java .util .Locale ;
19
18
import java .util .Properties ;
19
+ import java .util .stream .Collectors ;
20
20
21
21
@ SuppressWarnings ({"SystemOut" , "SystemExitOutsideMain" })
22
22
public class JmxScraperConfigFactory {
23
23
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" ;
43
38
44
39
// These properties need to be copied into System Properties if provided via the property
45
40
// 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 =
47
42
Arrays .asList (
48
43
"javax.net.ssl.keyStore" ,
49
44
"javax.net.ssl.keyStorePassword" ,
@@ -52,7 +47,7 @@ public class JmxScraperConfigFactory {
52
47
"javax.net.ssl.trustStorePassword" ,
53
48
"javax.net.ssl.trustStoreType" );
54
49
55
- private static final List <String > AVAILABLE_TARGET_SYSTEMS =
50
+ static final List <String > AVAILABLE_TARGET_SYSTEMS =
56
51
Arrays .asList (
57
52
"activemq" ,
58
53
"cassandra" ,
@@ -94,6 +89,8 @@ public JmxScraperConfig createConfigFromArgs(List<String> args) {
94
89
95
90
JmxScraperConfig config = createConfig (loadedProperties );
96
91
validateConfig (config );
92
+ populateJmxSystemProperties ();
93
+
97
94
return config ;
98
95
}
99
96
@@ -128,26 +125,21 @@ JmxScraperConfig createConfig(Properties props) {
128
125
JmxScraperConfig config = new JmxScraperConfig ();
129
126
130
127
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 =
133
130
properties .getProperty (TARGET_SYSTEM , "" ).toLowerCase (Locale .ENGLISH ).trim ();
134
131
135
132
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 ());
139
135
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 );
144
139
145
- config .metricsExporterType = getAndSetProperty (METRICS_EXPORTER_TYPE , "logging" );
140
+ config .metricsExporterType = getAndSetPropertyIfUndefined (METRICS_EXPORTER_TYPE , "logging" );
146
141
config .otlpExporterEndpoint = properties .getProperty (OTLP_ENDPOINT );
147
142
148
- config .prometheusExporterHost = getAndSetProperty (PROMETHEUS_HOST , "0.0.0.0" );
149
- config .prometheusExporterPort = getAndSetProperty (PROMETHEUS_PORT , 9464 );
150
-
151
143
config .username = properties .getProperty (JMX_USERNAME );
152
144
config .password = properties .getProperty (JMX_PASSWORD );
153
145
@@ -156,6 +148,10 @@ JmxScraperConfig createConfig(Properties props) {
156
148
157
149
config .registrySsl = Boolean .parseBoolean (properties .getProperty (REGISTRY_SSL ));
158
150
151
+ return config ;
152
+ }
153
+
154
+ private void populateJmxSystemProperties () {
159
155
// For the list of System Properties, if they have been set in the properties file
160
156
// they need to be set in Java System Properties.
161
157
JAVA_SYSTEM_PROPERTIES .forEach (
@@ -170,8 +166,6 @@ JmxScraperConfig createConfig(Properties props) {
170
166
System .setProperty (key , value );
171
167
}
172
168
});
173
-
174
- return config ;
175
169
}
176
170
177
171
private int getProperty (String key , int defaultValue ) {
@@ -189,15 +183,15 @@ private int getProperty(String key, int defaultValue) {
189
183
/**
190
184
* Similar to getProperty(key, defaultValue) but sets the property to default if not in object.
191
185
*/
192
- private String getAndSetProperty (String key , String defaultValue ) {
186
+ private String getAndSetPropertyIfUndefined (String key , String defaultValue ) {
193
187
String propVal = properties .getProperty (key , defaultValue );
194
188
if (propVal .equals (defaultValue )) {
195
189
properties .setProperty (key , defaultValue );
196
190
}
197
191
return propVal ;
198
192
}
199
193
200
- private int getAndSetProperty (String key , int defaultValue ) {
194
+ private int getAndSetPropertyIfUndefined (String key , int defaultValue ) {
201
195
int propVal = getProperty (key , defaultValue );
202
196
if (propVal == defaultValue ) {
203
197
properties .setProperty (key , String .valueOf (defaultValue ));
@@ -211,7 +205,7 @@ void validateConfig(JmxScraperConfig config) {
211
205
throw new ConfigurationException (SERVICE_URL + " must be specified." );
212
206
}
213
207
214
- if (isBlank (config .customJmxScrapingConfig ) && isBlank ( config .targetSystem )) {
208
+ if (isBlank (config .customJmxScrapingConfigPath ) && config .targetSystems . isEmpty ( )) {
215
209
throw new ConfigurationException (
216
210
CUSTOM_JMX_SCRAPING_CONFIG + " or " + TARGET_SYSTEM + " must be specified." );
217
211
}
0 commit comments