Search in sources :

Example 1 with Dimension

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

the class PutMetricData method putMetData.

// snippet-start:[cloudwatch.java2.put_metric_data.main]
public static void putMetData(CloudWatchClient cw, Double dataPoint) {
    try {
        Dimension dimension = Dimension.builder().name("UNIQUE_PAGES").value("URLS").build();
        // Set an Instant object
        String time = ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT);
        Instant instant = Instant.parse(time);
        MetricDatum datum = MetricDatum.builder().metricName("PAGES_VISITED").unit(StandardUnit.NONE).value(dataPoint).timestamp(instant).dimensions(dimension).build();
        PutMetricDataRequest request = PutMetricDataRequest.builder().namespace("SITE/TRAFFIC").metricData(datum).build();
        cw.putMetricData(request);
    } catch (CloudWatchException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    System.out.printf("Successfully put data point %f", dataPoint);
}
Also used : CloudWatchException(software.amazon.awssdk.services.cloudwatch.model.CloudWatchException) PutMetricDataRequest(software.amazon.awssdk.services.cloudwatch.model.PutMetricDataRequest) Instant(java.time.Instant) MetricDatum(software.amazon.awssdk.services.cloudwatch.model.MetricDatum) Dimension(software.amazon.awssdk.services.cloudwatch.model.Dimension)

Example 2 with Dimension

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

the class PutMetricAlarm method putMetricAlarm.

// snippet-start:[cloudwatch.java2.put_metric_alarm.main]
public static void putMetricAlarm(CloudWatchClient cw, String alarmName, String instanceId) {
    try {
        Dimension dimension = Dimension.builder().name("InstanceId").value(instanceId).build();
        PutMetricAlarmRequest request = PutMetricAlarmRequest.builder().alarmName(alarmName).comparisonOperator(ComparisonOperator.GREATER_THAN_THRESHOLD).evaluationPeriods(1).metricName("CPUUtilization").namespace("AWS/EC2").period(60).statistic(Statistic.AVERAGE).threshold(70.0).actionsEnabled(false).alarmDescription("Alarm when server CPU utilization exceeds 70%").unit(StandardUnit.SECONDS).dimensions(dimension).build();
        cw.putMetricAlarm(request);
        System.out.printf("Successfully created alarm with name %s", alarmName);
    } catch (CloudWatchException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : CloudWatchException(software.amazon.awssdk.services.cloudwatch.model.CloudWatchException) PutMetricAlarmRequest(software.amazon.awssdk.services.cloudwatch.model.PutMetricAlarmRequest) Dimension(software.amazon.awssdk.services.cloudwatch.model.Dimension)

Aggregations

CloudWatchException (software.amazon.awssdk.services.cloudwatch.model.CloudWatchException)2 Dimension (software.amazon.awssdk.services.cloudwatch.model.Dimension)2 Instant (java.time.Instant)1 MetricDatum (software.amazon.awssdk.services.cloudwatch.model.MetricDatum)1 PutMetricAlarmRequest (software.amazon.awssdk.services.cloudwatch.model.PutMetricAlarmRequest)1 PutMetricDataRequest (software.amazon.awssdk.services.cloudwatch.model.PutMetricDataRequest)1