Search code examples
spring-bootmetricsspring-boot-actuatormicrometer

How to stop publishing metrics but still keep accessible under /metrics in spring boot micrometer?


I want to keep collecting jvm and other metrics, but just don't want to push all of them to cloudwatch or prometheus (or anything else).

If I do like this

@Bean
MeterRegistryCustomizer<MeterRegistry> customizer() {
    return (registry) -> registry.config()
        .meterFilter(MeterFilter.denyNameStartsWith("jvm"));
}

it stops publishing to cloudwatch but at the same time, the metrics are gone from /actuator/metrics/ as well.

Is there anyway I can keep them accessible under /actuator/metrics/ but stop pushing those to remote metrics collector ?


Solution

  • If you are looking for how to expose Spring Boot Actuator metrics endpoint but not to publish metrics to any remote meter registry like Prometheus, just adding the org.springframework.boot:spring-boot-starter-actuator without any monitoring system-specific dependency like io.micrometer:micrometer-registry-prometheus should work. This is a working sample.

    However, based on the project owner's comment, the endpoint is intended for a diagnostic purpose only, so you should use one of monitoring system-specific meter registries for monitoring purposes as follows:

    The actuator endpoint is at best a toy, a diagnostic tool. The real heavy lifting of structuring the data for publishing happens in each registry implementation.


    UPDATED:

    Based on your comment, if I understand correctly, you want to expose all metrics on the metrics endpoint but publish some of them to a remote meter registry.

    For doing so, you can add a SimpleMeterRegistry for the metrics endpoint and add a MeterRegistryCustomizer for the remote meter registry to add some MeterFilter.

    See this commit and this branch for a Prometheus example.