17

Is there a way to turn off some of the returned metric values in Actuator/Micrometer? Looking at them now I'm seeing around 1000 and would like to whittle them down to a select few say 100 to actually be sent to our registry.

Joel Holmes
  • 943
  • 2
  • 12
  • 23
  • 1
    Maybe Meter Filters could help? http://micrometer.io/docs/concepts#_meter_filters – Mark Jan 25 '18 at 20:43

3 Answers3

27

Let me elaborate on the answer posted by checketts with a few examples. You can enable/disable certain metrics in your application.yml like this (Spring Boot docs):

management:
  metrics:
    enable:
      tomcat: true
      jvm: false
      process: false
      hikaricp: false
      system: false
      jdbc: false
      http: false
      logback: true

Or in code by defining a MeterFilter bean:

@Bean
public MeterFilter meterFilter() {
    return new MeterFilter() {
        @Override
        public MeterFilterReply accept(Meter.Id id) {
            if(id.getName().startsWith("tomcat.")) {
                return MeterFilterReply.DENY;
            }
            if(id.getName().startsWith("jvm.")) {
                return MeterFilterReply.DENY;
            }
            if(id.getName().startsWith("process.")) {
                return MeterFilterReply.DENY;
            }
            if(id.getName().startsWith("system.")) {
                return MeterFilterReply.DENY;
            }
            return MeterFilterReply.NEUTRAL;
        }
    };
}
Adam Michalik
  • 9,678
  • 13
  • 71
  • 102
Mzzl
  • 3,926
  • 28
  • 39
  • This post is a few years old, and may now be obsolete. The syntax changed in spring boot 2.x – Mzzl Oct 21 '21 at 14:02
7

Meter filters can help in 3 ways that have been discussed with the Micrometer slack channel:

  1. Disabling metrics
  2. Combining dimensions
  3. High cardinality capping filter

Micrometer comes with the first type of meter filter built in. It also support hierarchical enabling/disabling similar to how logging works (As in if have meter like my.request.total and my.request.latency you can disable all metrics that start with my.request.

I've implemented my own example of a combining filter which is useful if you have a metrics with high cardinality and want to combine them in new dimensions. Take for example you have a dimension on status codes, this lets you combine 200, 201, 203 response codes as a tag 2xx. This is similar to Netflix Spectator 'placeholder' support. I would love to contribute that code upstream, but currently it is pretty custom, so it would need some work to make it generally usable.

The last type for catching high cardinality dimensions, hasn't been created yet, but would exist as a safety valve ensuring that if a metric can potentially have a high number of tag value, it will count the number of unique tags and once a max value is hit, either disable or combine the additional tags into a common bucket, so that value doesn't explode and potentially overwhelm your monitoring (or cost you lots of $$$, if paying per-metric)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
checketts
  • 14,167
  • 10
  • 53
  • 82
  • I guess as a follow up to this question, I was able to change the global meter filter by using an @PostConstruct but this didn't feel right. Is there a better way or a way of overriding the Globabl Registry bean? – Joel Holmes Jan 31 '18 at 16:39
  • 2
    Use a `MeterRegistryCustomizer` like so: ```@Bean MeterRegistryCustomizer customizer( @Value("${spring.application.name}") String applicationName) { return (registry) -> registry.config().commonTags("application", applicationName); }``` Will be easier to read in a new questions. – checketts Jan 31 '18 at 22:03
  • Is there any way to remove specific tags/labels from metrics before publishing metric – omjego Feb 28 '19 at 11:45
  • 1
    @omjego Ask this as a new question. As a quick answer yes: http://micrometer.io/docs/concepts#_transforming_metrics. See 'ignoreTags' – checketts Feb 28 '19 at 17:05
6

The property naming in Mzzl's answer has changed in Spring Boot 2. For example, to disable the JVM metrics it's now:

management.metrics.binders.jvm.enabled=false

See this class for the other options. The Spring team have refactored yet again in 2.1.x and those inner factory bean classes are now lifted out into standalone files but the property naming remains the same as 2.0.x.

Andy Brown
  • 11,766
  • 2
  • 42
  • 61