-
Notifications
You must be signed in to change notification settings - Fork 236
improve: Make MicrometerMetrics extendable #2912
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
improve: Make MicrometerMetrics extendable #2912
Conversation
e03b214 to
ae1d074
Compare
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.
Pull Request Overview
This PR makes the MicrometerMetrics class extendable by exposing previously private constructor and cleaner implementations. It introduces a builder pattern for cleaners and allows custom implementations to extend the base functionality.
- Exposes the
MicrometerMetricsconstructor as protected instead of private - Introduces public
CleanerBuilderandCleanerTypeenum for configurable cleaner creation - Refactors existing builder classes to use the new
CleanerBuilderinternally
Reviewed Changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| MicrometerMetrics.java | Main implementation changes - exposes constructor, adds CleanerBuilder and CleanerType enum, refactors existing builders |
| CleanerBuilderTest.java | New test file for the CleanerBuilder functionality |
| *WithCustomImplementationIT.java | New test files demonstrating custom extension capability |
| AbstractMicrometerMetricsTestFixture.java | Changed TestSimpleMeterRegistry visibility to protected |
| DelayedMetricsCleaningOnDeleteIT.java | Changed testDelay field visibility to protected |
| DefaultBehaviorIT.java, NoPerResourceCollectionIT.java | Added blank lines for formatting |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
...pport/src/main/java/io/javaoperatorsdk/operator/monitoring/micrometer/MicrometerMetrics.java
Outdated
Show resolved
Hide resolved
...pport/src/main/java/io/javaoperatorsdk/operator/monitoring/micrometer/MicrometerMetrics.java
Outdated
Show resolved
Hide resolved
221faa7 to
6dbd417
Compare
| } | ||
| } | ||
|
|
||
| public static class CleanerBuilder { |
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.
Does this need to be exposed?
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.
The problem is that the constructor needs a Cleaner implementation:
protected MicrometerMetrics(
MeterRegistry registry, Cleaner cleaner, boolean collectingPerResourceMetrics) {So how can we re-use the existing implementations (beside the static Cleaner.NOOP)?
I didn't want to make them all public, so I tried to hide them behind that new CleanerBuilder.
| * @param cleanerType the type of cleaner to use, defaults to {@link CleanerType#NOOP} if not | ||
| * specified | ||
| */ | ||
| public CleanerBuilder withCleanerType(CleanerType cleanerType) { |
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.
I would rather not expose the type if possible but rather switch the type internally based on whether the user sets the number of cleaning threads or the delay, which would switch the implementation rather than throwing an exception if the type is not amenable to set these values.
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.
That might actually work, yes! With that we might not need the types at all. The build() method could just check if a delay parameter is set, otherwise use Cleaner.NOOP. The DefaultCleaner is actually not directly used and exposed right now at all, so no need to change that.
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.
I've updated the PR to get rid of the CleanerType and derive the type dynamically from the set builder parameters.
|
My main motivation to extend the public class CustomResourceMetrics implements Metrics {
private final @NotNull Metrics delegate;
@Override
public void controllerRegistered(final @NotNull Controller<? extends HasMetadata> controller) {
delegate.controllerRegistered(controller);
...
}
@Override
public void reconcileCustomResource(
final @NotNull HasMetadata resource,
final @NotNull RetryInfo retryInfo,
final @NotNull Map<String, Object> metadata) {
delegate.reconcileCustomResource(resource, retryInfo, metadata);
}
}This works fine, but we need to implement all methods of An alternative approach would be to allow multiple public class ConfigurationServiceOverrider {
private List<Metrics> metrics;
public ConfigurationServiceOverrider withMetrics(Metrics metrics) {
return withMetrics(List.of(metrics));
}
public ConfigurationServiceOverrider withMetrics(List<Metrics> metrics) {
this.metrics = metrics;
return this;
}
public ConfigurationService build() {
return new BaseConfigurationService(original.getVersion(), cloner, client) {
@Override
public List<Metrics> getMetrics() {
return metrics != null ? metrics : original.getMetrics();
}
}
}public interface ConfigurationService {
default List<Metrics> getMetrics() {
return List.of(Metrics.NOOP);
}
} |
Alternatively, one could implement an aggregate For that matter, such an aggregate implementation could even be provided by JOSDK. |
6dbd417 to
d9223b4
Compare
Signed-off-by: David Sondermann <[email protected]>
d9223b4 to
8211d3f
Compare
I think that might actually be the smartest approach with the least impact to the existing classes and APIs! I'm happy to rewrite this PR or create an alternative one with that approach. |
Whatever you think is more appropriate but I do think that this approach would be better, indeed. What do you think @csviri, @xstefank? |
Partially fixes #2884
As mentioned in the issue:
The latter is not possible due to the private constructor and private Cleaner implementations. This is an attempt to make the
MicrometerMetricsclass extendable without exposing allCleanerimplementations.