Inthis tutorial, we plan to learn about Spring Boot Actuator and how to add custom information to the /info endpoint. We will need Java 17 or higher, Maven, and your favorite IDE as a prerequisite.
Spring Boot Actuator is a Spring Framework sub-project. It allows us to monitor our application, get information about the project, gather metrics, or just check the state of our application/database.
A very convenient part of the Actuator is that it provides all support out of the box without any additional configuration. All we need to do to access all the information is to add the following dependency in our pom.xml file:
After adding the actuator dependency, the only remaining step is to enable the endpoints provided by it. Spring boot can provide information to any target type, such as JMX or Web. To use an endpoint:
Now we can check the endpoint at the following path: /actuator/info. And we will see the following empty response by default because we did not configure any properties for the /info endpoint.
All we need to do is to prefix the name of the property with info.". We can also group the properties, giving them the same prefixes. We can see below how we added three new pieces of information to our endpoint: the name of the project, the description, and the organization name.
We can add build information and additional properties to our /info endpoint using Spring Boot Maven Plugin. Spring Boot Actuator will show build details if a proper META-INF/build-info.properties file is present.
This Spring boot tutorial examined various methods to add custom data to our /info endpoint. This endpoint may provide very useful information about our project but may also be a sensitive endpoint that should not be exposed to everyone.
In a distributed, fast-paced environment, dev teams often want to find out at what time they deployed the app, what version of the app they deployed, what Git commit was deployed, and more.
Spring Boot Actuator is a sub-project of Spring Boot. In this section, we will quickly see how to bootstrap the sample project and enable the /info endpoint. If you want to know more about Spring Boot Actuator, there is already a great tutorial.
After the project is built we will expose the built-in /info endpoint over HTTP.By default the /info web endpoint is disabled. We can simply enable it by adding the the management.endpoints.web.exposure.include property in the application.properties configuration:
Adding useful build information helps to quickly identify the build artifact name, version, time created, etc. It could come in handy to check if the team deployed the relevant version of the app. Spring Boot allows easy ways to add this using Maven or Gradle build plugins.
The Spring Boot Maven Plugin comes bundled with plenty of useful features such as creating executable jar or war archives, running the application, etc. It also provides a way to add application build info.
If we sync the Gradle project now, we can see a new task bootBuildInfo is available for use. Running the task will generate similar build/resources/main/META-INF/build-info.properties file with build info (derived from the project). Using the DSL we can customize existing values or add new properties:
Time to run the app using ./gradlew bootRun (for macOS/Linux) or gradlew.bat bootRun (for Windows) command. Once the app is running, we can open the :8080/actuator/info endpoint in the browser and find the response as:
Git information comes handy to quickly identify if the relevant code is present in production or if the distributed deployments are in sync with expectations. Spring Boot can easily include Git properties in the Actuator endpoint using the Maven and Gradle plugins.
Using this plugin we can generate a git.properties file. The presence of this file will auto-configure the GitProperties bean to be used by the GitInfoContributor bean to collate relevant information.
In this article, we learned how to use Spring Actuator to expose relevant information about our application. We found out how information about the build, environment, Git, and Java environment can be added to the Actuator /info endpoint. We also looked at how all this information can be configured and controlled by the Maven/Gradle build plugins.
Spring Boot Actuator endpoints offer a way to get insights from the running Spring Boot application. Out of the box, /actuator/info endpoint exposes information about the operating system the app is running on, the JVM version or Git information.
In this short article you'll learn how you can add a list of the Maven dependencies used in your project to the info endpoint. Actually, you'll learn how to add much more information than just the dependencies.
Similar to getting Git information, we need to hook into the build process to generate the dependency list from pom.xml and put it into target/classes directory so that it gets packaged into application jar. Sounds a bit tedious? For sure it would be, but lucklily we don't need to do it manually as there are ready to use solutions for that. One of them is CycloneDX Maven Plugin.
CycloneDX in fact is much more than a plugin that gathers list of dependencies. It's a cross platform standard for Software Bill Of Materials. I'll not go deeper into it, as it's not relevant for this article - we're just going to use it to extract the list of the dependencies.
Now, once you execute ./mvnw package, you'll find in target/classes/bom.json quite a big file containing serious amount of information about dependencies including licenses, checksums. I stripped it out heavily just to give you a feeling how it looks, as the original file even for relatively small project is over 1k lines long:
If that's what you need - great - but it's likely more than you actually need. Assuming we need just the list of structures like group id, artifact id and version, we can filter greater part of what's currently returned.
To filter out the noise, we need to deserialize JSON to something more meaningful and then filter and map to object structures we are interested in. While we could create classes that represent the JSON structure, we can save ourselves time by adding the dependency containing the CycloneDX model (but note that it does contain few transitive dependencies).
One important thing to note here is that we should not use Spring Boot managed ObjectMapper for deserialization because its configuration may not reflect how the CycloneDX JSON file was generated. Instead, it's safer to use Cyclone's JsonParser class.
CycloneDX is a standardized way to describe Software Bill Of Materials, and we can use its Maven plugin to generate the list of the dependencies exposed by Spring Boot Actuator in the info endpoint. In addition to the dependencies coordinates, we can also list licenses, checksums and links to project websites.
Spring Boot includes over 20 built-in endpoints that are auto-configured if available. Just like the rest of the springboot features, actuators are a big productivity boost for development teams. In large and complex production systems with several microservices, the ability to monitor individual instances over a simple HTTP interface is a fundamental SRE need.
To add some custom information to the build info, we will provide additional properties to the plugin configuration. You can add your custom key-value pairs or even use any property within the pom.xml. Here is an example where we add a custom "build_level.random_property" with some value and "java.version" derived from Maven pom.xml properties.
When you check :8080/actuator/info URL again, the output will be:You can check the generated build information from the target\classes\META-INF\build-info.properties file, which contains the source for this information.
In today's #HOWTO blog post I want to show you a way to expose Git information (like commit hash, commit timestamp, branch) of the deployed application using Spring Boot Actuator. If you are not versioning your Maven application with the X.Y.Z tag and you want to know which state of your application is deployed to production/development, this blog post can be quite helpful. In addition, you will also get information about the Maven build (version, the timestamp of the build, etc.). You could use this information for displaying the version of your API backend service in your frontend application.
To extract the information about your git repository your application has to reside in a folder with a) a .git directory in one of the parent folders or b) the .git directory directly in the working folder.
The pl.project13.maven:git-commit-id-plugin will pick up the information during the build and store it per default to target/classes/git.properties. In addition, you have to configure an execution goal for your spring-boot-maven-plugin (this maven plugin is always present if you generated your Spring Boot app with the Spring Initializer). Your section in your pom.xml should look like the following:
The collected data will be available at /actuator/info which is enabled per default and you don't have to configure the Actuator endpoints with your application.properties file. You can now build your Spring Boot application with mvn clean package and start it from your IDEA or with java -jar target/your-application-name.jar on the console. If you use the default port you can access the endpoint with your browser on :8080/actuator/info. The output should contain the following information:
If you don't want to expose this information directly to your clients you can secure it with Spring Security. Another solution is to use a different port for your Actuator endpoints and disallow public access to it.
With Spring Boot you just have to set the property management.server.port to a different port than server.port property in your application.properties or application.yml file and the Actuator endpoints won't be accessible on port 8080.
3a8082e126