Spring Boot Actuator, Custom Metrics, Prometheus and Grafana
Spring Boot Actuator provides production-ready endpoints for monitoring and managing a Spring Boot application. Micrometer provides application metrics, Prometheus collects and stores those metrics, and Grafana visualizes them using dashboards and graphs.
Big Picture
Spring Boot Application
|
+---- Actuator
| |
| +---- /manage/health
| +---- /manage/info
| +---- /manage/metrics
| +---- /manage/prometheus
| +---- Custom @Endpoint
|
+---- Micrometer Custom Metrics
|
v
Prometheus
|
v
GrafanaWhat is Spring Boot Actuator?
Spring Boot Actuator provides endpoints that expose operational information about an application. Common endpoints include health, info, metrics, loggers, beans, env and prometheus.
management.endpoints.web.base-path=/manage
management.endpoints.web.exposure.include=health,info,metrics,prometheus
management.endpoint.health.show-details=alwaysCommon Actuator Endpoints
- /manage/health - Application health
- /manage/info - Application information
- /manage/metrics - Available metrics
- /manage/prometheus - Metrics in Prometheus format
- /manage/loggers - Logger configuration
- /manage/beans - Spring beans
What is a Custom Actuator Endpoint?
A custom Actuator endpoint allows us to expose application-specific operational information or management operations through the Actuator infrastructure.
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
@Endpoint(id = "myinfo")
public class MyInfoEndpoint {
@ReadOperation
public Map<String, Object> getInfo() {
return Map.of(
"application", "Order Service",
"version", "2.5.1",
"environment", "production",
"region", "Mumbai"
);
}
}With management.endpoints.web.base-path=/manage, this endpoint can be accessed using /manage/myinfo after the endpoint is exposed.
management.endpoints.web.exposure.include=health,info,metrics,prometheus,myinfoGET /manage/myinfo{
"application": "Order Service",
"version": "2.5.1",
"environment": "production",
"region": "Mumbai"
}When Should We Use a Custom Actuator Endpoint?
- Expose application version information
- Expose feature flag status
- Expose operational configuration information
- Expose cache status or diagnostic information
- Provide controlled management operations
- Expose information that is not naturally represented as a metric
What is a Custom Metric?
A custom metric is an application-specific measurement created using Micrometer. Custom metrics are useful when we want to monitor business or application behavior over time.
- Number of orders created
- Number of payment failures
- Number of emails sent
- Number of users registered
- Payment processing time
- Number of cache hits
Real-World Custom Counter Example
Suppose an e-commerce application wants to monitor how many orders are created.
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.stereotype.Service;
@Service
public class OrderService {
private final Counter ordersCreated;
public OrderService(MeterRegistry registry) {
ordersCreated = Counter.builder("orders.created")
.description("Number of orders created")
.register(registry);
}
public void createOrder() {
// Save order
ordersCreated.increment();
}
}Every time createOrder() successfully creates an order, the counter is incremented.
Custom Metric Example
orders.created = 15000Counter, Gauge and Timer
- Counter - Used for values that continuously increase, such as orders created or payment failures
- Gauge - Used for a current value that can increase or decrease, such as active users or queue size
- Timer - Used to measure execution time, such as API response time or payment processing time
Real-World Timer Example
Timer paymentTimer;
paymentTimer = Timer.builder("payment.processing.time")
.description("Payment processing duration")
.register(meterRegistry);
paymentTimer.record(() -> processPayment());This allows us to monitor payment processing latency, such as average duration, percentile latency and maximum duration.
Prometheus
Prometheus is a monitoring system and time-series database. It periodically scrapes metrics exposed by applications and stores the values over time.
Spring Boot
|
| /manage/prometheus
v
Prometheus
|
| stores time-series metrics
v
Metrics DatabasePrometheus Dependency
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>Expose Prometheus Endpoint
management.endpoints.web.exposure.include=health,info,metrics,prometheusWith a custom Actuator base path of /manage, Prometheus can scrape /manage/prometheus.
Grafana
Grafana is primarily a visualization and dashboarding platform. It connects to Prometheus as a data source, queries the stored metrics and displays them using graphs, tables, gauges and dashboards.
Application
|
v
Micrometer
|
v
Actuator /manage/prometheus
|
v
Prometheus
|
| PromQL
v
Grafana
|
v
DashboardPrometheus vs Grafana
- Prometheus collects and stores metrics
- Prometheus provides PromQL for querying metrics
- Grafana visualizes metrics
- Grafana creates dashboards and panels
- Grafana can use Prometheus as a data source
Do We Create Graphs in Prometheus?
Normally, no. Prometheus is responsible for collecting, storing and querying metrics. Grafana is normally used to create dashboards, graphs, tables, gauges and other visualizations.
Custom Actuator Endpoint vs Custom Metric
A custom Actuator endpoint and a custom metric solve different problems. A custom endpoint exposes operational information or management operations, while a custom metric represents numerical data that should be monitored over time.
- Application version -> Custom Actuator endpoint or info
- Feature flag status -> Custom Actuator endpoint
- Cache diagnostic information -> Custom Actuator endpoint or HealthIndicator
- Orders created -> Custom Micrometer metric
- Payment failures -> Custom Micrometer metric
- API latency -> Timer metric
- Active database connections -> Hikari/Micrometer metric
Real-World E-Commerce Example
Order Service
Health Information:
/manage/health
Operational Information:
/manage/myinfo
/manage/features
Metrics:
orders.created
payments.failed
payment.processing.time
hikaricp.connections.active
Monitoring:
Actuator + Micrometer
|
v
Prometheus
|
v
GrafanaReal-World Decision Example
- Is PostgreSQL available? -> HealthIndicator
- How many orders were created? -> Counter
- How many payments failed? -> Counter
- How long does payment processing take? -> Timer
- How many users are currently online? -> Gauge
- What application version is running? -> Info or custom endpoint
- Which feature flags are enabled? -> Custom Actuator endpoint
- Show metrics as graphs -> Grafana
- Collect and store metrics -> Prometheus
Important Architecture
Spring Boot
|
+--------------+--------------+
| | |
v v v
Health Metrics Operations
| | |
v v v
HealthIndicator Micrometer @Endpoint
|
v
/manage/prometheus
|
v
Prometheus
|
v
GrafanaImportant Interview Point
Do not create a custom Actuator endpoint simply because you want a Grafana dashboard. If the data is a numerical value that needs to be monitored over time, create a Micrometer metric and expose it through the Prometheus endpoint. Use a custom Actuator endpoint when you need structured operational information or a management operation.
Example of the Wrong Approach
GET /manage/ordersCount
{
"count": 15000
}If the goal is to monitor order count over time, a custom endpoint is usually not the best approach. A Micrometer counter is more appropriate because Prometheus can scrape and store the metric over time.
Better Approach
orders_created_total 15000Prometheus stores the time-series values and Grafana can query them and create dashboards.
Interview Questions
- What is Spring Boot Actuator?
- What is a custom Actuator endpoint?
- How do you create a custom Actuator endpoint?
- What is the difference between @Endpoint and @RestController?
- What is Micrometer?
- What is a custom metric?
- What is the difference between Counter, Gauge and Timer?
- What is Prometheus?
- What is Grafana?
- What is the difference between Prometheus and Grafana?
- How does Prometheus get metrics from Spring Boot?
- How does Grafana get metrics from Prometheus?
- Should we create a custom Actuator endpoint for every custom metric?
- When should you use a custom metric instead of a custom endpoint?
- How would you monitor HikariCP connections?
- How would you monitor payment failures?
- How would you monitor API response time?
- How would you expose application version information?
- Why should high-cardinality metric tags be avoided?
Short Interview Answer
Actuator exposes operational endpoints, Micrometer provides application metrics, Prometheus collects and stores those metrics, and Grafana visualizes them. Custom Actuator endpoints are useful for operational information or management operations, while custom Micrometer metrics are better for numerical values that need to be monitored over time.
Easy Way to Remember
HealthIndicator
-> Is the application healthy?
@Endpoint
-> Give me operational information / perform management operation
Micrometer
-> Measure something
Prometheus
-> Collect + Store + Query metrics
Grafana
-> Visualize metrics