Johny19
Johny19

Reputation: 5582

Springboot with Spring-cloud-aws and cloudwatch metrics

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?

Upvotes: 13

Views: 14899

Answers (6)

Vlad Saveluc
Vlad Saveluc

Reputation: 126

I struggled with this for a few hours.

The only dependency you need besides actuator is io.awspring.cloud:spring-cloud-aws-starter-metrics

Then in application.properties you need to set your namespace with management.cloudwatch.metrics.export.namespace

You can see the other properties in the official documentation: https://docs.awspring.io/spring-cloud-aws/docs/3.2.0/reference/html/index.html#configuring-credentials

Upvotes: 1

DiNagpal
DiNagpal

Reputation: 51

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();

Upvotes: 5

dawid
dawid

Reputation: 207

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

Upvotes: 5

pbthorste
pbthorste

Reputation: 488

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

Upvotes: -1

Dipayan
Dipayan

Reputation: 213

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

Upvotes: 1

Hendy Irawan
Hendy Irawan

Reputation: 21434

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.

Upvotes: 3

Related Questions