Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jmx-scraper add missing custom yaml support #1741

Merged
merged 4 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -100,6 +100,12 @@ public JmxScraperContainer withExtraJar(String jarPath) {
return this;
}

/**
* Adds custom metrics yaml from classpath resource
*
* @param yamlPath path to resource in classpath
* @return this
*/
@CanIgnoreReturnValue
public JmxScraperContainer withCustomYaml(String yamlPath) {
this.customYamlFiles.add(yamlPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ protected GenericContainer<?> createTargetContainer(int jmxPort) {
@Override
protected JmxScraperContainer customizeScraperContainer(
JmxScraperContainer scraper, GenericContainer<?> target, Path tempDir) {
return scraper.withTargetSystem("jvm");
return scraper
.withTargetSystem("jvm")
// also testing custom yaml
.withCustomYaml("custom-metrics.yaml");
}

@Override
Expand All @@ -48,6 +51,16 @@ protected MetricsVerifier createMetricsVerifier() {
nameAttributeMatchers("PS MarkSweep", "PS Scavenge");

return MetricsVerifier.create()
// custom metric in custom-metrics.yaml
.add(
"custom.jvm.uptime",
metric ->
metric
.hasDescription("JVM uptime in milliseconds")
.hasUnit("ms")
.isCounter()
.hasDataPointsWithoutAttributes())
// metrics for 'jvm' target system
.add(
"jvm.classes.loaded",
metric ->
Expand Down
11 changes: 11 additions & 0 deletions jmx-scraper/src/integrationTest/resources/custom-metrics.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---

rules:

- bean: java.lang:type=Runtime
mapping:
Uptime:
metric: custom.jvm.uptime
type: counter
unit: ms
desc: JVM uptime in milliseconds
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -225,7 +226,9 @@ private static MetricConfiguration getMetricConfig(JmxScraperConfig scraperConfi
for (String system : scraperConfig.getTargetSystems()) {
addRulesForSystem(system, config);
}
// TODO : add ability for user to provide custom yaml configurations
for (String file : scraperConfig.getJmxConfig()) {
addRulesFromFile(file, config);
}
return config;
}

Expand All @@ -234,13 +237,25 @@ private static void addRulesForSystem(String system, MetricConfiguration conf) {
try (InputStream inputStream =
JmxScraper.class.getClassLoader().getResourceAsStream(yamlResource)) {
if (inputStream != null) {
RuleParser parserInstance = RuleParser.get();
parserInstance.addMetricDefsTo(conf, inputStream, system);
RuleParser.get().addMetricDefsTo(conf, inputStream, system);
} else {
throw new IllegalArgumentException("No support for system " + system);
}
} catch (Exception e) {
throw new IllegalStateException("Error while loading rules for system " + system, e);
}
}

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