How to pass map to AWS cloudwatch

274 views Asked by At

I have a metrics map coming from different applications and I want to write them into AWSCloud watch.

How can I do that?

PS: I tried putting them using metrics name and metrics value after parsing whole map, but that does not looks a good approach to me.

Thanks!

1

There are 1 answers

4
Dave Maple On

Cloudwatch should easily be able to keep up with your metric volume so I would just use 1 PutMetricDataRequest for each batch coming in from the source applications and let Cloudwatch do the aggregation.

If you look at PutMetricDataRequest it takes a Collection<MetricDatum> which is well suited to receive any number of keys you're receiving from the source applications in a single request.

====

Converting a HashMap to a List is pretty straightforward with the Java8 streams API:

Assuming your map is a key => value map:

Map<String, Object> metricMap = new HashMap<>();
metricMap.put("metricKey", 99);

You can use the Java8 streams API with map and collect:

List<MetricDatum> metrics = metricMap.entrySet().parallelStream()
        .map(map -> new MetricDatum()
                .withMetricName(map.getKey())
                .withUnit(StandardUnit.Count)
                .withValue(Double.valueOf(map.getValue().toString()))
        ).collect(Collectors.toList());