Mastering Micrometer: From Counters to Grafana Dashboards in Spring Boot
This tutorial walks through Micrometer's metric types, how to register them with MeterRegistry, apply tags and naming conventions, and integrate the framework into Spring Boot applications with Actuator, Prometheus scraping, and Grafana visualization for comprehensive backend monitoring.
JVM Metrics Framework Micrometer in Practice
Spring Actuator collects metrics and exposes them via Prometheus and Grafana for monitoring production environments. Micrometer provides various Meter types such as Counter, Timer, Gauge, DistributionSummary, LongTaskTimer, FunctionCounter, FunctionTimer, and TimeGauge.
MeterRegistry
MeterRegistry is an abstract class with implementations like SimpleMeterRegistry, CompositeMeterRegistry, and a global CompositeMeterRegistry. SimpleMeterRegistry stores metrics in memory, while CompositeMeterRegistry aggregates multiple registries.
MeterRegistry registry = new SimpleMeterRegistry();
Counter counter = registry.counter("counter");
counter.increment();Tag Naming and NamingConvention
Meter names use dot notation and can be transformed for different monitoring systems via NamingConvention. Common conventions include dot, snake_case, camelCase, upperCamelCase, and slashes. Global tags can be added to all meters for dimensions like stack, environment, or region.
registry.config().commonTags("stack", "prod", "region", "us-east-1");Meter Types and Usage
Counter
Counter records a monotonically increasing count, suitable for events like order creation or HTTP request totals.
Metrics.counter("order.create", "channel", order.getChannel(), "createTime", formatter.format(order.getCreateTime())).increment();FunctionCounter
FunctionCounter derives its value from a function, allowing external state to drive the count.
FunctionCounter.builder("functionCounter", atomicInt, AtomicInteger::get)
.description("functionCounter")
.register(new SimpleMeterRegistry());
atomicInt.incrementAndGet();Timer
Timer measures the duration of short-lived events. It can be used directly or via functional wrappers.
Timer timer = Metrics.timer("timer", "createOrder", "cost");
timer.record(() -> createOrder(order));LongTaskTimer
LongTaskTimer records the duration of long-running tasks until they complete.
LongTaskTimer longTaskTimer = registry.more().longTaskTimer("longTaskTimer");
longTaskTimer.record(() -> {/* task logic */});Gauge
Gauge provides a snapshot of a value that can go up and down, such as memory usage or collection sizes.
Gauge gauge = registry.gauge("queue.size", queue, Collection::size);
AtomicInteger n = registry.gauge("numberGauge", new AtomicInteger(0));DistributionSummary
DistributionSummary tracks the distribution of non‑time values, like payload sizes.
DistributionSummary summary = DistributionSummary.builder("response.size")
.baseUnit("bytes")
.register(registry);
summary.record(payloadSize);Integrating Micrometer with Spring Boot, Prometheus, and Grafana
Spring Boot's
spring-boot-starter-actuatorincludes Micrometer support. Adding
micrometer-registry-prometheusexposes a
/management/prometheusendpoint that Prometheus scrapes.
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
prometheus:
enabled: truePrometheus configuration (prometheus.yml) defines a job that scrapes the Spring Boot application's Prometheus endpoint.
scrape_configs:
- job_name: 'spring-app'
metrics_path: /management/prometheus
static_configs:
- targets: ['localhost:10091']Grafana is added as a data source pointing to Prometheus (default port 9090). Dashboards can query metrics such as
order_count_totalor
method_cost_time_seconds_sumto visualize order counts and method execution times.
SELECT sum(rate(order_count_total[1m])) FROM metricBy combining Micrometer, Spring Boot Actuator, Prometheus, and Grafana, developers gain fine‑grained, multi‑dimensional observability of backend services.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.