Create a Micronaut Application to Collect Metrics and Monitor Them on Amazon CloudWatch

This guide describes how to create a Micronaut application that collects standard and custom metrics, then publishes and monitors them on Amazon CloudWatch Metrics, using the Micronaut AWS integration.

The application stores book information in a database and provides endpoints to query books. The application collects metrics for the whole application and measures total computation time for a particular endpoint.

Amazon CloudWatch Metrics is an observability and monitoring service that gives you a unified view of operational health and complete visibility of your AWS resources. This enables you to monitor your applications, optimize resource utilization, and respond to system-wide performance changes.

Micronaut AWS provides integration between Micronaut and Amazon Web Services (AWS), including CloudWatch.

The application uses Micronaut Micrometer to expose application metric data with Micrometer.

Prerequisites #

Follow the steps below to create the application from scratch. However, you can also download the completed example in Java:

A note regarding your development environment

Consider using Visual Studio Code that provides native support for developing applications with the Graal Cloud Native Tools extension.

Note: If you use IntelliJ IDEA, enable annotation processing.

1. Create the Application #

Create an application using the GCN Launcher.

  1. Open the GCN Launcher in advanced mode.

  2. Create a new project using the following selections.
    • Project Type: Application (Default)
    • Project name: aws-metrics-demo
    • Base Package: com.example (Default)
    • Clouds: AWS
    • Build Tool: Gradle (Groovy) or Maven
    • Test Framework: JUnit (Default)
    • Java Version: 17 (Default)
    • Micronaut Version: (Default)
    • Cloud Services: Database and Metrics
    • Features: Flyway Database Migration, GraalVM Native Image, and Micrometer Annotation
    • Sample Code: No
  3. Click Generate Project. The GCN Launcher creates a Java project with the default package com.example in a directory named aws-metrics-demo. The application ZIP file will be downloaded in your default downloads directory. Unzip it, open in your code editor, and proceed to the next steps.

Alternatively, use the GCN CLI as follows:

gcn create-app \
    --clouds=aws \
    --services=database,metrics \
    --features=graalvm,flyway,micrometer-annotation \
    --example-code=false \
    --build=gradle \
gcn create-app \
    --clouds=aws \
    --services=database,metrics \
    --features=graalvm,flyway,micrometer-annotation \
    --example-code=false \
    --build=maven \

For more information, see Using the GCN CLI.

GCN Launcher creates a multi-module project with two subprojects: aws, and lib. The common application logic is in the lib subproject, and the aws subproject contain logic specific to the AWS cloud platform. When creating the application with GCN, the necessary features such as the Logback framework, Micronaut Micrometer, and others were added for you. These features will be used in the guide.

1.1. Configure Metrics Collection #

The project generated by the GCN Launcher has Micrometer as a dependency.

Micrometer provides a simple facade over the instrumentation clients for a number of popular monitoring systems.

To configure Micrometer, the following properties were added to the configuration file, aws/src/main/resources/, as follows:


Several groups of metrics are enabled by default: these include system metrics (such as JVM information and uptime), as well as metrics tracking web requests, data sources activity, and others. Overall metrics can be enabled or disabled, and groups can be individually enabled or disabled in configuration. In this case all metrics are enabled. To disable, change to false, for example, per-environment.

To export metrics to Amazon CloudWatch, enable CloudWatch as an export location for Micrometer. Then, create queries and monitor your metrics on CloudWatch.

Set the namespace property for your metrics to something meaningful, for example, an application name such as aws-metrics-demo. The namespace will be used to group the metrics in CloudWatch.

1.2. Database Migration with Flyway #

To create the database schema, the application uses the Micronaut integration with Flyway. Flyway automates schema changes, significantly simplifying schema management tasks, such as migrating, rolling back, and reproducing in multiple environments.

  1. You specified Flyway as a project feature in the GCN Launcher, so the build file includes it as a dependency:




  2. The file aws/src/main/resources/ was modified to enable Flyway to perform migrations on the default datasources, by adding the following:


    Configuring multiple datasources is as simple. You can also specify directories that will be used for migrating each datasource. For more information, see Micronaut integration with Flyway.

  3. The launcher created a migration file in the lib/src/main/resources/db/migration/ directory named V1__schema.sql to contain the database schema, as follows:

    CREATE TABLE book (
       name  VARCHAR(255) NOT NULL UNIQUE,
       isbn  CHAR(13) NOT NULL UNIQUE
    INSERT INTO book (isbn, name)
    VALUES ("9781491950357", "Building Microservices"),
           ("9781680502398", "Release It!"),
           ("9780321601919", "Continuous Delivery"),
           ("9781617294549", "Microservices Patterns");

Flyway migration is automatically triggered before your application starts and Flyway reads migration files from the lib/src/main/resources/db/migration/ directory. During application startup, Flyway runs the commands in the SQL file and creates the schema needed for the application.

Using the SQL statements in V1__schema.sql, Flyway creates a table with the name book and populates it with four records.

1.3. Domain Entity #

The launcher created a Book domain class that uses Micronaut Data JDBC in a file named lib/src/main/java/com/example/, as follows:

package com.example;


import static;

import io.micronaut.serde.annotation.Serdeable;
import jakarta.validation.constraints.Size;

@MappedEntity // <1>
public class Book {

    private Long id;

    private String name;

    @Size(min=13, max=13)
    private String isbn;

    public Book(String isbn, String name) {
        this.isbn = isbn; = name;

    public Long getId() {
        return id;

    public void setId(Long id) { = id;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public String getIsbn() {
        return isbn;

    public void setIsbn(String isbn) {
        this.isbn = isbn;

1 The annotation @MappedEntity maps the class to the table defined in the schema.

1.4. Repository Interface #

A repository interface defines the operations to access the database. Micronaut Data implements the interface at compilation time. The launcher created a BookRepository interface in a file named lib/src/main/java/com/example/

package com.example;

import io.micronaut.core.annotation.NonNull;
import jakarta.validation.constraints.NotBlank;

import java.util.Optional;

import static;

@JdbcRepository(dialect = MYSQL) // <1>
public interface BookRepository extends CrudRepository<Book, Long> { // <2>

    Optional<Book> findByIsbn(@NotBlank String isbn);

1 A database dialect is specified with the @JdbcRepository annotation.

2 The Book is the root entity, and the primary key type is Long.

1.5. Controller Class #

The launcher created a controller to access Book instances (and to trigger the JDBC metric data) in a file named lib/src/main/java/com/example/ with the following contents:

package com.example;

import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.scheduling.TaskExecutors;
import io.micronaut.scheduling.annotation.ExecuteOn;
import io.micrometer.core.annotation.Timed;
import io.micrometer.core.annotation.Counted;

import java.util.Optional;

@Controller("/books") // <1>
class BookController {

    private final BookRepository bookRepository;

    BookController(BookRepository bookRepository) {
        this.bookRepository = bookRepository;

    @Get // <2>
    @Timed("books.index") // <3>
    Iterable<Book> index() {
        return bookRepository.findAll();

    @Get("/{isbn}") // <3>
    @Counted("books.find") // <4>
    Optional<Book> findBook(String isbn) {
        return bookRepository.findByIsbn(isbn);

1 The class is defined as a controller with the @Controller annotation mapped to the path /books.

2 Use a Micrometer @Timed annotation, with the value “books.index”, to create a timer metric.

3 The controller maps a GET request to /books, which returns a list of Book.

4 Use a Micrometer @Counted annotation, with the value “books.find”, to create a counter metric.

2. Create Tests #

Note: You require a running Docker container to run the tests.

  1. For tests to run correctly, the launcher created a test configuration file named aws/src/test/resources/ with the following contents:


    The configuration enables the metrics and specifies a MySQL datasource. Since it does not specify a URL for the source, Micronaut Test Resources automatically creates a test container for the database. The configuration also enables Flyway to create the books tables the same way as in a production environment.

  2. The launcher created a test class, named BookControllerMetricsTest, to verify metrics functionality in a file named aws/src/test/java/com/example/ with the following contents:

     package com.example;
     import io.micrometer.core.instrument.Counter;
     import io.micrometer.core.instrument.MeterRegistry;
     import io.micrometer.core.instrument.Tags;
     import io.micrometer.core.instrument.Timer;
     import io.micronaut.core.type.Argument;
     import io.micronaut.http.HttpRequest;
     import io.micronaut.http.client.HttpClient;
     import io.micronaut.http.client.annotation.Client;
     import io.micronaut.logging.LoggingSystem;
     import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
     import jakarta.inject.Inject;
     import org.junit.jupiter.api.Test;
     import org.slf4j.Logger;
     import org.slf4j.LoggerFactory;
     import java.util.List;
     import java.util.Map;
     import java.util.Set;
     import java.util.concurrent.TimeUnit;
     import static io.micronaut.logging.LogLevel.ALL;
     import static org.junit.jupiter.api.Assertions.assertEquals;
     import static org.junit.jupiter.api.Assertions.assertFalse;
     import static org.junit.jupiter.api.Assertions.assertTrue;
     class BookControllerMetricsTest {
         MeterRegistry meterRegistry;
         LoggingSystem loggingSystem;
         HttpClient httpClient;
         void testExpectedMeters() {
             Set<String> names = meterRegistry.getMeters().stream()
                     .map(meter -> meter.getId().getName())
             // check that a subset of expected meters exist
             // these will be lazily created
         void testHttp() {
             Timer timer = meterRegistry.timer("http.server.requests", Tags.of(
                     "exception", "none",
                     "method", "GET",
                     "status", "200",
                     "uri", "/books"));
             assertEquals(0, timer.count());
             Timer bookIndexTimer = meterRegistry.timer("books.index",
                     Tags.of("exception", "none"));
             assertEquals(0, bookIndexTimer.count());
             assertEquals(1, timer.count());
             assertEquals(1, bookIndexTimer.count());
             assertTrue(0.0 < bookIndexTimer.totalTime(TimeUnit.MILLISECONDS));
             assertTrue(0.0 < bookIndexTimer.max(TimeUnit.MILLISECONDS));
             Counter bookFindCounter = meterRegistry.counter("books.find",
                     Tags.of("result", "success",
                             "exception", "none"));
             assertEquals(0, bookFindCounter.count());
             assertEquals(1, bookFindCounter.count());
         void testLogback() {
             Counter counter = meterRegistry.counter("", Tags.of("level", "info"));
             double initial = counter.count();
             Logger logger = LoggerFactory.getLogger("testing.testing");
             loggingSystem.setLogLevel("testing.testing", ALL);
             assertEquals(initial + 1, counter.count(), 0.000001);
         void testMetricsEndpoint() {
             Map<String, Object> response = httpClient.toBlocking().retrieve(
                     Argument.mapOf(String.class, Object.class));
             assertTrue(response.get("names") instanceof List);
             List<String> names = (List<String>) response.get("names");
             // check that a subset of expected meters exist
         void testOneMetricEndpoint() {
             Map<String, Object> response = httpClient.toBlocking().retrieve(
                     Argument.mapOf(String.class, Object.class));
             String name = (String) response.get("name");
             assertEquals("jvm.memory.used", name);
             List<Map<String, Object>> measurements = (List<Map<String, Object>>) response.get("measurements");
             assertEquals(1, measurements.size());
             double value = (double) measurements.get(0).get("value");
             assertTrue(value > 0);

    The tests verify that certain metrics are present, including the ones that you enabled in the application configuration file, and the ones that you collected thanks to the @Counter and @Timer annotations.

    Note that, since the @MicronautTest annotation is used, Micronaut initializes the application context and the embedded server with the endpoints you created earlier. (For more information, see the Micronaut Test guide.)

3. Create Custom Metrics #

  1. The launcher created a service that retrieves information from the database and publishes custom metrics based on it. The custom metrics provide the number of books about microservices. The service is in a file named lib/src/main/java/com/example/ with the following contents:

     package com.example;
     import io.micrometer.core.instrument.Counter;
     import io.micrometer.core.instrument.MeterRegistry;
     import io.micrometer.core.instrument.Timer;
     import io.micronaut.scheduling.annotation.Scheduled;
     import jakarta.inject.Singleton;
     import org.slf4j.Logger;
     import org.slf4j.LoggerFactory;
     import java.util.concurrent.atomic.AtomicInteger;
     public class MicroserviceBooksNumberService {
         private final Logger log = LoggerFactory.getLogger(getClass().getName());
         private final BookRepository bookRepository;
         private final Counter checks;
         private final Timer time;
         private final AtomicInteger microserviceBooksNumber = new AtomicInteger(0);
         private static final String SEARCH_KEY = "microservice";
         MicroserviceBooksNumberService(BookRepository bookRepository,
                                        MeterRegistry meterRegistry) { // <1>
             this.bookRepository = bookRepository;
             checks = meterRegistry.counter("microserviceBooksNumber.checks");
             time = meterRegistry.timer("microserviceBooksNumber.time");
             meterRegistry.gauge("microserviceBooksNumber.latest", microserviceBooksNumber);
         @Scheduled(fixedRate = "${customMetrics.updateFrequency:1h}",
                    initialDelay = "${customMetrics.initialDelay:0s}") // <2>
         public void updateNumber() {
             time.record(() -> {
                 try {
                     Iterable<Book> allBooks = bookRepository.findAll();
                     long booksNumber =, false)
                             .filter(b -> b.getName().toLowerCase().contains(SEARCH_KEY))
                     microserviceBooksNumber.set((int) booksNumber);
                 } catch (Exception e) {
                     log.error("Problem setting the number of microservice books", e);

    1 The code registers custom meters, queries the database, counts books containing microservice in the name and updates the microserviceBooksNumber.latest meter with the value. microserviceBooksNumber.checks stores the number of updates performed, and microserviceBooksNumber.time stores the total time spent on the updates for the metric.

    2 The metric update is scheduled. The customMetrics.updateFrequency parameter corresponds to the update rate and has the default value of one hour. The customMetrics.initialDelay parameter corresponds to a delay after application startup before metrics calculation and has a default value of zero seconds.

  2. Create a test for the custom metrics in a class named MicroserviceBooksNumberTest. Create a new file named aws/src/test/java/com/example/ with the following contents:

     package com.example;
     import io.micrometer.core.instrument.Counter;
     import io.micrometer.core.instrument.Gauge;
     import io.micrometer.core.instrument.MeterRegistry;
     import io.micrometer.core.instrument.Timer;
     import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
     import jakarta.inject.Inject;
     import org.junit.jupiter.api.Test;
     import static java.util.concurrent.TimeUnit.MILLISECONDS;
     import static org.junit.jupiter.api.Assertions.assertEquals;
     import static org.junit.jupiter.api.Assertions.assertTrue;
     class MicroserviceBooksNumberTest {
         MeterRegistry meterRegistry;
         MicroserviceBooksNumberService service;
         void testMicroserviceBooksNumberUpdates() {
             Counter counter = meterRegistry.counter("microserviceBooksNumber.checks");
             Timer timer = meterRegistry.timer("microserviceBooksNumber.time");
             Gauge gauge = meterRegistry.get("microserviceBooksNumber.latest").gauge();
             assertEquals(0.0, counter.count());
             assertEquals(0.0, timer.totalTime(MILLISECONDS));
             assertEquals(0.0, gauge.value());
             int checks = 3;
             for (int i = 0; i < checks; i++) { // <1>
             assertEquals((double) checks, counter.count());
             assertTrue(timer.totalTime(MILLISECONDS) > 0);
             assertEquals(2.0, gauge.value());

    1 The test calls the service three times and verifies that the metrics are collected correctly. Because the Flyway schema added two books with titles containing the word “microservices”, the value of microserviceBooksNumber.latest is 2.0.

    To make sure that the test works as expected and only three updates of the metric are performed, change the test configuration (in the file aws/src/test/resources/ so that it does not perform scheduled updates for the custom metric. To achieve this, change the customMetrics.initialDelay parameter to a large value, such as 10 hours. For example:


4. Run the Tests #

To run the tests, use the following command:

./gradlew test

Then open the file aws/build/reports/tests/test/index.html in a browser to view the results.

./mvnw test

The tests could be run locally without any cloud credentials, as metrics are not exported to the cloud, and a database is automatically provided by Micronaut test containers.

5. Configure the Application #

  1. The launcher configured an initial datasource in aws/src/main/resources/ with the following properties:


    You can set values for the missing datasources.default.url, datasources.default.username, and datasources.default.password properties with these environment variables:

     export DATASOURCES_DEFAULT_URL=jdbc:mysql://<Datasource URL, IP or localhost>:3306/gcn

    This approach requires an existing database, or requires you to manually start a server in a Docker container. Instead, to simplify things, do not set the environment variables: if you do not specify a datasource URL, Micronaut Test Resources automatically starts a MySQL server in a Docker container when running the application locally or running tests.

  2. To test publishing metrics to AWS Cloudwatch from a local machine, you may use an AWS Access key configured in the AWS CLI. If you do not have AWS CLI, follow the AWS documentation to install or update to the latest version of the AWS CLI.

    If you do not have an access key, complete the steps in the Programmatic Acess section of the AWS Secure Credential Types guide to create a long-term access key for your account. Run aws configure and provide access key ID and secret access key to authenticate as your user.

    Instead of using your AWS root account, it is highly recommended to use the administrator account. If you do not have one already, run the commands below one by one to create the administrator account:

     aws iam create-group --group-name Administrators
     aws iam create-user --user-name Administrator
     aws iam add-user-to-group --user-name Administrator --group-name Administrators
     aws iam attach-group-policy --group-name Administrators --policy-arn $(aws iam list-policies --query 'Policies[?PolicyName==`AdministratorAccess`].{ARN:Arn}' --output text)
     aws iam create-access-key --user-name Administrator

    Then run aws configure to configure your AWS CLI to use the administrator account just created.

    After completing these steps, you can start the application and it will have access to AWS Services, including CloudWatch.

6. Run the Application #

To run the application, use the following command, which starts the application on port 8080.

./gradlew :aws:run

Alternatively, to cause the custom metric update to occur more frequently (to see the effects on metrics), start the application with a configuration override to update every five seconds, as follows:

./gradlew :aws:run --args="-customMetrics.updateFrequency=5s"
./mvnw install -pl lib -am && ./mvnw mn:run -pl aws

Alternatively, to cause the custom metric update to occur more frequently (to see the effects on metrics), start the application with a configuration override to update every five seconds, as follows:

./mvnw install -pl lib -am && ./mvnw mn:run -pl aws -Dmn.appArgs="-customMetrics.updateFrequency=5s"

Send a few test requests with curl, as follows:

  • Get all the books:
    curl localhost:8080/books
    [{"id": 1, "name": "Building Microservices", "isbn": "9781491950357"},
    {"id": 2, "name": "Release It!", "isbn": "9781680502398"},
    {"id": 3, "name": "Continuous Delivery", "isbn": "9780321601919"}]
  • Get a book by its ISBN:
    curl localhost:8080/books/9781680502398
    {"id": 2, "name": "Release It!", "isbn": "9781680502398"}
  • Get a list of all the available metrics:
    curl localhost:8080/metrics
    {"names": [
  • Get the value of a particular metric:
    curl localhost:8080/metrics/http.server.requests
    {"name": "http.server.requests",
    "measurements": [
        {"statistic": "COUNT", "value": 3.0},
        {"statistic": "TOTAL_TIME", "value": 0.6045995000000001},
        {"statistic": "MAX", "value": 0.0343463}
    ], ...
  • Get the value of the metric that you created on the books endpoint:
    curl localhost:8080/metrics/books.index
    {"name": "books.index",
    "measurements": [
        {"statistic": "COUNT", "value": 1.0},
        {"statistic": "TOTAL_TIME", "value": 0.3218636},
    ], ...
  • Get the value of the custom metric calculating the number of books with microservice in their title:
    curl localhost:8080/metrics/microserviceBooksNumber.latest
    {"name": "microserviceBooksNumber.latest",
    "measurements": [{"statistic": "VALUE", "value" :2.0}]

7. Monitor Metrics on Amazon CloudWatch #

To use CloudWatch, first authenticate to AWS. Use any of these methods.

  1. Sign in to the AWS console and navigate to CloudWatch:

    AWS CloudWatch console

  2. Then go to Metrics > All metrics.

    AWS All Metrics

  3. In the Metrics Browse panel, select the search field, and enter the metrics you want to observe, for example, microserviceBooksNumber.latest. Click Enter to search:

    AWS Metrics Browse

  4. Select the namespace and dimensions: aws-metrics-demo > Metrics with no dimensions.

  5. Select the microserviceBooksNumber.latest.value metric with a check box. Observe the changes of the value over time on the graph:

    AWS Metrics Graph

8. Generate a Native Executable using GraalVM #

GCN supports compiling Java applications ahead-of-time into native executables using GraalVM Native Image and Native Build Tools. Packaged as a native executable, it significantly reduces the application startup time and memory footprint.

8.1. Build the Native Executable #

Add the aws/src/main/resources/META-INF/native-image/ file with this contents:

Args = --initialize-at-run-time=io.micrometer.core.instrument.push.PushMeterRegistry,io.micrometer.core.instrument.binder.jvm.JvmGcMetrics,io.micrometer.core.instrument.internal.DefaultGauge,io.micrometer.core.instrument.binder.jvm.ExecutorServiceMetrics

To generate a native executable, run the following command:

./gradlew :aws:nativeCompile
./mvnw install -pl lib -am && ./mvnw package -pl aws -Dpackaging=native-image

8.2. Run the Native Executable #

To start the native executable, run a Docker container with a MySQL database:

docker run -it --rm \
    --name "mysql.8" \
    -p 3306:3306 \
    -e MYSQL_DATABASE=gcn \
    -e MYSQL_USER=guide_user \
    -e MYSQL_PASSWORD=User123User! \

Set environment variables to provide values for the missing datasource url, username, and password properties:

export DATASOURCES_DEFAULT_URL=jdbc:mysql://localhost:3306/gcn

Note: On Windows use set instead of the export keyword in the commands above.

Start the generated native executable:


Note: With Gradle you can run ./gradlew :aws:nativeRun to start the native executable in development mode, which uses Micronaut Test Resources and therefore doesn’t require you to start a MySQL server.

Run the same curl commands from above to confirm that the application works the same way as before, but with faster startup and response times.

Then return to Amazon CloudWatch console to review the metrics.

To stop your container run the following command:

docker stop mysql.8

Summary #

This guide demonstrated how to create a Micronaut application that collects standard and custom metrics, then publishes and monitors them on Amazon CloudWatch Metrics. The application uses Micronaut Micrometer to expose application metric data with Micrometer.