How to Expose and Customize Spring Boot Metrics with Micrometer
This guide explains how to enable Spring Boot's default metrics via Actuator, view them through the /actuator/metrics endpoint, and enrich them with Micrometer MeterBinders for JVM, cache, Hibernate, and Tomcat, including code samples and logging output.
Spring Boot generates a wide range of default metrics using Micrometer. By adding the org.springframework.boot:spring-boot-starter-actuator dependency, you can access them at the /actuator/metrics endpoint, which lists metric names such as jvm.threads.states, process.files.max, and system.load.average.1m.
Display Metrics
Include the actuator dependency in build.gradle.kts:
dependencies {
implementation("org.springframework.boot:spring-boot-starter-actuator")
}Visiting http://localhost:8080/actuator/metrics returns a JSON object with a names array. To see details of a specific metric, append its name, e.g., http://localhost:8080/actuator/metrics/system.cpu.count, which yields fields like name, description, baseUnit, and measurements.
Metrics can be exported to external systems (Prometheus, New Relic, CloudWatch, Graphite) via a MeterRegistry. The simplest registry is LoggingMeterRegistry, which logs all metrics periodically.
@Configuration
class MetricsConfig {
@Bean
LoggingMeterRegistry loggingMeterRegistry() {
return new LoggingMeterRegistry();
}
}Sample log output shows JVM and cache metrics:
2019-07-17 11:07:09.406 INFO --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry : jvm.buffer.count{id=direct} value=0 buffers
2019-07-17 13:08:45.113 INFO --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry : cache.gets{cache=playCache,cacheManager=cacheManager,name=playCache,result=hit} throughput=12.95/s
...Metric Supply
Metrics are supplied by the components they monitor. For example, EhCache provides statistics via cache.getStatistics(), and Hibernate via sessionFactory.getStatistics(). JVM metrics come from RuntimeMXBean, and Tomcat metrics from Tomcat MBeans.
Micrometer introduces the MeterBinder interface to bind such statistics to metrics. Inspecting the MeterBinder hierarchy reveals available groups (JVM, cache, Tomcat, etc.).
Registering MeterBinders can be done manually:
new ClassLoaderMetrics().bindTo(registry);
new JvmMemoryMetrics().bindTo(registry);
new EhCache2Metrics(cache, Tags.of("name", cache.getName())).bindTo(registry);
new TomcatMetrics(manager, tags).bindTo(registry);
...When using Spring Boot, the framework automatically registers the appropriate AutoConfigurations and MeterBinders for you.
To monitor EhCache, add the cache starter and EhCache library:
implementation("org.springframework.boot:spring-boot-starter-cache")
implementation("net.sf.ehcache:ehcache")Define a cache bean (or configure via ehcache.xml) and Spring will register EhCache2Metrics for each cache:
@Bean
Cache playCache(EhCacheCacheManager cacheManager) {
CacheConfiguration cfg = new CacheConfiguration()
.name(CACHE_NAME)
.maxEntriesLocalHeap(MAX_ELEMENTS_IN_MEMORY);
Cache cache = new Cache(cfg);
cacheManager.getCacheManager().addCache(cache);
cacheManager.initializeCaches();
return cache;
}Running the application produces cache‑related logs similar to the earlier JVM logs, showing hits, misses, puts, and size metrics.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
