- AWS Code Sample demonstrates how to get metrics data with Amazon CloudWatch.

/* * Copyright, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ package com.example.cloudwatch; import; import; import; import; import; import; import; import; import; import java.time.Instant; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * Gets a sample metric data point */ public class GetMetricData { public static void main(String[] args) { Region region = Region.US_EAST_1; CloudWatchClient cw = CloudWatchClient.builder() .region(region) .build(); getMetData(cw) ; } public static void getMetData( CloudWatchClient cw) { try { // Set the date Instant start = Instant.ofEpochMilli(new Date().getTime()); start = Instant.parse("2019-10-23T10:12:35Z"); Instant endDate =; Metric met = Metric.builder() .metricName("DiskReadBytes") .namespace("AWS/EC2") .build(); MetricStat metStat = MetricStat.builder() .stat("Minimum") .period(60) .metric(met) .build(); MetricDataQuery dataQUery = MetricDataQuery.builder() .metricStat(metStat) .id("foo2") .returnData(true) .build(); List<MetricDataQuery> dq = new ArrayList(); dq.add(dataQUery); GetMetricDataRequest getMetReq = GetMetricDataRequest.builder() .maxDatapoints(100) .startTime(start) .endTime(endDate) .metricDataQueries(dq) .build(); GetMetricDataResponse response = cw.getMetricData(getMetReq); List<MetricDataResult> data = response.metricDataResults(); for (int i = 0; i < data.size(); i++) { MetricDataResult item = (MetricDataResult) data.get(i); System.out.println("The label is "+item.label()); System.out.println("The status code is "+item.statusCode().toString()); } } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }

Sample Details

Service: Amazon CloudWatch

Last tested: 03/02/2020

Author: scmacdon

Type: full-example