Skip to content
Open
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 @@ -35,7 +35,9 @@ public void afterAgent(AutoConfiguredOpenTelemetrySdk autoConfiguredSdk) {

try {
config.getList("otel.jmx.config").stream().map(Paths::get).forEach(jmx::addCustomRules);
config.getList("otel.jmx.target.system").forEach(jmx::addClassPathRules);
config.getList("otel.jmx.target.system").stream()
.map(v -> String.format("jmx/rules/%s.yaml", v))
.forEach(jmx::addClassPathResourceRules);
} catch (RuntimeException e) {
// for now only log JMX errors as they do not prevent agent startup
logger.log(Level.SEVERE, "Error while loading JMX configuration", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,43 @@ public JmxTelemetryBuilder beanDiscoveryDelay(Duration delay) {
}

/**
* Adds built-in JMX rules from classpath resource
* Adds built-in JMX rules from classpath resource.
*
* @param target name of target in /jmx/rules/{target}.yaml classpath resource
* @return builder instance
* @throws IllegalArgumentException when classpath resource does not exist or can't be parsed
*/
// TODO: deprecate this method after 2.23.0 release in favor of addClassPathResourceRules
@CanIgnoreReturnValue
public JmxTelemetryBuilder addClassPathRules(String target) {
String yamlResource = String.format("jmx/rules/%s.yaml", target);
return addClassPathResourceRules(String.format("jmx/rules/%s.yaml", target));
}

/**
* Adds built-in JMX rules from classpath resource
*
* @param resourcePath relative path of the classpath resource yaml from classpath root, must not
* start with '/'
* @return builder instance
* @throws IllegalArgumentException when classpath resource does not exist or can't be parsed
*/
@CanIgnoreReturnValue
public JmxTelemetryBuilder addClassPathResourceRules(String resourcePath) {
boolean found = false;
try (InputStream inputStream =
JmxTelemetryBuilder.class.getClassLoader().getResourceAsStream(yamlResource)) {
if (inputStream == null) {
throw new IllegalArgumentException("JMX rules not found in classpath: " + yamlResource);
JmxTelemetryBuilder.class.getClassLoader().getResourceAsStream(resourcePath)) {
if (inputStream != null) {
found = true;
logger.log(FINE, "Adding JMX config from classpath for {0}", resourcePath);
RuleParser parserInstance = RuleParser.get();
parserInstance.addMetricDefsTo(metricConfiguration, inputStream, resourcePath);
}
logger.log(FINE, "Adding JMX config from classpath for {0}", yamlResource);
RuleParser parserInstance = RuleParser.get();
parserInstance.addMetricDefsTo(metricConfiguration, inputStream, target);
} catch (Exception e) {
} catch (RuntimeException | IOException e) {
throw new IllegalArgumentException(
"Unable to load JMX rules from classpath: " + yamlResource, e);
"Unable to load JMX rules from classpath: " + resourcePath, e);
}
if (!found) {
throw new IllegalArgumentException("JMX rules not found in classpath: " + resourcePath);
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,28 @@ void createDefault() {
@Test
void missingClasspathTarget() {
JmxTelemetryBuilder builder = JmxTelemetry.builder(OpenTelemetry.noop());
assertThatThrownBy(() -> builder.addClassPathRules("should-not-exist"))
.isInstanceOf(IllegalArgumentException.class);
assertThatThrownBy(() -> builder.addClassPathResourceRules("should-not-exist"))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("not found")
.hasMessageContaining("should-not-exist");
}

@Test
void invalidClasspathTarget() {
JmxTelemetryBuilder builder = JmxTelemetry.builder(OpenTelemetry.noop());
assertThatThrownBy(() -> builder.addClassPathRules("invalid"))
.isInstanceOf(IllegalArgumentException.class);
assertThatThrownBy(() -> builder.addClassPathResourceRules("jmx/rules/invalid.yaml"))
.isInstanceOf(IllegalArgumentException.class)
.describedAs("must have an exception message including the invalid resource path")
.hasMessageContaining("jmx/rules/invalid.yaml");
}

@Test
void knownClassPathTarget() {
JmxTelemetry.builder(OpenTelemetry.noop()).addClassPathRules("jvm").build();
JmxTelemetry jmxtelemetry =
JmxTelemetry.builder(OpenTelemetry.noop())
.addClassPathResourceRules("jmx/rules/jvm.yaml")
.build();
assertThat(jmxtelemetry).isNotNull();
}

@Test
Expand Down
Loading