简体   繁体   English

Cloudwatch指标永不返回任何结果

[英]Cloudwatch metrics never returning any results

I've been trying to get a simple application going that returns some cloudwatch metrics. 我一直在尝试使一个简单的应用程序运行,该应用程序返回一些cloudwatch指标。 I've tried 3 different web examples and none are returning any data. 我尝试了3种不同的网络示例,但都没有返回任何数据。 I can see the data on the AWS console. 我可以在AWS控制台上查看数据。 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. 我要实现的原理是:我想知道我们的应用程序何时接近其dynamoDB读/写限制,然后增加它们。 I assume I need to write some thread that polls asking for stats, and if it reaches say 80% of capacity, increases the limits. 我假设我需要编写一些线程来轮询以获取统计信息,并且如果达到说容量的80%,则会增加限制。

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

I found the problem. 我发现了问题。 If i change "SuccessfulRequestLatency" to this: 如果我将“ SuccessfulRequestLatency”更改为此:

    getMetricStatisticsRequest.setMetricName("ConsumedWriteCapacityUnits");

Or getMetricStatisticsRequest.setMetricName("ConsumedReadCapacityUnits"); 或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. 令人尴尬的是,我错误地使用了日期范围,并假设month = 9是9月,而实际上month = 8是9月。 Dynamo does not throw any invalid range exceptions. Dynamo不会抛出任何无效的范围异常。

I suggested in the comments, you may rely on CloudWatch alarms for this. 我在评论中建议,您可能需要依靠CloudWatch警报。 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 topic SNS in turn can be used to trigger HTTP(S) call, send JSON, feed SQS , ... 反过来, SNS可用于触发HTTP(S)调用,发送JSON,提要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. 顺便说一句,您将轻松加入免费套餐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM