CodeGym /Courses /Module 5. Spring /Lecture 115: Practice: setting up and viewing app metrics...

Lecture 115: Practice: setting up and viewing app metrics

Module 5. Spring
Level 19 , Lesson 4
Available

1. Integrating Actuator into your learning project

First, we need to add Spring Boot Actuator to our existing (or new) project. Adding Actuator is basically just adding a dependency.

If you use Maven, add the following dependency to pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

If you're working with Gradle, add this line to your build.gradle:

implementation 'org.springframework.boot:spring-boot-starter-actuator'

After that, refresh your project dependencies. Actuator is enabled! Easy, right? Spring magic!


2. Checking endpoint availability

After adding the Actuator dependency, start your application. By default Actuator exposes several standard endpoints for monitoring.

For example:

  • /actuator/health — the app's health status.
  • /actuator/info — info about the application.
  • /actuator/metrics — various performance metrics.

Try opening /actuator/health in your browser or Postman. If you see something like {"status":"UP"}, Actuator is hooked up successfully.

The secret path to happiness: customizing the base path for endpoints

You might be thinking — "Can I make the path to these endpoints more convenient or, on the flip side, hide them from overly curious eyes?" — yes, you can. In application.properties you can change the base path:

management.endpoints.web.base-path=/my-actuator

Now all your endpoints will be available, for example, at /my-actuator/health.


3. Viewing metrics

Working with /metrics

The /actuator/metrics endpoint is a real treasure chest. Here you'll find metrics about active threads, memory usage, CPU load, and more. Type this in your browser or Postman:

http://localhost:8080/actuator/metrics

The metrics list can be pretty long. Here's an example of what you might see:

{
  "names": [
    "jvm.memory.used",
    "jvm.memory.max",
    "process.cpu.usage",
    "system.cpu.usage",
    "http.server.requests",
    ...
  ]
}

You can request detailed info for a specific metric. For example, to see how much memory the JVM is using, request:

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

And you'll get something like this:

{
  "name": "jvm.memory.used",
  "measurements": [
    {
      "statistic": "VALUE",
      "value": 12345678
    }
  ],
  "availableTags": [
    {
      "tag": "area",
      "values": ["heap", "nonheap"]
    }
  ]
}

"Aha — now I know how much memory is being used!" — you'll say. And you'll be right.


Practical task: enabling endpoints

Not all Actuator endpoints are enabled by default. Let's activate some extra metrics. In application.properties add the following setting:

management.endpoints.web.exposure.include=*

Now all Actuator endpoints are available. Try refreshing the list of available metrics by requesting /actuator.


4. Real-time metrics analysis

Imagine you're the admin (or the "metrics overlord"), and your app suddenly starts lagging. You can check /metrics to see if there's increased load. For example, the http.server.requests metric will show which requests are causing the strain.

Here's how that might look:

{
  "name": "http.server.requests",
  "measurements": [
    {
      "statistic": "COUNT",
      "value": 120
    },
    {
      "statistic": "MAX",
      "value": 150.5
    }
  ],
  "availableTags": [
    {
      "tag": "method",
      "values": ["GET", "POST"]
    },
    {
      "tag": "uri",
      "values": ["/api/users", "/api/orders"]
    }
  ]
}

You can find out how many requests, for example, are being handled at /api/users. Useful? Definitely.


5. Practical task: setting up a custom metric

Now it's time to get a little creative and add your own metric. Imagine you want to track how many items are added to users' carts.

Add the following code to one of your components:


import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.stereotype.Component;

@Component
public class CartMetrics {

    private final MeterRegistry meterRegistry;

    public CartMetrics(MeterRegistry meterRegistry) {
        this.meterRegistry = meterRegistry;
    }

    public void incrementCartItems(int items) {
        meterRegistry.counter("cart.items.added").increment(items);
    }
}

Now call incrementCartItems() whenever items are added to the cart. The cart.items.added metric will start tracking the total number of added items. You can check it via:

http://localhost:8080/actuator/metrics/cart.items.added

6. Improving through observation

Based on real-time data you can make decisions. For example, if you see jvm.memory.used constantly close to the max, you might want to refactor how large objects are handled or increase the JVM heap size.

If http.server.requests shows abnormally low response times — that's a good sign your app is handling load well. But if response times increase, maybe it's time to think about caching or scaling.


Self-study assignment

Now that you're an expert in setting up and using metrics, try the following:

  1. Set up Actuator in your learning project.
  2. Enable all endpoints.
  3. Find and analyze three key app metrics (for example, request performance, memory usage, and CPU load).
  4. Set up a custom metric to track some important aspect of your app (for example, the number of user operations performed per day).
  5. Try to find issues in your app using the metrics.

And don't forget to feel like a real monitoring superhero!

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