Skip to content

Enable Gauge builders to take a subclass of Number - #5601

Merged
shakuzen merged 1 commit into
micrometer-metrics:mainfrom
s-ste:enable-gauge-builder-to-take-sub-class-of-number
Nov 12, 2024
Merged

shakuzen merged 1 commit into
micrometer-metrics:mainfrom
s-ste:enable-gauge-builder-to-take-sub-class-of-number

Conversation

@s-ste

@s-ste s-ste commented Oct 22, 2024 •

Copy link
Copy Markdown
Contributor

This is an idea to make the various Gauge.builder(..) methods more flexible. Currently, due to Java's type erasure, if you want to instantiate a gauge using a Supplier<Integer>, you need to cast this to a Supplier<Number>, weakening your type safety.

You can extend the signature in a backward-compatible way and allow usage, as demonstrated in the tests I've added. I tried fitting the tests into appropriate places, but if this isn't where they should be, I'd be happy to move/refactor them.

I tried to look into issues and PRs to see if something similar had been proposed before, but I couldn't find anything, so I thought of sharing an idea as a PR. In any case, if you believe this is not useful for the project, please reject the PR.

@pivotal-cla

Copy link
Copy Markdown

@s-ste Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

@pivotal-cla

Copy link
Copy Markdown

@s-ste Thank you for signing the Contributor License Agreement!

@s-ste
s-ste force-pushed the enable-gauge-builder-to-take-sub-class-of-number branch from 247c4e6 to 13aa2d2 Compare October 22, 2024 14:48
@jonatan-ivanov

Copy link
Copy Markdown
Member

Right now, as a workaround, can you turn this:

Supplier<Long> supplier = () -> 42L;
Gauge.builder("test", supplier).register(registry);

into this?

Supplier<Long> supplier = () -> 42L;
Gauge.builder("test", supplier::get).register(registry);

@shakuzen shakuzen added enhancement A general enhancement module: micrometer-core An issue that is related to our core module labels Oct 23, 2024
@shakuzen shakuzen added this to the 1.15.0-M1 milestone Oct 23, 2024

@shakuzen shakuzen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the pull request.
I can't think of a reason to not do this, but I'm also not sure the situation in which a reference to a Supplier will be passed to a Gauge builder in real world code. I would imagine typically a method reference or lambda will be used, which avoids the issue. I looked at usage in our code - outside of test code, most is in MicrometerMetricsPublisherThreadPool where method references and lambdas are used with methods that return various types: Number, long, Integer.
That said, as long as it is backward compatible, this makes things nicer for certain usage, so I'm fine doing this. We'll wait until we branch for 1.15 development to merge.

@s-ste

s-ste commented Oct 23, 2024

Copy link
Copy Markdown
Contributor Author

@jonatan-ivanov Yes, that's right, and you'll still need to fall back to that way for some, e.g (because IntSupplier doesn't extend the same Supplier<T> interface):

IntSupplier supplier = () -> 42;
Gauge.builder("test", supplier::getAsInt).register(registry);

@shakuzen My main use case is to support wrapping the metric registration in an agnostic interface. That keeps my dependencies separated and my business code isn't aware of micrometer. In my case, that interface also makes testing nicer. Hope that description helps and thanks for the really quick response.

interface MetricRegistration {
  void register(String id, Supplier<Long> valueSupplier);
}

@shakuzen

Copy link
Copy Markdown
Member

@s-ste thanks for the explanation. That helps bring some clarity about use cases.

In my case, that interface also makes testing nicer.

Let us know if there's something about testing with Micrometer that's more difficult than it could/should be.

@s-ste

s-ste commented Oct 28, 2024 •

Copy link
Copy Markdown
Contributor Author

Thanks for the question. All in all, I love how testable Micrometer is, which makes working with it easy. For multi-module projects, I prefer testing business logic that provides metrics without having Micrometer on the classpath. So, from my side, I just appreciate the great work you do and have nothing to complain about. 😄

@shakuzen shakuzen changed the title Enable Gauges builders to take a subclass of Number Enable Gauge builders to take a subclass of Number Nov 12, 2024
@shakuzen
shakuzen merged commit 659ae47 into micrometer-metrics:main Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement A general enhancement module: micrometer-core An issue that is related to our core module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants