1616import io .opentelemetry .contrib .jmxscraper .config .JmxScraperConfig ;
1717import io .opentelemetry .contrib .jmxscraper .config .PropertiesCustomizer ;
1818import io .opentelemetry .contrib .jmxscraper .config .PropertiesSupplier ;
19- import io .opentelemetry .instrumentation .jmx .engine .JmxMetricInsight ;
20- import io .opentelemetry .instrumentation .jmx .engine .MetricConfiguration ;
21- import io .opentelemetry .instrumentation .jmx .yaml .RuleParser ;
19+ import io .opentelemetry .instrumentation .jmx .JmxTelemetry ;
20+ import io .opentelemetry .instrumentation .jmx .JmxTelemetryBuilder ;
2221import io .opentelemetry .sdk .autoconfigure .AutoConfiguredOpenTelemetrySdk ;
2322import io .opentelemetry .sdk .autoconfigure .spi .ConfigProperties ;
2423import io .opentelemetry .sdk .autoconfigure .spi .ConfigurationException ;
3029import java .nio .file .Files ;
3130import java .nio .file .Path ;
3231import java .nio .file .Paths ;
32+ import java .nio .file .StandardCopyOption ;
3333import java .util .ArrayList ;
3434import java .util .Arrays ;
3535import java .util .List ;
@@ -51,10 +51,9 @@ public final class JmxScraper {
5151 private static final String TEST_ARG = "-test" ;
5252
5353 private final JmxConnectorBuilder client ;
54- private final JmxMetricInsight service ;
55- private final JmxScraperConfig config ;
5654
5755 private final AtomicBoolean running = new AtomicBoolean (false );
56+ private final JmxTelemetry jmxTelemetry ;
5857
5958 /**
6059 * Main method to create and run a {@link JmxScraper} instance.
@@ -105,10 +104,7 @@ public static void main(String[] args) {
105104 if (testMode ) {
106105 System .exit (testConnection (connectorBuilder ) ? 0 : 1 );
107106 } else {
108- JmxMetricInsight jmxInsight =
109- JmxMetricInsight .createService (
110- openTelemetry , scraperConfig .getSamplingInterval ().toMillis ());
111- JmxScraper jmxScraper = new JmxScraper (connectorBuilder , jmxInsight , scraperConfig );
107+ JmxScraper jmxScraper = new JmxScraper (connectorBuilder , openTelemetry , scraperConfig );
112108 jmxScraper .start ();
113109 }
114110 } catch (ConfigurationException e ) {
@@ -233,10 +229,42 @@ private static Properties loadPropertiesFromPath(String path) throws InvalidArgu
233229 }
234230
235231 private JmxScraper (
236- JmxConnectorBuilder client , JmxMetricInsight service , JmxScraperConfig config ) {
232+ JmxConnectorBuilder client , OpenTelemetry openTelemetry , JmxScraperConfig config ) {
237233 this .client = client ;
238- this .service = service ;
239- this .config = config ;
234+ this .jmxTelemetry = createJmxTelemetry (openTelemetry , config );
235+ }
236+
237+ private static JmxTelemetry createJmxTelemetry (
238+ OpenTelemetry openTelemetry , JmxScraperConfig config ) {
239+
240+ JmxTelemetryBuilder builder = JmxTelemetry .builder (openTelemetry );
241+ builder .beanDiscoveryDelay (config .getSamplingInterval ());
242+
243+ // Unfortunately we can't use the convenient 'addClassPathRules' here as it does not yet
244+ // allow to customize the path of the yaml resources in classpath.
245+ // config.getTargetSystems().forEach(builder::addClassPathRules);
246+ //
247+ // As a temporary workaround we load configuration through temporary files and register them
248+ // as if they were custom rules.
249+ config
250+ .getTargetSystems ()
251+ .forEach (
252+ system -> {
253+ try (InputStream input = config .getTargetSystemYaml (system )) {
254+ Path tempFile = Files .createTempFile ("jmx-scraper-" + system , ".yaml" );
255+ try {
256+ Files .copy (input , tempFile , StandardCopyOption .REPLACE_EXISTING );
257+ builder .addCustomRules (tempFile );
258+ } finally {
259+ Files .delete (tempFile );
260+ }
261+ } catch (IOException e ) {
262+ throw new IllegalStateException (e );
263+ }
264+ });
265+
266+ config .getJmxConfig ().stream ().map (Paths ::get ).forEach (builder ::addCustomRules );
267+ return builder .build ();
240268 }
241269
242270 private void start () throws IOException {
@@ -250,7 +278,8 @@ private void start() throws IOException {
250278
251279 try (JMXConnector connector = client .build ()) {
252280 MBeanServerConnection connection = connector .getMBeanServerConnection ();
253- service .startRemote (getMetricConfig (config ), () -> singletonList (connection ));
281+
282+ jmxTelemetry .start (() -> singletonList (connection ));
254283
255284 running .set (true );
256285 logger .info ("JMX scraping started" );
@@ -264,32 +293,4 @@ private void start() throws IOException {
264293 }
265294 }
266295 }
267-
268- private static MetricConfiguration getMetricConfig (JmxScraperConfig scraperConfig ) {
269- MetricConfiguration config = new MetricConfiguration ();
270- for (String system : scraperConfig .getTargetSystems ()) {
271- try (InputStream yaml = scraperConfig .getTargetSystemYaml (system )) {
272- RuleParser .get ().addMetricDefsTo (config , yaml , system );
273- } catch (IOException e ) {
274- throw new IllegalStateException ("Error while loading rules for system " + system , e );
275- }
276- }
277- for (String file : scraperConfig .getJmxConfig ()) {
278- addRulesFromFile (file , config );
279- }
280- return config ;
281- }
282-
283- private static void addRulesFromFile (String file , MetricConfiguration conf ) {
284- Path path = Paths .get (file );
285- if (!Files .isReadable (path )) {
286- throw new IllegalArgumentException ("Unable to read file: " + path );
287- }
288-
289- try (InputStream inputStream = Files .newInputStream (path )) {
290- RuleParser .get ().addMetricDefsTo (conf , inputStream , file );
291- } catch (IOException e ) {
292- throw new IllegalArgumentException ("Error while loading rules from file: " + file , e );
293- }
294- }
295296}
0 commit comments