Skip to content
Rotha Dapravith

Metrics for Spring microservices

Inventory, order and cart services registered with Eureka and configured from a Config Server, each exposing Micrometer metrics that Prometheus scrapes and Grafana charts.

Type
Personal project
Year
2025
Status
Runs locally
Stack
  • Java
  • Spring Boot
  • Eureka
  • Spring Cloud Config
  • Spring Cloud Gateway
  • Micrometer
  • Prometheus
  • Grafana

The problem

A small e-commerce-shaped system used to practise running several Spring services together and seeing what each of them is doing.

With six processes running, it is hard to tell which one is slow or down. The goal was one place to see request rates, latencies and JVM health for every service.

My role

Personal project. I wrote the services, the Prometheus scrape configuration and the Grafana dashboard.

Approach

Every service exposes the same Actuator metrics endpoint, so adding a service to monitoring is one scrape target rather than new code. Discovery and configuration are centralised the same way, through Eureka and the Config Server.

Architecture

Services pull configuration from the Config Server and register with Eureka at startup. Each exposes /actuator/prometheus; Prometheus scrapes them every 15 seconds and Grafana reads from Prometheus.

  1. Edge
    • api-gatewaySpring Cloud Gateway
  2. Platform
    • service-registryEureka server
    • configserverSpring Cloud Config, native profile
  3. Services
    • inventory-service
    • order-service
    • cart-service
  4. Observability
    • Prometheus15 s scrape interval
    • Grafanadashboard committed as JSON
System overview. Tiers read top to bottom; connections are listed below.

Request flow

  1. On startup each service imports its configuration from the Config Server and registers with Eureka.
  2. Every service exposes Micrometer metrics at /actuator/prometheus.
  3. Prometheus runs in Docker and scrapes the services on the host every 15 seconds, labelling each series with the application name.
  4. Grafana uses Prometheus as its data source; the dashboard is committed as Grafana_Dashboard.json.

Technical decisions

Eureka
Services find each other by name, so ports can change without editing every caller.
Spring Cloud Config
Shared configuration in one place instead of copied into each service.
Micrometer + Actuator
Standard JVM and HTTP metrics with no custom instrumentation code.
Prometheus
Pull-based scraping: a service that stops responding shows up as a failed target, not silence.
Grafana
Dashboards kept as JSON next to the code they describe.

Implementation

Each service is its own Gradle project with the Actuator and the Micrometer Prometheus registry on the runtime classpath. Scrape targets and labels are declared in Prometheus/Prometheus.yml.

Prometheus and Grafana run in Docker Compose while the Spring services run on the host and are reached through host.docker.internal.

Challenges and solutions

  1. Challenge

    With six processes, it is hard to tell which one is slow or down.

    Solution

    Prometheus scrapes /actuator/prometheus on every service every 15 seconds and labels each series with the application name; one Grafana dashboard reads them all.

  2. Challenge

    Hard-coded ports break callers whenever a service moves.

    Solution

    Services register with Eureka at startup and find each other by name.

  3. Challenge

    Prometheus and Grafana run in Docker, while the services run on the host.

    Solution

    Scrape targets point at host.docker.internal, so the containers can reach the host processes.

Security and reliability

  • Actuator endpoints are reachable without authentication. That is acceptable on a laptop; anywhere shared they belong on a separate management port or behind authentication.
  • Grafana’s admin credentials are set in the Compose file and are for local use only.

Testing

Each service includes the generated context-load test. The monitoring configuration has no automated checks.

Trade-offs

  1. Choice

    Pull-based scraping every 15 seconds

    What it costs

    Values are sampled, so a short-lived change between two scrapes is not recorded, and Prometheus must be able to reach every service.

  2. Choice

    Services on the host, monitoring in Docker

    What it costs

    Fast to iterate on a service, but the system is not one reproducible Compose stack.

  3. Choice

    Metrics only

    What it costs

    A dashboard shows that something is slow, not which call in a request chain caused it.

Results

A compact, reproducible example of service discovery, central configuration and metrics collection across several Spring services.

Lessons and next steps

Known limitations

  • The Compose file mounts ./prometheus/prometheus.yml, but the file is committed as Prometheus/Prometheus.yml. On case-sensitive file systems such as Linux the paths must match.
  • Grafana’s runtime database and plugin files are committed alongside the dashboard JSON; only the JSON needs to be versioned.
  • Images use the latest tag, so a fresh pull can change behaviour.
  • Metrics only — no distributed tracing or log aggregation yet.

What I would change next

  • Fix the case of the Prometheus configuration path so the stack runs on Linux.
  • Version only the dashboard JSON, and pin image tags.
  • Add distributed tracing so a slow request can be followed across services.

Next projectDevSecOps pipeline with Jenkins