mn create-app example.micronaut.micronautguide \
--features=management \
--build=maven \
--lang=groovy \
--test=spock
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 Groovy.
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
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.
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-management</artifactId>
<scope>compile</scope>
</dependency>
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.spock.annotation.MicronautTest
import jakarta.inject.Inject
import spock.lang.Specification
@MicronautTest (1)
class HealthSpec extends Specification {
@Inject
@Client("/") (2)
HttpClient client
void "health endpoint exposed" () {
when:
HttpStatus status = client.toBlocking().retrieve(HttpRequest.GET("/health"), HttpStatus) (3)
then:
status == HttpStatus.OK
}
}
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.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.spock.annotation.MicronautTest
import jakarta.inject.Inject
import spock.lang.Specification
@Property(name = "endpoints.all.path", value = "/endpoints/") (1)
@MicronautTest
class HealthPathSpec extends Specification {
@Inject
@Client("/")
HttpClient client
void "health endpoint exposed at non-default endpoints path"() {
when:
HttpStatus status = client.toBlocking().retrieve(HttpRequest.GET("/endpoints/health"), HttpStatus) (2)
then:
status == HttpStatus.OK
when:
client.toBlocking().retrieve(HttpRequest.GET("/health"), HttpStatus) (3)
then:
HttpClientResponseException thrown = thrown()
thrown.status == HttpStatus.NOT_FOUND
}
}
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.spock.annotation.MicronautTest
import jakarta.inject.Inject
import spock.lang.Specification
@Property(name = "endpoints.health.disk-space.threshold", value = "999999999999999999") (1)
@MicronautTest
class PoorHealthSpec extends Specification {
@Inject
@Client("/")
HttpClient client
void "health endpoint exposes out of disc space"() {
when:
client.toBlocking().retrieve(HttpRequest.GET("/health"))
then:
HttpClientResponseException thrown = thrown()
thrown.status == HttpStatus.SERVICE_UNAVAILABLE (2)
thrown.response.getBody(String).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. Next steps
Visit Micronaut Management & Monitoring to learn more.
8. Help with the Micronaut Framework
The Micronaut Foundation sponsored the creation of this Guide. A variety of consulting and support services are available.
9. 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…). |