Sub topic 15
Dashboards, alerting, and production practices
This chapter assumes you can expose Actuator endpoints, scrape /actuator/prometheus, and secure endpoints with Spring Security. It shows how those pieces fit together in production.
From endpoints to a monitoring loop
Actuator produces data, but it does not store or display it. A monitoring system does that. The usual loop has four steps: the application exposes metrics, a scraper collects them on a schedule, a dashboard shows trends, and alert rules notify people when a value crosses a threshold.
What to put on a dashboard
Start with the signals that tell you whether users are affected. Micrometer already records most of them for Spring MVC.
| Signal | Metric (Prometheus name) | What to watch |
|---|---|---|
| Traffic | `http_server_requests_seconds_count` | Requests per second |
| Errors | same metric, filtered by `status` 5xx | Error ratio |
| Latency | `http_server_requests_seconds_sum` / `_count`, or percentiles | Average and tail latency |
| JVM memory | `jvm_memory_used_bytes` | Heap growth over time |
| Garbage collection | `jvm_gc_pause_seconds_sum` | Pause time |
| Connection pool | `hikaricp_connections_active` | Saturation |
Add your own business metrics, such as orders_placed_total, next to these. A drop in orders with healthy infrastructure is often the first sign of a logic bug.
Writing alert rules
Alert on symptoms that users feel, not on every internal fluctuation. The following Prometheus rule fires when more than 5% of requests fail for five minutes.
groups:
- name: spring-boot-app
rules:
- alert: HighErrorRate
expr: |
sum(rate(http_server_requests_seconds_count{status=~"5.."}[5m]))
/
sum(rate(http_server_requests_seconds_count[5m])) > 0.05
for: 5m
labels:
severity: page
annotations:
summary: "More than 5% of requests are failing"
- alert: InstanceDown
expr: up{job="orders-service"} == 0
for: 2m
labels:
severity: page
annotations:
summary: "orders-service cannot be scraped"The for clause matters. It requires the condition to hold for a period, which prevents a single slow scrape from waking someone at night.
Production practices
- Limit exposure. Include only the endpoints you need. Keep
env,heapdump, andthreaddumpoff the public network. - Separate the port. Serve Actuator on
management.server.portand block that port at the load balancer. - Mind tag cardinality. Every unique tag value creates a new time series. Tagging a metric with a user ID or full URL can exhaust memory in both your app and Prometheus.
- Tag all metrics with the application and environment. This lets one dashboard serve many services.
- Keep health checks cheap. Probes run every few seconds. A slow indicator that calls an external system can make Kubernetes restart healthy pods.
- Use readiness for dependencies, liveness for the process. Putting a database check in liveness can cause restart loops during a database outage.
The following setting adds common tags to every metric.
management:
metrics:
tags:
application: orders-service
environment: productionExercises
Exercise
Your alert rule fires every time one request in a quiet period fails, which wakes the on-call engineer needlessly. Which two changes reduce these false alarms?
Show answer
Add or lengthen the for clause so the condition must persist, and use a ratio of errors to total requests instead of an absolute error count. A ratio is stable when traffic is low only if you also require a minimum request rate, so combine it with a traffic condition if needed.
Exercise
A teammate adds a metric tag containing the customer email address. Why is this a problem, and what should be tagged instead?
Show answer
Each distinct email creates a separate time series, which causes unbounded cardinality and high memory use in the application and Prometheus. Tag with low-cardinality values, such as channel, region, or order type.