CodeGym /Courses /Module 5. Spring /Lecture 112: Key Metrics and Application Health Info

Lecture 112: Key Metrics and Application Health Info

Module 5. Spring
Level 19 , Lesson 1
Available

Today we'll talk about one of the most useful parts of Spring Boot Actuator — metrics and app health. Monitoring system health isn't just a chance to feel a little like the "doctor" of your app, it's an essential tool for performance analysis and troubleshooting.


What metrics are available via Actuator?

Spring Boot Actuator provides a wide range of metrics out of the box. Instead of digging through code to figure out why your server suddenly decided to "take a nap", Actuator does that for you by collecting and exposing data about your application's state.

Here are some of the metrics Actuator gives you by default (and a bit of humor for the list):

  • JVM memory: how much memory is actively used, how much is free, and what's going on in your "garbage bin".
  • Threads: current thread activity. Useful to check if your app is trying to use everything available, like a kid with candy.
  • CPU load: helps figure out if your CPU is working up a sweat.
  • HTTP sessions: monitoring the number of active sessions (yes, clients can sometimes be a little too curious).
  • Request response time: how fast your app responds (or doesn't) to incoming requests.
  • Error rate: a lifesaver for those who want to find where the system started going sideways.

Viewing metrics

These metrics can be viewed through the /actuator/metrics endpoint. For example, open in your browser:


http://localhost:8080/actuator/metrics

You'll see a list of all available metrics, like jvm.memory.used, system.cpu.usage, http.server.requests, and others.

To see detailed info for a specific metric you can request:


http://localhost:8080/actuator/metrics/{metricName}

For example, to view the metric related to JVM memory usage:


http://localhost:8080/actuator/metrics/jvm.memory.used

Endpoint /health: checking application health

What if your app is feeling under the weather? The /actuator/health endpoint is your window into its status. It lets you check whether the app is functioning correctly and reports "healthy" or "not so much".

The /health endpoint returns application status as a JSON response. Here's how a basic healthy response might look:


{
  "status": "UP"
}

If there's a problem in the system, for example the database is unreachable, the status will change to DOWN, and a details field will show what's wrong.

Example response if the database is unavailable:


{
  "status": "DOWN",
  "components": {
    "db": {
      "status": "DOWN",
      "details": {
        "error": "java.sql.SQLException: Cannot connect to database"
      }
    }
  }
}

Showing additional details

By default, the /health endpoint is conservative and only shows the status. You can configure it to reveal more info. This is done via properties in application.properties or application.yml. For example:


management.endpoint.health.show-details=always

Now the /health endpoint will show details like database status, sessions, etc.


Configuring metrics visibility

Spring Boot Actuator is careful by default and won't expose everything to the world. But you can configure access to metrics and what gets exposed.

If you want to control which Actuator endpoints are visible, you can use this property:


management.endpoints.web.exposure.include=health, metrics

Here we're saying that only the health and metrics endpoints will be exposed externally. To enable all standard endpoints, use *:


management.endpoints.web.exposure.include=*

If you want to exclude some endpoints, do this:


management.endpoints.web.exposure.exclude=shutdown

Restricting access by roles

For production it's important to protect endpoints with Spring Security so only authorized users can see metrics. For example, you can restrict access to users with the ADMIN role.


Applying metrics in real life

Knowing your metrics helps avoid situations where your app "throws in the towel" under load and you're the last to know. Here's how you can use that info:

  • Performance analysis: metrics let you track how fast and efficient your app is. This helps spot performance bottlenecks.
  • Debugging: the error rate in requests will show that something's off before users start filing a flood of complaints.
  • Preventing outages: monitoring things like memory and CPU lets you spot problems early and take action.

Example: using the "request count" metric

Say you want to know how often users hit your app. You can use the http.server.requests metric. Do this:

Open in your browser:


http://localhost:8080/actuator/metrics/http.server.requests

The response might look like this:


{
  "name": "http.server.requests",
  "measurements": [
    {
      "statistic": "count",
      "value": 150
    },
    {
      "statistic": "totalTime",
      "value": 123.45
    }
  ],
  "availableTags": [
    {
      "tag": "status",
      "values": ["200", "404", "500"]
    },
    {
      "tag": "method",
      "values": ["GET", "POST"]
    }
  ]
}

Here we can see 150 requests were handled and the total processing time was 123.45 seconds. This helps you understand how heavily your API is being used.


Now that you know what metrics Spring Boot Actuator exposes and how to check your application's health, you'll find it easier to manage your app. Next time your server decides to "feel down", you'll be armed with the right tools. Or at least you'll hear about it before users start DM-ing support.

Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION