1818import java .io .IOException ;
1919import java .io .InputStream ;
2020import java .nio .file .Files ;
21+ import java .nio .file .Path ;
2122import java .nio .file .Paths ;
2223import java .util .ArrayList ;
2324import 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