0Pricing
Spring Boot 4 Microservices & REST APIs · درس

مقاييس مخصصة باستخدام Micrometer

سجّل مقاييس تطبيقك الخاصة

مقاييس مخصصة باستخدام Micrometer درس مجاني في Spring Boot 4 Microservices & REST APIs على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Spring Boot 4 Microservices & REST APIs، انتقل إلى CoddyKit PRO. تتضمن دورة Spring Boot 4 Microservices & REST APIs 4 دروس في المجموع.

ماذا ستتعلم في «مقاييس مخصصة باستخدام Micrometer»؟

سجّل مقاييس تطبيقك الخاصة تتمرن على Spring Boot 4 Microservices & REST APIs مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 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 يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. تفعيل نقاط نهاية Actuator
  2. مؤشرات الحالة
  3. مقاييس مخصصة باستخدام Micrometer
  4. تأمين نقاط نهاية Actuator
← العودة إلى Spring Boot 4 Microservices & REST APIs