Video in progress
In Spring Boot, how do you manage metrics with `MeterFilter` and the `management.metrics.*` properties: adding common tags, disabling unneeded metrics, renaming?
Common tags (application, region) come from management.metrics.tags.*, and management.metrics.enable.* turns off a metric you don't need. For finer rules such as renaming a tag, normalizing a value or denying a meter, you write a MeterFilter bean.
📖 Answer
In a multi-service system the first thing you need is to know which service and which environment each metric comes from:
management:
metrics:
tags:
application: ${spring.application.name}
region: tas-1
enable:
jvm.buffer: false # every meter with this prefix is turned off
tomcat.sessions: false
MeterFilter handles the finer work:
@Bean
MeterFilter renameLegacyTag() {
// old dashboards expect "service", new ones use "application"
return MeterFilter.renameTag("http.server.requests", "application", "service");
}
@Bean
MeterFilter onlyBusinessMetrics() {
return MeterFilter.denyUnless(id -> !id.getName().startsWith("cache.")
|| "orders".equals(id.getTag("cache")));
}
A filter runs when a meter is registered for the first time. So a filter added after the meter was created does not affect existing meters. In Boot, MeterFilter beans are attached to the registry before the application fully starts, but with a registry you create by hand, watch the order.
Two mistakes are common in practice. One is adding instance or pod as a common tag: Prometheus adds it itself at scrape time, so you end up with a duplicate label. The other is using enable: false to turn off a metric that a dashboard still uses. Before disabling a metric, check that no panel or alert uses it.
Sign up to mark studied questions and save your progress.