Search code examples
javaspring-bootspring-boot-actuatormicrometerspring-micrometer

MicroMeter: remove/disable a certain tag


I'm using the default MicroMeter binders, one of them creating the Gauage jvm.memory.used. The issue is that it comes with 2 availableTags: "area" and "id". This is practically generating 6 Gauges which I'm not interested in.

I was able to do this:

@Bean
public MeterRegistryCustomizer<MeterRegistry> metricsCustomizer() {

    return  registry -> {
        registry.config().meterFilter(MeterFilter.ignoreTags("id"));
    };
}

but this will ignore all meters with this tag.

How can I deny() only jvm.memory.used with an id tag?

Thanks!

EDIT:
Looking at the globalRegistry:

Metrics.globalRegistry.getRegistries().iterator().next().meterMap

Every Meter of jvm.memory.used is of the following structure:

"MeterId{name='jvm.memory.used', tags=[tag(area=nonheap),tag(id=Metaspace)]}"

So we can't just filter by getName().equals("jvm.memory.used") && getTag("id") != null


Solution

  • I think you maybe conflating concepts. The tags just "decorate" the metrics/gauges with additional information, they don't actually result in more gauges being sent. For example, the line Metrics.gauge("name", Tags.of("id","a", "area","b"), 1) is just a single gauge object. Denying jvm.memory.used with an id tag will probably deny all jvm.memory.used. If you still want to filter jvm.memory.used with the id tag you can use

    @Bean
    public MeterFilter meterFilter() {
        return MeterFilter.deny(
            id -> "jvm.memory.used".equals(id.getName()) &&
                  id.getTags().stream().anyMatch(t -> "id".equals(t.getKey()))
             );
        }
    

    If on the other hand you just wanted to remove just the id tag, you will need to use the replaceTags method.

    @Bean
    public MeterFilter meterFilter() {
        return new MeterFilter() {
            @Override
            public Meter.Id map(Meter.Id id) {
                if("jvm.memory.used".equals(id.getName())) {
                    List<Tag> tags = id.getTags().stream().filter(t -> !"id".equals(t.getKey())).collect(Collectors.toList());
                    return id.replaceTags(tags);
                }
               return id;
            }
        };
    }
    

    You can also completely customize the Meter.Id returned by calling new Meter.Id(name, tags, baseUnit, description, type) as below, and chaning whichever values you want to:

    @Bean
    public MeterFilter meterFilter() {
        return new MeterFilter() {
            @Override
            public Meter.Id map(Meter.Id id) {
                if("jvm.memory.used".equals(id.getName())) {
                    return new Meter.Id(id.getName(), <put tags here>, id.getBaseUnit(), id.getDescription(), id.getType()
                }
               return id;
            }
        };
    }