- Notifications
You must be signed in to change notification settings - Fork237
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
improve: Make MicrometerMetrics extendable#2912
Uh oh!
There was an error while loading.Please reload this page.
Conversation
e03b214 toae1d074CompareThere 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 theMicrometerMetrics 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 orlearn how to get started.
...pport/src/main/java/io/javaoperatorsdk/operator/monitoring/micrometer/MicrometerMetrics.java OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
...pport/src/main/java/io/javaoperatorsdk/operator/monitoring/micrometer/MicrometerMetrics.java OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
221faa7 to6dbd417Compare| } | ||
| } | ||
| publicstaticclassCleanerBuilder { |
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 aCleaner implementation:
protectedMicrometerMetrics(MeterRegistryregistry,Cleanercleaner,booleancollectingPerResourceMetrics) {
So how can we re-use the existing implementations (beside the staticCleaner.NOOP)?
I didn't want to make them all public, so I tried to hide them behind that newCleanerBuilder.
| * @param cleanerType the type of cleaner to use, defaults to {@link CleanerType#NOOP} if not | ||
| * specified | ||
| */ | ||
| publicCleanerBuilderwithCleanerType(CleanerTypecleanerType) { |
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.
DonnerbartAug 27, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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. Thebuild() method could just check if a delay parameter is set, otherwise use Cleaner.NOOP. TheDefaultCleaner 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.
Donnerbart commentedAug 27, 2025
My main motivation to extend the publicclassCustomResourceMetricsimplementsMetrics {privatefinal@NotNullMetricsdelegate;@OverridepublicvoidcontrollerRegistered(final@NotNullController<?extendsHasMetadata>controller) {delegate.controllerRegistered(controller); ... }@OverridepublicvoidreconcileCustomResource(final@NotNullHasMetadataresource,final@NotNullRetryInforetryInfo,final@NotNullMap<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 publicclassConfigurationServiceOverrider {privateList<Metrics>metrics;publicConfigurationServiceOverriderwithMetrics(Metricsmetrics) {returnwithMetrics(List.of(metrics)); }publicConfigurationServiceOverriderwithMetrics(List<Metrics>metrics) {this.metrics =metrics;returnthis; }publicConfigurationServicebuild() {returnnewBaseConfigurationService(original.getVersion(),cloner,client) {@OverridepublicList<Metrics>getMetrics() {returnmetrics !=null ?metrics :original.getMetrics(); } }} publicinterfaceConfigurationService {defaultList<Metrics>getMetrics() {returnList.of(Metrics.NOOP); }} |
metacosm commentedAug 27, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Alternatively, one could implement an aggregate For that matter, such an aggregate implementation could even be provided by JOSDK. |
6dbd417 tod9223b4CompareSigned-off-by: David Sondermann <david.sondermann@hivemq.com>
d9223b4 to8211d3fCompareDonnerbart commentedAug 27, 2025
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. |
metacosm commentedAug 27, 2025
Whatever you think is more appropriate but I do think that this approach would be better, indeed. What do you think@csviri,@xstefank? |
Partiallyfixes#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.