diff --git a/jmx-scraper/src/main/java/io/opentelemetry/contrib/jmxscraper/JmxScraper.java b/jmx-scraper/src/main/java/io/opentelemetry/contrib/jmxscraper/JmxScraper.java index 678368d7b..9537b9471 100644 --- a/jmx-scraper/src/main/java/io/opentelemetry/contrib/jmxscraper/JmxScraper.java +++ b/jmx-scraper/src/main/java/io/opentelemetry/contrib/jmxscraper/JmxScraper.java @@ -16,9 +16,8 @@ import io.opentelemetry.contrib.jmxscraper.config.JmxScraperConfig; import io.opentelemetry.contrib.jmxscraper.config.PropertiesCustomizer; import io.opentelemetry.contrib.jmxscraper.config.PropertiesSupplier; -import io.opentelemetry.instrumentation.jmx.engine.JmxMetricInsight; -import io.opentelemetry.instrumentation.jmx.engine.MetricConfiguration; -import io.opentelemetry.instrumentation.jmx.yaml.RuleParser; +import io.opentelemetry.instrumentation.jmx.JmxTelemetry; +import io.opentelemetry.instrumentation.jmx.JmxTelemetryBuilder; import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException; @@ -30,6 +29,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -51,10 +51,9 @@ public final class JmxScraper { private static final String TEST_ARG = "-test"; private final JmxConnectorBuilder client; - private final JmxMetricInsight service; - private final JmxScraperConfig config; private final AtomicBoolean running = new AtomicBoolean(false); + private final JmxTelemetry jmxTelemetry; /** * Main method to create and run a {@link JmxScraper} instance. @@ -105,10 +104,7 @@ public static void main(String[] args) { if (testMode) { System.exit(testConnection(connectorBuilder) ? 0 : 1); } else { - JmxMetricInsight jmxInsight = - JmxMetricInsight.createService( - openTelemetry, scraperConfig.getSamplingInterval().toMillis()); - JmxScraper jmxScraper = new JmxScraper(connectorBuilder, jmxInsight, scraperConfig); + JmxScraper jmxScraper = new JmxScraper(connectorBuilder, openTelemetry, scraperConfig); jmxScraper.start(); } } catch (ConfigurationException e) { @@ -233,10 +229,42 @@ private static Properties loadPropertiesFromPath(String path) throws InvalidArgu } private JmxScraper( - JmxConnectorBuilder client, JmxMetricInsight service, JmxScraperConfig config) { + JmxConnectorBuilder client, OpenTelemetry openTelemetry, JmxScraperConfig config) { this.client = client; - this.service = service; - this.config = config; + this.jmxTelemetry = createJmxTelemetry(openTelemetry, config); + } + + private static JmxTelemetry createJmxTelemetry( + OpenTelemetry openTelemetry, JmxScraperConfig config) { + + JmxTelemetryBuilder builder = JmxTelemetry.builder(openTelemetry); + builder.beanDiscoveryDelay(config.getSamplingInterval()); + + // Unfortunately we can't use the convenient 'addClassPathRules' here as it does not yet + // allow to customize the path of the yaml resources in classpath. + // config.getTargetSystems().forEach(builder::addClassPathRules); + // + // As a temporary workaround we load configuration through temporary files and register them + // as if they were custom rules. + config + .getTargetSystems() + .forEach( + system -> { + try (InputStream input = config.getTargetSystemYaml(system)) { + Path tempFile = Files.createTempFile("jmx-scraper-" + system, ".yaml"); + try { + Files.copy(input, tempFile, StandardCopyOption.REPLACE_EXISTING); + builder.addCustomRules(tempFile); + } finally { + Files.delete(tempFile); + } + } catch (IOException e) { + throw new IllegalStateException(e); + } + }); + + config.getJmxConfig().stream().map(Paths::get).forEach(builder::addCustomRules); + return builder.build(); } private void start() throws IOException { @@ -250,7 +278,8 @@ private void start() throws IOException { try (JMXConnector connector = client.build()) { MBeanServerConnection connection = connector.getMBeanServerConnection(); - service.startRemote(getMetricConfig(config), () -> singletonList(connection)); + + jmxTelemetry.start(() -> singletonList(connection)); running.set(true); logger.info("JMX scraping started"); @@ -264,32 +293,4 @@ private void start() throws IOException { } } } - - private static MetricConfiguration getMetricConfig(JmxScraperConfig scraperConfig) { - MetricConfiguration config = new MetricConfiguration(); - for (String system : scraperConfig.getTargetSystems()) { - try (InputStream yaml = scraperConfig.getTargetSystemYaml(system)) { - RuleParser.get().addMetricDefsTo(config, yaml, system); - } catch (IOException e) { - throw new IllegalStateException("Error while loading rules for system " + system, e); - } - } - for (String file : scraperConfig.getJmxConfig()) { - addRulesFromFile(file, config); - } - return config; - } - - private static void addRulesFromFile(String file, MetricConfiguration conf) { - Path path = Paths.get(file); - if (!Files.isReadable(path)) { - throw new IllegalArgumentException("Unable to read file: " + path); - } - - try (InputStream inputStream = Files.newInputStream(path)) { - RuleParser.get().addMetricDefsTo(conf, inputStream, file); - } catch (IOException e) { - throw new IllegalArgumentException("Error while loading rules from file: " + file, e); - } - } }