-
Notifications
You must be signed in to change notification settings - Fork 1k
jmx-metrics: allow any resource path for classpath resources rules #15413
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
base: main
Are you sure you want to change the base?
Changes from all commits
e852dad
d688fc4
2514661
04bf7db
eb0555c
549ff19
0c6e261
5ff7e24
c9750f2
013f4eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,47 +49,70 @@ 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 addRules | ||
| @CanIgnoreReturnValue | ||
| public JmxTelemetryBuilder addClassPathRules(String target) { | ||
| String yamlResource = String.format("jmx/rules/%s.yaml", target); | ||
| try (InputStream inputStream = | ||
| JmxTelemetryBuilder.class.getClassLoader().getResourceAsStream(yamlResource)) { | ||
| if (inputStream == null) { | ||
| throw new IllegalArgumentException("JMX rules not found in classpath: " + yamlResource); | ||
| } | ||
| logger.log(FINE, "Adding JMX config from classpath for {0}", yamlResource); | ||
| RuleParser parserInstance = RuleParser.get(); | ||
| parserInstance.addMetricDefsTo(metricConfiguration, inputStream, target); | ||
| } catch (Exception e) { | ||
| String resourcePath = String.format("jmx/rules/%s.yaml", target); | ||
| ClassLoader classLoader = JmxTelemetryBuilder.class.getClassLoader(); | ||
| try (InputStream inputStream = classLoader.getResourceAsStream(resourcePath)) { | ||
| return addRules(inputStream, resourcePath); | ||
| } catch (IOException e) { | ||
| throw new IllegalArgumentException( | ||
| "Unable to load JMX rules from classpath: " + yamlResource, e); | ||
| "Unable to load JMX rules from resource " + resourcePath, e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Adds JMX rules from input stream | ||
| * | ||
| * @param input input to read rules from | ||
| * @param description input description, used for user-friendly logs and parsing error messages | ||
| * @throws IllegalArgumentException when input is {@literal null} | ||
| */ | ||
| @CanIgnoreReturnValue | ||
| public JmxTelemetryBuilder addRules(InputStream input, String description) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure it makes sense to ask for description. You have no way of telling whether the provided description is correct or user just copy-pasted "tomcat" everywhere.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The description is used:
It is only used to provide human-friendly error and log messages, so even if an invalid or constant value is used there is no functional impact.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It does look odd to me in a public API. Can JMX scraper provide friendly message on its own without this arg? |
||
| if (input == null) { | ||
| throw new IllegalArgumentException("JMX rules not found for " + description); | ||
| } | ||
| logger.log(FINE, "Adding JMX config from {0}", description); | ||
| RuleParser parserInstance = RuleParser.get(); | ||
| parserInstance.addMetricDefsTo(metricConfiguration, input, description); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Adds custom JMX rules from file system path | ||
| * Adds JMX rules from file system path | ||
| * | ||
| * @param path path to yaml file | ||
| * @return builder instance | ||
| * @throws IllegalArgumentException when classpath resource does not exist or can't be parsed | ||
| * @throws IllegalArgumentException in case of parsing errors or when file does not exist | ||
| */ | ||
| @CanIgnoreReturnValue | ||
| public JmxTelemetryBuilder addCustomRules(Path path) { | ||
| logger.log(FINE, "Adding JMX config from file: {0}", path); | ||
| RuleParser parserInstance = RuleParser.get(); | ||
| public JmxTelemetryBuilder addRules(Path path) { | ||
| try (InputStream inputStream = Files.newInputStream(path)) { | ||
| parserInstance.addMetricDefsTo(metricConfiguration, inputStream, path.toString()); | ||
| return addRules(inputStream, "file " + path); | ||
| } catch (IOException e) { | ||
| throw new IllegalArgumentException("Unable to load JMX rules in path: " + path, e); | ||
| throw new IllegalArgumentException("Unable to load JMX rules from: " + path, e); | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Adds custom JMX rules from file system path | ||
| * | ||
| * @param path path to yaml file | ||
| * @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 addRules | ||
| @CanIgnoreReturnValue | ||
| public JmxTelemetryBuilder addCustomRules(Path path) { | ||
| return addRules(path); | ||
| } | ||
|
|
||
| public JmxTelemetry build() { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why don't you deprecate it immediately?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we deprecate it right now, it means the next update of instrumentation dependency in contrib will require to immediately modify the code of
jmx-scraperin contrib to switch to the new API. Even if the changes are minor it's an added constraint on the dependency update.Doing that after 2.23.0 gives a bit more time with a timeline like this:
jmx-scraperto use the new API introduced by this PR