0Pricing
Spring Boot 4 Microservices & REST APIs · Lesson

Custom Metrics with Micrometer

Record your own application metrics.

Custom Metrics with Micrometer is a free Spring Boot 4 Microservices & REST APIs lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Spring Boot 4 Microservices & REST APIs learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Micrometer: The Metrics Facade

Spring Boot uses Micrometer as a vendor-neutral metrics facade — the SLF4J of metrics. You instrument code against one API, then export to Prometheus, Datadog, CloudWatch, and others by adding a registry.

The MeterRegistry

The central component is the MeterRegistry. Boot auto-configures one and registers it as a bean, so you simply inject it wherever you need to record measurements.

@Service
public class OrderService {
    private final MeterRegistry registry;
    public OrderService(MeterRegistry registry) {
        this.registry = registry;
    }
}

Counters

A Counter records a value that only increases — perfect for counting events like orders placed or errors encountered. Create it once and increment it as events occur.

@Service
public class OrderService {
    private final Counter ordersPlaced;
    public OrderService(MeterRegistry registry) {
        this.ordersPlaced = registry.counter("orders.placed");
    }
    public void place(Order o) {
        // ... business logic
        ordersPlaced.increment();
    }
}

Tags (Dimensions)

Add tags to slice a metric by dimension — channel, region, status. Each unique tag combination is a separate time series, so keep tag cardinality bounded.

registry.counter("orders.placed",
        "channel", "web",
        "region", "eu")
    .increment();

Timers

A Timer measures both how often something happens and how long it takes, yielding count, total time, and max. Use it to track operation latency.

Timer timer = registry.timer("orders.process.time");
timer.record(() -> processOrder(order));

Recording Time Manually

When you cannot wrap the work in a lambda, capture a Timer.Sample at the start and stop it against a timer at the end.

Timer.Sample sample = Timer.start(registry);
try {
    processOrder(order);
} finally {
    sample.stop(registry.timer("orders.process.time"));
}

Gauges

A Gauge reports a value that can go up or down — a queue depth, active sessions, cache size. You register a function that Micrometer samples on demand.

registry.gauge("orders.queue.size",
    orderQueue, q -> q.size());

Distribution Summaries

A DistributionSummary tracks the distribution of arbitrary values, such as request payload sizes, providing count, total, and percentiles.

DistributionSummary summary = registry.summary("orders.amount");
summary.record(order.getTotal().doubleValue());

Declarative Timing with @Timed

The @Timed annotation times a method without manual instrumentation. It requires a TimedAspect bean to be registered.

@Bean
TimedAspect timedAspect(MeterRegistry registry) {
    return new TimedAspect(registry);
}

@Timed(value = "orders.process.time")
public void process(Order o) { /* ... */ }

Exporting to Prometheus

Add the Prometheus registry dependency and expose the prometheus endpoint. Micrometer then publishes a scrape endpoint with all your meters.

# dependency: micrometer-registry-prometheus
management:
  endpoints:
    web:
      exposure:
        include: prometheus,metrics,health
# scrape: /actuator/prometheus

Cardinality Discipline

The biggest metrics pitfall is high-cardinality tags. Never tag with user ids, request ids, or raw URLs — each unique value multiplies time series and can overwhelm your backend.

Quick Check

Test your understanding of meter types.

Recap

Micrometer instruments your app.

  • Inject the auto-configured MeterRegistry
  • Counter for monotonic counts, Timer for latency
  • Gauge for fluctuating values, DistributionSummary for distributions
  • Use bounded tags; avoid high cardinality
  • Export to Prometheus and friends via a registry dependency

Frequently asked questions

Is the “Custom Metrics with Micrometer” lesson free?

Yes — the full text of “Custom Metrics with Micrometer” is free to read here on the web, and the Spring Boot 4 Microservices & REST APIs course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Spring Boot 4 Microservices & REST APIs course, upgrade to CoddyKit PRO.

What will I learn in “Custom Metrics with Micrometer”?

Record your own application metrics. You practise Spring Boot 4 Microservices & REST APIs with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Spring Boot 4 Microservices & REST APIs?

No prior experience is required. Spring Boot 4 Microservices & REST APIs on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Custom Metrics with Micrometer” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Spring Boot 4 Microservices & REST APIs lesson?

Yes. Every Spring Boot 4 Microservices & REST APIs lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Enabling Actuator Endpoints
  2. Health Indicators
  3. Custom Metrics with Micrometer
  4. Securing Actuator Endpoints
← Back to Spring Boot 4 Microservices & REST APIs