0Pricing
Spring Boot 4 Microservices & REST APIs · บทเรียน

เมตริกแบบกำหนดเองด้วย Micrometer

บันทึกเมตริกของแอปด้วยตนเอง

เมตริกแบบกำหนดเองด้วย Micrometer เป็นบทเรียน Spring Boot 4 Microservices & REST APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Boot 4 Microservices & REST APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “เมตริกแบบกำหนดเองด้วย Micrometer”

บันทึกเมตริกของแอปด้วยตนเอง คุณปฏิบัติ Spring Boot 4 Microservices & REST APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Boot 4 Microservices & REST APIs หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Boot 4 Microservices & REST APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 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