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