Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 62

    Monitoring & Logging

    You cannot fix what you cannot see.

    Course progress0%
    Focus
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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

    1. Add spring-boot-starter-actuator and micrometer-registry-prometheus.
    2. Expose only needed endpoints; set health.show-details=when-authorized.
    3. Add a custom counter for orders.placed in the service layer.
    4. Confirm Prometheus scrapes and Grafana shows the series; verify MDC includes trace ids.

    Informative example

    Actuator + Prometheus + a business metric.

    yaml
    # application.yml
    management:
    endpoints:
    web:
    exposure:
    include: health,info,metrics,prometheus
    endpoint:
    health:
    show-details: when-authorized
    metrics:
    tags:
    application: ${spring.application.name}
    # Custom metric in OrderService
    @Service
    public class OrderService {
    private final MeterRegistry meter;
    private final OrderRepository orders;
    public OrderService(MeterRegistry meter, OrderRepository orders) {
    this.meter = meter;
    this.orders = orders;
    }
    @Transactional
    public 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/heapdump publicly.
    • 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.

    3 questions
    1BeginnerQuestionWhat scrapes /actuator/prometheus?+

    Answer

    Prometheus (or a compatible scraper). Micrometer formats the registry for that endpoint.
    2IntermediateQuestionWhy avoid orderId as a metric tag?+

    Answer

    Each unique id creates a new time series — cardinality explosion and costly queries.
    3IntermediateQuestionHow do logs relate to traces?+

    Answer

    Put the Micrometer/OpenTelemetry trace id into MDC so a log line jumps to the matching trace.

    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.

    Ready to mark this lesson complete?Track your journey across the entire course.