Centralised logs and metrics

Microservices · lesson 13 of 33 · 4 min read

Ship structured logs to one place and export the few metrics that reveal a sick service.

Open this lesson in the learning hub

Key points

  • Log to stdout and let the platform collect it. Files written inside a container disappear with the container.
  • Structured beats pretty: fields like orderId are searchable, a sentence is not. Spring Boot 3.4+ writes JSON logs natively.
  • Never log tokens, passwords or full card numbers. Redact at the source, because a log pipeline is a copy machine.
  • Expose Micrometer metrics on /actuator/prometheus. Watch RED: rate, errors, duration, per endpoint and per dependency.
  • Alert on symptoms users feel, like p99 latency and error rate. CPU spikes are normal; a broken checkout is not.

Example

management:
  endpoints:
    web:
      exposure:
        include: health,info,prometheus
  endpoint:
    health:
      probes:
        enabled: true
  metrics:
    tags:
      application: orders              # common tag on every metric
  distribution:
    percentiles-histogram:
      http.server.requests: true       # real p95/p99, computed server side

logging:
  structured:
    format:
      console: ecs                     # Spring Boot 3.4+: JSON logs to stdout

If you cannot search the logs and graph the errors, you are not running microservices, you are guessing.

This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the Microservices course, and every lesson in it is listed on the Microservices contents page.