AWS SDK for Java
Developer Guide

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.


A list of metrics and dimensions that are posted by AWS services can be found within the Amazon CloudWatch Metrics and Dimensions Reference in the Amazon CloudWatch User Guide.


import; import; import; import; import;


final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.defaultClient(); boolean done = false; while(!done) { ListMetricsRequest request = new ListMetricsRequest() .withMetricName(name) .withNamespace(namespace); 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