Skip to content

Conversation

peterjeschke
Copy link
Contributor

I have noticed that I often want to configure MeterFilters only for a selection of meters that start with a given prefix. For example, I might want to ignore tags for a selection of metrics, but not for all of them. The "normal" approach would be to write a custom MeterFilter that checks the name before filtering. The new utility method helps with this task.

I have noticed that I often want to configure MeterFilters only for a selection of meters that start with a given prefix. For example, I might want to ignore tags for a selection of metrics, but not for all of them. The "normal" approach would be to write a custom MeterFilter that checks the name before filtering. The new utility method helps with this task.

Signed-off-by: Peter Jeschke <peter@jeschke.dev>
@jonatan-ivanov
Copy link
Member

Thank you for the PR!

I'm thinking if it would make sense making this a bit more general:

static MeterFilter forMeters(Predicate<Meter.Id> predicate, MeterFilter delegate) { ... }

Using forPrefix looks like this:

private static final String MAGIC_PREFIX = "test2;

MeterFilter.forPrefix("test", MeterFilter.ignoreTags("ignored"));
MeterFilter.forPrefix(MAGIC_PREFIX, MeterFilter.ignoreTags("ignored"));

While forMeters could look like:

private static final Predicate<Meter.Id> MAGIC_PREFIX_SELECTOR = id -> id.getName().startsWith("test2");

MeterFilter.forMeters(id -> id.getName().startsWith("test"), MeterFilter.ignoreTags("ignored"));
MeterFilter.forMeters(MAGIC_PREFIX_SELECTOR, MeterFilter.ignoreTags("ignored"));
MeterFilter.forMeters(startsWith("test"), MeterFilter.ignoreTags("ignored")); // you implement startsWith it but it's a one-liner

@jonatan-ivanov jonatan-ivanov added the waiting for feedback We need additional information before we can continue label Aug 7, 2025
@jonatan-ivanov jonatan-ivanov changed the title Add MeterFilter.forPrefix(..) utility method Add MeterFilter.forMeters utility method Aug 9, 2025
@jonatan-ivanov jonatan-ivanov added enhancement A general enhancement module: micrometer-core An issue that is related to our core module and removed waiting for feedback We need additional information before we can continue labels Aug 9, 2025
@jonatan-ivanov jonatan-ivanov added this to the 1.16.0-M2 milestone Aug 9, 2025
@jonatan-ivanov jonatan-ivanov requested a review from shakuzen August 9, 2025 00:40
@jonatan-ivanov
Copy link
Member

I introduced forMeters instead of forPrefix and polished the PR a little bit.
@shakuzen What do you think?

Copy link
Member

@shakuzen shakuzen left a comment

Choose a reason for hiding this comment

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

It seems nice for convenience. It's a bit inefficient to override all the methods and use the predicate if the delegate only implements one of the methods, but in practice I suppose this shouldn't be much of an issue thanks to the pre-filter cache we have in place. For calls that don't hit the cache, using this may be less efficient than more directly implemented filters.

@shakuzen shakuzen merged commit 9fcf30b into micrometer-metrics:main Aug 12, 2025
11 checks passed
@peterjeschke peterjeschke deleted the feature/forPrefix branch August 17, 2025 17:48
peterjeschke added a commit to peterjeschke/micrometer that referenced this pull request Aug 17, 2025
A follow-up to micrometer-metrics#6594, this adds the most likely predicates for usage with MeterFilter.forMeters

Signed-off-by: Peter Jeschke <peter@jeschke.dev>
peterjeschke added a commit to peterjeschke/micrometer that referenced this pull request Aug 17, 2025
A follow-up to micrometer-metrics#6594, this adds the most likely predicates for usage with MeterFilter.forMeters

Signed-off-by: Peter Jeschke <peter@jeschke.dev>
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.

3 participants