Skip to content

Commit 3a4e519

Browse files
committed
add missing parsing of custom yaml
1 parent b3f93ad commit 3a4e519

File tree

1 file changed

+18
-3
lines changed
  • jmx-scraper/src/main/java/io/opentelemetry/contrib/jmxscraper

1 file changed

+18
-3
lines changed

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.IOException;
1919
import java.io.InputStream;
2020
import java.nio.file.Files;
21+
import java.nio.file.Path;
2122
import java.nio.file.Paths;
2223
import java.util.ArrayList;
2324
import java.util.Arrays;
@@ -225,7 +226,9 @@ private static MetricConfiguration getMetricConfig(JmxScraperConfig scraperConfi
225226
for (String system : scraperConfig.getTargetSystems()) {
226227
addRulesForSystem(system, config);
227228
}
228-
// TODO : add ability for user to provide custom yaml configurations
229+
for (String file : scraperConfig.getJmxConfig()) {
230+
addRulesFromFile(file, config);
231+
}
229232
return config;
230233
}
231234

@@ -234,13 +237,25 @@ private static void addRulesForSystem(String system, MetricConfiguration conf) {
234237
try (InputStream inputStream =
235238
JmxScraper.class.getClassLoader().getResourceAsStream(yamlResource)) {
236239
if (inputStream != null) {
237-
RuleParser parserInstance = RuleParser.get();
238-
parserInstance.addMetricDefsTo(conf, inputStream, system);
240+
RuleParser.get().addMetricDefsTo(conf, inputStream, system);
239241
} else {
240242
throw new IllegalArgumentException("No support for system " + system);
241243
}
242244
} catch (Exception e) {
243245
throw new IllegalStateException("Error while loading rules for system " + system, e);
244246
}
245247
}
248+
249+
private static void addRulesFromFile(String file, MetricConfiguration conf) {
250+
Path path = Paths.get(file);
251+
if (!Files.isReadable(path)) {
252+
throw new IllegalArgumentException("Unable to read file: " + path);
253+
}
254+
255+
try (InputStream inputStream = Files.newInputStream(path)) {
256+
RuleParser.get().addMetricDefsTo(conf, inputStream, file);
257+
} catch (IOException e) {
258+
throw new IllegalArgumentException("Error while loading rules from file: " + file, e);
259+
}
260+
}
246261
}

0 commit comments

Comments
 (0)