less than 1 minute read

Spring Boot includes Actuator for Health and Metrics.

Enable

In application.properties, enable the endpoints that you want to include.

management.endpoints.web.exposure.include=prometheus,health,metrics,env
management.endpoints.web.path-mapping.prometheus=metrics
management.endpoints.web.path-mapping.metrics=metrics.json
management.endpoint.health.show-details=always
management.metrics.web.server.request.autotime.enabled=true

Create health check

Add logic as needed to build up or down.

@Component
public class LeagueAgeApiHealthIndicator implements HealthIndicator {

    public LeagueAgeApiHealthIndicator() {
    }

    @Override
    public Health health() {
        Health.Builder builder = new Health.Builder();
        return builder.up().build();
    }
}

Accessing health endpoint

Endpoint: http://localhost:9080/actuator/health

{
"status": "UP",
  "details": {
    "leagueAgeApi": {
      "status": "UP"
    },
    "diskSpace": {
      "status": "UP",
      "details": {
        "total": 62725623808,
        "free": 38508240896,
        "threshold": 10485760
      }
    }
  }
}

Accessing metrics endpoint

Endpoint: http://localhost:9080/actuator/metrics.json By default, this will deploy to endpoint metrics, but I leave that for prometheus.

This endpoint will give you a long list of available metrics:

{
"names": [
"jvm.threads.states",
"process.files.max",
"jvm.memory.used",
"jvm.gc.memory.promoted",
"tomcat.cache.hit",
"jvm.memory.max",
"system.load.average.1m",
"tomcat.cache.access",
"jvm.gc.max.data.size",
"jvm.memory.committed",
"system.cpu.count",
"logback.events",
"http.server.requests"
]
}

To access http.server.request metrics, navigate the next layer to endpoint http://localhost:9080/actuator/metrics.json/http.server.requests

To access a specific http endpoint “hello-world”, navigate to endpoint http://localhost:9080/actuator/metrics.json/http.server.requests?tag=uri:/hello-world