我正在尝试使用以下函数列出存储在CloudWatch中的所有指标:ListMetrics。 该函数返回大约500个度量和一个名为NextToken的字符串值,该值将在下一次调用中用于获取其余度量。
这是我的代码,但我不知道如何使用NextToken来获取其余的指标。
// creates the CloudWatch client
var cw = Amazon.AWSClientFactory.CreateAmazonCloudWatchClient(Amazon.RegionEndpoint.EUWest1);
// initialses the list metrics request
ListMetricsRequest lmr = new ListMetricsRequest();
ListMetricsResponse lmresponse = cw.ListMetrics(lmr);
// loop that uses the token to get all the metrics available
// not finished yet
do
{
lmresponse = cw.ListMetrics(lmr);
lmresponse.NextToken;
} while (lmresponse.NextToken != null);
我想知道如何使用NextToken来获取其余的指标。遗憾的是,我在网上找不到任何例子。
答案 0 :(得分:1)
将其置于do-while循环中可确保ListMetrics函数不断被调用,并且只要nextToken不为null,就会列出所有度量标准。
private string nextToken;
do
{
lmrequest.NextToken = nextToken;
lmresponse = cloudwatch.ListMetrics(lmrequest);
nextToken = lmresponse.NextToken;
} while (nextToken != null);
答案 1 :(得分:0)
如果您只需要遍历整个指标列表,我会使用foreach
循环。代码如下所示:
// creates the CloudWatch client
var cw = Amazon.AWSClientFactory.CreateAmazonCloudWatchClient(Amazon.RegionEndpoint.EUWest1);
// initialses the list metrics request
ListMetricsRequest lmr = new ListMetricsRequest();
ListMetricsResponse lmresponse = cw.ListMetrics(lmr);
foreach (Metric metric in lmresponse.Metrics)
{
// do something with
// metric.MetricName;
// metric.Dimensions;
// etc
}
答案 2 :(得分:0)
如果响应中有NextToken,您可以在下一个请求中使用它:
// creates the CloudWatch client
var cw = Amazon.AWSClientFactory.CreateAmazonCloudWatchClient(Amazon.RegionEndpoint.EUWest1);
// initialses the list metrics request
ListMetricsRequest lmr = new ListMetricsRequest();
ListMetricsResponse lmresponse = cw.ListMetrics(lmr);
while (lmresponse.NextToken != null);
{
// set request token
lmr.NextToken = lmresponse.NextToken;
lmresponse = cw.ListMetrics(lmr);
// Process metrics found in lmresponse.Metrics
}