Własne metryki z Micrometer
Rejestruj własne metryki aplikacji
Własne metryki z Micrometer to bezpłatna lekcja Spring Boot 4 Microservices & REST APIs na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Spring Boot 4 Microservices & REST APIs, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Spring Boot 4 Microservices & REST APIs zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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/prometheusCardinality 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 Counterfor monotonic counts,Timerfor latencyGaugefor fluctuating values,DistributionSummaryfor distributions- Use bounded tags; avoid high cardinality
- Export to Prometheus and friends via a registry dependency
Często zadawane pytania
Czy lekcja „Własne metryki z Micrometer” jest bezpłatna?
Tak — pełny tekst „Własne metryki z Micrometer” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Spring Boot 4 Microservices & REST APIs, przejdź na CoddyKit PRO. Kurs Spring Boot 4 Microservices & REST APIs zawiera 4 lekcji w sumie.
Co nauczysz się w „Własne metryki z Micrometer”?
Rejestruj własne metryki aplikacji Ćwiczysz Spring Boot 4 Microservices & REST APIs z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Spring Boot 4 Microservices & REST APIs?
Nie wymagamy żadnego doświadczenia. Spring Boot 4 Microservices & REST APIs w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.
Ile czasu zajmuje lekcja „Własne metryki z Micrometer”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Spring Boot 4 Microservices & REST APIs?
Tak. Każda lekcja Spring Boot 4 Microservices & REST APIs zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Włączanie endpointów Actuator
- Wskaźniki stanu
- Własne metryki z Micrometer
- Zabezpieczanie endpointów Actuator