Using AWS CloudWatch for spring boot project metrics

Published On: 19 May 2023.By .

In this blog, we will discuss how to use AWS CloudWatch to monitor a Spring Boot application running on AWS. Specifically, we’ll cover the following topics:

  1. Setting up a Spring Boot application
  2. Creating custom metrics with Micrometer and AWS
  3. Checking metrics on CloudWatch
  4. Setting up alarms and notifications

Setting up a Spring Boot application and AWS

Create a Spring Project from with the spring-boot-starter-web dependency and download the zip file. Now we will create an IAM user at AWS to access Cloudwatch from our spring application.

  1. Log in to your AWS Management Console and navigate to the IAM service.
  2. Click on the “Users” link in the left-hand navigation menu and then click the “Add user” button.
  3. Enter a name for the new user and select the “Programmatic access” checkbox. This will generate an access key and secret key that the user can use to access AWS services programmatically.
  4. Click the “Next: Permissions” button.
  5. On the permissions screen, click the “Attach existing policies directly” button.
  6. Search for the policy named “CloudWatchFullAccess” and select it by clicking the checkbox next to it.
  7. Click the “Next: Tags” button and add any relevant tags to the user if desired.
  8. Click the “Next: Review” button to review the details of the new user.
  9. Review the information and click the “Create user” button to create the user.

After creating the user, the final confirmation screen will display their access key and secret key. Make sure to securely save these keys as they will not be displayed again. You can use these keys to access CloudWatch from our spring boot application.

Creating custom metrics with Micrometer

Now that our application is running on AWS, we need to start monitoring it. One way to do this is to use Micrometer.

Micrometer is a metrics collection library for Java-based applications. It provides a simple and flexible way to instrument code and gather metrics on a variety of systems and applications that integrates with popular monitoring systems, including CloudWatch.

Micrometer offers different meters to measure various aspects of an application’s performance, including:

  • Counter: It increments only and can count events or keep track of the number of times something has happened.
  • Gauge: It measures the current value of a particular variable, such as the number of active threads in a thread pool.
  • Timer: It measures the duration of an operation, such as the time it takes to execute a particular method.
  • Long Task Timer: It measures the duration of long-running tasks, such as database queries or network calls.
  • Histogram: It measures the distribution of values for a particular metric over time.
  • Distribution Summary: It tracks the distribution of values for a particular metric, similar to a histogram.

To use Micrometer in our Spring Boot application, we first need to add the following dependency to our pom.xml file:

In this blog, we will use the Counter, Gauge, and Timer meter. We will create an application where we will track the number of requests received on a particular endpoint using a Counter Meter, the time taken to complete a particular request using a Timer Meter, and the price change of a product using a Guage Meter.

1. App Config

The cloudWatchAsyncClient() method returns an instance of CloudWatchAsyncClient which is a client that can asynchronously interact with the CloudWatch service in AWS. It is constructed using the builder() method, which sets some properties like the region where the client will be used and the credentials provider. The credentials provider is defined using the StaticCredentialsProvider.create() method and provides the AWS access key and secret key needed to authenticate with the CloudWatch service.

The getMeterRegistry() method returns an instance of MeterRegistry, which is an interface that provides a way to collect and record application metrics. The CloudWatchConfig object is set up using the setupCloudWatchConfig() method. The CloudWatchMeterRegistry is then created with the CloudWatchConfig object, a Clock instance, and the CloudWatchAsyncClient.

The setupCloudWatchConfig() method sets up the configuration for the CloudWatch client. It sets the namespace for the metrics to be “productsApp” and the interval at which metrics should be sent to CloudWatch to be 1 minute.

2. Price Engine

The computePrice() method is annotated with the @Scheduled annotation, which indicates that it should be executed every 60 seconds. This method generates a random price using the Random class and assigns it to the price variable.

3. Product Controller

The @GetMapping annotation annotates the fetchProducts() method to handle HTTP GET requests to the /products endpoint. It fetches a list of products from a database, increments the pageViewsCounter, and records the time taken to execute the method using the productTimer.

To set up the priceGauge, Gauge.builder() method is used to create a gauge metric that records the current price of the product. A lambda function is built to return the product price obtained from the PricingEngine. The metric’s description and baseUnit are provided using the description and baseUnit methods, respectively. The register method is used to register the gauge metric with the meterRegistry.

To set up the pageViewsCounter, the meterRegistry.counter() method creates a counter metric that records the number of page views for the /products endpoint.

To set up the productTimer, the meterRegistry.timer() method creates a timer metric that records the time taken to execute the fetchProducts() method.

3. Checking metrics on AWS CloudWatch

To check a metrics on AWS CloudWatch, you can follow these steps:

  1. Navigate to the AWS CloudWatch console and select “Metrics” from the left-hand menu.
  2. Click on “All Metrics” to view all available metrics for your AWS account.
  3. Use the search bar to find the metric you want to view by searching for its namespace, metric name, or dimension.
  4. Once you have found the metric, click on it to expand the metric details.
  5. You can view the metric data in different formats, such as a graph or a table. You can also customize the graph by adding or removing dimensions, changing the time range, or applying different statistical functions.

Checking matrix on AWS CloudWatch

AWS CloudWatch graph

4. Setting up alarms and notifications

To set up alarms and notifications on AWS CloudWatch, you can follow these steps:

  1. Navigate to the AWS CloudWatch console and select “Alarms” from the left-hand menu.
  2. Click on “Create Alarm” to start creating a new alarm.
  3. Select the metric you want to monitor from the list of available metrics.
  4. Configure the conditions for the alarm, such as the threshold values and the evaluation period.
  5. Specify the actions to take when the alarm is triggered, such as sending an email or a text message or invoking an AWS Lambda function.
  6. Review and confirm the alarm settings, and then create the alarm.

Here are some additional details on each step:

  1. To create a new alarm, you need to select the metric you want to monitor. You can either select a predefined metric provided by AWS, or create a custom metric based on your own application data.
  2. Once you have selected the metric, you can configure the conditions for the alarm. For example, you might want to create an alarm that triggers when the CPU utilization of an EC2 instance exceeds 80% for more than 5 minutes.
  3. You can specify the threshold values and the evaluation period for the alarm. The evaluation period is the time period over which CloudWatch evaluates the metric data to determine if the alarm condition is met.
  4. You can also specify the actions to take when the alarm is triggered. You can send an email or a text message to one or more recipients, or invoke an AWS Lambda function to perform custom actions.
  5. Once you have configured the alarm settings, you can review and confirm the settings before creating the alarm.

CloudWatch supports a wide range of notifications and actions, including sending notifications to Amazon SNS topics, invoking AWS Lambda functions, and auto-scaling EC2 instances. With alarms and notifications, you can monitor your AWS resources in real time and respond to any issues before they become critical.

AWS CloudWatch Matrix


In conclusion, monitoring and observability are critical components of any production system. AWS CloudWatch provides a powerful set of tools for monitoring and collecting metrics, logs, and traces from your AWS resources and applications. With CloudWatch, you can gain visibility into your system’s health and performance, detect issues early, and troubleshoot problems quickly.

In this blog, we have covered the basics of AWS CloudWatch, including metrics, logs, alarms, and notifications. We have explored how to use Cloud Watch to collect and visualize metrics, create custom dashboards, and set up alarms and notifications to alert you to any issues.

Related content

Got an Idea?

Let’s create a better tomorrow together