Search in sources :

Example 1 with ListMetricsRequest

use of software.amazon.awssdk.services.cloudwatch.model.ListMetricsRequest in project aws-doc-sdk-examples by awsdocs.

the class ListMetrics method listMets.

// snippet-start:[cloudwatch.java2.list_metrics.main]
public static void listMets(CloudWatchClient cw, String namespace) {
    boolean done = false;
    String nextToken = null;
    try {
        while (!done) {
            ListMetricsResponse response;
            if (nextToken == null) {
                ListMetricsRequest request = ListMetricsRequest.builder().namespace(namespace).build();
                response = cw.listMetrics(request);
            } else {
                ListMetricsRequest request = ListMetricsRequest.builder().namespace(namespace).nextToken(nextToken).build();
                response = cw.listMetrics(request);
            }
            for (Metric metric : response.metrics()) {
                System.out.printf("Retrieved metric %s", metric.metricName());
                System.out.println();
            }
            if (response.nextToken() == null) {
                done = true;
            } else {
                nextToken = response.nextToken();
            }
        }
    } catch (CloudWatchException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : CloudWatchException(software.amazon.awssdk.services.cloudwatch.model.CloudWatchException) ListMetricsResponse(software.amazon.awssdk.services.cloudwatch.model.ListMetricsResponse) ListMetricsRequest(software.amazon.awssdk.services.cloudwatch.model.ListMetricsRequest) Metric(software.amazon.awssdk.services.cloudwatch.model.Metric)

Aggregations

CloudWatchException (software.amazon.awssdk.services.cloudwatch.model.CloudWatchException)1 ListMetricsRequest (software.amazon.awssdk.services.cloudwatch.model.ListMetricsRequest)1 ListMetricsResponse (software.amazon.awssdk.services.cloudwatch.model.ListMetricsResponse)1 Metric (software.amazon.awssdk.services.cloudwatch.model.Metric)1