Lesson 1: Fundamentals of Monitoring with Boot

1. Module 8 Overview

This module is all about monitoring your API and tracking good, useful metrics from the start.

It's also a Spring Boot heavy lesson - mainly because the framework makes the Ops story a lot better and saves us time and effort in having to implement this low level support ourselves. And while this isn't a Spring Boot focused module, we do want to use the best tool for the job.


2. Goals

Learn how Spring Boot makes monitoring and tracking the metrics of a Spring API very easy.


3. Lesson Notes

The relevant module you need to import when you're starting with this lesson is: m8-lesson1-start
If you want to skip and see the complete implementation, feel free to jump ahead and import: m8-lesson1


We are going to use Spring Boot actuators to start tracking metrics and expose these via several endpoints:

  • /info
  • /health
  • /metrics

We are also going to configure these endpoints via application.properties:

- "endpoints.{endpointname}.{option}" - with the following options:

  • id
  • sensitive
  • enabled

- ex: endpoints.health.sensitive=false

The metrics endpoint is not enabled by default, so we need to add the property:

endpoints.metrics.sensitive=false


- other endpoint related options can be configured as well:
management.context-path = actuators
management.port = 8081

/info endpoint configuration

info.app.name = My App
info.build.version = @project.version@

Note how we are using the @@ syntax here to get the value of the property from the pom of the parent (after maven runs package)

URLs for a local deployment

http://localhost:8081/api/management/info

http://localhost:8081/api/management/metrics

Note that the assumption here is that the deployment happens via Spring Boot (where the port is already configured to 8081 and the app will be deployed at the root). If deployment happens in a standard Tomcat server, then these URLs will be slightly different.


3.1. Upgrade Notes

With respect to Spring Boot 1, the actuators in Spring Boot 2 have undergone important changes and some of them are breaking. For example, Spring Boot 2 uses prefix management.* for the names of application properties that configure the actuators' endpoints. Similarly, the management.context-path property is now management.endpoints.web.base-path.

Additionally, the actuator endpoints are now located beneath the /actuator base path by default, so for example, if we don't provide the management.endpoints.web.base-path property in our application, our info endpoint will be located in /api/actuator/info.

Then, most actuators' endpoints by default are enabled but not exposed via the web. In order to be able to use an endpoint via the web, we should first enable it (which is the default for all endpoints except for shutdown) and make sure that it is exposed:

In addition, access to the actuators' endpoints is now integrated into the Spring Security. It means that we can now configure the access to them using the Spring Security of our application. For more details, check the links in the resource section.

Particularly for the /metrics endpoint, you'll notice it now returns just a list of metric names. Each of these can be further queried by browsing /metrics/{metric.name}. So, for example, in order to access HTTP Request metrics similar to the ones we explore in the video, we should send a request to the /metric/http.server.requests endpoint.


4. Resources

Spring Boot Actuator: Production-ready features (the reference)

Building a RESTful Web Service with Spring Boot Actuator (official guide)

Spring Boot Actuator (my article)

- Spring Boot Actuator 2.x Changes

- Securing Actuator Endpoints

RwS - Fundamentals of Monitoring with Boot - transcript.pdf
Complete and Continue