0Pricing
Spring Boot 4 Microservices & REST APIs · レッスン

Micrometerでカスタムメトリクスを作る

独自のアプリケーションメトリクスを記録します

「Micrometerでカスタムメトリクスを作る」はCoddyKit上の無料Spring Boot 4 Microservices & REST APIsレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSpring Boot 4 Microservices & REST APIs学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Spring Boot 4 Microservices & REST APIsコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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

よくある質問

「Micrometerでカスタムメトリクスを作る」レッスンは無料ですか?

はい。「Micrometerでカスタムメトリクスを作る」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Spring Boot 4 Microservices & REST APIsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Spring Boot 4 Microservices & REST APIsコースには全4レッスンが含まれています。

「Micrometerでカスタムメトリクスを作る」で何を学びますか?

独自のアプリケーションメトリクスを記録します ブラウザで直接実行するハンズオンコードでSpring Boot 4 Microservices & REST APIsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Spring Boot 4 Microservices & REST APIsを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのSpring Boot 4 Microservices & REST APIsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「Micrometerでカスタムメトリクスを作る」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このSpring Boot 4 Microservices & REST APIsレッスンでコードを書いて実行できますか?

はい。すべてのSpring Boot 4 Microservices & REST APIsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. Actuatorエンドポイントを有効にする
  2. ヘルスインジケーター
  3. Micrometerでカスタムメトリクスを作る
  4. Actuatorエンドポイントを保護する
← Spring Boot 4 Microservices & REST APIsに戻る