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;
}
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));
}
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));
}
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));
}
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);
}
Aggregations