11

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?

Johny19
  • 5,364
  • 14
  • 61
  • 99
  • It's worth noting that it is possible to do this without using Spring Cloud - if like me you are finding it too much of a pain to configure and looking for a more lightweight solution. See: https://stackoverflow.com/questions/56897336 – RCB Sep 15 '21 at 10:20

5 Answers5

5

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

dawid
  • 207
  • 2
  • 5
  • I had gave up since then even by reading @Hendy answer. your article 'seems' to have a complete example.I did not tested it – Johny19 Nov 01 '17 at 11:43
  • Looks like there is an updated version of the article which works with more recent library versions: http://dkublik.github.io/springboot-metrics-with-micrometer-and-aws-cloudwatch/ The original referenced article used different property names for configuration, which do not work for the library versions. Using the updated blog post should at least allow you to start seeing metrics in AWS. – jewelsea Oct 01 '18 at 19:32
  • that's exactly why we don't want link only answers on SO, link is dead:-( – KIC Jun 20 '19 at 06:56
5

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();
DiNagpal
  • 51
  • 1
  • 3
3

Check this conversation:

@sachinlad Indeed the documentation is unfortunately missing, we will create a updated version within the next releases. Do enable the metic export to Cloud Formation, you will need to configure the namespace with the property cloud.aws.cloudwatch.namespace

Have a look at the integration test https://github.com/spring-cloud/spring-cloud-aws/blob/master/spring-cloud-aws-integration-test/src/test/java/org/springframework/cloud/aws/metric/MetricExporterTest.java that is an integration test and export the metrics to cloud formation.

Hope that helps, feel free to come back in case of any problems.

Hendy Irawan
  • 20,498
  • 11
  • 103
  • 114
1

If you want a readymade solution that doesn't involve using the whole spring cloud library, you could use: https://github.com/dipayan90/spring-actuator-cloudwatch

Dipayan
  • 203
  • 4
  • 12
-1

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
pbthorste
  • 309
  • 2
  • 6