Search in sources :

Example 1 with MetricStat

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

the class GetMetricData method getMetData.

// snippet-start:[cloudwatch.java2.get_metric_alarm.main]
public static void getMetData(CloudWatchClient cw) {
    try {
        // Set the date
        Instant start = Instant.parse("2019-10-23T10:12:35Z");
        Instant endDate = Instant.now();
        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);
    }
}
Also used : CloudWatchException(software.amazon.awssdk.services.cloudwatch.model.CloudWatchException) GetMetricDataRequest(software.amazon.awssdk.services.cloudwatch.model.GetMetricDataRequest) Instant(java.time.Instant) MetricStat(software.amazon.awssdk.services.cloudwatch.model.MetricStat) ArrayList(java.util.ArrayList) MetricDataResult(software.amazon.awssdk.services.cloudwatch.model.MetricDataResult) Metric(software.amazon.awssdk.services.cloudwatch.model.Metric) MetricDataQuery(software.amazon.awssdk.services.cloudwatch.model.MetricDataQuery) GetMetricDataResponse(software.amazon.awssdk.services.cloudwatch.model.GetMetricDataResponse)

Aggregations

Instant (java.time.Instant)1 ArrayList (java.util.ArrayList)1 CloudWatchException (software.amazon.awssdk.services.cloudwatch.model.CloudWatchException)1 GetMetricDataRequest (software.amazon.awssdk.services.cloudwatch.model.GetMetricDataRequest)1 GetMetricDataResponse (software.amazon.awssdk.services.cloudwatch.model.GetMetricDataResponse)1 Metric (software.amazon.awssdk.services.cloudwatch.model.Metric)1 MetricDataQuery (software.amazon.awssdk.services.cloudwatch.model.MetricDataQuery)1 MetricDataResult (software.amazon.awssdk.services.cloudwatch.model.MetricDataResult)1 MetricStat (software.amazon.awssdk.services.cloudwatch.model.MetricStat)1