Spring Actuator Cloudwatch

High availability, scalability, fault tolerance , robustness are no longer nice things to have but have become fundamental core principles of any reliable web application. Telemetry ( logs and metrics ) plays a very important role not only to help us react faster to any issues brought up , but also to help setup proactive counter measures to foreseeable future problems.

This post is relevant to web applications that make use of the popular Spring-boot  framework for development and run the application on AWS. Spring boot provides a very niche solution to host and expose several key metrics regarding your application at run time using spring-actuator project. You can take a look at all the data points that this library exposes here.  You can access all of this information by hitting the /metrics endpoint. However if you want to publish these metrics to some centralized place where you can visualize them, set up alerting and trigger automated actions based off certain threshold violations, then AWS cloudwatch is the monitoring solution to have.

Now normally integrating your application to AWS cloudwatch is very easy, however it needs some level of boiler plate code and repetitive tasks. Publishing data points from spring metrics to cloudwatch on regular intervals also requires some setup work.

Spring Actuator Cloudwatch is a very light weight library that tries to solve these problems and provides a very seamless easy integration of spring boot metrics with AWS cloudwatch.

Here is what you would have to do:

  1. Add the library:
<dependency> 
    <groupId>com.kajjoy.spring.devops</groupId> 
    <artifactId>cloudwatch-metrics</artifactId>
    <version>1.1</version> 
</dependency>

2. Have your spring applications scan for the 2 beans:

com.kajjoy.spring.devops.cloudwatchmetrics.publisher.CloudWatchPublisher
com.kajjoy.spring.devops.cloudwatchmetrics.service.CloudWatchService

3. Register two necessary beans to provide permissions to post to your AWS accounts:

@Bean
    public ClientConfiguration clientConfiguration(){
        ClientConfiguration config = new ClientConfiguration();
        config.setProxyHost(proxyHost);
        config.setProxyPort(proxyPort);
        return config;
    }

   @Bean
    public AWSCredentialsProvider credentialsProvider(){
        return new CredentialProviderChain();
    }

4. Now add configuration properties on your application.properties file:

spring.operational.metrics.cloudwatch.publish.cron=*/10 * * * * *
aws.cloudwatch.region=us-west-2
actuator.metrics.to.publish=mem.free,heap.used,threads.totalStarted
actuator.metrics.units=Kilobytes,Kilobytes,Count
cloudwatch.namespace=spring-actuator-cloudwatch

You can configure the following using these properties:

  • How often do you want to post these metrics, defaults to every 10 seconds.
  • Whats your AWS Cloudwatch region, defaults to us-west-2
  • Which all metrics you want to publish, defaults to memory and heap space utilized and active thread count.
  • What are the units for the metrics you want to publish. Acceptable values are:
Valid Values: Seconds | Microseconds | Milliseconds | Bytes | Kilobytes | Megabytes | Gigabytes | Terabytes | Bits | Kilobits | Megabits | Gigabits | Terabits | Percent | Count | Bytes/Second | Kilobytes/Second | Megabytes/Second | Gigabytes/Second | Terabytes/Second | Bits/Second | Kilobits/Second | Megabits/Second | Gigabits/Second | Terabits/Second | Count/Second | None
  • Custom name space where you would want to publish these metrics to.

 

And that’s it. All the metrics that you choose to publish will be published to cloud watch where you can seamlessly create dashboards, setup cloudwatch alarms and trigger actions using AWS lambdas to correct the problems automatically.

Let me know your comments and suggestions and feel free to make contributions to extend this library or make it better 🙂