Question

I've been trying to get a simple application going that returns some cloudwatch metrics. I've tried 3 different web examples and none are returning any data. I can see the data on the AWS console. Perhaps someone can spot my error or point me to a simple example that is know to work?

The principal I want to achieve is: I want to know when our application gets close to its dynamoDB read/write limits and then increase them. I assume I need to write some thread that polls asking for stats, and if it reaches say 80% of capacity, increases the limits.

private static void findCloudWatchData() throws InterruptedException {
    AmazonCloudWatchClient cloudWatch = new AmazonCloudWatchClient(new BasicAWSCredentials(key, secret));
    cloudWatch.setEndpoint("monitoring.eu-west-1.amazonaws.com");

    GetMetricStatisticsRequest getMetricStatisticsRequest = new GetMetricStatisticsRequest();

    GregorianCalendar g = new GregorianCalendar(2012, 9, 17, 12, 0);

    getMetricStatisticsRequest.setStartTime(g.getTime());
    g.add(Calendar.DAY_OF_MONTH, 2);
    getMetricStatisticsRequest.setEndTime(g.getTime());

    getMetricStatisticsRequest.setNamespace("AWS/DynamoDB");
    getMetricStatisticsRequest.setMetricName("SuccessfulRequestLatency");
    getMetricStatisticsRequest.setPeriod(5*60);

    Dimension tableDimension = new Dimension();
    tableDimension.setName("TableName");
    tableDimension.setValue("personalisation.user_favourite-int");

    Dimension operationDimension = new Dimension();
    operationDimension.setName("Operation");
    operationDimension.setValue("Query");

    getMetricStatisticsRequest.setDimensions(Arrays.asList(tableDimension, operationDimension));

    Collection<String> statistics = new ArrayList<String>();
    statistics.add("Average");

    getMetricStatisticsRequest.setStatistics(statistics);
    getMetricStatisticsRequest.setUnit(StandardUnit.Count);

    //results
    GetMetricStatisticsResult result = cloudWatch.getMetricStatistics(getMetricStatisticsRequest);
    System.err.println(result.getDatapoints().size());  //this returns 0 always
    for (Datapoint p : result.getDatapoints()) {
        System.out.println(p.getTimestamp() + " " + p.getAverage());
    }
 }
Was it helpful?

Solution

I found the problem. If i change "SuccessfulRequestLatency" to this:

    getMetricStatisticsRequest.setMetricName("ConsumedWriteCapacityUnits");

Or getMetricStatisticsRequest.setMetricName("ConsumedReadCapacityUnits");

and set the date like this:

    now.add(Calendar.MINUTE, -5);
    getMetricStatisticsRequest.setEndTime(now.getTime());
    now.add(Calendar.DAY_OF_MONTH, -1);
    getMetricStatisticsRequest.setStartTime(now.getTime());

It starts to return data. Embarrassingly, I've used the date range incorrectly, and assumed month=9 was Sept, when in fact month=8 is September. Dynamo does not throw any invalid range exceptions.

OTHER TIPS

I suggested in the comments, you may rely on CloudWatch alarms for this. It will avoid you the pain of running you own monitoring tool.

You can attach 'actions' to alarm. One of the actions is SNS topic. SNS in turn can be used to trigger HTTP(S) call, send JSON, feed SQS, ...

This may be a bit more work work to setup but I believe it will be much, much easier to maintain. Btw, you will easily fit in the free Tier.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top