Skip to content

Commit 1271bba

Browse files
committed
refactor to use logger for text output
1 parent ce43846 commit 1271bba

File tree

5 files changed

+17
-15
lines changed

5 files changed

+17
-15
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.Optional;
2727
import java.util.Properties;
2828
import java.util.concurrent.atomic.AtomicBoolean;
29+
import java.util.logging.Level;
2930
import java.util.logging.Logger;
3031
import javax.management.MBeanServerConnection;
3132
import javax.management.remote.JMXConnector;
@@ -45,11 +46,11 @@ public class JmxScraper {
4546
*
4647
* @param args - must be of the form "-config {jmx_config_path,'-'}"
4748
*/
48-
@SuppressWarnings({"SystemOut", "SystemExitOutsideMain"})
49+
@SuppressWarnings("SystemExitOutsideMain")
4950
public static void main(String[] args) {
5051

5152
// set log format
52-
System.setProperty("java.util.logging.SimpleFormatter.format", "%1$tFT%1$tT.%1$tL%1$tz | %4$s: %5$s%n");
53+
System.setProperty("java.util.logging.SimpleFormatter.format", "%1$tF %1$tT %4$s %5$s%n");
5354

5455
try {
5556
Properties argsConfig = parseArgs(Arrays.asList(args));
@@ -77,18 +78,19 @@ public static void main(String[] args) {
7778
JmxScraper jmxScraper = new JmxScraper(connectorBuilder, service, scraperConfig);
7879
jmxScraper.start();
7980
} catch (ConfigurationException e) {
80-
System.err.println("ERROR: invalid configuration " + e.getMessage());
81+
logger.log(Level.SEVERE, "ERROR: invalid configuration ", e);
8182
System.exit(1);
8283
} catch (ArgumentsParsingException e) {
83-
System.err.println(
84+
logger.log(Level.SEVERE, "ERROR: invalid configuration provided through arguments", e);
85+
logger.info(
8486
"Usage: java -jar <path_to_jmxscraper.jar> "
8587
+ "-config <path_to_config.properties or - for stdin>");
8688
System.exit(1);
8789
} catch (IOException e) {
88-
System.err.println("Unable to connect " + e.getMessage());
90+
logger.log(Level.SEVERE, "Unable to connect ", e);
8991
System.exit(2);
9092
} catch (RuntimeException e) {
91-
e.printStackTrace(System.err);
93+
logger.log(Level.SEVERE, e.getMessage(), e);
9294
System.exit(3);
9395
}
9496
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,13 @@ public static JmxScraperConfig fromConfig(ConfigProperties config) {
137137
String customConfig = config.getString(JMX_CUSTOM_CONFIG);
138138
List<String> targetSystem = config.getList(JMX_TARGET_SYSTEM);
139139
if (targetSystem.isEmpty() && customConfig == null) {
140-
throw new ConfigurationException("at least one of '" + JMX_TARGET_SYSTEM + "' or '" + JMX_CUSTOM_CONFIG + "' must be set");
140+
throw new ConfigurationException(
141+
"at least one of '" + JMX_TARGET_SYSTEM + "' or '" + JMX_CUSTOM_CONFIG + "' must be set");
141142
}
142143
targetSystem.forEach(
143144
s -> {
144145
if (!AVAILABLE_TARGET_SYSTEMS.contains(s)) {
145-
throw new ConfigurationException(
146-
"unsupported target system: '" + s + "'");
146+
throw new ConfigurationException("unsupported target system: '" + s + "'");
147147
}
148148
});
149149
scraperConfig.customJmxScrapingConfigPath = customConfig;
@@ -157,5 +157,4 @@ public static JmxScraperConfig fromConfig(ConfigProperties config) {
157157

158158
return scraperConfig;
159159
}
160-
161160
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ public Map<String, String> apply(ConfigProperties config) {
3737
// providing compatibility with the existing 'otel.jmx.interval.milliseconds' config option
3838
long intervalLegacy = config.getLong(JMX_INTERVAL_LEGACY, -1);
3939
if (config.getDuration(METRIC_EXPORT_INTERVAL) == null && intervalLegacy > 0) {
40-
logger.warning(METRIC_EXPORT_INTERVAL + " deprecated option is used, replacing with '"
41-
+ METRIC_EXPORT_INTERVAL + "' metric sdk configuration is recommended");
40+
logger.warning(
41+
METRIC_EXPORT_INTERVAL
42+
+ " deprecated option is used, replacing with '"
43+
+ METRIC_EXPORT_INTERVAL
44+
+ "' metric sdk configuration is recommended");
4245
result.put(METRIC_EXPORT_INTERVAL, intervalLegacy + "ms");
4346
}
4447

jmx-scraper/src/test/java/io/opentelemetry/contrib/jmxscraper/config/JmxScraperConfigTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ void shouldPassValidation() {
6363
void shouldCreateMinimalValidConfiguration() {
6464
// Given
6565
Properties properties = new Properties();
66-
properties.setProperty(
67-
JMX_SERVICE_URL, "jservice:jmx:rmi:///jndi/rmi://localhost:9010/jmxrmi");
66+
properties.setProperty(JMX_SERVICE_URL, "jservice:jmx:rmi:///jndi/rmi://localhost:9010/jmxrmi");
6867
properties.setProperty(JMX_CUSTOM_CONFIG, "/file.properties");
6968

7069
// When

jmx-scraper/src/test/java/io/opentelemetry/contrib/jmxscraper/config/PropertiesCustomizerTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,5 @@ void sdkMetricExportIntervalPriority() {
9898
assertThat(customizer.getScraperConfig().getSamplingInterval())
9999
.describedAs("jmx export interval must be ignored")
100100
.isEqualTo(Duration.ofSeconds(15));
101-
102101
}
103102
}

0 commit comments

Comments
 (0)