0

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!

Lovey
  • 880
  • 3
  • 15
  • 31

1 Answers1

0

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());
Dave Maple
  • 8,102
  • 4
  • 45
  • 64
  • Thanks for the answer. In my case data is coming in form of a HashMap. Basically I have a facade with different subscriber modes (Cloud watch is one of them). The map is pushed to Facade and then it is available to different subscribers. Thus in this case I am getting same hashmap. Are you suggesting I need to iterate over all key and values in hashmap to convert it in MetricDatum map.? – Lovey Jan 04 '17 at 01:49
  • @Lovey: I would use the Java8 streams API which should make this pretty simple. I've added a code sample to the answer illustrating the operation. – Dave Maple Jan 04 '17 at 02:37
  • still same . But thats ok. I got what you are trying to conclude here. Thanks. – Lovey Jan 04 '17 at 22:09
  • sorry -- perhaps i misunderstood the question. would be happy to help further if you can clarify a bit. – Dave Maple Jan 04 '17 at 22:50