mn create-app example.micronaut.micronautguide \
--features=management,graalvm \
--build=gradle \
--lang=java \
--test=junit
Exposing a Health endpoint for your Micronaut application
Learn how to expose a health endpoint for your Micronaut application.
Authors: Sergio del Amo, Dean Wette
Micronaut Version: 4.6.3
1. Getting Started
In this guide, we will create a Micronaut application written in Java.
You will learn how to use the Micronaut Management feature to enable the "health" endpoint for your application.
2. What you will need
To complete this guide, you will need the following:
-
Some time on your hands
-
A decent text editor or IDE (e.g. IntelliJ IDEA)
-
JDK 17 or greater installed with
JAVA_HOME
configured appropriately
3. Solution
We recommend that you follow the instructions in the next sections and create the application step by step. However, you can go right to the completed example.
-
Download and unzip the source
4. Writing the Application
Create an application using the Micronaut Command Line Interface or with Micronaut Launch.
If you don’t specify the --build argument, Gradle with the Kotlin DSL is used as the build tool. If you don’t specify the --lang argument, Java is used as the language.If you don’t specify the --test argument, JUnit is used for Java and Kotlin, and Spock is used for Groovy.
|
The previous command creates a Micronaut application with the default package example.micronaut
in a directory named micronautguide
.
If you use Micronaut Launch, select Micronaut Application as application type and add management
, and graalvm
features.
If you have an existing Micronaut application and want to add the functionality described here, you can view the dependency and configuration changes from the specified features, and apply those changes to your application. |
4.1. Testing Health Endpoints
The Micronaut management dependency added for the project supports monitoring your application via endpoints: special URIs that return details about the health and state of your application. Once the management
dependency is included a /health
endpoint is exposed.
implementation("io.micronaut:micronaut-management")
package example.micronaut;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpStatus;
import io.micronaut.http.client.HttpClient;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import org.junit.jupiter.api.Test;
import jakarta.inject.Inject;
import static org.junit.jupiter.api.Assertions.assertEquals;
@MicronautTest (1)
public class HealthTest {
@Inject
@Client("/") (2)
HttpClient client;
@Test
public void healthEndpointExposed() {
HttpStatus status = client.toBlocking().retrieve(HttpRequest.GET("/health"), HttpStatus.class); (3)
assertEquals(HttpStatus.OK, status);
}
}
1 | Annotate the class with @MicronautTest so the Micronaut framework will initialize the application context and the embedded server. More info. |
2 | Inject the HttpClient bean and point it to the embedded server. |
3 | The health endpoint returns information about the "health" of the application, which is determined by any number of "health indicators". |
4.1.1. Base Path
The base path for all endpoints is /
by default. If you prefer the management endpoints to be available under a different base path, configure endpoints.all.path
as in the following test.
The leading and trailing / are required for endpoints.all.path , unless micronaut.server.context-path is set, in which case just the leading / isn’t necessary.
|
package example.micronaut;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpStatus;
import io.micronaut.http.client.HttpClient;
import io.micronaut.http.client.exceptions.HttpClientResponseException;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import io.micronaut.context.annotation.Property;
import jakarta.inject.Inject;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
@Property(name = "endpoints.all.path", value = "/endpoints/") (1)
@MicronautTest
public class HealthPathTest {
@Inject
@Client("/")
HttpClient client;
@Test
public void healthEndpointExposedAtNonDefaultEndpointsPath() {
HttpStatus status = client.toBlocking().retrieve(HttpRequest.GET("/endpoints/health"), HttpStatus.class); (2)
assertEquals(HttpStatus.OK, status);
Executable e = () -> client.toBlocking().retrieve(HttpRequest.GET("/health"), HttpStatus.class);
HttpClientResponseException thrown = assertThrows(HttpClientResponseException.class, e);
assertEquals(HttpStatus.NOT_FOUND, thrown.getStatus()); (3)
}
}
1 | Sets the base path for all management endpoints to /endpoints/ . This is normally specified in the application configuration (e.g. application.yml ). |
2 | The "health" endpoint is now rooted at /endpoints/health |
3 | The "health" endpoint is no longer reachable at the default path and results in a "Not Found" status (HTTP 404). |
4.1.2. Failed Health Status
Disk-space threshold is one of the build-in indicators. This test demonstrates a failed health status when free disk space drops below the specified threshold. The endpoints.health.disk-space.threshold
configuration property can be provided as a string, like "10MB" or "200KB", or the number of bytes.
package example.micronaut;
import io.micronaut.context.annotation.Property;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpStatus;
import io.micronaut.http.client.HttpClient;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.http.client.exceptions.HttpClientResponseException;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@Property(name = "endpoints.health.disk-space.threshold", value = "999999999999999999") (1)
@MicronautTest
public class PoorHealthTest {
@Inject
@Client("/")
HttpClient client;
@Test
public void healthEndpointExposesOutOfDiscSpace() {
Executable e = () -> client.toBlocking().retrieve(HttpRequest.GET("/health"));
HttpClientResponseException thrown = assertThrows(HttpClientResponseException.class, e);
assertEquals(HttpStatus.SERVICE_UNAVAILABLE, thrown.getStatus()); (2)
assertTrue(thrown.getResponse().getBody(String.class).orElse("").contains("DOWN")); (3)
}
}
1 | Sets the endpoints.health.disk-space.threshold property to an impossibly high value to force a service down status. This is normally specified in the application configuration (e.g. application.yml ). |
2 | A failed heath check results in a Service Unavailable status (HTTP 503) |
3 | The response body of the error contains the json string {"status":"DOWN"} |
5. Testing the Application
To run the tests:
./gradlew test
Then open build/reports/tests/test/index.html
in a browser to see the results.
6. Running the Application
To run the application, use the ./gradlew run
command, which starts the application on port 8080.
You can execute the health endpoint exposed by the application:
curl localhost:8080/health
{"status":"UP"}
7. Generate a Micronaut Application Native Executable with GraalVM
We will use GraalVM, the polyglot embeddable virtual machine, to generate a native executable of our Micronaut application.
Compiling native executables ahead of time with GraalVM improves startup time and reduces the memory footprint of JVM-based applications.
Only Java and Kotlin projects support using GraalVM’s native-image tool. Groovy relies heavily on reflection, which is only partially supported by GraalVM.
|
7.1. GraalVM installation
sdk install java 21.0.5-graal
sdk use java 21.0.5-graal
For installation on Windows, or for manual installation on Linux or Mac, see the GraalVM Getting Started documentation.
The previous command installs Oracle GraalVM, which is free to use in production and free to redistribute, at no cost, under the GraalVM Free Terms and Conditions.
Alternatively, you can use the GraalVM Community Edition:
sdk install java 21.0.2-graalce
sdk use java 21.0.2-graalce
7.2. Native executable generation
To generate a native executable using Gradle, run:
./gradlew nativeCompile
The native executable is created in build/native/nativeCompile
directory and can be run with build/native/nativeCompile/micronautguide
.
It is possible to customize the name of the native executable or pass additional parameters to GraalVM:
graalvmNative {
binaries {
main {
imageName.set('mn-graalvm-application') (1)
buildArgs.add('--verbose') (2)
}
}
}
1 | The native executable name will now be mn-graalvm-application |
2 | It is possible to pass extra arguments to build the native executable |
You can execute the health endpoint exposed by the native image:
curl localhost:8080/health
{"status":"UP"}
8. Next steps
Visit Micronaut Management & Monitoring to learn more.
9. Help with the Micronaut Framework
The Micronaut Foundation sponsored the creation of this Guide. A variety of consulting and support services are available.
10. License
All guides are released with an Apache license 2.0 license for the code and a Creative Commons Attribution 4.0 license for the writing and media (images…). |