Allow user to specify the Unit on CloudWatch queries

This commit is contained in:
andreisavu 2011-12-22 17:30:33 +02:00
parent aef44b26b9
commit b5228d2299
12 changed files with 170 additions and 23 deletions

View File

@ -27,6 +27,7 @@ import javax.ws.rs.Path;
import org.jclouds.aws.filters.FormSigner; import org.jclouds.aws.filters.FormSigner;
import org.jclouds.cloudwatch.domain.Datapoint; import org.jclouds.cloudwatch.domain.Datapoint;
import org.jclouds.cloudwatch.domain.Statistics;
import org.jclouds.cloudwatch.functions.ISO8601Format; import org.jclouds.cloudwatch.functions.ISO8601Format;
import org.jclouds.cloudwatch.options.GetMetricStatisticsOptions; import org.jclouds.cloudwatch.options.GetMetricStatisticsOptions;
import org.jclouds.cloudwatch.xml.GetMetricStatisticsResponseHandler; import org.jclouds.cloudwatch.xml.GetMetricStatisticsResponseHandler;
@ -70,6 +71,6 @@ public interface CloudWatchAsyncClient {
@FormParam("StartTime") @ParamParser(ISO8601Format.class) Date startTime, @FormParam("StartTime") @ParamParser(ISO8601Format.class) Date startTime,
@FormParam("EndTime") @ParamParser(ISO8601Format.class) Date endTime, @FormParam("EndTime") @ParamParser(ISO8601Format.class) Date endTime,
@FormParam("Period") int period, @FormParam("Period") int period,
@FormParam("Statistics.member.1") String statistics, @FormParam("Statistics.member.1") Statistics statistics,
GetMetricStatisticsOptions... options); GetMetricStatisticsOptions... options);
} }

View File

@ -23,6 +23,7 @@ import java.util.Set;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.jclouds.cloudwatch.domain.Datapoint; import org.jclouds.cloudwatch.domain.Datapoint;
import org.jclouds.cloudwatch.domain.Statistics;
import org.jclouds.cloudwatch.options.GetMetricStatisticsOptions; import org.jclouds.cloudwatch.options.GetMetricStatisticsOptions;
import org.jclouds.concurrent.Timeout; import org.jclouds.concurrent.Timeout;
import org.jclouds.javax.annotation.Nullable; import org.jclouds.javax.annotation.Nullable;
@ -77,6 +78,6 @@ public interface CloudWatchClient {
* more filtering options (e.g. instance ID) * more filtering options (e.g. instance ID)
*/ */
Set<Datapoint> getMetricStatisticsInRegion(@Nullable String region, String metricName, String namespace, Set<Datapoint> getMetricStatisticsInRegion(@Nullable String region, String metricName, String namespace,
Date startTime, Date endTime, int period, String statistics, GetMetricStatisticsOptions... options); Date startTime, Date endTime, int period, Statistics statistics, GetMetricStatisticsOptions... options);
} }

View File

@ -36,11 +36,11 @@ public class Datapoint {
private final Date timestamp; private final Date timestamp;
private final Double samples; private final Double samples;
private final Double sum; private final Double sum;
private final StandardUnit unit; private final Unit unit;
private final String customUnit; private final String customUnit;
public Datapoint(@Nullable Double average, @Nullable Double maximum, @Nullable Double minimum, public Datapoint(@Nullable Double average, @Nullable Double maximum, @Nullable Double minimum,
@Nullable Date timestamp, @Nullable Double samples, @Nullable Double sum, @Nullable StandardUnit unit, @Nullable Date timestamp, @Nullable Double samples, @Nullable Double sum, @Nullable Unit unit,
@Nullable String customUnit) { @Nullable String customUnit) {
this.average = average; this.average = average;
this.maximum = maximum; this.maximum = maximum;
@ -104,7 +104,7 @@ public class Datapoint {
* return Standard unit used for the datapoint. * return Standard unit used for the datapoint.
*/ */
@Nullable @Nullable
public StandardUnit getUnit() { public Unit getUnit() {
return unit; return unit;
} }

View File

@ -0,0 +1,38 @@
package org.jclouds.cloudwatch.domain;
import com.google.common.base.CaseFormat;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* @see <a href="http://docs.amazonwebservices.com/AmazonCloudWatch/latest/APIReference/API_GetMetricStatistics.html?r=5424"/>
*
* @author Andrei Savu
*/
public enum Statistics {
AVERAGE,
SUM,
SAMPLE_COUNT,
MAXIMUM,
MINIMUM,
UNRECOGNIZED;
public String value() {
return (CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, name()));
}
@Override
public String toString() {
return value();
}
public static Statistics fromValue(String value) {
try {
return valueOf(CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, checkNotNull(value, "value")));
} catch (IllegalArgumentException e) {
return UNRECOGNIZED;
}
}
}

View File

@ -23,15 +23,42 @@ import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.base.CaseFormat; import com.google.common.base.CaseFormat;
/** /**
* @see <a href="http://docs.amazonwebservices.com/AmazonCloudWatch/latest/APIReference/API_GetMetricStatistics.html?r=5424"/>
* *
* * @author Adrian Cole, Andrei Savu
* @author Adrian Cole
*/ */
public enum StandardUnit { public enum Unit {
SECONDS, PERCENT, BYTES, BITS, COUNT, BITS_PER_SECOND, COUNT_PER_SECOND, NONE, UNRECOGNIZED; SECONDS,
MICROSECONDS,
MILLISECONDS,
BYTES,
KILOBYTES,
MEGABYTES,
GIGABYTES,
TERABYTES,
BITS,
KILOBITS,
MEGABITS,
GIGABITS,
TERABITS,
PERCENT,
COUNT,
BYTES_PER_SECOND,
KILOBYTES_PER_SECOND,
MEGABYTES_PER_SECOND,
GIGABYTES_PER_SECOND,
TERABYTES_PER_SECOND,
BITS_PER_SECOND,
KILOBITS_PER_SECOND,
MEGABITS_PER_SECOND,
GIGABITS_PER_SECOND,
TERABITS_PER_SECOND,
COUNT_PER_SECOND,
NONE,
UNRECOGNIZED;
public String value() { public String value() {
return (CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, name().replace("_PER_", "/"))); return (CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, name()).replace("Per", "/"));
} }
@Override @Override
@ -39,7 +66,7 @@ public enum StandardUnit {
return value(); return value();
} }
public static StandardUnit fromValue(String state) { public static Unit fromValue(String state) {
try { try {
return valueOf(CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, checkNotNull(state, "state").replace( return valueOf(CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, checkNotNull(state, "state").replace(
"/", "_PER_"))); "/", "_PER_")));

View File

@ -19,6 +19,7 @@
package org.jclouds.cloudwatch.options; package org.jclouds.cloudwatch.options;
import org.jclouds.aws.util.AWSUtils; import org.jclouds.aws.util.AWSUtils;
import org.jclouds.cloudwatch.domain.Unit;
import org.jclouds.http.options.BaseHttpRequestOptions; import org.jclouds.http.options.BaseHttpRequestOptions;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
@ -46,6 +47,15 @@ public class GetMetricStatisticsOptions extends BaseHttpRequestOptions {
return this; return this;
} }
/**
* @param unit
* the unit of the metric
*/
public GetMetricStatisticsOptions unit(Unit unit) {
this.formParameters.put("Unit", unit.toString());
return this;
}
public static class Builder { public static class Builder {
/** /**
@ -55,5 +65,13 @@ public class GetMetricStatisticsOptions extends BaseHttpRequestOptions {
GetMetricStatisticsOptions options = new GetMetricStatisticsOptions(); GetMetricStatisticsOptions options = new GetMetricStatisticsOptions();
return options.instanceId(instanceId); return options.instanceId(instanceId);
} }
/**
* @see GetMetricStatisticsOptions#unit
*/
public static GetMetricStatisticsOptions unit(Unit unit) {
GetMetricStatisticsOptions options = new GetMetricStatisticsOptions();
return options.unit(unit);
}
} }
} }

View File

@ -23,7 +23,7 @@ import java.util.Date;
import javax.inject.Inject; import javax.inject.Inject;
import org.jclouds.cloudwatch.domain.Datapoint; import org.jclouds.cloudwatch.domain.Datapoint;
import org.jclouds.cloudwatch.domain.StandardUnit; import org.jclouds.cloudwatch.domain.Unit;
import org.jclouds.date.DateService; import org.jclouds.date.DateService;
import org.jclouds.http.functions.ParseSax; import org.jclouds.http.functions.ParseSax;
@ -41,7 +41,7 @@ public class DatapointHandler extends ParseSax.HandlerForGeneratedRequestWithRes
private Date timestamp; private Date timestamp;
private Double samples; private Double samples;
private Double sum; private Double sum;
private StandardUnit unit; private Unit unit;
private String customUnit; private String customUnit;
@Inject @Inject
@ -76,7 +76,7 @@ public class DatapointHandler extends ParseSax.HandlerForGeneratedRequestWithRes
} else if (qName.equals("Sum")) { } else if (qName.equals("Sum")) {
sum = doubleOrNull(); sum = doubleOrNull();
} else if (qName.equals("Unit")) { } else if (qName.equals("Unit")) {
unit = StandardUnit.fromValue(currentText.toString().trim()); unit = Unit.fromValue(currentText.toString().trim());
} else if (qName.equals("CustomUnit")) { } else if (qName.equals("CustomUnit")) {
customUnit = currentText.toString().trim(); customUnit = currentText.toString().trim();
} }

View File

@ -33,6 +33,7 @@ import org.jclouds.Constants;
import org.jclouds.aws.domain.Region; import org.jclouds.aws.domain.Region;
import org.jclouds.aws.filters.FormSigner; import org.jclouds.aws.filters.FormSigner;
import org.jclouds.cloudwatch.config.CloudWatchRestClientModule; import org.jclouds.cloudwatch.config.CloudWatchRestClientModule;
import org.jclouds.cloudwatch.domain.Statistics;
import org.jclouds.cloudwatch.options.GetMetricStatisticsOptions; import org.jclouds.cloudwatch.options.GetMetricStatisticsOptions;
import org.jclouds.cloudwatch.xml.GetMetricStatisticsResponseHandler; import org.jclouds.cloudwatch.xml.GetMetricStatisticsResponseHandler;
import org.jclouds.date.DateService; import org.jclouds.date.DateService;
@ -63,9 +64,9 @@ public class CloudWatchAsyncClientTest extends RestClientTest<CloudWatchAsyncCli
public void testRegisterInstancesWithMeasure() throws SecurityException, NoSuchMethodException, IOException { public void testRegisterInstancesWithMeasure() throws SecurityException, NoSuchMethodException, IOException {
Date date = new Date(10000000l); Date date = new Date(10000000l);
Method method = CloudWatchAsyncClient.class.getMethod("getMetricStatisticsInRegion", String.class, String.class, Method method = CloudWatchAsyncClient.class.getMethod("getMetricStatisticsInRegion", String.class, String.class,
String.class, Date.class, Date.class, int.class, String.class, GetMetricStatisticsOptions[].class); String.class, Date.class, Date.class, int.class, Statistics.class, GetMetricStatisticsOptions[].class);
HttpRequest request = processor.createRequest(method, (String) null, "CPUUtilization", "AWS/EC2", date, date, 60, "Average", HttpRequest request = processor.createRequest(method, (String) null, "CPUUtilization", "AWS/EC2", date, date, 60,
GetMetricStatisticsOptions.Builder.instanceId("i-12312313")); Statistics.AVERAGE, GetMetricStatisticsOptions.Builder.instanceId("i-12312313"));
assertRequestLineEquals(request, "POST https://monitoring.us-east-1.amazonaws.com/ HTTP/1.1"); assertRequestLineEquals(request, "POST https://monitoring.us-east-1.amazonaws.com/ HTTP/1.1");
assertNonPayloadHeadersEqual(request, "Host: monitoring.us-east-1.amazonaws.com\n"); assertNonPayloadHeadersEqual(request, "Host: monitoring.us-east-1.amazonaws.com\n");

View File

@ -27,6 +27,7 @@ import java.util.Set;
import org.jclouds.Constants; import org.jclouds.Constants;
import org.jclouds.cloudwatch.domain.Datapoint; import org.jclouds.cloudwatch.domain.Datapoint;
import org.jclouds.cloudwatch.domain.Statistics;
import org.jclouds.logging.log4j.config.Log4JLoggingModule; import org.jclouds.logging.log4j.config.Log4JLoggingModule;
import org.jclouds.rest.RestContext; import org.jclouds.rest.RestContext;
import org.jclouds.rest.RestContextFactory; import org.jclouds.rest.RestContextFactory;
@ -93,7 +94,7 @@ public class CloudWatchClientLiveTest {
cal.add(Calendar.MINUTE, -60 * 24 * 3); // 3 days cal.add(Calendar.MINUTE, -60 * 24 * 3); // 3 days
Set<Datapoint> datapoints = client.getMetricStatisticsInRegion( Set<Datapoint> datapoints = client.getMetricStatisticsInRegion(
region, "CPUUtilization", "AWS/EC2", cal.getTime(), new Date(), 180, "Average"); region, "CPUUtilization", "AWS/EC2", cal.getTime(), new Date(), 180, Statistics.AVERAGE);
return checkNotNull(datapoints, "Got null response for EC2 datapoints in region "); return checkNotNull(datapoints, "Got null response for EC2 datapoints in region ");
} }

View File

@ -0,0 +1,58 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.cloudwatch.options;
import com.google.common.collect.ImmutableSet;
import org.jclouds.cloudwatch.domain.Unit;
import org.testng.annotations.Test;
import static org.jclouds.cloudwatch.options.GetMetricStatisticsOptions.Builder.instanceId;
import static org.jclouds.cloudwatch.options.GetMetricStatisticsOptions.Builder.unit;
import static org.testng.Assert.assertEquals;
/**
* Tests behavior of {@code GetMetricStatisticsOptions}
*
* @author Andrei Savu
*/
@Test(groups = "unit")
public class GetMetricStatisticsOptionsTest {
public void testInstanceId() {
GetMetricStatisticsOptions options = new GetMetricStatisticsOptions().instanceId("us-east-1/i-123");
assertEquals(ImmutableSet.of("InstanceId"), options.buildFormParameters().get("Dimensions.member.1.Name"));
assertEquals(ImmutableSet.of("i-123"), options.buildFormParameters().get("Dimensions.member.1.Value"));
}
public void testInstanceIdStatic() {
GetMetricStatisticsOptions options = instanceId("us-east-1/i-123");
assertEquals(ImmutableSet.of("InstanceId"), options.buildFormParameters().get("Dimensions.member.1.Name"));
assertEquals(ImmutableSet.of("i-123"), options.buildFormParameters().get("Dimensions.member.1.Value"));
}
public void testUnit() {
GetMetricStatisticsOptions options = new GetMetricStatisticsOptions().unit(Unit.GIGABYTES_PER_SECOND);
assertEquals(ImmutableSet.of("Gigabytes/Second"), options.buildFormParameters().get("Unit"));
}
public void testUnitStatic() {
GetMetricStatisticsOptions options = unit(Unit.GIGABYTES_PER_SECOND);
assertEquals(ImmutableSet.of("Gigabytes/Second"), options.buildFormParameters().get("Unit"));
}
}

View File

@ -24,7 +24,7 @@ import java.io.InputStream;
import java.util.Set; import java.util.Set;
import org.jclouds.cloudwatch.domain.Datapoint; import org.jclouds.cloudwatch.domain.Datapoint;
import org.jclouds.cloudwatch.domain.StandardUnit; import org.jclouds.cloudwatch.domain.Unit;
import org.jclouds.date.DateService; import org.jclouds.date.DateService;
import org.jclouds.http.functions.BaseHandlerTest; import org.jclouds.http.functions.BaseHandlerTest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -44,9 +44,9 @@ public class GetMetricStatisticsResponseHandlerTest extends BaseHandlerTest {
InputStream is = getClass().getResourceAsStream("/get_metric_statistics.xml"); InputStream is = getClass().getResourceAsStream("/get_metric_statistics.xml");
Set<Datapoint> expected = ImmutableSet.of(new Datapoint(0.17777777777777778, null, null, dateService Set<Datapoint> expected = ImmutableSet.of(new Datapoint(0.17777777777777778, null, null, dateService
.iso8601SecondsDateParse("2009-01-16T00:00:00Z"), 9.0, null, StandardUnit.PERCENT, null), new Datapoint( .iso8601SecondsDateParse("2009-01-16T00:00:00Z"), 9.0, null, Unit.PERCENT, null), new Datapoint(
0.1, null, null, dateService.iso8601SecondsDateParse("2009-01-16T00:01:00Z"), 8.0, null, 0.1, null, null, dateService.iso8601SecondsDateParse("2009-01-16T00:01:00Z"), 8.0, null,
StandardUnit.PERCENT, null)); Unit.PERCENT, null));
GetMetricStatisticsResponseHandler handler = injector.getInstance(GetMetricStatisticsResponseHandler.class); GetMetricStatisticsResponseHandler handler = injector.getInstance(GetMetricStatisticsResponseHandler.class);
Set<Datapoint> result = factory.create(handler).parse(is); Set<Datapoint> result = factory.create(handler).parse(is);

View File

@ -30,6 +30,8 @@ import org.jclouds.aws.ec2.services.AWSSecurityGroupClient;
import org.jclouds.cloudwatch.CloudWatchAsyncClient; import org.jclouds.cloudwatch.CloudWatchAsyncClient;
import org.jclouds.cloudwatch.CloudWatchClient; import org.jclouds.cloudwatch.CloudWatchClient;
import org.jclouds.cloudwatch.domain.Datapoint; import org.jclouds.cloudwatch.domain.Datapoint;
import org.jclouds.cloudwatch.domain.Statistics;
import org.jclouds.cloudwatch.domain.Unit;
import org.jclouds.cloudwatch.options.GetMetricStatisticsOptions; import org.jclouds.cloudwatch.options.GetMetricStatisticsOptions;
import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.Template; import org.jclouds.compute.domain.Template;
@ -156,8 +158,8 @@ public class AWSEC2ComputeServiceLiveTest extends EC2ComputeServiceLiveTest {
try { try {
Set<Datapoint> datapoints = monitoringContext.getApi().getMetricStatisticsInRegion(instance.getRegion(), Set<Datapoint> datapoints = monitoringContext.getApi().getMetricStatisticsInRegion(instance.getRegion(),
"CPUUtilization", "AWS/EC2", before, new Date(), 60, "Average", "CPUUtilization", "AWS/EC2", before, new Date(), 60, Statistics.AVERAGE,
GetMetricStatisticsOptions.Builder.instanceId(instance.getId())); GetMetricStatisticsOptions.Builder.instanceId(instance.getId()).unit(Unit.PERCENT));
assert datapoints != null && datapoints.size() > 0; assert datapoints != null && datapoints.size() > 0;
} finally { } finally {