Springboot with Spring-cloud-aws and cloudwatch metrics

Viewed 10151

I would like to start using metrics in my Springboot app and I would also like to publish them my amazon cloudwatch

I know that with Springboot we can activate spring-actuator that provides in memory metrics and published them to the /metrics endpoint.

I stumbled across Spring-cloud that seems to have some lib to periodically publish these metrics to Cloudwatch, however I have no clue how to set them up? There is absolutely 0 examples of how to use it.

Anyone could explain what are the step to enable the metric to be sent to cloudwatch?

5 Answers

You can check my article here:

https://dkublik.github.io/2017/10/28/springboot-metrics-with-servo-and-aws-cloudwatch.html

I wrote it after setting this up in my project.

From header:

"Article explains how to send Spring Boot and Netflix Servo metrics to AWS CloudWatch. Morover it describes mechanisms making it happen. It also mentions problems I run into trying to do the same with Spring Boot and Spectator."

EDIT: new version: https://dkublik.github.io/2018/08/26/springboot-metrics-with-micrometer-and-aws-cloudwatch.html

I went through multiple documents for this. Something or the other was missing in them. So, I am writing everything required to set up custom metrics on Cloudwatch from your spring boot application.

Set these properties:

management.metrics.export.cloudwatch.namespace=my-application
management.metrics.export.cloudwatch.batchSize=20
management.metrics.export.cloudwatch.step=5s

Mention namespace carefully. This name will be reflected in your cloudwatch metrics. Default "step" for cloudwatch registry is 1 minute. So you can change it here.

Add these dependency in your pom if not already present:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-aws-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-aws-messaging</artifactId>
</dependency>

It's done. Now you'll be able to see metrics on cloudwatch. Now if you want to push custom metrics somewhere then Autowire instance of MetricsRegistry and simply create whichever metrics you want.

Let's create a counter for sending sms eg:

Counter smsCounter = Counter.builder(COUNT_METRICS)
            .tag("type", "sms")
            .description("The number of sms sent")
            .register(meterRegistry);

Now update the counter where the action is performed as follows:

smsCounter.increment();

Here is a setup for spring boot 2.

Using spring boot 2.0.3.

Add these dependencies:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-aws-actuator</artifactId>
    <version>2.0.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-aws</artifactId>
    <version>2.0.0.RELEASE</version>
</dependency>

application.yml:

# you might want to set this to true depending on your setup
cloud.aws.stack.auto: false
# set static region to avoid s3 error - adjust region accordingly
cloud.aws.region.static: eu-west-1

management:
  metrics.export.cloudwatch.namespace: my-app
  metrics.export.cloudwatch.batch-size: 20
Related