Skip to content
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

Add shortcuts to assign dynamic tags to Meters #4097

Merged

Conversation

jonatan-ivanov
Copy link
Member

@jonatan-ivanov jonatan-ivanov commented Sep 23, 2023

This feature is a shortcut to create/get certain meters in a simpler way using the builder of that meter but without the need of recreating the builder every time.

This is what dynamic tagging looks like before this change (a bit longer and multiple builder instances will be created):

private final MeterRegistry registry = ...;

void favoriteColor(Color color) {
    Counter.builder("colors")
        .tags(Tags.of("color", color.name()))
        .register(registry)
        .increment();
}

And this is what you can do after (a bit shorter and one builder instance will be created):

private final MeterRegistry registry = ...;
private final MeterProvider<Counter> meterProvider = Counter.builder("colors").withRegistry(registry);

void favoriteColor(Color color) {
    meterProvider.withTags(Tags.of("color", color.name())).increment();
}

Closes gh-535
See gh-4092

@jonatan-ivanov
Copy link
Member Author

/cc: @gavlyukovskiy

@gavlyukovskiy
Copy link
Contributor

This looks absolutely awesome!

@jonatan-ivanov
Copy link
Member Author

@gavlyukovskiy @qweek I modified this a little bit, what do you think?

* @param tags Tags to attach to the Meter about to be registered
* @return A new or existing Meter
*/
T withTags(Iterable<? extends Tag> tags);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why withTags() versus the more commonly used elsewhere tags()?

Copy link
Member

Choose a reason for hiding this comment

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

I don't want to speak for @jonatan-ivanov, but I think the idea was to differentiate from the tags() method on the builders because those return a builder and can chain, whereas this method returns a registered Meter and cannot be chained to add more tags. I spent some time trying to think of better naming, but I was not able to come up with something. This is definitely something we're hoping to get feedback on and improve. Ideally, we would not change the naming after RC1, but it may be worth making an exception in this case if we cannot come up with something better before merging this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, tags might be confusing since that is on the Builder as well as MeterProvider.

@jonatan-ivanov jonatan-ivanov merged commit f61a4fa into micrometer-metrics:main Oct 9, 2023
6 checks passed
@jonatan-ivanov jonatan-ivanov deleted the dynamic-tags-shortcut branch October 9, 2023 22:09
@shakuzen shakuzen mentioned this pull request Mar 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add shortcut to assign dynamic tags to Meters
4 participants