public void start() { int mb = 1024 * 1024; LOG.info("Max memory: {} mb", Runtime.getRuntime().maxMemory() / mb); LOG.info("Starting up Kinesis Consumer... (may take a few seconds)"); AmazonKinesisClient kinesisClient = new AmazonKinesisClient(kinesisCfg.getKinesisCredentialsProvider(), kinesisCfg.getKinesisClientConfiguration()); AmazonDynamoDBClient dynamoDBClient = new AmazonDynamoDBClient(kinesisCfg.getDynamoDBCredentialsProvider(), kinesisCfg.getDynamoDBClientConfiguration()); AmazonCloudWatch cloudWatchClient = new AmazonCloudWatchClient(kinesisCfg.getCloudWatchCredentialsProvider(), kinesisCfg.getCloudWatchClientConfiguration()); Worker worker = new Worker.Builder() .recordProcessorFactory(() -> new RecordProcessor(unitOfWorkListener, exceptionStrategy, metricsCallback, dry)) .config(kinesisCfg) .kinesisClient(kinesisClient) .dynamoDBClient(dynamoDBClient) .cloudWatchClient(cloudWatchClient) .build(); worker.run(); }
@Bean public AmazonCloudWatch amazonCloudWatchClient(final AWSCredentialsProvider awsCredentialsProvider, final ClientConfiguration awsClientConfig, final Region awsRegion) { return AmazonCloudWatchClientBuilder.standard() .withCredentials(awsCredentialsProvider) .withClientConfiguration(awsClientConfig) .withRegion(awsRegion.getName()) .build(); }
public static void main(String[] args) { final String USAGE = "To run this example, supply an alarm name\n" + "Ex: DisableAlarmActions <alarm-name>\n"; if (args.length != 1) { System.out.println(USAGE); System.exit(1); } String alarmName = args[0]; final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.defaultClient(); DisableAlarmActionsRequest request = new DisableAlarmActionsRequest() .withAlarmNames(alarmName); DisableAlarmActionsResult response = cw.disableAlarmActions(request); System.out.printf( "Successfully disabled actions on alarm %s", alarmName); }
public static void main(String[] args) { final String USAGE = "To run this example, supply an alarm name\n" + "Ex: DeleteAlarm <alarm-name>\n"; if (args.length != 1) { System.out.println(USAGE); System.exit(1); } String alarm_name = args[0]; final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.defaultClient(); DeleteAlarmsRequest request = new DeleteAlarmsRequest() .withAlarmNames(alarm_name); DeleteAlarmsResult response = cw.deleteAlarms(request); System.out.printf("Successfully deleted alarm %s", alarm_name); }
public static void main(String[] args) { final String USAGE = "To run this example, supply an alarm name\n" + "Ex: EnableAlarmActions <alarm-name>\n"; if (args.length != 1) { System.out.println(USAGE); System.exit(1); } String alarm = args[0]; final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.defaultClient(); EnableAlarmActionsRequest request = new EnableAlarmActionsRequest() .withAlarmNames(alarm); EnableAlarmActionsResult response = cw.enableAlarmActions(request); System.out.printf( "Successfully enabled actions on alarm %s", alarm); }
public static void main(String[] args) { final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.defaultClient(); boolean done = false; DescribeAlarmsRequest request = new DescribeAlarmsRequest(); while(!done) { DescribeAlarmsResult response = cw.describeAlarms(request); for(MetricAlarm alarm : response.getMetricAlarms()) { System.out.printf("Retrieved alarm %s", alarm.getAlarmName()); } request.setNextToken(response.getNextToken()); if(response.getNextToken() == null) { done = true; } } }
CloudWatch(AmazonCloudWatch cloudWatch, String cloudWatchNamespace, String prefix, String application, String hostName) { super(); assert cloudWatch != null; assert cloudWatchNamespace != null && !cloudWatchNamespace.startsWith("AWS/") && cloudWatchNamespace.length() > 0 && cloudWatchNamespace.length() <= 255; assert prefix != null; assert application.length() >= 1 && application.length() <= 255; assert hostName.length() >= 1 && hostName.length() <= 255; this.awsCloudWatch = cloudWatch; this.cloudWatchNamespace = cloudWatchNamespace; this.prefix = prefix; // A dimension is like a tag which can be used to filter metrics in the CloudWatch UI. // Name and value of dimensions have min length 1 and max length 255. dimensions.add(new Dimension().withName("application").withValue(application)); dimensions.add(new Dimension().withName("hostname").withValue(hostName)); // note: to add other dimensions (max 10), we could call // new URL("http://instance-data/latest/meta-data/instance-id").openStream(), // or /ami-id, /placement/availability-zone, /instance-type, /local-hostname, /local-ipv4, /public-hostname, /public-ipv4 // see http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html // except that it would not work from the collector server }
/** * Default constructor */ public ElapsedTimeAggregatorTest() { AmazonEC2 ec2Client = mock(AmazonEC2.class); AmazonCloudWatch cloudWatchClient = mock(AmazonCloudWatch.class); Region region = Region.getRegion(Regions.US_WEST_1); when(ec2Client.describeTags(any(DescribeTagsRequest.class))). thenReturn(new DescribeTagsResult()); instanceOnlyAggregator = new ElapsedTimeAggregator("TEST", region, "i-500f6ca6", null, ec2Client, cloudWatchClient); when(ec2Client.describeTags(any(DescribeTagsRequest.class))). thenReturn(new DescribeTagsResult().withTags( new TagDescription(). withKey("aws:autoscaling:groupName"). withValue("TEST") )); asgAggregator = new ElapsedTimeAggregator("TEST", region, "i-500f6ca6", null, ec2Client, cloudWatchClient); }
/** * Create a new recorder instance. * This recorder will periodically send aggregated metric events to CloudWatch * via the provided client. Requests will be queued and sent using a * single-threaded ScheduledExecutorService every publishFrequency (in seconds). * The initial submission to CloudWatch will be delayed by a random amount * on top of the publish frequency, bounded by maxJitter. * * The {@link #shutdown} method must be called when the application is done * with the recorder in order to flush and stop the reporting thread. * * @param client Client to use for connecting to CloudWatch * @param namespace CloudWatch namespace to publish under * @param maxJitter Maximum delay before counting publish frequency for * initial request, in seconds. * A value of 0 will provide no jitter. * @param publishFrequency Batch up and publish at this interval, in seconds. * Suggested value of 60, for one minute aggregation. * @param dimensionMapper Configuration specifying which dimensions to sent * to CloudWatch for each metric event. */ public CloudWatchRecorder( final AmazonCloudWatch client, final String namespace, final int maxJitter, final int publishFrequency, final DimensionMapper dimensionMapper) { if (client == null) { throw new IllegalArgumentException("AmazonCloudWatch must be provided"); } if (namespace == null || namespace.isEmpty()) { throw new IllegalArgumentException("namespace must be provided"); } if (dimensionMapper == null) { throw new IllegalArgumentException("DimensionMapper must be provided"); } this.metricsClient = client; this.namespace = namespace; //TODO: is there value in injecting this? Yes, but then how to handle lifecycle? this.publishExecutor = Executors.newSingleThreadScheduledExecutor(); this.aggregator = new MetricDataAggregator(dimensionMapper); start(maxJitter, publishFrequency); }
@Test public void record_and_shutdown() throws Exception { final DimensionMapper mapper = new DimensionMapper.Builder() .addGlobalDimension(ContextData.ID) .build(); final String namespace = "spacename"; final String id = UUID.randomUUID().toString(); final TypedMap data = ContextData.withId(id).build(); final double time = 123.45; final AmazonCloudWatch client = mock(AmazonCloudWatch.class); final CloudWatchRecorder recorder = new CloudWatchRecorder(client, namespace, 60, 120, mapper); final MetricRecorder.Context context = recorder.context(data); context.record(M_TIME, time, Unit.MILLISECOND, Instant.now()); context.close(); // shutdown right away, before first scheduled publish recorder.shutdown(); final List<MetricDatum> expected = new ArrayList<>(1); expected.add(makeDatum(id, M_TIME.toString(), time, time, time, 1, StandardUnit.Milliseconds)); verify(client).putMetricData(argThat(new RequestMatcher(namespace, expected))); }
@Provides @Singleton protected AmazonCloudWatch provideAmazonCloudWatch(Region region, AWSCredentialsProvider credentialsProvider) { AmazonCloudWatch cloudWatch = new AmazonCloudWatchClient(credentialsProvider); cloudWatch.setRegion(region); return cloudWatch; }
public static void main(String[] args) { final String USAGE = "To run this example, supply a metric name and metric namespace\n" + "Ex: ListMetrics <metric-name> <metric-namespace>\n"; if (args.length != 2) { System.out.println(USAGE); System.exit(1); } String name = args[0]; String namespace = args[1]; final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.defaultClient(); ListMetricsRequest request = new ListMetricsRequest() .withMetricName(name) .withNamespace(namespace); boolean done = false; while(!done) { ListMetricsResult response = cw.listMetrics(request); for(Metric metric : response.getMetrics()) { System.out.printf( "Retrieved metric %s", metric.getMetricName()); } request.setNextToken(response.getNextToken()); if(response.getNextToken() == null) { done = true; } } }
public static void main(String[] args) { final String USAGE = "To run this example, supply a data point:\n" + "Ex: PutMetricData <data_point>\n"; if (args.length != 1) { System.out.println(USAGE); System.exit(1); } Double data_point = Double.parseDouble(args[0]); final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.defaultClient(); Dimension dimension = new Dimension() .withName("UNIQUE_PAGES") .withValue("URLS"); MetricDatum datum = new MetricDatum() .withMetricName("PAGES_VISITED") .withUnit(StandardUnit.None) .withValue(data_point) .withDimensions(dimension); PutMetricDataRequest request = new PutMetricDataRequest() .withNamespace("SITE/TRAFFIC") .withMetricData(datum); PutMetricDataResult response = cw.putMetricData(request); System.out.printf("Successfully put data point %f", data_point); }
@Override public AmazonCloudWatch getCloudWatchClient() { AmazonCloudWatchClientBuilder clientBuilder = AmazonCloudWatchClientBuilder.standard().withCredentials(getCredentialsProvider()); if (serviceEndpoint == null) { clientBuilder.withRegion(region); } else { clientBuilder.withEndpointConfiguration( new AwsClientBuilder.EndpointConfiguration(serviceEndpoint, region.getName())); } return clientBuilder.build(); }
/** * A metric is not fully filtered, but some stats within the metric are */ @Test public void testPublishFilteredMetrics_metricStatFiltered() throws InterruptedException { MetricRegistry metricRegistry = new MetricRegistry(); metricRegistry.meter("UnitTestMeter1").mark(); metricRegistry.meter("UnitTestMeter2").mark(); final AmazonCloudWatch amazonCloudWatch = Mockito.mock(AmazonCloudWatch.class); reporter = new MetricsCloudWatchReporter( APP_NAME, APP_VERSION, APP_ENVIRONMENT, "utm1=UnitTestMeter1,utm2=UnitTestMeter2:1m:5m:15m", 2, TimeUnit.SECONDS, metricRegistry, createCloudWatchFactory(amazonCloudWatch)); reporter.start(); //give the reporter a chance to publish Thread.sleep(3000); PutMetricDataRequestMatcher matcher = new PutMetricDataRequestMatcher( new MetricDatumValidator("utm1.1m", APP_ENVIRONMENT, 0d), new MetricDatumValidator("utm1.5m", APP_ENVIRONMENT, 0d), new MetricDatumValidator("utm1.15m", APP_ENVIRONMENT, 0d), new MetricDatumValidator("utm1.mean", APP_ENVIRONMENT, null), new MetricDatumValidator("utm2.1m", APP_ENVIRONMENT, 0d), new MetricDatumValidator("utm2.5m", APP_ENVIRONMENT, 0d), new MetricDatumValidator("utm2.15m", APP_ENVIRONMENT, 0d)); Mockito.verify(amazonCloudWatch, Mockito.times(1)).putMetricData(Mockito.argThat(matcher)); }
/** * 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)); }
private CloudWatchFactory createCloudWatchFactory(final AmazonCloudWatch amazonCloudWatch) { return new CloudWatchFactory() { @Override public AmazonCloudWatch getCloudWatchClient() { return amazonCloudWatch; } }; }
@Test public void no_aggregation() throws Exception { final DimensionMapper mapper = new DimensionMapper.Builder() .addGlobalDimension(ContextData.ID) .build(); final String namespace = "testytesttest"; final String id = UUID.randomUUID().toString(); final Integer time = Integer.valueOf(23); final Integer load = Integer.valueOf(87); final AmazonCloudWatch client = mock(AmazonCloudWatch.class); CloudWatchRecorder recorder = null; try { // no jitter, publish soon recorder = new CloudWatchRecorder(client, namespace, 0, 1, mapper); final TypedMap data = ContextData.withId(id).build(); final MetricRecorder.Context context = recorder.context(data); final Instant timestamp = Instant.now(); context.record(M_TIME, time, Unit.MILLISECOND, timestamp); context.count(M_FAIL, 1); context.record(M_PERC, load, Unit.PERCENT, timestamp); context.close(); // allow time for one publish Thread.sleep(1024); } finally { recorder.shutdown(); } final List<MetricDatum> expected = new ArrayList<>(2); expected.add(makeDatum(id, M_TIME.toString(), time, time, time, 1, StandardUnit.Milliseconds)); expected.add(makeDatum(id, M_PERC.toString(), load, load, load, 1, StandardUnit.Percent)); expected.add(makeDatum(id, M_FAIL.toString(), 1, 1, 1, 1, StandardUnit.Count)); verify(client).putMetricData(argThat(new RequestMatcher(namespace, expected))); }
@Test public void aggregation() throws Exception { final DimensionMapper mapper = new DimensionMapper.Builder() .addGlobalDimension(ContextData.ID) .build(); final String namespace = "testytesttest"; final String id = UUID.randomUUID().toString(); final double[] timeVals = {867, 5309}; final double[] percVals = {0.01, 97.3, 3.1415}; final int[] failCnts = {1, 3, 0, 42}; final List<MetricDatum> expected = new ArrayList<>(3); expected.add(makeDatum(id, M_TIME.toString(), Arrays.stream(timeVals).sum(), Arrays.stream(timeVals).min().getAsDouble(), Arrays.stream(timeVals).max().getAsDouble(), timeVals.length, StandardUnit.Milliseconds)); expected.add(makeDatum(id, M_PERC.toString(), Arrays.stream(percVals).sum(), Arrays.stream(percVals).min().getAsDouble(), Arrays.stream(percVals).max().getAsDouble(), percVals.length, StandardUnit.Percent)); expected.add(makeDatum(id, M_FAIL.toString(), Arrays.stream(failCnts).sum(), Arrays.stream(failCnts).min().getAsInt(), Arrays.stream(failCnts).max().getAsInt(), failCnts.length, StandardUnit.Count)); final AmazonCloudWatch client = mock(AmazonCloudWatch.class); CloudWatchRecorder recorder = null; try { // no jitter, publish soon recorder = new CloudWatchRecorder(client, namespace, 0, 1, mapper); final TypedMap data = ContextData.withId(id).build(); final MetricRecorder.Context context = recorder.context(data); context.count(M_FAIL, failCnts[0]); context.record(M_PERC, percVals[0], Unit.PERCENT, Instant.now()); context.record(M_TIME, timeVals[0], Unit.MILLISECOND, Instant.now()); context.count(M_FAIL, failCnts[1]); context.record(M_PERC, percVals[1], Unit.PERCENT, Instant.now()); context.record(M_TIME, timeVals[1], Unit.MILLISECOND, Instant.now()); context.count(M_FAIL, failCnts[2]); context.record(M_PERC, percVals[2], Unit.PERCENT, Instant.now()); context.count(M_FAIL, failCnts[3]); context.close(); // allow time for one publish Thread.sleep(1024); } finally { recorder.shutdown(); } verify(client).putMetricData(argThat(new RequestMatcher(namespace, expected))); }
@Inject public CloudWatchScanCountListener(AmazonCloudWatch cloudWatch, List<Dimension> dimensions, LifeCycleRegistry lifecycle) { _cloudWatch = cloudWatch; _dimensions = dimensions; lifecycle.manage(new ManagedGuavaService(this)); }
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); }
public SimplifiedKinesisClient(AmazonKinesis kinesis, AmazonCloudWatch cloudWatch) { this.kinesis = checkNotNull(kinesis, "kinesis"); this.cloudWatch = checkNotNull(cloudWatch, "cloudWatch"); }
@Override public AmazonCloudWatch getCloudWatchClient() { return Mockito.mock(AmazonCloudWatch.class); }
public AmazonCloudWatch getAmazonCwClient() { return amazonCwClient; }
/** * To use the AmazonCloudWatch as the client */ public void setAmazonCwClient(AmazonCloudWatch amazonCwClient) { this.amazonCwClient = amazonCwClient; }
public void setCloudWatchClient(AmazonCloudWatch cloudWatchClient) { this.cloudWatchClient = cloudWatchClient; }
public AmazonCloudWatch getCloudWatchClient() { return cloudWatchClient; }
/** * Set the client to use to publish the metrics. */ // @NotRequired("Default is create one in initialize() with the credentials") public void setCloudWatchClient(AmazonCloudWatch cloudWatchClient) { this.cloudWatchClient = cloudWatchClient; }
/** * 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)); }
/** * Create a new recorder instance. * This recorder will periodically send aggregated metric events to CloudWatch * via the provided client. * * The {@link #shutdown} method must be called when the application is done * with the recorder in order to flush and stop the reporting thread. * * This is equivalent to constructing a CloudWatch recorder with a * maxJitter of 60, a publish frequency of 60, and a default dimension * mapping that includes just the metric context ID. * * @param client Client to use for connecting to CloudWatch * @param namespace CloudWatch namespace to publish under */ public CloudWatchRecorder( final AmazonCloudWatch client, final String namespace) { this(client, namespace, DEFAULT_JITTER, DEFAULT_PUBLISH_FREQ, DEFAULT_DIMENSIONS); }
/** * get an AmazonCloudWatch * @return AmazonCloudWatch */ public AmazonCloudWatch getCloudWatchClient();
AmazonCloudWatch getCloudWatchClient();