Search code examples
springspring-mvcsplunkmicrometerspring-micrometer

Spring Actuator Metrics generate logs


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?


Solution

  • 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