private double getInstanceAverageLoad(AmazonCloudWatchClient cloudWatchClient, String instanceId) { long offsetInMilliseconds = 1000 * 60 * 60; GetMetricStatisticsRequest request = new GetMetricStatisticsRequest() .withStartTime(new Date(new Date().getTime() - offsetInMilliseconds)) .withNamespace("AWS/EC2") .withPeriod(60 * 60) .withDimensions(new Dimension().withName("InstanceId").withValue(instanceId)) .withMetricName("CPUUtilization") .withStatistics("Average", "Maximum") .withEndTime(new Date()); GetMetricStatisticsResult getMetricStatisticsResult = cloudWatchClient.getMetricStatistics(request); double avgCPUUtilization = 0; List dataPoint = getMetricStatisticsResult.getDatapoints(); for (Object aDataPoint : dataPoint) { Datapoint dp = (Datapoint) aDataPoint; avgCPUUtilization = dp.getAverage(); } return avgCPUUtilization; }
/** * Gets total size in bytes of all events that remain in Kinesis stream between specified * instants. * * @return total size in bytes of all Kinesis events after specified instant */ public long getBacklogBytes(final String streamName, final Instant countSince, final Instant countTo) throws TransientKinesisException { return wrapExceptions(new Callable<Long>() { @Override public Long call() throws Exception { Minutes period = Minutes.minutesBetween(countSince, countTo); if (period.isLessThan(Minutes.ONE)) { return 0L; } GetMetricStatisticsRequest request = createMetricStatisticsRequest(streamName, countSince, countTo, period); long totalSizeInBytes = 0; GetMetricStatisticsResult result = cloudWatch.getMetricStatistics(request); for (Datapoint point : result.getDatapoints()) { totalSizeInBytes += point .getSum() .longValue(); } return totalSizeInBytes; } }); }
@Test public void shouldCountBytesWhenSingleDataPointReturned() throws Exception { Instant countSince = new Instant("2017-04-06T10:00:00.000Z"); Instant countTo = new Instant("2017-04-06T11:00:00.000Z"); Minutes periodTime = Minutes.minutesBetween(countSince, countTo); GetMetricStatisticsRequest metricStatisticsRequest = underTest.createMetricStatisticsRequest(STREAM, countSince, countTo, periodTime); GetMetricStatisticsResult result = new GetMetricStatisticsResult() .withDatapoints(new Datapoint().withSum(1.0)); given(cloudWatch.getMetricStatistics(metricStatisticsRequest)).willReturn(result); long backlogBytes = underTest.getBacklogBytes(STREAM, countSince, countTo); assertThat(backlogBytes).isEqualTo(1L); }
@Test public void shouldCountBytesWhenMultipleDataPointsReturned() throws Exception { Instant countSince = new Instant("2017-04-06T10:00:00.000Z"); Instant countTo = new Instant("2017-04-06T11:00:00.000Z"); Minutes periodTime = Minutes.minutesBetween(countSince, countTo); GetMetricStatisticsRequest metricStatisticsRequest = underTest.createMetricStatisticsRequest(STREAM, countSince, countTo, periodTime); GetMetricStatisticsResult result = new GetMetricStatisticsResult() .withDatapoints( new Datapoint().withSum(1.0), new Datapoint().withSum(3.0), new Datapoint().withSum(2.0) ); given(cloudWatch.getMetricStatistics(metricStatisticsRequest)).willReturn(result); long backlogBytes = underTest.getBacklogBytes(STREAM, countSince, countTo); assertThat(backlogBytes).isEqualTo(6L); }
@Test public void testMetricPeriod() { new CloudWatchCollector( "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/ELB\n aws_metric_name: RequestCount\n period_seconds: 100\n range_seconds: 200\n delay_seconds: 300", client).register(registry); Mockito.when(client.getMetricStatistics((GetMetricStatisticsRequest) anyObject())) .thenReturn(new GetMetricStatisticsResult()); registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance"}, new String[]{"aws_elb", ""}); Mockito.verify(client).getMetricStatistics((GetMetricStatisticsRequest) argThat( new GetMetricStatisticsRequestMatcher() .Namespace("AWS/ELB") .MetricName("RequestCount") .Period(100) )); }
@Test public void testDefaultPeriod() { new CloudWatchCollector( "---\nregion: reg\nperiod_seconds: 100\nrange_seconds: 200\ndelay_seconds: 300\nmetrics:\n- aws_namespace: AWS/ELB\n aws_metric_name: RequestCount", client).register(registry); Mockito.when(client.getMetricStatistics((GetMetricStatisticsRequest) anyObject())) .thenReturn(new GetMetricStatisticsResult()); registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance"}, new String[]{"aws_elb", ""}); Mockito.verify(client).getMetricStatistics((GetMetricStatisticsRequest) argThat( new GetMetricStatisticsRequestMatcher() .Namespace("AWS/ELB") .MetricName("RequestCount") .Period(100) )); }
@Test public void testAllStatistics() throws Exception { new CloudWatchCollector( "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/ELB\n aws_metric_name: RequestCount", client).register(registry); Mockito.when(client.getMetricStatistics((GetMetricStatisticsRequest)argThat( new GetMetricStatisticsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount")))) .thenReturn(new GetMetricStatisticsResult().withDatapoints( new Datapoint().withTimestamp(new Date()).withAverage(1.0) .withMaximum(2.0).withMinimum(3.0).withSampleCount(4.0).withSum(5.0))); assertEquals(1.0, registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance"}, new String[]{"aws_elb", ""}), .01); assertEquals(2.0, registry.getSampleValue("aws_elb_request_count_maximum", new String[]{"job", "instance"}, new String[]{"aws_elb", ""}), .01); assertEquals(3.0, registry.getSampleValue("aws_elb_request_count_minimum", new String[]{"job", "instance"}, new String[]{"aws_elb", ""}), .01); assertEquals(4.0, registry.getSampleValue("aws_elb_request_count_sample_count", new String[]{"job", "instance"}, new String[]{"aws_elb", ""}), .01); assertEquals(5.0, registry.getSampleValue("aws_elb_request_count_sum", new String[]{"job", "instance"}, new String[]{"aws_elb", ""}), .01); }
@Test public void testDimensions() throws Exception { new CloudWatchCollector( "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/ELB\n aws_metric_name: RequestCount\n aws_dimensions:\n - AvailabilityZone\n - LoadBalancerName", client).register(registry); Mockito.when(client.listMetrics((ListMetricsRequest)argThat( new ListMetricsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimensions("AvailabilityZone", "LoadBalancerName")))) .thenReturn(new ListMetricsResult().withMetrics( new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("a"), new Dimension().withName("LoadBalancerName").withValue("myLB")), new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("a"), new Dimension().withName("LoadBalancerName").withValue("myLB"), new Dimension().withName("ThisExtraDimensionIsIgnored").withValue("dummy")), new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("b"), new Dimension().withName("LoadBalancerName").withValue("myOtherLB")))); Mockito.when(client.getMetricStatistics((GetMetricStatisticsRequest)argThat( new GetMetricStatisticsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimension("AvailabilityZone", "a").Dimension("LoadBalancerName", "myLB")))) .thenReturn(new GetMetricStatisticsResult().withDatapoints( new Datapoint().withTimestamp(new Date()).withAverage(2.0))); Mockito.when(client.getMetricStatistics((GetMetricStatisticsRequest)argThat( new GetMetricStatisticsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimension("AvailabilityZone", "b").Dimension("LoadBalancerName", "myOtherLB")))) .thenReturn(new GetMetricStatisticsResult().withDatapoints( new Datapoint().withTimestamp(new Date()).withAverage(3.0))); assertEquals(2.0, registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name"}, new String[]{"aws_elb", "", "a", "myLB"}), .01); assertEquals(3.0, registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name"}, new String[]{"aws_elb", "", "b", "myOtherLB"}), .01); assertNull(registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name", "this_extra_dimension_is_ignored"}, new String[]{"aws_elb", "", "a", "myLB", "dummy"})); }
@Test public void testDimensionSelect() throws Exception { new CloudWatchCollector( "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/ELB\n aws_metric_name: RequestCount\n aws_dimensions:\n - AvailabilityZone\n - LoadBalancerName\n aws_dimension_select:\n LoadBalancerName:\n - myLB", client).register(registry); Mockito.when(client.listMetrics((ListMetricsRequest)argThat( new ListMetricsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimensions("AvailabilityZone", "LoadBalancerName")))) .thenReturn(new ListMetricsResult().withMetrics( new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("a"), new Dimension().withName("LoadBalancerName").withValue("myLB")), new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("b"), new Dimension().withName("LoadBalancerName").withValue("myLB")), new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("a"), new Dimension().withName("LoadBalancerName").withValue("myOtherLB")))); Mockito.when(client.getMetricStatistics((GetMetricStatisticsRequest)argThat( new GetMetricStatisticsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimension("AvailabilityZone", "a").Dimension("LoadBalancerName", "myLB")))) .thenReturn(new GetMetricStatisticsResult().withDatapoints( new Datapoint().withTimestamp(new Date()).withAverage(2.0))); Mockito.when(client.getMetricStatistics((GetMetricStatisticsRequest)argThat( new GetMetricStatisticsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimension("AvailabilityZone", "b").Dimension("LoadBalancerName", "myLB")))) .thenReturn(new GetMetricStatisticsResult().withDatapoints( new Datapoint().withTimestamp(new Date()).withAverage(2.0))); assertEquals(2.0, registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name"}, new String[]{"aws_elb", "", "a", "myLB"}), .01); assertEquals(2.0, registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name"}, new String[]{"aws_elb", "", "b", "myLB"}), .01); assertNull(registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name"}, new String[]{"aws_elb", "", "a", "myOtherLB"})); }
@Test public void testDimensionSelectRegex() throws Exception { new CloudWatchCollector( "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/ELB\n aws_metric_name: RequestCount\n aws_dimensions:\n - AvailabilityZone\n - LoadBalancerName\n aws_dimension_select_regex:\n LoadBalancerName:\n - myLB(.*)", client).register(registry); Mockito.when(client.listMetrics((ListMetricsRequest) argThat( new ListMetricsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimensions("AvailabilityZone", "LoadBalancerName")))) .thenReturn(new ListMetricsResult().withMetrics( new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("a"), new Dimension().withName("LoadBalancerName").withValue("myLB1")), new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("b"), new Dimension().withName("LoadBalancerName").withValue("myLB2")), new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("a"), new Dimension().withName("LoadBalancerName").withValue("myOtherLB")))); Mockito.when(client.getMetricStatistics((GetMetricStatisticsRequest) argThat( new GetMetricStatisticsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimension("AvailabilityZone", "a").Dimension("LoadBalancerName", "myLB1")))) .thenReturn(new GetMetricStatisticsResult().withDatapoints( new Datapoint().withTimestamp(new Date()).withAverage(2.0))); Mockito.when(client.getMetricStatistics((GetMetricStatisticsRequest) argThat( new GetMetricStatisticsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimension("AvailabilityZone", "b").Dimension("LoadBalancerName", "myLB2")))) .thenReturn(new GetMetricStatisticsResult().withDatapoints( new Datapoint().withTimestamp(new Date()).withAverage(2.0))); assertEquals(2.0, registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name"}, new String[]{"aws_elb", "", "a", "myLB1"}), .01); assertEquals(2.0, registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name"}, new String[]{"aws_elb", "", "b", "myLB2"}), .01); assertNull(registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name"}, new String[]{"aws_elb", "", "a", "myOtherLB"})); }
@Test public void testGetDimensionsUsesNextToken() throws Exception { new CloudWatchCollector( "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/ELB\n aws_metric_name: RequestCount\n aws_dimensions:\n - AvailabilityZone\n - LoadBalancerName\n aws_dimension_select:\n LoadBalancerName:\n - myLB", client).register(registry); Mockito.when(client.listMetrics((ListMetricsRequest)argThat( new ListMetricsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimensions("AvailabilityZone", "LoadBalancerName")))) .thenReturn(new ListMetricsResult().withNextToken("ABC")); Mockito.when(client.listMetrics((ListMetricsRequest)argThat( new ListMetricsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimensions("AvailabilityZone", "LoadBalancerName").NextToken("ABC")))) .thenReturn(new ListMetricsResult().withMetrics( new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("a"), new Dimension().withName("LoadBalancerName").withValue("myLB")))); Mockito.when(client.getMetricStatistics((GetMetricStatisticsRequest)argThat( new GetMetricStatisticsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimension("AvailabilityZone", "a").Dimension("LoadBalancerName", "myLB")))) .thenReturn(new GetMetricStatisticsResult().withDatapoints( new Datapoint().withTimestamp(new Date()).withAverage(2.0))); assertEquals(2.0, registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name"}, new String[]{"aws_elb", "", "a", "myLB"}), .01); }
@Test public void testExtendedStatistics() throws Exception { new CloudWatchCollector( "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/ELB\n aws_metric_name: Latency\n aws_extended_statistics:\n - p95\n - p99.99", client).register(registry); HashMap<String, Double> extendedStatistics = new HashMap<String, Double>(); extendedStatistics.put("p95", 1.0); extendedStatistics.put("p99.99", 2.0); Mockito.when(client.getMetricStatistics((GetMetricStatisticsRequest)argThat( new GetMetricStatisticsRequestMatcher().Namespace("AWS/ELB").MetricName("Latency")))) .thenReturn(new GetMetricStatisticsResult().withDatapoints( new Datapoint().withTimestamp(new Date()).withExtendedStatistics(extendedStatistics))); assertEquals(1.0, registry.getSampleValue("aws_elb_latency_p95", new String[]{"job", "instance"}, new String[]{"aws_elb", ""}), .01); assertEquals(2.0, registry.getSampleValue("aws_elb_latency_p99_99", new String[]{"job", "instance"}, new String[]{"aws_elb", ""}), .01); }
public void monitorCPUUsage() { AmazonCloudWatchClient cloudClient = new AmazonCloudWatchClient( getAwsCredentials()); GetMetricStatisticsRequest request = new GetMetricStatisticsRequest(); Calendar cal = Calendar.getInstance(); request.setEndTime(cal.getTime()); cal.add(Calendar.MINUTE, -5); request.setStartTime(cal.getTime()); request.setNamespace("AWS/EC2"); List<String> statistics = new ArrayList<String>(); statistics.add("Maximium"); statistics.add("Average"); request.setStatistics(statistics); request.setMetricName("CPUUtilization"); request.setPeriod(300); Dimension dimension = new Dimension(); dimension.setName("InstanceId"); dimension.setValue("i-d93fa2a4"); List<Dimension> dimensions = new ArrayList<Dimension>(); dimensions.add(dimension); request.setDimensions(dimensions); GetMetricStatisticsResult result = cloudClient .getMetricStatistics(request); List<Datapoint> dataPoints = result.getDatapoints(); for (Datapoint dataPoint : dataPoints) { System.out.println(dataPoint.getAverage()); } }
/** * GetMetricStaticticsTest to get the data points * * @return Datapoint */ protected final Datapoint getMetricStaticticsTest(String metricName) { Datapoint dataPoint = null; GetMetricStatisticsRequest request = new GetMetricStatisticsRequest(); request.setStartTime(new DateTime().plusHours(HOURS).toDate()); request.withNamespace(NAMESPACE); request.withPeriod(60 * 60); request.withMetricName(metricName); request.withStatistics("Average", "SampleCount"); request.withEndTime(new Date()); GetMetricStatisticsResult result = amazonCloudWatchClient.getMetricStatistics(request); if (result != null && !result.getDatapoints().isEmpty()) { dataPoint = result.getDatapoints().get(0); } return dataPoint; }
public GetMetricStatisticsResult getCpuUtilization(String name) { Dimension instanceDimension = new Dimension(); if (namespace.endsWith("EC2")) { instanceDimension.setName("InstanceId"); } else { instanceDimension.setName("DBInstanceIdentifier"); } instanceDimension.setValue(name); Calendar startCal = Calendar.getInstance(); Calendar endCal = Calendar.getInstance(); startCal.add(Calendar.MINUTE, -10); startCal.set(Calendar.SECOND, 0); GetMetricStatisticsRequest request = new GetMetricStatisticsRequest() .withStartTime(startCal.getTime()) .withEndTime(endCal.getTime()) .withNamespace(namespace) .withPeriod(60) .withMetricName("CPUUtilization") .withStatistics("Average") .withStatistics("Maximum") .withDimensions(Arrays.asList(instanceDimension)); GetMetricStatisticsResult result = sort(cloudWatch.getMetricStatistics(request).getDatapoints()); if (logger.isDebugEnabled()) { if (result.getDatapoints() != null && result.getDatapoints().size() > 0) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); logger.debug("[{}:{}] {}, {} ~ {}", new Object[] {namespace, pulse.getId(), result, sdf.format(startCal.getTime()), sdf.format(endCal.getTime())}); } } return result; }
public GetMetricStatisticsResult getRequestCount(String name) { Dimension instanceDimension = new Dimension(); String ns = null; if (pulse.getType().equals("application")) { ns = "AWS/ApplicationELB"; instanceDimension.setName("LoadBalancer"); } else { ns = "AWS/ELB"; instanceDimension.setName("LoadBalancerName"); } instanceDimension.setValue(name); Calendar startCal = Calendar.getInstance(); Calendar endCal = Calendar.getInstance(); startCal.add(Calendar.MINUTE, -10); startCal.set(Calendar.SECOND, 0); GetMetricStatisticsRequest request = new GetMetricStatisticsRequest() .withStartTime(startCal.getTime()) .withEndTime(endCal.getTime()) .withNamespace(ns) .withPeriod(60) .withMetricName("RequestCount") .withStatistics("Sum") .withDimensions(Arrays.asList(instanceDimension)); GetMetricStatisticsResult result = sort(cloudWatch.getMetricStatistics(request).getDatapoints()); if (logger.isDebugEnabled()) { if (result.getDatapoints() != null && result.getDatapoints().size() > 0) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); logger.debug("[{}:{}] {}, {} ~ {}", new Object[] {namespace, pulse.getId(), result, sdf.format(startCal.getTime()), sdf.format(endCal.getTime())}); } } return result; }
private GetMetricStatisticsResult sort(List<Datapoint> resultSet) { Collections.sort(resultSet, new Comparator<Datapoint>() { @Override public int compare(Datapoint o1, Datapoint o2) { return o1.getTimestamp().compareTo(o2.getTimestamp()); } }); return new GetMetricStatisticsResult().withDatapoints(resultSet); }
@Override public void onSuccess(GetMetricStatisticsRequest request, GetMetricStatisticsResult result) { try { OperationContext.restoreOperationContext(this.opContext); List<ServiceStat> statDatapoints = new ArrayList<>(); List<Datapoint> dpList = result.getDatapoints(); if (dpList != null && dpList.size() != 0) { for (Datapoint dp : dpList) { ServiceStat stat = new ServiceStat(); stat.latestValue = dp.getAverage(); stat.unit = AWSStatsNormalizer.getNormalizedUnitValue(dp.getUnit()); stat.sourceTimeMicrosUtc = TimeUnit.MILLISECONDS .toMicros(dp.getTimestamp().getTime()); statDatapoints.add(stat); } this.statsData.statsResponse.statValues .put(AWSStatsNormalizer.getNormalizedStatKeyValue(result.getLabel()), statDatapoints); } if (this.statsData.numResponses.incrementAndGet() == this.numOfMetrics) { sendStats(this.statsData); } } catch (Exception e) { this.statsData.taskManager.patchTaskToFailure(e); } }
public void lookupAndSaveMetricData( CloudWatchMetric metric, String dimensionValue, String type ) throws CandlestackAWSException, CandlestackException { String datapointDateMapKey = getDatapointDateMapKey( metric, dimensionValue ); // Determine the last time we fetched datapoints for this metric and dimension Date lastDatapointDate = lastDatapointDateMap.get( datapointDateMapKey ); if ( lastDatapointDate == null ) { lastDatapointDate = metricsReaderWriter.readMostRecentMetricDate( type, dimensionValue, metric.getName() ); } // Build the request and execute it GetMetricStatisticsRequest request = cloudWatchRequest( metric, dimensionValue, lastDatapointDate ); GetMetricStatisticsResult result = cloudWatchClient.getMetricStatistics( request ); // Sort the datapoints in chronological order List<Datapoint> datapoints = result.getDatapoints(); datapoints.sort( new DatapointComparator() ); // Write the data points for ( Datapoint datapoint : datapoints ) { // Only care about data points that have happened after the last one if ( lastDatapointDate == null || datapoint.getTimestamp().after( lastDatapointDate ) ) { lastDatapointDate = datapoint.getTimestamp(); metricsReaderWriter.writeMetric( type, dimensionValue, datapoint.getTimestamp(), metric.getName(), metric.getStatistic().getValueFromDatapoint( datapoint ) ); } } // Update the date map lastDatapointDateMap.put( datapointDateMapKey, lastDatapointDate ); }
public static List<Datum> getMetric(String metricName, Dimension dim, String namespace, Date startTime, int period, Statistic statistic, StandardUnit unit) { connect(); GetMetricStatisticsRequest req = new GetMetricStatisticsRequest(); req.setMetricName(metricName); List<Dimension> dimensions = new ArrayList<Dimension>(); dimensions.add(dim); req.setDimensions(dimensions); req.setNamespace(namespace); req.setStartTime(startTime); req.setEndTime(new Date()); if (period % 60 != 0) period = (period / 60) * 60; req.setPeriod(period); if (unit != null) req.setUnit(unit); if (statistic == null) statistic = Statistic.Average; List<String> statistics = new ArrayList<String>(); statistics.add(statistic.name()); req.setStatistics(statistics); GetMetricStatisticsResult res = client.getMetricStatistics(req); return Datum.getAllData(res.getDatapoints(), metricName, statistic); }
@Test public void testUsesNewestDatapoint() throws Exception { new CloudWatchCollector( "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/ELB\n aws_metric_name: RequestCount", client).register(registry); Mockito.when(client.getMetricStatistics((GetMetricStatisticsRequest)argThat( new GetMetricStatisticsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount")))) .thenReturn(new GetMetricStatisticsResult().withDatapoints( new Datapoint().withTimestamp(new Date(1)).withAverage(1.0), new Datapoint().withTimestamp(new Date(3)).withAverage(3.0), new Datapoint().withTimestamp(new Date(2)).withAverage(2.0))); assertEquals(3.0, registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance"}, new String[]{"aws_elb", ""}), .01); }
@Test public void testDynamoIndexDimensions() throws Exception { new CloudWatchCollector( "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/DynamoDB\n aws_metric_name: ConsumedReadCapacityUnits\n aws_dimensions:\n - TableName\n - GlobalSecondaryIndexName\n- aws_namespace: AWS/DynamoDB\n aws_metric_name: OnlineIndexConsumedWriteCapacity\n aws_dimensions:\n - TableName\n - GlobalSecondaryIndexName\n- aws_namespace: AWS/DynamoDB\n aws_metric_name: ConsumedReadCapacityUnits\n aws_dimensions:\n - TableName", client).register(registry); Mockito.when(client.listMetrics((ListMetricsRequest)argThat( new ListMetricsRequestMatcher().Namespace("AWS/DynamoDB").MetricName("ConsumedReadCapacityUnits").Dimensions("TableName", "GlobalSecondaryIndexName")))) .thenReturn(new ListMetricsResult().withMetrics( new Metric().withDimensions(new Dimension().withName("TableName").withValue("myTable"), new Dimension().withName("GlobalSecondaryIndexName").withValue("myIndex")))); Mockito.when(client.listMetrics((ListMetricsRequest)argThat( new ListMetricsRequestMatcher().Namespace("AWS/DynamoDB").MetricName("OnlineIndexConsumedWriteCapacity").Dimensions("TableName", "GlobalSecondaryIndexName")))) .thenReturn(new ListMetricsResult().withMetrics( new Metric().withDimensions(new Dimension().withName("TableName").withValue("myTable"), new Dimension().withName("GlobalSecondaryIndexName").withValue("myIndex")))); Mockito.when(client.listMetrics((ListMetricsRequest)argThat( new ListMetricsRequestMatcher().Namespace("AWS/DynamoDB").MetricName("ConsumedReadCapacityUnits").Dimensions("TableName")))) .thenReturn(new ListMetricsResult().withMetrics( new Metric().withDimensions(new Dimension().withName("TableName").withValue("myTable")))); Mockito.when(client.getMetricStatistics((GetMetricStatisticsRequest)argThat( new GetMetricStatisticsRequestMatcher().Namespace("AWS/DynamoDB").MetricName("ConsumedReadCapacityUnits").Dimension("TableName", "myTable").Dimension("GlobalSecondaryIndexName", "myIndex")))) .thenReturn(new GetMetricStatisticsResult().withDatapoints( new Datapoint().withTimestamp(new Date()).withSum(1.0))); Mockito.when(client.getMetricStatistics((GetMetricStatisticsRequest)argThat( new GetMetricStatisticsRequestMatcher().Namespace("AWS/DynamoDB").MetricName("OnlineIndexConsumedWriteCapacity").Dimension("TableName", "myTable").Dimension("GlobalSecondaryIndexName", "myIndex")))) .thenReturn(new GetMetricStatisticsResult().withDatapoints( new Datapoint().withTimestamp(new Date()).withSum(2.0))); Mockito.when(client.getMetricStatistics((GetMetricStatisticsRequest)argThat( new GetMetricStatisticsRequestMatcher().Namespace("AWS/DynamoDB").MetricName("ConsumedReadCapacityUnits").Dimension("TableName", "myTable")))) .thenReturn(new GetMetricStatisticsResult().withDatapoints( new Datapoint().withTimestamp(new Date()).withSum(3.0))); assertEquals(1.0, registry.getSampleValue("aws_dynamodb_consumed_read_capacity_units_index_sum", new String[]{"job", "instance", "table_name", "global_secondary_index_name"}, new String[]{"aws_dynamodb", "", "myTable", "myIndex"}), .01); assertEquals(2.0, registry.getSampleValue("aws_dynamodb_online_index_consumed_write_capacity_sum", new String[]{"job", "instance", "table_name", "global_secondary_index_name"}, new String[]{"aws_dynamodb", "", "myTable", "myIndex"}), .01); assertEquals(3.0, registry.getSampleValue("aws_dynamodb_consumed_read_capacity_units_sum", new String[]{"job", "instance", "table_name"}, new String[]{"aws_dynamodb", "", "myTable"}), .01); }
public GetMetricStatisticsResult getMemoryUtilization(String name) { Dimension instanceDimension = new Dimension(); String ns = null; String metric = null; if (namespace.endsWith("EC2")) { ns = "System/Linux"; instanceDimension.setName("InstanceId"); metric = "MemoryUtilization"; } else { ns = namespace; instanceDimension.setName("DBInstanceIdentifier"); metric = "FreeableMemory"; } instanceDimension.setValue(name); Calendar startCal = Calendar.getInstance(); Calendar endCal = Calendar.getInstance(); startCal.add(Calendar.MINUTE, -10); startCal.set(Calendar.SECOND, 0); GetMetricStatisticsRequest request = new GetMetricStatisticsRequest() .withStartTime(startCal.getTime()) .withEndTime(endCal.getTime()) .withNamespace(ns) .withPeriod(60) .withMetricName(metric) .withStatistics("Average") .withStatistics("Maximum") .withDimensions(Arrays.asList(instanceDimension)); GetMetricStatisticsResult result = sort(cloudWatch.getMetricStatistics(request).getDatapoints()); if (logger.isDebugEnabled()) { if (result.getDatapoints() != null && result.getDatapoints().size() > 0) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); logger.debug("[{}:{}] {}, {} ~ {}", new Object[] {namespace, pulse.getId(), result, sdf.format(startCal.getTime()), sdf.format(endCal.getTime())}); } } return result; }
/** * Gets EC2 statistics. * * @param statsData The context object for stats. * @param metricNames The metrics names to gather stats for. * @param isAggregateStats Indicates where we are interested in aggregate stats or not. */ private void getEC2Stats(AWSStatsDataHolder statsData, String[] metricNames, boolean isAggregateStats) { if (getAWSAsyncStatsClient(statsData) == null) { return; } Long collectionPeriod = Long.getLong(AWS_COLLECTION_PERIOD_SECONDS, DEFAULT_AWS_COLLECTION_PERIOD_SECONDS); for (String metricName : metricNames) { GetMetricStatisticsRequest metricRequest = new GetMetricStatisticsRequest(); // get datapoint for the for the passed in time window. try { setRequestCollectionWindow( TimeUnit.MINUTES.toMicros(MAX_METRIC_COLLECTION_WINDOW_IN_MINUTES), statsData.statsRequest.lastCollectionTimeMicrosUtc, collectionPeriod, metricRequest); } catch (IllegalStateException e) { // no data to process. notify parent statsData.taskManager.finishTask(); return; } metricRequest.setPeriod(collectionPeriod.intValue()); metricRequest.setStatistics(Arrays.asList(STATISTICS)); metricRequest.setNamespace(NAMESPACE); // Provide instance id dimension only if it is not aggregate stats. if (!isAggregateStats) { List<Dimension> dimensions = new ArrayList<>(); Dimension dimension = new Dimension(); dimension.setName(DIMENSION_INSTANCE_ID); String instanceId = statsData.computeDesc.id; dimension.setValue(instanceId); dimensions.add(dimension); metricRequest.setDimensions(dimensions); } metricRequest.setMetricName(metricName); logFine(() -> String.format("Retrieving %s metric from AWS", metricName)); AsyncHandler<GetMetricStatisticsRequest, GetMetricStatisticsResult> resultHandler = new AWSStatsHandler( statsData, metricNames.length, isAggregateStats); statsData.statsClient.getMetricStatisticsAsync(metricRequest, resultHandler); } }
private void getBillingStats(AWSStatsDataHolder statsData) { if (getAWSAsyncBillingClient(statsData) == null) { return; } Dimension dimension = new Dimension(); dimension.setName(DIMENSION_CURRENCY); dimension.setValue(DIMENSION_CURRENCY_VALUE); GetMetricStatisticsRequest request = new GetMetricStatisticsRequest(); // AWS pushes billing metrics every 4 hours. However the timeseries returned does not have // static time stamps associated with the data points. The timestamps range from // (currentTime - 4 hrs) and are spaced at 4 hrs. // Get all 14 days worth of estimated charges data by default when last collection time is not set. // Otherwise set the window to lastCollectionTime - 4 hrs. Long lastCollectionTimeForEstimatedCharges = null; Long collectionPeriod = Long.getLong(AWS_COLLECTION_PERIOD_SECONDS, DEFAULT_AWS_COLLECTION_PERIOD_SECONDS); if (statsData.statsRequest.lastCollectionTimeMicrosUtc != null) { lastCollectionTimeForEstimatedCharges = statsData.statsRequest.lastCollectionTimeMicrosUtc - TimeUnit.HOURS.toMicros(COST_COLLECTION_PERIOD_IN_HOURS); } // defaulting to fetch 14 days of estimated charges data try { setRequestCollectionWindow( TimeUnit.DAYS.toMicros(COST_COLLECTION_WINDOW_IN_DAYS), lastCollectionTimeForEstimatedCharges, collectionPeriod, request); } catch (IllegalStateException e) { // no data to process. notify parent statsData.taskManager.finishTask(); return; } request.setPeriod(COST_COLLECTION_PERIOD_IN_SECONDS); request.setStatistics(Arrays.asList(STATISTICS)); request.setNamespace(BILLING_NAMESPACE); request.setDimensions(Collections.singletonList(dimension)); request.setMetricName(AWSConstants.ESTIMATED_CHARGES); logFine(() -> String.format("Retrieving %s metric from AWS", AWSConstants.ESTIMATED_CHARGES)); AsyncHandler<GetMetricStatisticsRequest, GetMetricStatisticsResult> resultHandler = new AWSBillingStatsHandler( statsData, lastCollectionTimeForEstimatedCharges); statsData.billingClient.getMetricStatisticsAsync(request, resultHandler); }
@Override public void onSuccess(GetMetricStatisticsRequest request, GetMetricStatisticsResult result) { try { OperationContext.restoreOperationContext(this.opContext); List<Datapoint> dpList = result.getDatapoints(); // Sort the data points in increasing order of timestamp to calculate Burn rate Collections .sort(dpList, (o1, o2) -> o1.getTimestamp().compareTo(o2.getTimestamp())); List<ServiceStat> estimatedChargesDatapoints = new ArrayList<>(); if (dpList != null && dpList.size() != 0) { for (Datapoint dp : dpList) { // If the datapoint collected is older than the last collection time, skip it. if (this.lastCollectionTimeMicrosUtc != null && TimeUnit.MILLISECONDS.toMicros(dp.getTimestamp() .getTime()) <= this.lastCollectionTimeMicrosUtc) { continue; } // If there is no lastCollectionTime or the datapoint collected in newer // than the lastCollectionTime, push it. ServiceStat stat = new ServiceStat(); stat.latestValue = dp.getAverage(); stat.unit = AWSStatsNormalizer .getNormalizedUnitValue(DIMENSION_CURRENCY_VALUE); stat.sourceTimeMicrosUtc = TimeUnit.MILLISECONDS .toMicros(dp.getTimestamp().getTime()); estimatedChargesDatapoints.add(stat); } this.statsData.statsResponse.statValues.put( AWSStatsNormalizer.getNormalizedStatKeyValue(result.getLabel()), estimatedChargesDatapoints); // Calculate average burn rate only if there is more than 1 datapoint available. // This will ensure that NaN errors will not occur. if (dpList.size() > 1) { ServiceStat averageBurnRate = new ServiceStat(); averageBurnRate.latestValue = AWSUtils.calculateAverageBurnRate(dpList); averageBurnRate.unit = AWSStatsNormalizer .getNormalizedUnitValue(DIMENSION_CURRENCY_VALUE); averageBurnRate.sourceTimeMicrosUtc = Utils.getSystemNowMicrosUtc(); this.statsData.statsResponse.statValues.put( AWSStatsNormalizer .getNormalizedStatKeyValue(AWSConstants.AVERAGE_BURN_RATE), Collections.singletonList(averageBurnRate)); } // Calculate current burn rate only if there is more than 1 day worth of data available. if (dpList.size() > NUM_OF_COST_DATAPOINTS_IN_A_DAY) { ServiceStat currentBurnRate = new ServiceStat(); currentBurnRate.latestValue = AWSUtils.calculateCurrentBurnRate(dpList); currentBurnRate.unit = AWSStatsNormalizer .getNormalizedUnitValue(DIMENSION_CURRENCY_VALUE); currentBurnRate.sourceTimeMicrosUtc = Utils.getSystemNowMicrosUtc(); this.statsData.statsResponse.statValues.put( AWSStatsNormalizer .getNormalizedStatKeyValue(AWSConstants.CURRENT_BURN_RATE), Collections.singletonList(currentBurnRate)); } } sendStats(this.statsData); } catch (Exception e) { this.statsData.taskManager.patchTaskToFailure(e); } }
/** * Retrieves the total number of requests that were made to the load * balancer during the given time interval in the past * * @param loadBalancerName * @param region * @param timeInterval in seconds which must be multiple of 60 * @return number of requests made */ public int getRequestCount(String loadBalancerName, String region, int timeInterval) { int count = 0; try { GetMetricStatisticsRequest request = new GetMetricStatisticsRequest(); request.setMetricName(Constants.REQUEST_COUNT_METRIC_NAME); request.setNamespace(Constants.CLOUD_WATCH_NAMESPACE_NAME); Date currentTime = new DateTime(DateTimeZone.UTC).toDate(); Date pastTime = new DateTime(DateTimeZone.UTC).minusSeconds( timeInterval).toDate(); request.setStartTime(pastTime); request.setEndTime(currentTime); request.setPeriod(timeInterval); HashSet<String> statistics = new HashSet<String>(); statistics.add(Constants.SUM_STATISTICS_NAME); request.setStatistics(statistics); HashSet<Dimension> dimensions = new HashSet<Dimension>(); Dimension loadBalancerDimension = new Dimension(); loadBalancerDimension .setName(Constants.LOAD_BALANCER_DIMENTION_NAME); loadBalancerDimension.setValue(loadBalancerName); dimensions.add(loadBalancerDimension); request.setDimensions(dimensions); cloudWatchClient.setEndpoint(String.format( Constants.CLOUD_WATCH_ENDPOINT_URL_FORMAT, region)); GetMetricStatisticsResult result = cloudWatchClient .getMetricStatistics(request); List<Datapoint> dataPoints = result.getDatapoints(); if (dataPoints != null && dataPoints.size() > 0) { count = dataPoints.get(0).getSum().intValue(); } } catch (AmazonClientException e) { log.error( "Could not get request count statistics of load balancer " + loadBalancerName, e); } return count; }
/** * Retrieves the number of responses generated for a particular response * code like 2XX, 3XX, 4XX, 5XX * * @param loadBalancerName * @param region * @param metricName which is one among HTTPCode_Backend_2XX or * HTTPCode_Backend_3XX or HTTPCode_Backend_4XX or * HTTPCode_Backend_5XX * @param startTime of the window to be scanned * @param endTime of the window to be scanned * @param timeInterval in seconds * @return number for response for this metric */ public int getResponseCountForMetric(String loadBalancerName, String region, String metricName, Date startTime, Date endTime, int timeInterval) { int count = 0; try { GetMetricStatisticsRequest request = new GetMetricStatisticsRequest(); request.setMetricName(metricName); request.setNamespace(Constants.CLOUD_WATCH_NAMESPACE_NAME); request.setStartTime(startTime); request.setEndTime(endTime); request.setPeriod(timeInterval); HashSet<String> statistics = new HashSet<String>(); statistics.add(Constants.SUM_STATISTICS_NAME); request.setStatistics(statistics); HashSet<Dimension> dimensions = new HashSet<Dimension>(); Dimension loadBalancerDimension = new Dimension(); loadBalancerDimension .setName(Constants.LOAD_BALANCER_DIMENTION_NAME); loadBalancerDimension.setValue(loadBalancerName); dimensions.add(loadBalancerDimension); request.setDimensions(dimensions); cloudWatchClient.setEndpoint(String.format( Constants.CLOUD_WATCH_ENDPOINT_URL_FORMAT, region)); GetMetricStatisticsResult result = cloudWatchClient .getMetricStatistics(request); List<Datapoint> dataPoints = result.getDatapoints(); if (dataPoints != null && dataPoints.size() > 0) { count = dataPoints.get(0).getSum().intValue(); } } catch (AmazonClientException e) { log.error("Could not get the statistics for metric " + metricName + " of load balancer " + loadBalancerName, e); } return count; }