Spring Boot Actuator/Micrometer Metrics Disable Some

一世执手 提交于 2019-12-06 02:02:07

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 dimmensions. 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 overwhel your monitoring (or cost you lots of $$$, if paying per-metric)

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:

management:
  metrics:
    enable:
      tomcat: true
      jvm: false
      process: false
      hikaricp: false
      system: false
      jdbc: 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;
        }
    };
}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!