Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -233,10 +229,39 @@ 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");
Files.copy(input, tempFile, StandardCopyOption.REPLACE_EXISTING);
builder.addCustomRules(tempFile);
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 {
Expand All @@ -250,7 +275,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");
Expand All @@ -264,32 +290,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);
}
}
}
Loading