0
0
SpringbootHow-ToBeginner · 4 min read

How to Use Metrics Endpoint Actuator in Spring Boot

To use the metrics endpoint in Spring Boot Actuator, add the spring-boot-starter-actuator dependency and enable the endpoint in your application.properties. Access metrics data via /actuator/metrics URL to monitor application performance and health.
📐

Syntax

The metrics endpoint is part of Spring Boot Actuator and exposes application metrics in JSON format. You enable it by adding the Actuator starter and configuring your application properties.

  • spring-boot-starter-actuator: Dependency to include Actuator features.
  • management.endpoints.web.exposure.include=metrics: Property to expose the metrics endpoint over HTTP.
  • /actuator/metrics: The HTTP path to access all available metrics.
  • /actuator/metrics/{metricName}: Access specific metric details by name.
properties
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
}

# application.properties
management.endpoints.web.exposure.include=metrics

# Access metrics at http://localhost:8080/actuator/metrics
# Access specific metric at http://localhost:8080/actuator/metrics/{metricName}
💻

Example

This example shows a simple Spring Boot application with Actuator enabled to expose the metrics endpoint. You can access general metrics or specific ones like jvm.memory.used.

java
package com.example.metricsdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MetricsDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(MetricsDemoApplication.class, args);
    }
}

# application.properties
management.endpoints.web.exposure.include=metrics

# Run the app and visit:
# http://localhost:8080/actuator/metrics
# http://localhost:8080/actuator/metrics/jvm.memory.used
Output
{ "names": [ "jvm.memory.used", "jvm.gc.pause", "http.server.requests", "process.uptime", "system.cpu.usage" ] } // Example output for /actuator/metrics/jvm.memory.used { "name": "jvm.memory.used", "measurements": [ { "statistic": "VALUE", "value": 12345678.0 } ], "availableTags": [ { "tag": "area", "values": ["heap", "nonheap"] }, { "tag": "id", "values": ["PS Eden Space", "PS Survivor Space", "PS Old Gen"] } ] }
⚠️

Common Pitfalls

Common mistakes when using the metrics endpoint include:

  • Not including the Actuator dependency, so the endpoint is missing.
  • Failing to expose the metrics endpoint in application.properties, which keeps it hidden by default.
  • Trying to access the endpoint without proper server running or wrong URL path.
  • Expecting metrics without any instrumentation or custom metrics configured.

Always verify your dependencies and configuration to avoid these issues.

properties
# Wrong: Missing exposure property
# application.properties
management.endpoints.web.exposure.include=health,info

# Right: Include metrics
management.endpoints.web.exposure.include=health,info,metrics
📊

Quick Reference

Summary tips for using the metrics endpoint:

  • Add spring-boot-starter-actuator to your project.
  • Expose the metrics endpoint via management.endpoints.web.exposure.include.
  • Access all metrics at /actuator/metrics.
  • Access specific metric details at /actuator/metrics/{metricName}.
  • Use metrics data to monitor JVM, HTTP requests, system, and custom metrics.

Key Takeaways

Add spring-boot-starter-actuator and expose metrics endpoint in application properties.
Access metrics at /actuator/metrics for all metrics or /actuator/metrics/{metricName} for details.
Without exposing the endpoint, metrics data will not be available over HTTP.
Metrics help monitor JVM, HTTP requests, and system performance easily.
Check your configuration if metrics endpoint returns 404 or is missing.