Getting Metrics from CloudWatch - Amazon SDK for Java 1.x
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

We announced the upcoming end-of-support for Amazon SDK for Java (v1). We recommend that you migrate to Amazon SDK for Java v2. For dates, additional details, and information on how to migrate, please refer to the linked announcement.

Getting Metrics from CloudWatch

Listing Metrics

To list CloudWatch metrics, create a ListMetricsRequest and call the AmazonCloudWatchClient’s listMetrics method. You can use the ListMetricsRequest to filter the returned metrics by namespace, metric name, or dimensions.

Note

A list of metrics and dimensions that are posted by Amazon services can be found within the {https---docs-aws-amazon-com-AmazonCloudWatch-latest-monitoring-CW-Support-For-Amazon-html}[Amazon CloudWatch Metrics and Dimensions Reference] in the Amazon CloudWatch User Guide.

Imports

import com.amazonaws.services.cloudwatch.AmazonCloudWatch; import com.amazonaws.services.cloudwatch.AmazonCloudWatchClientBuilder; import com.amazonaws.services.cloudwatch.model.ListMetricsRequest; import com.amazonaws.services.cloudwatch.model.ListMetricsResult; import com.amazonaws.services.cloudwatch.model.Metric;

Code

final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.defaultClient(); ListMetricsRequest request = new ListMetricsRequest() .withMetricName(name) .withNamespace(namespace); boolean done = false; while(!done) { ListMetricsResult response = cw.listMetrics(request); for(Metric metric : response.getMetrics()) { System.out.printf( "Retrieved metric %s", metric.getMetricName()); } request.setNextToken(response.getNextToken()); if(response.getNextToken() == null) { done = true; } }

The metrics are returned in a ListMetricsResult by calling its getMetrics method. The results may be paged. To retrieve the next batch of results, call setNextToken on the original request object with the return value of the ListMetricsResult object’s getNextToken method, and pass the modified request object back to another call to listMetrics.

More Information