In a previous post you learned how to add information to the /observe/info
endpoint.
You can also add Git information to the endpoint.
For example you can add the Git commit id so you can see check, when the application is running in a production environment, which Git commit for the code was deployed.
In order to achieve this you must first generate a properties file with all Git information.
The next step is to process this file in your Helidon SE application and add the properties to the /observe/info
endpoint.
Define using default configuration
First you need to add the git-commit-id-maven-plugin
Maven plugin to your pom.xml
file.
This plugin can generate a properties file (git.properties
) with Git information.
In order to generate property names that can be automatically picked up by Helidon you must add the prefix server.features.observe.observers.info.values.git
.
The Maven plugin will generate a lot of information.
You can define regular expressions for property names that should be included in the generated git.properties
file.
The default location of the generated file is ${project.build.outputDirectory}/git.properties
.
This means it will be in the root of the classpath of your application.
In the following snippet you can see the configuration of the Maven plugin:
<!-- File: pom.xml --> ... <plugin> <groupId>io.github.git-commit-id</groupId> <artifactId>git-commit-id-maven-plugin</artifactId> <version>${git-commit-id-maven-plugin.version}</version> <executions> <execution> <id>get-git-info</id> <goals> <goal>revision</goal> </goals> </execution> </executions> <configuration> <!-- Enable generation of file ${project.build.outputDirectory}/git.properties with Git properties. --> <generateGitPropertiesFile>true</generateGitPropertiesFile> <!-- Set prefix so Helidon can include the generated properties automatically. --> <prefix>server.features.observe.observers.info.values.git</prefix> <!-- Include all properties that match the following patterns. --> <includeOnlyProperties> <includeOnlyProperty>.*git.branch$</includeOnlyProperty> <includeOnlyProperty>.*git.commit.(time|id)$</includeOnlyProperty> </includeOnlyProperties> </configuration> </plugin> ...
When you run the Maven compile
phase a new git.properties
file is generated.
The following shows an example of the contents of this properties file:
#Generated by Git-Commit-Id-Plugin server.features.observe.observers.info.values.git.branch=main server.features.observe.observers.info.values.git.commit.id=68eb0e361ff27dea59e34c40719fa84125fc46cb server.features.observe.observers.info.values.git.commit.time=2025-02-15T11\:26\:14+01\:00
To include this information in the output of the /observer/info
endpoint you must first include the following dependency so Helidon enables this endpoint:
<dependency> <groupId>io.helidon.webserver.observe</groupId> <artifactId>helidon-webserver-observe-info</artifactId> </dependency>
Next you must add the generated git.properties
file as a source of configuration properties using the Config.builder().addSource()
method.
Then you can use the configuration to create a WebServer
instance.
The properties prefixed with server.features.observe.observers.info.values
will be picked up by Helidon automatically and added to the info endpoint.
This includes the properties from git.properties
.
In the following code example you can see how to create a WebServer
instance using the configuration:
// File: Main.java ... import io.helidon.webserver.WebServer; import io.helidon.config.Config; ... public static void main(String[] args) { ... Config config = Config.builder() // Support environment variables and system properties. .metaConfig() .sources( // Use the application.properties file from the classpath. ConfigSources.classpath("application.properties"), // Use the git.properties file from the classpath. // This file is created by the git-commit-id-maven-plugin // Maven plugin. ConfigSources.classpath("git.properties")) .build(); WebServer server = WebServer.builder() ... // Read configuration properties. // This will use all properties with the prefix // "server.features.observe.observers.info.values." // for the /observe/info endpoint. .config(config.get("server")) .build() .start(); } ...
If you start the application and go to the /observe/info
endpoint you should see the following output:
$ curl -s -X GET --location "http://localhost:8080/observe/info" \ -H "Accept: application/json" | jq -r . { "git.branch": "main", "git.commit.time": "2025-02-15T11:26:14+01:00", "app.name": "blog-sample", "git.commit.id": "68eb0e361ff27dea59e34c40719fa84125fc46cb", "app.version": "0.0.0-SNAPSHOT" }
Define using code
Instead of using the default configuration and a prefix of server.features.observe.observers.info.values.git
you can also define the logic to define to read the git.properties
file in your code.
Once the properties are available they can be added to a InfoObserver
instance.
First you can remove the prefix option in our pom.xml
for the plugin configuration and generate all property names and values as we will filter these in code:
<!-- File: pom.xml --> ... <plugin> <groupId>io.github.git-commit-id</groupId> <artifactId>git-commit-id-maven-plugin</artifactId> <version>${git-commit-id-maven-plugin.version}</version> <executions> <execution> <id>get-git-info</id> <goals> <goal>revision</goal> </goals> </execution> </executions> <configuration> <!-- Enable generation of file ${project.build.outputDirectory}/git.properties with Git properties. --> <generateGitPropertiesFile>true</generateGitPropertiesFile> </configuration> </plugin> ...
Next you must add code to read the git.properties
file and add the properties to an InfoObserver
instance.
In the following code example you can see how this can be done:
// File: Main.java ... import io.helidon.webserver.observe.health.HealthObserver; import io.helidon.webserver.observe.info.InfoObserver; import io.helidon.webserver.WebServer; ... public static void main(String[] args) throws IOException { ... InfoObserver infoObserver = InfoObserver.builder() // Add extra key-value pairs for the // /observe/info endpoint based on // git.properties file. .addValues(getGitInfo()) // Extra custom properties that will be // added to the /observe/info endpoint. .addValues(Map.of("app.name", "blog-sample", "app.version", "0.0.0-SNAPSHOT")) .build(); WebServer server = WebServer.builder() ... // Add the info observer to the server. .addFeature(ObserveFeature.create(infoObserver)) .build() .start(); } /** * Read {@code git.properties} file and return a map with a subset of the properties. * * @return Map with {@code git.branch}, {@code git.commit.id} and {@code git.commit.time}. * @throws IOException Error reading {@code git.properties} file. */ private static Map<String, String> getGitInfo() throws IOException { Properties props = new Properties(); props.load(Main.class.getResourceAsStream("/git.properties")); return Map.of( "git.branch", props.getProperty("git.branch"), "git.commit.id", props.getProperty("git.commit.id"), "git.commit.time", props.getProperty("git.commit.time")); } ...
Written with Helidon 4.1.6.