These tutorials target Micronaut Framework 3. Read, Guides for Micronaut Framework 4.

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: 3.9.2

1. Getting Started

In this guide, we will create a Micronaut application written in Kotlin.

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

  • JDK 1.8 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.

4. Writing the Application

Create an application using the Micronaut Command Line Interface or with Micronaut Launch.

mn create-app example.micronaut.micronautguide \
    --features=management,graalvm \
    --build=maven
    --lang=kotlin
If you don’t specify the --build argument, Gradle is used as the build tool.
If you don’t specify the --lang argument, Java is used as the language.

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.

pom.xml
<dependency>
    <groupId>io.micronaut</groupId>
    <artifactId>micronaut-management</artifactId>
    <scope>compile</scope>
</dependency>
src/test/kotlin/example/micronaut/HealthTest.kt
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 jakarta.inject.Inject
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

@MicronautTest  (1)
class HealthTest(@Client("/") val client: HttpClient) { (2)

    @Test
    fun healthEndpointExposed() {
        val status = client.toBlocking().retrieve(HttpRequest.GET<Any>("/health"), HttpStatus::class.java) (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.
src/test/kotlin/example/micronaut/HealthPathTest.kt
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.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertThrows
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.function.Executable

@Property(name = "endpoints.all.path", value = "/endpoints/") (1)
@MicronautTest
class HealthPathTest(@Client("/") val client: HttpClient) { (2)

    @Test
    fun healthEndpointExposedAtNonDefaultEndpointsPath() {
        val status = client.toBlocking().retrieve(HttpRequest.GET<Any>("/endpoints/health"), HttpStatus::class.java) (2)
        assertEquals(HttpStatus.OK, status)

        val e = Executable { client.toBlocking().retrieve(HttpRequest.GET<Any>("/health"), HttpStatus::class.java) } (3)
        val thrown = assertThrows(HttpClientResponseException::class.java, e)
        assertEquals(HttpStatus.NOT_FOUND, thrown.status) (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.

src/test/kotlin/example/micronaut/PoorHealthTest.kt
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.Assertions.*
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.function.Executable

@Property(name = "endpoints.health.disk-space.threshold", value = "999999999999999999") (1)
@MicronautTest
class PoorHealthTest(@Client("/") val client: HttpClient) { (2)

    @Test
    fun healthEndpointExposed() {
        val e = Executable { client.toBlocking().retrieve(HttpRequest.GET<Any>("/health")) }
        val thrown = assertThrows(HttpClientResponseException::class.java, e)
        assertEquals(HttpStatus.SERVICE_UNAVAILABLE, thrown.status) (2)
        assertTrue(thrown.response.getBody(String::class.java).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:

./mvnw test

6. Running the Application

To run the application, use the ./mvnw mn: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. Native executable generation

The easiest way to install GraalVM on Linux or Mac is to use SDKMan.io.

Java 11
sdk install java 22.3.r11-grl
If you still use Java 8, use the JDK11 version of GraalVM.
Java 17
sdk install java 22.3.r17-grl

For installation on Windows, or for manual installation on Linux or Mac, see the GraalVM Getting Started documentation.

After installing GraalVM, install the native-image component, which is not installed by default:

gu install native-image

To generate a native executable using Maven, run:

./mvnw package -Dpackaging=native-image

The native executable is created in the target directory and can be run with target/micronautguide.

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.