Skip to content

Commit 1a506f6

Browse files
committed
make jmx-scraper use JmxTelemetry new API
1 parent c18bee7 commit 1a506f6

1 file changed

Lines changed: 16 additions & 42 deletions

File tree

  • jmx-scraper/src/main/java/io/opentelemetry/contrib/jmxscraper

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

Lines changed: 16 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616
import io.opentelemetry.contrib.jmxscraper.config.JmxScraperConfig;
1717
import io.opentelemetry.contrib.jmxscraper.config.PropertiesCustomizer;
1818
import 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;
2221
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
2322
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
2423
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
@@ -28,7 +27,6 @@
2827
import java.io.InputStream;
2928
import java.nio.charset.StandardCharsets;
3029
import java.nio.file.Files;
31-
import java.nio.file.Path;
3230
import java.nio.file.Paths;
3331
import java.util.ArrayList;
3432
import java.util.Arrays;
@@ -51,10 +49,9 @@ public final class JmxScraper {
5149
private static final String TEST_ARG = "-test";
5250

5351
private final JmxConnectorBuilder client;
54-
private final JmxMetricInsight service;
55-
private final JmxScraperConfig config;
5652

5753
private final AtomicBoolean running = new AtomicBoolean(false);
54+
private final JmxTelemetry jmxTelemetry;
5855

5956
/**
6057
* Main method to create and run a {@link JmxScraper} instance.
@@ -105,10 +102,7 @@ public static void main(String[] args) {
105102
if (testMode) {
106103
System.exit(testConnection(connectorBuilder) ? 0 : 1);
107104
} else {
108-
JmxMetricInsight jmxInsight =
109-
JmxMetricInsight.createService(
110-
openTelemetry, scraperConfig.getSamplingInterval().toMillis());
111-
JmxScraper jmxScraper = new JmxScraper(connectorBuilder, jmxInsight, scraperConfig);
105+
JmxScraper jmxScraper = new JmxScraper(connectorBuilder, openTelemetry, scraperConfig);
112106
jmxScraper.start();
113107
}
114108
} catch (ConfigurationException e) {
@@ -233,10 +227,17 @@ private static Properties loadPropertiesFromPath(String path) throws InvalidArgu
233227
}
234228

235229
private JmxScraper(
236-
JmxConnectorBuilder client, JmxMetricInsight service, JmxScraperConfig config) {
230+
JmxConnectorBuilder client, OpenTelemetry openTelemetry, JmxScraperConfig config) {
237231
this.client = client;
238-
this.service = service;
239-
this.config = config;
232+
this.jmxTelemetry = jmxTelemetry(openTelemetry, config);
233+
}
234+
235+
private static JmxTelemetry jmxTelemetry(OpenTelemetry openTelemetry, JmxScraperConfig config) {
236+
JmxTelemetryBuilder jmxTelemetryBuilder =
237+
JmxTelemetry.builder(openTelemetry).beanDiscoveryDelay(config.getSamplingInterval());
238+
config.getTargetSystems().forEach(jmxTelemetryBuilder::addClassPathRules);
239+
config.getJmxConfig().stream().map(Paths::get).forEach(jmxTelemetryBuilder::addCustomRules);
240+
return jmxTelemetryBuilder.build();
240241
}
241242

242243
private void start() throws IOException {
@@ -250,7 +251,8 @@ private void start() throws IOException {
250251

251252
try (JMXConnector connector = client.build()) {
252253
MBeanServerConnection connection = connector.getMBeanServerConnection();
253-
service.startRemote(getMetricConfig(config), () -> singletonList(connection));
254+
255+
jmxTelemetry.start(() -> singletonList(connection));
254256

255257
running.set(true);
256258
logger.info("JMX scraping started");
@@ -264,32 +266,4 @@ private void start() throws IOException {
264266
}
265267
}
266268
}
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-
}
295269
}

0 commit comments

Comments
 (0)