Search in sources :

Example 1 with AmazonCloudWatch

use of com.amazonaws.services.cloudwatch.AmazonCloudWatch in project camel by apache.

the class CwEndpoint method createCloudWatchClient.

AmazonCloudWatch createCloudWatchClient() {
    AmazonCloudWatch client = null;
    ClientConfiguration clientConfiguration = null;
    boolean isClientConfigFound = false;
    if (ObjectHelper.isNotEmpty(configuration.getProxyHost()) && ObjectHelper.isNotEmpty(configuration.getProxyPort())) {
        clientConfiguration = new ClientConfiguration();
        clientConfiguration.setProxyHost(configuration.getProxyHost());
        clientConfiguration.setProxyPort(configuration.getProxyPort());
        isClientConfigFound = true;
    }
    if (configuration.getAccessKey() != null && configuration.getSecretKey() != null) {
        AWSCredentials credentials = new BasicAWSCredentials(configuration.getAccessKey(), configuration.getSecretKey());
        if (isClientConfigFound) {
            client = new AmazonCloudWatchClient(credentials, clientConfiguration);
        } else {
            client = new AmazonCloudWatchClient(credentials);
        }
    } else {
        if (isClientConfigFound) {
            client = new AmazonCloudWatchClient();
        } else {
            client = new AmazonCloudWatchClient(clientConfiguration);
        }
    }
    return client;
}
Also used : AmazonCloudWatchClient(com.amazonaws.services.cloudwatch.AmazonCloudWatchClient) AmazonCloudWatch(com.amazonaws.services.cloudwatch.AmazonCloudWatch) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) AWSCredentials(com.amazonaws.auth.AWSCredentials) ClientConfiguration(com.amazonaws.ClientConfiguration) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials)

Example 2 with AmazonCloudWatch

use of com.amazonaws.services.cloudwatch.AmazonCloudWatch in project chassis by Kixeye.

the class CloudWatchReporterTest method testRecoverAfterFailedPublication.

/**
     * Ensure that a failed publication does not prevent subsequent attempts
     */
@Test
@Ignore("Fix the thread sleeps.")
public void testRecoverAfterFailedPublication() throws InterruptedException {
    MetricRegistry metricRegistry = new MetricRegistry();
    metricRegistry.counter("UnitTestCounter").inc();
    final AmazonCloudWatch amazonCloudWatch = Mockito.mock(AmazonCloudWatch.class);
    reporter = new MetricsCloudWatchReporter(APP_NAME, APP_VERSION, APP_ENVIRONMENT, "utc=UnitTestCounter", 2, TimeUnit.SECONDS, metricRegistry, createCloudWatchFactory(amazonCloudWatch));
    Mockito.doThrow(new RuntimeException("CloudWatch request error")).when(amazonCloudWatch).putMetricData(Mockito.any(PutMetricDataRequest.class));
    reporter.start();
    //give the reporter a chance to publish
    Thread.sleep(3000);
    //verify that
    Mockito.verify(amazonCloudWatch, Mockito.times(1)).putMetricData(Mockito.any(PutMetricDataRequest.class));
    Mockito.reset(amazonCloudWatch);
    metricRegistry.counter("UnitTestCounter").inc();
    Thread.sleep(3000);
    PutMetricDataRequestMatcher matcher = new PutMetricDataRequestMatcher(new MetricDatumValidator("utc", APP_ENVIRONMENT, 2d));
    Mockito.verify(amazonCloudWatch, Mockito.times(2)).putMetricData(Mockito.argThat(matcher));
}
Also used : MetricsCloudWatchReporter(com.kixeye.chassis.support.metrics.aws.MetricsCloudWatchReporter) AmazonCloudWatch(com.amazonaws.services.cloudwatch.AmazonCloudWatch) PutMetricDataRequest(com.amazonaws.services.cloudwatch.model.PutMetricDataRequest) MetricRegistry(com.codahale.metrics.MetricRegistry) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with AmazonCloudWatch

use of com.amazonaws.services.cloudwatch.AmazonCloudWatch in project chassis by Kixeye.

the class CloudWatchReporterTest method testPublishInMultipleCloudWatchRequests.

/**
     * Ensure all metrics are published event when the max number of cloudwatch metrics (per request) is exceeded.
     */
@Test
public void testPublishInMultipleCloudWatchRequests() throws InterruptedException {
    MetricRegistry metricRegistry = new MetricRegistry();
    StringBuilder filter = new StringBuilder();
    for (int i = 0; i < MetricsCloudWatchReporter.MAX_CLOUDWATCH_DATUM_PER_REQUEST + 1; i++) {
        String metric = "UnitTestCounter" + i;
        metricRegistry.counter(metric).inc();
        if (i > 0) {
            filter.append(",");
        }
        filter.append(metric).append("=").append(metric);
    }
    final AmazonCloudWatch amazonCloudWatch = Mockito.mock(AmazonCloudWatch.class);
    reporter = new MetricsCloudWatchReporter(APP_NAME, APP_VERSION, APP_ENVIRONMENT, filter.toString(), 2, TimeUnit.SECONDS, metricRegistry, createCloudWatchFactory(amazonCloudWatch));
    reporter.start();
    Mockito.verify(amazonCloudWatch, Mockito.never()).putMetricData(Mockito.any(PutMetricDataRequest.class));
    Thread.sleep(3000);
    Mockito.verify(amazonCloudWatch, Mockito.times(2)).putMetricData(Mockito.any(PutMetricDataRequest.class));
}
Also used : MetricsCloudWatchReporter(com.kixeye.chassis.support.metrics.aws.MetricsCloudWatchReporter) AmazonCloudWatch(com.amazonaws.services.cloudwatch.AmazonCloudWatch) PutMetricDataRequest(com.amazonaws.services.cloudwatch.model.PutMetricDataRequest) MetricRegistry(com.codahale.metrics.MetricRegistry) Test(org.junit.Test)

Example 4 with AmazonCloudWatch

use of com.amazonaws.services.cloudwatch.AmazonCloudWatch in project chassis by Kixeye.

the class CloudWatchReporterTest method testPublishMetrics.

/**
     * successfully publish metrics. no stat filtering
     */
@Test
public void testPublishMetrics() throws InterruptedException {
    MetricRegistry metricRegistry = new MetricRegistry();
    metricRegistry.counter("UnitTestCounter1").inc();
    metricRegistry.counter("UnitTestCounter2").inc();
    metricRegistry.counter("UnitTestCounter2").inc();
    metricRegistry.counter("UnitTestCounter3").inc();
    metricRegistry.meter("UnitTestMeter");
    metricRegistry.histogram("UnitTestHistogram");
    metricRegistry.timer("UnitTestTimer");
    metricRegistry.register("UnitTestGauge", new Gauge<Object>() {

        @Override
        public Object getValue() {
            return 1;
        }
    });
    //this gauge should not be reported to AWS because its value is not numeric
    metricRegistry.register("InvalidUnitTestGauge", new Gauge<Object>() {

        @Override
        public Object getValue() {
            return "foo";
        }
    });
    final AmazonCloudWatch amazonCloudWatch = Mockito.mock(AmazonCloudWatch.class);
    reporter = new MetricsCloudWatchReporter(APP_NAME, APP_VERSION, APP_ENVIRONMENT, "utc1=UnitTestCounter1,utc2=UnitTestCounter2,utg=UnitTestGauge,utm=UnitTestMeter,uth=UnitTestHistogram,utt=UnitTestTimer", 2, TimeUnit.SECONDS, metricRegistry, createCloudWatchFactory(amazonCloudWatch));
    reporter.start();
    //give the reporter a chance to publish
    Thread.sleep(3000);
    PutMetricDataRequestMatcher matcher = new PutMetricDataRequestMatcher(new MetricDatumValidator("utg", APP_ENVIRONMENT, 1d), new MetricDatumValidator("utc1", APP_ENVIRONMENT, 1d), new MetricDatumValidator("utc2", APP_ENVIRONMENT, 2d), new MetricDatumValidator("uth.count", APP_ENVIRONMENT, 0d), new MetricDatumValidator("uth.min", APP_ENVIRONMENT, 0d), new MetricDatumValidator("uth.max", APP_ENVIRONMENT, 0d), new MetricDatumValidator("uth.mean", APP_ENVIRONMENT, 0d), new MetricDatumValidator("uth.stddev", APP_ENVIRONMENT, 0d), new MetricDatumValidator("uth.75p", APP_ENVIRONMENT, 0d), new MetricDatumValidator("uth.95p", APP_ENVIRONMENT, 0d), new MetricDatumValidator("uth.98p", APP_ENVIRONMENT, 0d), new MetricDatumValidator("uth.99p", APP_ENVIRONMENT, 0d), new MetricDatumValidator("uth.999p", APP_ENVIRONMENT, 0d), new MetricDatumValidator("utm.1m", APP_ENVIRONMENT, 0d), new MetricDatumValidator("utm.5m", APP_ENVIRONMENT, 0d), new MetricDatumValidator("utm.15m", APP_ENVIRONMENT, 0d), new MetricDatumValidator("utm.mean", APP_ENVIRONMENT, 0d), new MetricDatumValidator("utt.count", APP_ENVIRONMENT, 0d), new MetricDatumValidator("utt.1m", APP_ENVIRONMENT, 0d), new MetricDatumValidator("utt.5m", APP_ENVIRONMENT, 0d));
    PutMetricDataRequestMatcher matcher2 = new PutMetricDataRequestMatcher(new MetricDatumValidator("utt.15m", APP_ENVIRONMENT, 0d), new MetricDatumValidator("utt.mean", APP_ENVIRONMENT, 0d));
    //first request to AWS with 20 events
    Mockito.verify(amazonCloudWatch, Mockito.times(1)).putMetricData(Mockito.argThat(matcher));
    //seconds request to AWS with 2 events
    Mockito.verify(amazonCloudWatch, Mockito.times(1)).putMetricData(Mockito.argThat(matcher2));
}
Also used : MetricsCloudWatchReporter(com.kixeye.chassis.support.metrics.aws.MetricsCloudWatchReporter) AmazonCloudWatch(com.amazonaws.services.cloudwatch.AmazonCloudWatch) MetricRegistry(com.codahale.metrics.MetricRegistry) Test(org.junit.Test)

Example 5 with AmazonCloudWatch

use of com.amazonaws.services.cloudwatch.AmazonCloudWatch in project aws-doc-sdk-examples by awsdocs.

the class PutMetricAlarm method main.

public static void main(String[] args) {
    final String USAGE = "To run this example, supply an alarm name and instance id\n" + "Ex: DeleteAlarm <alarm-name> <instance-id>\n";
    if (args.length != 2) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String alarmName = args[0];
    String instanceId = args[1];
    final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.defaultClient();
    Dimension dimension = new Dimension().withName("InstanceId").withValue(instanceId);
    PutMetricAlarmRequest request = new PutMetricAlarmRequest().withAlarmName(alarmName).withComparisonOperator(ComparisonOperator.GreaterThanThreshold).withEvaluationPeriods(1).withMetricName("CPUUtilization").withNamespace("AWS/EC2").withPeriod(60).withStatistic(Statistic.Average).withThreshold(70.0).withActionsEnabled(false).withAlarmDescription("Alarm when server CPU utilization exceeds 70%").withUnit(StandardUnit.Seconds).withDimensions(dimension);
    PutMetricAlarmResult response = cw.putMetricAlarm(request);
    System.out.printf("Successfully created alarm with name %s", alarmName);
}
Also used : PutMetricAlarmResult(com.amazonaws.services.cloudwatch.model.PutMetricAlarmResult) PutMetricAlarmRequest(com.amazonaws.services.cloudwatch.model.PutMetricAlarmRequest) AmazonCloudWatch(com.amazonaws.services.cloudwatch.AmazonCloudWatch) Dimension(com.amazonaws.services.cloudwatch.model.Dimension)

Aggregations

AmazonCloudWatch (com.amazonaws.services.cloudwatch.AmazonCloudWatch)12 MetricRegistry (com.codahale.metrics.MetricRegistry)4 MetricsCloudWatchReporter (com.kixeye.chassis.support.metrics.aws.MetricsCloudWatchReporter)4 Test (org.junit.Test)4 PutMetricDataRequest (com.amazonaws.services.cloudwatch.model.PutMetricDataRequest)3 Dimension (com.amazonaws.services.cloudwatch.model.Dimension)2 ClientConfiguration (com.amazonaws.ClientConfiguration)1 AWSCredentials (com.amazonaws.auth.AWSCredentials)1 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)1 AmazonCloudWatchClient (com.amazonaws.services.cloudwatch.AmazonCloudWatchClient)1 DeleteAlarmsRequest (com.amazonaws.services.cloudwatch.model.DeleteAlarmsRequest)1 DeleteAlarmsResult (com.amazonaws.services.cloudwatch.model.DeleteAlarmsResult)1 DescribeAlarmsRequest (com.amazonaws.services.cloudwatch.model.DescribeAlarmsRequest)1 DescribeAlarmsResult (com.amazonaws.services.cloudwatch.model.DescribeAlarmsResult)1 DisableAlarmActionsRequest (com.amazonaws.services.cloudwatch.model.DisableAlarmActionsRequest)1 DisableAlarmActionsResult (com.amazonaws.services.cloudwatch.model.DisableAlarmActionsResult)1 EnableAlarmActionsRequest (com.amazonaws.services.cloudwatch.model.EnableAlarmActionsRequest)1 EnableAlarmActionsResult (com.amazonaws.services.cloudwatch.model.EnableAlarmActionsResult)1 ListMetricsRequest (com.amazonaws.services.cloudwatch.model.ListMetricsRequest)1 ListMetricsResult (com.amazonaws.services.cloudwatch.model.ListMetricsResult)1