Monitoring & Logging
You cannot fix what you cannot see.
Introduction
You cannot fix what you cannot see. For Boot 3 Orders, the modern stack is Actuator (health/info) + Micrometer (metrics) + Prometheus scrape + Grafana dashboards, with structured logs carrying a trace id from Micrometer Tracing.
Expose /actuator/prometheus on a management port or locked-down path. Tag metrics with application and environment. Prefer JSON logs (Logback/Log4j2) so Loki or your log platform can query order ids without brittle regex.
Java 21 virtual-thread workloads still need the same signals: latency histograms, error rates, pool saturation, and JVM heap. Monitoring without alerts is just a museum.
This lesson wires TechLearningPRO Orders for day-2 operations before performance tuning.
Purpose of this lesson
Wire Actuator, Micrometer, and Prometheus for Orders, and structure logs so metrics and traces correlate.
Understanding the topic
Metrics, logs, and traces are three pillars — link them with ids and tags.
- Actuator: health, info, metrics, prometheus endpoints.
- Micrometer: counters, timers, gauges — registry-agnostic API.
- Prometheus: scrapes
/actuator/prometheus. - Structured logs: JSON + MDC fields (
traceId,orderId). - Authorization: lock down actuator beyond
health.
Step-by-step explanation
- Add
spring-boot-starter-actuatorandmicrometer-registry-prometheus. - Expose only needed endpoints; set
health.show-details=when-authorized. - Add a custom counter for
orders.placedin the service layer. - Confirm Prometheus scrapes and Grafana shows the series; verify MDC includes trace ids.
Informative example
Actuator + Prometheus + a business metric.
# application.ymlmanagement:endpoints:web:exposure:include: health,info,metrics,prometheusendpoint:health:show-details: when-authorizedmetrics:tags:application: ${spring.application.name}# Custom metric in OrderService@Servicepublic class OrderService {private final MeterRegistry meter;private final OrderRepository orders;public OrderService(MeterRegistry meter, OrderRepository orders) {this.meter = meter;this.orders = orders;}@Transactionalpublic OrderView place(CreateOrderRequest cmd) {OrderView view = OrderView.from(orders.save(OrderEntity.from(cmd)));meter.counter("orders.placed", "tier", cmd.tier()).increment();return view;}}
Real-world use
TechLearningPRO on-call once stared at 'Orders is slow' with no percentile charts. After Micrometer timers on OrderService.place and Postgres pool gauges, they saw p99 climb only when Hikari wait time spiked — a five-minute fix (pool size) instead of a day of guessing.
Security implications
Actuator is a sensitive surface.
- Authenticate or network-restrict non-health endpoints.
- Never log tokens, passwords, or full payment payloads.
- Use placeholders like
${DB_PASS}in samples — no real secrets.
Best practices
- Low-cardinality tags only (tier, region, outcome).
- Separate management port for actuator when possible.
- Alert on SLO burn (latency/error rate), not on every vanity gauge.
Common mistakes
- Exposing
env/heapdumppublicly. - High-cardinality tags (raw order ids as metric labels) exploding Prometheus series.
- Plain-text logs without request/trace ids, making incidents unsearchable.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionWhat scrapes /actuator/prometheus?+
Answer
2IntermediateQuestionWhy avoid orderId as a metric tag?+
Answer
3IntermediateQuestionHow do logs relate to traces?+
Answer
Hands-on exercise
Lab. Enable Actuator + Prometheus registry. Place three orders and scrape orders_placed_total. Add traceId to MDC and confirm it appears in a single log line for that request.
Summary
Monitoring makes Orders operable. Next: use those signals to drive performance optimization.