Search in sources :

Example 1 with GetMetricStatisticsRequest

use of com.amazonaws.services.cloudwatch.model.GetMetricStatisticsRequest in project photon-model by vmware.

the class AWSStatsService method getBillingStats.

private void getBillingStats(AWSStatsDataHolder statsData) {
    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);
}
Also used : GetMetricStatisticsRequest(com.amazonaws.services.cloudwatch.model.GetMetricStatisticsRequest) GetMetricStatisticsResult(com.amazonaws.services.cloudwatch.model.GetMetricStatisticsResult) Dimension(com.amazonaws.services.cloudwatch.model.Dimension)

Example 2 with GetMetricStatisticsRequest

use of com.amazonaws.services.cloudwatch.model.GetMetricStatisticsRequest in project photon-model by vmware.

the class AWSStatsService method getEC2Stats.

/**
 * 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) {
    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);
    }
}
Also used : GetMetricStatisticsRequest(com.amazonaws.services.cloudwatch.model.GetMetricStatisticsRequest) ArrayList(java.util.ArrayList) GetMetricStatisticsResult(com.amazonaws.services.cloudwatch.model.GetMetricStatisticsResult) Dimension(com.amazonaws.services.cloudwatch.model.Dimension)

Example 3 with GetMetricStatisticsRequest

use of com.amazonaws.services.cloudwatch.model.GetMetricStatisticsRequest in project beam by apache.

the class SimplifiedKinesisClient method getBacklogBytes.

/**
 * 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(() -> {
        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;
    });
}
Also used : Datapoint(com.amazonaws.services.cloudwatch.model.Datapoint) GetMetricStatisticsRequest(com.amazonaws.services.cloudwatch.model.GetMetricStatisticsRequest) GetMetricStatisticsResult(com.amazonaws.services.cloudwatch.model.GetMetricStatisticsResult) Minutes(org.joda.time.Minutes)

Example 4 with GetMetricStatisticsRequest

use of com.amazonaws.services.cloudwatch.model.GetMetricStatisticsRequest in project beam by apache.

the class SimplifiedKinesisClientTest method shouldCountBytesWhenMultipleDataPointsReturned.

@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));
    when(cloudWatch.getMetricStatistics(metricStatisticsRequest)).thenReturn(result);
    long backlogBytes = underTest.getBacklogBytes(STREAM, countSince, countTo);
    assertThat(backlogBytes).isEqualTo(6L);
}
Also used : Datapoint(com.amazonaws.services.cloudwatch.model.Datapoint) GetMetricStatisticsRequest(com.amazonaws.services.cloudwatch.model.GetMetricStatisticsRequest) Instant(org.joda.time.Instant) GetMetricStatisticsResult(com.amazonaws.services.cloudwatch.model.GetMetricStatisticsResult) Minutes(org.joda.time.Minutes) Test(org.junit.Test)

Example 5 with GetMetricStatisticsRequest

use of com.amazonaws.services.cloudwatch.model.GetMetricStatisticsRequest in project beam by apache.

the class SimplifiedKinesisClientTest method shouldHandleGetBacklogBytesError.

private void shouldHandleGetBacklogBytesError(Exception thrownException, Class<? extends Exception> expectedExceptionClass) {
    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);
    when(cloudWatch.getMetricStatistics(metricStatisticsRequest)).thenThrow(thrownException);
    try {
        underTest.getBacklogBytes(STREAM, countSince, countTo);
        failBecauseExceptionWasNotThrown(expectedExceptionClass);
    } catch (Exception e) {
        assertThat(e).isExactlyInstanceOf(expectedExceptionClass);
    } finally {
        reset(kinesis);
    }
}
Also used : GetMetricStatisticsRequest(com.amazonaws.services.cloudwatch.model.GetMetricStatisticsRequest) Instant(org.joda.time.Instant) Minutes(org.joda.time.Minutes) ProvisionedThroughputExceededException(com.amazonaws.services.kinesis.model.ProvisionedThroughputExceededException) AmazonServiceException(com.amazonaws.AmazonServiceException) LimitExceededException(com.amazonaws.services.kinesis.model.LimitExceededException) ExpiredIteratorException(com.amazonaws.services.kinesis.model.ExpiredIteratorException)

Aggregations

GetMetricStatisticsRequest (com.amazonaws.services.cloudwatch.model.GetMetricStatisticsRequest)6 GetMetricStatisticsResult (com.amazonaws.services.cloudwatch.model.GetMetricStatisticsResult)5 Minutes (org.joda.time.Minutes)4 Datapoint (com.amazonaws.services.cloudwatch.model.Datapoint)3 Instant (org.joda.time.Instant)3 Dimension (com.amazonaws.services.cloudwatch.model.Dimension)2 Test (org.junit.Test)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 ExpiredIteratorException (com.amazonaws.services.kinesis.model.ExpiredIteratorException)1 LimitExceededException (com.amazonaws.services.kinesis.model.LimitExceededException)1 ProvisionedThroughputExceededException (com.amazonaws.services.kinesis.model.ProvisionedThroughputExceededException)1 ArrayList (java.util.ArrayList)1