Actuator is like adding a health monitoring system to your Spring Boot app. It gives you real-time metrics, health checks, and other operational info β all via HTTP endpoints like:
http://localhost:8080/actuator/metrics
π― Why Is It Useful for Microservices?
Imagine you have 10 microservices running. You need to answer:
- Is my app healthy?
- Are the APIs responding fast?
- Is the memory usage too high?
- Are the DB connections overloaded?
π Actuator gives you these answers out-of-the-box.
π What Metrics Does Actuator Provide?
Letβs explore the main types of metrics youβll find useful for monitoring:
1. π§ JVM Metrics (Java Virtual Machine)
These tell you how your app is using memory, threads, and classes.
Metric | What It Tells You | Example |
---|---|---|
jvm.memory.used |
How much memory is used by your app | 128 MB |
jvm.memory.max |
The max memory your app can use | 512 MB |
jvm.threads.live |
How many threads are running | 100 |
jvm.classes.loaded |
Number of classes loaded into memory | 10,000 |
π‘ Why care? If memory or thread usage spikes too much, your app could crash.
2. π₯οΈ CPU & System Metrics
Metric | What It Tells You | Example |
---|---|---|
system.cpu.usage |
Total CPU usage (system-wide) | 0.60 (60%) |
process.cpu.usage |
CPU used by your app only | 0.20 (20%) |
system.cpu.count |
Number of processor cores | 8 |
process.uptime |
How long the app has been running | 4 hours |
π‘ Why care? High CPU usage may slow down your APIs.
3. π HTTP Request Metrics
Metric | What It Tells You | Example |
---|---|---|
http.server.requests |
Number of API calls + response time | 500 calls, avg 120ms |
β Tags: uri , method , status
|
Helps slice metrics per API route |
/login , POST , 200 OK
|
π‘ Why care? You can find slow endpoints or too many errors (e.g. 500s).
4. π½ Database (HikariCP) Metrics
If using a database connection pool (like HikariCP):
Metric | What It Tells You | Example |
---|---|---|
hikaricp.connections.active |
Number of DB connections in use | 5 |
hikaricp.connections.idle |
Available connections in pool | 10 |
hikaricp.connections.max |
Maximum allowed connections | 20 |
π‘ Why care? If all DB connections are used, your app can freeze or slow down.
5. π¦ Cache Metrics
If you use caching (like Caffeine, Redis, etc.):
Metric | What It Tells You | Example |
---|---|---|
cache.gets |
How many times you read from the cache | 1000 hits |
cache.misses |
How many times data wasn't in cache | 200 misses |
cache.puts |
How many times you added to cache | 300 puts |
π‘ Why care? Too many cache misses = your DB is doing extra work.
6. π Health Endpoint
Shows status of your app and its dependencies (DB, disk, etc.).
π Endpoint:
http://localhost:8080/actuator/health
Example output:
{
"status": "UP",
"components": {
"db": { "status": "UP" },
"diskSpace": { "status": "UP" }
}
}
π‘ Helps monitoring tools like Prometheus, Grafana, or Kubernetes to know if your service is healthy.
7. π Custom Metrics (Optional but Powerful)
You can define your own metrics like:
meterRegistry.counter("orders.placed").increment();
meterRegistry.gauge("temperature", () -> getRoomTemp());
π‘ Used to track business logic (e.g., number of signups, order volume).
βοΈ How to Enable Actuator Metrics
- Add the dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- In
application.properties
:
management.endpoints.web.exposure.include=*
- For Prometheus/Grafana:
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
Then hit:
http://localhost:8080/actuator/prometheus
π§ͺ Use Case: Example for Microservice Monitoring
Letβs say you have a payment microservice:
-
/actuator/health
: Used by Kubernetes for health checks. -
/actuator/metrics
: View API performance, DB usage. -
http.server.requests
: See if/pay
API is slow. -
hikaricp.connections.active
: Monitor DB pool overload. -
orders.placed
: Track how many payments succeeded.
Would you like a dashboard setup in Grafana, or a custom Prometheus config example to visualize these metrics?
Top comments (0)