我如何从aws cloudwatch中获取特定指标的数据?

时间:2018-04-10 04:30:25

标签: java amazon-web-services amazon-cloudwatch

    long offsetInMilliseconds = 1000 * 60 * 60;

    GetMetricStatisticsRequest request1 = new GetMetricStatisticsRequest()
            .withStartTime(new Date(new Date().getTime() - offsetInMilliseconds))
            .withNamespace("AWS/DynamoDB")
            .withPeriod(60*60)
            .withDimensions(new Dimension().withName("TableName").withValue("INT_LOG_TABLE").withName("Operation").withValue("PutItem"))
            .withMetricName("SuccessfulRequestLatency")
            .withStatistics("Average")
            .withEndTime(new Date());


    GetMetricStatisticsResult getMetricStatisticsResult = cw.getMetricStatistics(request1);

    double avgRequestLatency = 0;
    List dataPoint = getMetricStatisticsResult.getDatapoints();
    for (Object aDataPoint : dataPoint){
        Datapoint dp = (Datapoint) aDataPoint;
        avgRequestLatency = dp.getAverage();

    }

我试过这样。

我无法进入循环

那是因为“getMetricStatisticsResult”是

“Label:SuccessfulRequestLatency,Datapoints:[]”

我怎样才能获得“DataPoints”?

提前感谢。

2 个答案:

答案 0 :(得分:1)

为什么这不会返回任何数据点可能有很多原因。

让我们从代码本身开始:

.withDimensions(new Dimension().withName("TableName").withValue("INT_LOG_TABLE").withName("Operation").withValue("PutItem"))

在这里,您只创建一个维度并设置名称和值两次,因此结果是一个包含name="Operation"value="PutItem"的维度。

尝试这样做:

.withDimensions(Arrays.asList(
    new Dimension().withName("TableName").withValue("INT_LOG_TABLE"),
    new Dimension().withName("Operation").withValue("PutItem")))

答案 1 :(得分:0)

可以使用单个对象设置单个维度。您有两个维度需要创建两个维度对象。

尝试下面的代码:

long offsetInMilliseconds = 1000 * 60 * 60

Dimension dimension1 = new Dimension();
        dimension.setName("TableName");
        dimension.setValue("INT_LOG_TABLE");

Dimension dimension2 = new Dimension();
        dimension.setName("Operation");
        dimension.setValue("PutItem");
GetMetricStatisticsRequest request1 = new GetMetricStatisticsRequest()
        .withStartTime(new Date(new Date().getTime() - offsetInMilliseconds))
        .withNamespace("AWS/DynamoDB")
        .withPeriod(60*60)
        .withDimensions(dimension1, dimension2)
        .withMetricName("SuccessfulRequestLatency")
        .withStatistics("Average")
        .withEndTime(new Date());


GetMetricStatisticsResult getMetricStatisticsResult = cw.getMetricStatistics(request1);

double avgRequestLatency = 0;
List dataPoint = getMetricStatisticsResult.getDatapoints();
for (Object aDataPoint : dataPoint){
    Datapoint dp = (Datapoint) aDataPoint;
    avgRequestLatency = dp.getAverage();

}