I am coming from Prometheus background to monitor my Java based apps. The Prometheus library to publish metrics is straighforward, we have Counters, Gauges etc If I want to count the number of times my Spring Controller was called I could just use counter.incr()
Now we are moving to AWS and use CloudWatch to monitor the fargate container where the java app is running. However I want to publish same metrics in CloudWatch as I did in Prometheus.
Reading CloudWatch docs on publishing metrics didn't make sense to me. Here is the snapshot from the java doc example
Dimension dimension = new Dimension()
.withName("UNIQUE_PAGES")
.withValue("URLS");
MetricDatum datum = new MetricDatum()
.withMetricName("PAGES_VISITED")
.withUnit(StandardUnit.None)
.withValue(data_point)
.withDimensions(dimension);
PutMetricDataRequest request = new PutMetricDataRequest()
.withNamespace("SITE/TRAFFIC")
.withMetricData(datum);
I don't understand how to use it to count my Controller calls. Should it be something like this ?
@GetMapping()
String method(){
Dimension dimension = new Dimension()
.withName("controller_cnt")
.withValue("MyClass");
MetricDatum datum = new MetricDatum()
.withMetricName("controller_cnt")
.withUnit(StandardUnit.None)
.withValue(1/*was called once*/)
.withDimensions(dimension);
PutMetricDataRequest request = new PutMetricDataRequest()
.withNamespace("SITE/TRAFFIC")
.withMetricData(datum);
...call put metrics request
}