0
votes

I am currently trying to create a Java application that can call and reference the Amazon AWS API through the AWS Java SDK. I have been able to make calls directly to services like S3 and EC2 but when I try to pull data from Cloudwatch, I am unable to get any datapoints.

I have tried adjusting different variables (Dimensions, adjusting the time variables) and I have tried to pull the data through the CLI. When I request the data through the CLI, I AM able to get datapoints but the Java app does not get the same data. Here is my CLI code:

aws cloudwatch get-metric-statistics --metric-name BucketSizeBytes --namespace "AWS/S3" --start-time 2019-06-21T00:00:00Z --end-time 2019-06-22T00:00:00Z --period 3600 --statistics Average --unit Bytes --output json --region us-east-1 --dimensions Name=BucketName,Value=XXXXX Name=StorageType,Value=StandardStorage

Here is what I am using on the Java side. The variable namespace is equal to the string "AWS/S3" and the variable region is set to Region.US_EAST_1

Setting up CloudWatch Client

 private CloudWatchClient cwClient = CloudWatchClient.builder().region(region).build();

Calling the Data

public S3 individualS3BucketSize(S3 s3) {
        Instant now = Instant.now();
        Dimension dimensions = Dimension.builder().name("BucketName").value("XXXXX").name("StorageType").value("StandardStorage").build();

        GetMetricStatisticsRequest request = GetMetricStatisticsRequest.builder().namespace(namespace).metricName("BucketSizeBytes")
            .statistics(Statistic.AVERAGE)
            .startTime(now.minus(Duration.ofDays(1))).endTime(now).period(3600)
            .dimensions(dimensions)
            .build();

        GetMetricStatisticsResponse response;

        response = cwClient.getMetricStatistics(request);
        System.out.println(response.toString());
}

When the method is called and the print method is run, I get:

GetMetricStatisticsResponse(Label=BucketSizeBytes, Datapoints=[])

Any thoughts as to why it is coming back blank in the Java app but not the CLI?

1

1 Answers

0
votes

The problem with the above code lies within the time piece. This specific call (BucketSizeBytes) to Cloudwatch does not return data unless it is in a 1d window of time due to the reporting time of this specific metric. If you go onto the web dashboard for CloudWatch, no data will be pulled unless the time range is set to 1d.

Since the above code had the start and end time within 24 hours of each other, no data points were going to appear. I have revised the code for slight readability improvements and correct functionality.

public S3 individualS3BucketSize(S3 s3) {
        Instant now = Instant.now();
        Instant earlier = now.minusSeconds(259201); //3 Days in the past in seconds
        Statistic stat = Statistic.AVERAGE;
        GetMetricStatisticsResponse response;

        Dimension dimensionsName = Dimension.builder().name("BucketName").value(XXXXX).build();
        Dimension dimensionsStorage = Dimension.builder().name("StorageType").value("StandardStorage").build();

        Collection<Dimension> dimensions = new ArrayList<>();
        dimensions.add(dimensionsName);
        dimensions.add(dimensionsStorage);

        GetMetricStatisticsRequest request = GetMetricStatisticsRequest.builder().namespace(namespace).metricName("BucketSizeBytes")
            .dimensions(dimensions)
            .startTime(earlier).endTime(now).period(3600)
            .unit("Bytes").statistics(stat).build();

        response = cwClient.getMetricStatistics(request);
        System.out.println(response.toString());
        return null;
    }