2

I'm trying to get micrometer metrics data to Splunk. Each metric endpoint gives the current value of a metric, so I would need Splunk to send a http request to my application periodically, or I can write the metric values to a log file periodically.

So how do I get my application to write the metric values to logs?

George
  • 2,820
  • 4
  • 29
  • 56

1 Answers1

14

If you are in spring boot 2.x and Micrometer is of version 1.1.0+ you can create a bean of periodic (1 minute) special logging registry see (https://github.com/micrometer-metrics/micrometer/issues/605)

@Bean
LoggingMeterRegistry loggingMeterRegistry() {
    return new LoggingMeterRegistry();
}

This is by far the easiest way to log everything via logging system.

Another alternative is creating a scheduled job that will run some method on a bean with injected metering registry that will iterate over all the metrics (with possibly filtering out the metrics that you won't need) and preparing the log of your format.

If you think about this, this is exactly what the metrics endpoint of spring boot actuator does, except returning the data via http instead of writing to log.

Here is an up-to-date implementation of the relevant endpoint from the spring boot actuator source

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
  • 1
    You may also consider subclassing the `LoggingMeterRegistry` to change the format to match your needs. Eventually I would like to see a Splunk specific meter registry that could post to a Splunk metrics index eventually, but that doesn't exist yet. – checketts Nov 09 '19 at 00:08
  • Is there anyway we can get this same logging with Spring Boot v1.5? I don't see this registry in v1.5. – San Aug 07 '20 at 15:45