Implement PutMetricData for CloudWatch.

* Added support to create custom metrics in CloudWatch
* Fixed bug in Datapoint parsing that caused Sample(Count) to be null
This commit is contained in:
Jeremy Whitlock 2012-05-25 13:39:14 -06:00
parent 40bd08ee6b
commit 9f1ca865e8
14 changed files with 1028 additions and 55 deletions

View File

@ -18,10 +18,7 @@
*/
package org.jclouds.cloudwatch;
import java.util.Date;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import com.google.inject.Provides;
import org.jclouds.cloudwatch.domain.Datapoint;
import org.jclouds.cloudwatch.domain.Statistics;
import org.jclouds.cloudwatch.features.MetricClient;
@ -33,7 +30,9 @@ import org.jclouds.location.functions.RegionToEndpointOrProviderIfNull;
import org.jclouds.rest.annotations.Delegate;
import org.jclouds.rest.annotations.EndpointParam;
import com.google.inject.Provides;
import java.util.Date;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* Provides access to Amazon CloudWatch via the Query API
@ -90,7 +89,7 @@ public interface CloudWatchClient {
* The statistics to be returned for the given metric. ex. Average
* @param options
* more filtering options (e.g. instance ID)
* @see MetricsClient#getMetricStatistics
* @see MetricClient#getMetricStatistics(org.jclouds.cloudwatch.domain.GetMetricStatistics)
*/
@Deprecated
Set<Datapoint> getMetricStatisticsInRegion(@Nullable String region, String metricName, String namespace,

View File

@ -18,8 +18,8 @@
*/
package org.jclouds.cloudwatch.binders;
import javax.inject.Inject;
import com.google.common.annotations.Beta;
import com.google.common.collect.ImmutableMultimap;
import org.jclouds.cloudwatch.domain.Dimension;
import org.jclouds.cloudwatch.domain.GetMetricStatistics;
import org.jclouds.cloudwatch.domain.Statistics;
@ -27,8 +27,7 @@ import org.jclouds.date.DateService;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.utils.ModifyRequest;
import com.google.common.annotations.Beta;
import com.google.common.collect.ImmutableMultimap;
import javax.inject.Inject;
/**
* Binds the metrics request to the http request
@ -54,7 +53,7 @@ public class GetMetricStatisticsBinder implements org.jclouds.rest.Binder {
int dimensionIndex = 1;
int statisticIndex = 1;
ImmutableMultimap.Builder<String, String> formParameters = ImmutableMultimap.<String, String> builder();
ImmutableMultimap.Builder<String, String> formParameters = ImmutableMultimap.builder();
for (Dimension dimension : getRequest.getDimensions()) {
formParameters.put("Dimensions.member." + dimensionIndex + ".Name", dimension.getName());
formParameters.put("Dimensions.member." + dimensionIndex + ".Value", dimension.getValue());
@ -77,4 +76,5 @@ public class GetMetricStatisticsBinder implements org.jclouds.rest.Binder {
return ModifyRequest.putFormParams(request, formParameters.build());
}
}

View File

@ -0,0 +1,101 @@
/**
* 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.binders;
import com.google.common.annotations.Beta;
import com.google.common.collect.ImmutableMultimap;
import com.google.inject.Inject;
import org.jclouds.cloudwatch.domain.Dimension;
import org.jclouds.cloudwatch.domain.MetricDatum;
import org.jclouds.cloudwatch.domain.PutMetricData;
import org.jclouds.cloudwatch.domain.StatisticSet;
import org.jclouds.date.DateService;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.utils.ModifyRequest;
/**
* Binds the metrics request to the http request
*
* @see <a href="http://docs.amazonwebservices.com/AmazonCloudWatch/latest/APIReference/API_PutMetricData.html" />
*
* @author Jeremy Whitlock
*/
@Beta
public class PutMetricDataBinder implements org.jclouds.rest.Binder {
private final DateService dateService;
@Inject
protected PutMetricDataBinder(DateService dateService) {
this.dateService = dateService;
}
/**
* {@inheritDoc}
*/
@Override
public <R extends HttpRequest> R bindToRequest(R request, Object input) {
PutMetricData pmdRequest = PutMetricData.class.cast(input);
ImmutableMultimap.Builder<String, String> formParameters = ImmutableMultimap.builder();
int metricDatumIndex = 1;
formParameters.put("Namespace", pmdRequest.getNamespace());
for (MetricDatum metricDatum : pmdRequest.getMetricData()) {
int dimensionIndex = 1;
StatisticSet statisticSet = metricDatum.getStatisticSet();
for (Dimension dimension : metricDatum.getDimensions()) {
formParameters.put("MetricData.member." + metricDatumIndex + ".Dimensions.member." + dimensionIndex +
".Name", dimension.getName());
formParameters.put("MetricData.member." + metricDatumIndex + ".Dimensions.member." + dimensionIndex +
".Value", dimension.getValue());
dimensionIndex++;
}
formParameters.put("MetricData.member." + metricDatumIndex + ".MetricName", metricDatum.getMetricName());
if (statisticSet != null) {
formParameters.put("MetricData.member." + metricDatumIndex + ".StatisticValues.Maximum",
String.valueOf(statisticSet.getMaximum()));
formParameters.put("MetricData.member." + metricDatumIndex + ".StatisticValues.Minimum",
String.valueOf(statisticSet.getMinimum()));
formParameters.put("MetricData.member." + metricDatumIndex + ".StatisticValues.SampleCount",
String.valueOf(statisticSet.getSampleCount()));
formParameters.put("MetricData.member." + metricDatumIndex + ".StatisticValues.Sum",
String.valueOf(statisticSet.getSum()));
}
if (metricDatum.getTimestamp() != null) {
formParameters.put("MetricData.member." + metricDatumIndex + ".Timestamp",
dateService.iso8601SecondsDateFormat(metricDatum.getTimestamp()));
}
formParameters.put("MetricData.member." + metricDatumIndex + ".Unit", String.valueOf(metricDatum.getUnit()));
if (metricDatum.getValue() != null) {
formParameters.put("MetricData.member." + metricDatumIndex + ".Value", String.valueOf(metricDatum.getValue()));
}
metricDatumIndex++;
}
return ModifyRequest.putFormParams(request, formParameters.build());
}
}

View File

@ -18,15 +18,14 @@
*/
package org.jclouds.cloudwatch.domain;
import java.util.Date;
import java.util.Set;
import org.jclouds.cloudwatch.options.ListMetricsOptions;
import org.jclouds.javax.annotation.Nullable;
import com.google.common.annotations.Beta;
import com.google.common.base.Preconditions;
import com.google.common.collect.Sets;
import org.jclouds.cloudwatch.options.ListMetricsOptions;
import org.jclouds.javax.annotation.Nullable;
import java.util.Date;
import java.util.Set;
/**
* Options use to get statistics for the specified metric.
@ -152,7 +151,7 @@ public class GetMetricStatistics {
*
* @return this {@code Builder} object
*
* @throws IllegalArgumentException if this is invoked more than 10 times
* @throws IllegalArgumentException if the passed in dimensions has more than 10 members
*/
public Builder dimensions(Set<Dimension> dimensions) {
if (dimensions != null) {
@ -335,8 +334,10 @@ public class GetMetricStatistics {
Preconditions.checkNotNull(unit, "unit cannot be null.");
Preconditions.checkArgument(statistics.size() >= 1, "statistics must have at least one member");
return new GetMetricStatistics(dimensions, endTime, metricName,namespace, period, startTime, statistics, unit);
}
return new GetMetricStatistics(dimensions, endTime, metricName,namespace, period, startTime, statistics,
unit);
}
}
}

View File

@ -0,0 +1,260 @@
/**
* 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.domain;
import com.google.common.base.Preconditions;
import com.google.common.collect.Sets;
import org.jclouds.javax.annotation.Nullable;
import java.util.Date;
import java.util.Set;
/**
* @see <a href="http://docs.amazonwebservices.com/AmazonCloudWatch/latest/APIReference/API_MetricDatum.html" />
*
* @author Jeremy Whitlock
*/
public class MetricDatum {
private final Set<Dimension> dimensions;
private final String metricName;
private final StatisticSet statisticSet;
private final Date timestamp;
private final Unit unit;
private final Double value;
/**
* Private constructor to enforce using {@link Builder}.
*/
private MetricDatum(@Nullable Set<Dimension> dimensions, String metricName, StatisticSet statisticSet,
@Nullable Date timestamp, Unit unit, Double value) {
// Default to an empty set
if (dimensions == null) {
this.dimensions = Sets.newLinkedHashSet();
} else {
this.dimensions = dimensions;
}
this.metricName = metricName;
this.statisticSet = statisticSet;
this.timestamp = timestamp;
this.unit = unit;
this.value = value;
}
/**
* return the list of dimensions describing the the metric.
*/
@Nullable
public Set<Dimension> getDimensions() {
return dimensions;
}
/**
* return the metric name for the metric.
*/
public String getMetricName() {
return metricName;
}
/**
* return the object describing the set of statistical values for the metric (This and value are mutually
* exclusive.)
*/
public StatisticSet getStatisticSet() {
return statisticSet;
}
/**
* return the time stamp used for the metric
*/
@Nullable
public Date getTimestamp() {
return timestamp;
}
/**
* return Standard unit used for the metric.
*/
public Unit getUnit() {
return unit;
}
/**
* return the actual value of the metric (This and statisticSet are mutually exclusive.)
*
*/
public Double getValue() {
return value;
}
/**
* Returns a new builder. The generated builder is equivalent to the builder
* created by the {@link Builder} constructor.
*/
public static Builder builder() {
return new Builder();
}
public static class Builder {
private Set<Dimension> dimensions = Sets.newLinkedHashSet();
private String metricName;
private StatisticSet statisticSet;
private Date timestamp;
private Unit unit;
private Double value;
/**
* Creates a new builder. The returned builder is equivalent to the builder
* generated by {@link org.jclouds.cloudwatch.domain.MetricDatum#builder}.
*/
public Builder() {}
/**
* A list of dimensions describing qualities of the metric. (Set can be 10 or less items.)
*
* @param dimensions the dimensions describing the qualities of the metric
*
* @return this {@code Builder} object
*
* @throws IllegalArgumentException if the passed in dimensions has more than 10 members
*/
public Builder dimensions(Set<Dimension> dimensions) {
if (dimensions != null) {
Preconditions.checkArgument(dimensions.size() <= 10, "dimensions can have 10 or fewer members.");
this.dimensions = dimensions;
}
return this;
}
/**
* A dimension describing qualities of the metric. (Can be called multiple times up to a maximum of 10 times.)
*
* @param dimension the dimension describing the qualities of the metric
*
* @return this {@code Builder} object
*
* @throws IllegalArgumentException if the number of dimensions would be greater than 10 after adding
*/
public Builder dimension(Dimension dimension) {
if (dimension != null) {
Preconditions.checkArgument(dimensions.size() < 10, "dimension member maximum count of 10 exceeded.");
this.dimensions.add(dimension);
}
return this;
}
/**
* The name of the metric. (Should be called once. Subsequent calls will overwrite the previous value.)
*
* @param metricName the metric name
*
* @return this {@code Builder} object
*
* @throws NullPointerException if metricName is null
* @throws IllegalArgumentException if metricName is empty
*/
public Builder metricName(String metricName) {
Preconditions.checkNotNull(metricName, "metricName cannot be null.");
Preconditions.checkArgument(metricName.length() > 1, "metricName must not be empty.");
this.metricName = metricName;
return this;
}
/**
* The object describing the set of statistical values describing the metric. (Should be called once. Subsequent
* calls will overwrite the previous value. Also, this cannot be set once you've set the value.)
*
* @param statisticSet the object describing the set of statistical values for the metric
*
* @return this {@code Builder} object
*
* @throws NullPointerException if statisticSet is null
* @throws IllegalArgumentException if value has already been set
*/
public Builder statisticSet(StatisticSet statisticSet) {
Preconditions.checkArgument(value == null, "value and statisticSet are mutually exclusive");
this.statisticSet = statisticSet;
return this;
}
/**
* The time stamp used for the metric. If not specified, the default value is set to the time the metric data was
* received. (Should be called once. Subsequent calls will overwrite the previous value.)
*
* @param timestamp the time stamp used for the metric
*
* @return this {@code Builder} object
*/
public Builder timestamp(Date timestamp) {
this.timestamp = timestamp;
return this;
}
/**
* The unit for the metric. (Should be called once. Subsequent calls will overwrite the previous value.)
*
* @param unit the unit for the metric
*
* @return this {@code Builder} object
*
* @throws NullPointerException if unit is null
*/
public Builder unit(Unit unit) {
Preconditions.checkNotNull(unit, "unit cannot be null.");
this.unit = unit;
return this;
}
/**
* The value for the metric. (Should be called once. Subsequent calls will overwrite the previous value. Also,
* this cannot be set once you've set the statisticValue.)
*
* @param value the value for the metric
*
* @return this {@code Builder} object
*
* @throws IllegalArgumentException if statisticSet has already been set
*/
public Builder value(Double value) {
Preconditions.checkArgument(statisticSet == null, "statisticSet and value are mutually exclusive");
this.value = value;
return this;
}
/**
* Returns a newly-created {@code MetricDatum} based on the contents of the {@code Builder}.
*
* @throws NullPointerException if any of the required fields are null
* @throws IllegalArgumentException if any of the provided fields don't meet required criteria
*/
public MetricDatum build() {
Preconditions.checkNotNull(metricName, "metricName cannot be null.");
Preconditions.checkNotNull(unit, "unit cannot be null.");
Preconditions.checkArgument(metricName.length() > 1 && metricName.length() <= 255,
"metricName cannot be empty and must be 255 characters or less.");
Preconditions.checkArgument(statisticSet != null || value != null,
"statisticSet or value must be set");
return new MetricDatum(dimensions, metricName, statisticSet, timestamp, unit, value);
}
}
}

View File

@ -0,0 +1,154 @@
/**
* 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.domain;
import com.google.common.annotations.Beta;
import com.google.common.base.Preconditions;
import com.google.common.collect.Sets;
import java.util.Set;
/**
* Options use to get statistics for the specified metric.
*
* @see <a href="http://docs.amazonwebservices.com/AmazonCloudWatch/latest/APIReference/API_PutMetricData.html" />
*
* @author Jeremy Whitlock
*/
@Beta
public class PutMetricData {
private final String namespace;
private final Set<MetricDatum> metricData;
/**
* Private constructor to enforce using {@link Builder}.
*/
private PutMetricData(String namespace, Set<MetricDatum> metricData) {
this.namespace = namespace;
this.metricData = metricData;
}
/**
* return the namespace for this request
*/
public String getNamespace() {
return namespace;
}
/**
* return the metric data for this request
*/
public Set<MetricDatum> getMetricData() {
return metricData;
}
/**
* Returns a new builder. The generated builder is equivalent to the builder
* created by the {@link Builder} constructor.
*/
public static Builder builder() {
return new Builder();
}
public static class Builder {
private String namespace;
private Set<MetricDatum> metricData = Sets.newLinkedHashSet();
/**
* Creates a new builder. The returned builder is equivalent to the builder
* generated by {@link org.jclouds.cloudwatch.domain.PutMetricData#builder}.
*/
public Builder() {}
/**
* The namespace for the metric data. (Should be called once. Subsequent calls will overwrite the previous
* value.) This value <b>cannot</b> start with "AWS/" as that namespace prefix is reserved for AWS CloudWatch
* metrics.
*
* @param namespace the namespace for the metric data
*
* @return this {@code Builder} object
*
* @throws NullPointerException if namespace is null
* @throws IllegalArgumentException if namespace is empty or starts with "AWS/"
*/
public Builder namespace(String namespace) {
Preconditions.checkNotNull(namespace, "namespace cannot be null.");
Preconditions.checkArgument(namespace.length() > 1, "namespace must not be empty.");
Preconditions.checkArgument(!namespace.startsWith("AWS/"), "namespace cannot start with 'AWS/' as it's " +
"reserved for AWS CloudWatch metrics.");
this.namespace = namespace;
return this;
}
/**
* A metric to either create or aggregate to an existing metric. (Can be called multiple times up to a maximum of
* 10 times.)
*
* @param metricDatum the representation of a metric to either create a new metric or add new values to be
* aggregated into an existing metric
*
* @return this {@code Builder} object
*
* @throws IllegalArgumentException if the number of dimensions would be greater than 10 after adding
*/
public Builder metricDatum(MetricDatum metricDatum) {
if (metricDatum != null) {
Preconditions.checkArgument(metricData.size() < 10, "metric data member maximum count of 10 exceeded.");
this.metricData.add(metricDatum);
}
return this;
}
/**
* A list of data describing the metric. (Set can be 10 or less items.)
*
* @param metricData the list of data describing the data
*
* @return this {@code Builder} object
*
* @throws IllegalArgumentException if the passed in data has more than 10 members
*/
public Builder metricData(Set<MetricDatum> metricData) {
if (metricData != null) {
Preconditions.checkArgument(metricData.size() <= 10, "metric data can have 10 or fewer members.");
this.metricData = metricData;
}
return this;
}
/**
* Returns a newly-created {@code PutMetricData} based on the contents of the {@code Builder}.
*
* @throws NullPointerException if any of the required fields are null
* @throws IllegalArgumentException if any of the provided fields don't meet required criteria
*/
public PutMetricData build() {
Preconditions.checkNotNull(namespace, "namespace cannot be null.");
Preconditions.checkNotNull(metricData, "metricData cannot be null.");
Preconditions.checkArgument(metricData.size() > 0, "metricData must have at least one member.");
return new PutMetricData(namespace, metricData);
}
}
}

View File

@ -0,0 +1,172 @@
/**
* 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.domain;
import com.google.common.base.Preconditions;
import org.jclouds.javax.annotation.Nullable;
/**
* @see <a href="http://docs.amazonwebservices.com/AmazonCloudWatch/latest/APIReference/API_StatisticSet.html" />
*
* @author Jeremy Whitlock
*/
public class StatisticSet {
private final Double maximum;
private final Double minimum;
private final Double sampleCount;
private final Double sum;
public StatisticSet(@Nullable Double maximum, @Nullable Double minimum, @Nullable Double sampleCount,
@Nullable Double sum) {
this.maximum = maximum;
this.minimum = minimum;
this.sampleCount = sampleCount;
this.sum = sum;
}
/**
* return the maximum value of the sample set
*/
public Double getMaximum() {
return maximum;
}
/**
* return the minimum value of the sample set
*/
public Double getMinimum() {
return minimum;
}
/**
* return the number of samples used for the statistic set
*/
public Double getSampleCount() {
return sampleCount;
}
/**
* return the sum of values for the sample set
*/
public Double getSum() {
return sum;
}
/**
* Returns a new builder. The generated builder is equivalent to the builder
* created by the {@link Builder} constructor.
*/
public static Builder builder() {
return new Builder();
}
public static class Builder {
private Double maximum;
private Double minimum;
private Double sampleCount;
private Double sum;
/**
* Creates a new builder. The returned builder is equivalent to the builder
* generated by {@link org.jclouds.cloudwatch.domain.StatisticSet#builder}.
*/
public Builder() {}
/**
* The maximum value of the sample set. (Should be called once. Subsequent calls will overwrite the previous
* value.)
*
* @param maximum the maximum value of the sample set
*
* @return this {@code Builder} object
*
* @throws NullPointerException if maximum is null
*/
public Builder maximum(Double maximum) {
Preconditions.checkNotNull(maximum, "maximum cannot be null.");
this.maximum = maximum;
return this;
}
/**
* The minimum value of the sample set. (Should be called once. Subsequent calls will overwrite the previous
* value.)
*
* @param minimum the minimum value of the sample set
*
* @return this {@code Builder} object
*
* @throws NullPointerException if minimum is null
*/
public Builder minimum(Double minimum) {
Preconditions.checkNotNull(minimum, "minimum cannot be null.");
this.minimum = minimum;
return this;
}
/**
* The the number of samples used for the statistic set. (Should be called once. Subsequent calls will overwrite
* the previous value.)
*
* @param sampleCount the number of samples used for the statistic set
*
* @return this {@code Builder} object
*
* @throws NullPointerException if sampleCount is null
*/
public Builder sampleCount(Double sampleCount) {
Preconditions.checkNotNull(sampleCount, "sampleCount cannot be null.");
this.sampleCount = sampleCount;
return this;
}
/**
* The sum of values for the sample set. (Should be called once. Subsequent calls will overwrite the previous
* value.)
*
* @param sum the sum of values for the sample set
*
* @return this {@code Builder} object
*
* @throws NullPointerException if sum is null
*/
public Builder sum(Double sum) {
Preconditions.checkNotNull(sum, "sum cannot be null.");
this.sum = sum;
return this;
}
/**
* Returns a newly-created {@code StatisticSet} based on the contents of the {@code Builder}.
*
* @throws NullPointerException if any of the required fields are null
*/
public StatisticSet build() {
Preconditions.checkNotNull(maximum, "maximum cannot be null.");
Preconditions.checkNotNull(minimum, "minimum cannot be null.");
Preconditions.checkNotNull(sampleCount, "sampleCount cannot be null.");
Preconditions.checkNotNull(sum, "sum cannot be null.");
return new StatisticSet(maximum, minimum, sampleCount, sum);
}
}
}

View File

@ -18,14 +18,14 @@
*/
package org.jclouds.cloudwatch.features;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import com.google.common.util.concurrent.ListenableFuture;
import org.jclouds.aws.filters.FormSigner;
import org.jclouds.cloudwatch.binders.GetMetricStatisticsBinder;
import org.jclouds.cloudwatch.binders.PutMetricDataBinder;
import org.jclouds.cloudwatch.domain.GetMetricStatistics;
import org.jclouds.cloudwatch.domain.GetMetricStatisticsResponse;
import org.jclouds.cloudwatch.domain.ListMetricsResponse;
import org.jclouds.cloudwatch.domain.PutMetricData;
import org.jclouds.cloudwatch.options.GetMetricStatisticsOptions;
import org.jclouds.cloudwatch.options.ListMetricsOptions;
import org.jclouds.cloudwatch.xml.GetMetricStatisticsResponseHandlerV2;
@ -36,7 +36,8 @@ import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.annotations.VirtualHost;
import org.jclouds.rest.annotations.XMLResponseParser;
import com.google.common.util.concurrent.ListenableFuture;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
/**
* Provides access to Amazon CloudWatch via the Query API
@ -88,4 +89,12 @@ public interface MetricAsyncClient {
@BinderParam(GetMetricStatisticsBinder.class) GetMetricStatistics statistics,
GetMetricStatisticsOptions options);
/**
* @see MetricClient#putMetricData(org.jclouds.cloudwatch.domain.PutMetricData)
*/
@POST
@Path("/")
@FormParams(keys = "Action", values = "PutMetricData")
ListenableFuture<Void> putMetricData(@BinderParam(PutMetricDataBinder.class) PutMetricData putMetricData);
}

View File

@ -18,15 +18,16 @@
*/
package org.jclouds.cloudwatch.features;
import java.util.concurrent.TimeUnit;
import org.jclouds.cloudwatch.domain.GetMetricStatistics;
import org.jclouds.cloudwatch.domain.GetMetricStatisticsResponse;
import org.jclouds.cloudwatch.domain.ListMetricsResponse;
import org.jclouds.cloudwatch.domain.PutMetricData;
import org.jclouds.cloudwatch.options.GetMetricStatisticsOptions;
import org.jclouds.cloudwatch.options.ListMetricsOptions;
import org.jclouds.concurrent.Timeout;
import java.util.concurrent.TimeUnit;
/**
* Provides access to Amazon CloudWatch via the Query API
* <p/>
@ -45,10 +46,10 @@ public interface MetricClient {
* use returned NextToken (
* {@link org.jclouds.cloudwatch.domain.ListMetricsResponse#getNextToken()}) value with
* subsequent calls .To retrieve all available metrics with one call, use
* {@link #list(MetricsClient, String, org.jclouds.cloudwatch.options.ListMetricsOptions)}.
* {@link org.jclouds.cloudwatch.CloudWatch#listMetrics(MetricClient,
* org.jclouds.cloudwatch.options.ListMetricsOptions)}
*
* @param options
* the options describing the metrics query
* @param options the options describing the metrics query
*
* @return the response object
*/
@ -59,10 +60,8 @@ public interface MetricClient {
/**
* Gets statistics for the specified metric.
*
* @param statistics
* the statistics to gather
* @param options
* the options describing the metric statistics query
* @param statistics the statistics to gather
* @param options the options describing the metric statistics query
*
* @return the response object
*/
@ -70,4 +69,11 @@ public interface MetricClient {
GetMetricStatisticsResponse getMetricStatistics(GetMetricStatistics statistics);
/**
* Publishes metric data points to Amazon CloudWatch.
*
* @param putMetricData object describing the metric data
*/
void putMetricData(PutMetricData putMetricData);
}

View File

@ -18,15 +18,14 @@
*/
package org.jclouds.cloudwatch.xml;
import java.util.Date;
import javax.inject.Inject;
import org.jclouds.cloudwatch.domain.Datapoint;
import org.jclouds.cloudwatch.domain.Unit;
import org.jclouds.date.DateService;
import org.jclouds.http.functions.ParseSax;
import javax.inject.Inject;
import java.util.Date;
/**
*
* @author Adrian Cole
@ -71,7 +70,7 @@ public class DatapointHandler extends ParseSax.HandlerForGeneratedRequestWithRes
minimum = doubleOrNull();
} else if (qName.equals("Timestamp")) {
timestamp = dateService.iso8601SecondsDateParse(currentText.toString().trim());
} else if (qName.equals("Samples")) {
} else if (qName.equals("SampleCount")) {
samples = doubleOrNull();
} else if (qName.equals("Sum")) {
sum = doubleOrNull();

View File

@ -0,0 +1,168 @@
/**
* 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.binders;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Guice;
import com.google.inject.Injector;
import junit.framework.Assert;
import org.jclouds.cloudwatch.domain.Dimension;
import org.jclouds.cloudwatch.domain.MetricDatum;
import org.jclouds.cloudwatch.domain.PutMetricData;
import org.jclouds.cloudwatch.domain.StatisticSet;
import org.jclouds.cloudwatch.domain.Unit;
import org.jclouds.http.HttpRequest;
import org.testng.annotations.Test;
import java.net.URI;
import java.util.Date;
/**
* Tests behavior of {@link PutMetricDataBinder}.
*
* @author Jeremy Whitlock
*/
@Test(groups = "unit")
public class PutMetricDataBinderTest {
Injector injector = Guice.createInjector();
PutMetricDataBinder binder = injector.getInstance(PutMetricDataBinder.class);
HttpRequest request = HttpRequest.builder().method("POST").endpoint(URI.create("http://localhost")).build();
public void testMetricWithoutTimestamp() throws Exception {
StatisticSet ss = StatisticSet.builder()
.maximum(4.0)
.minimum(1.0)
.sampleCount(4.0)
.sum(10.0)
.build();
MetricDatum metricDatum = MetricDatum.builder()
.metricName("TestMetricName")
.statisticSet(ss)
.dimension(new Dimension("TestDimension", "FAKE"))
.unit(Unit.COUNT)
.build();
PutMetricData pmd = PutMetricData.builder()
.metricDatum(metricDatum)
.namespace("JCLOUDS/Test")
.build();
request = binder.bindToRequest(request, pmd);
Assert.assertEquals(request.getPayload().getRawContent(),
new StringBuilder()
.append("Namespace=JCLOUDS%2FTest")
.append("&MetricData.member.1.Dimensions.member.1.Name=TestDimension")
.append("&MetricData.member.1.Dimensions.member.1.Value=FAKE")
.append("&MetricData.member.1.MetricName=TestMetricName")
.append("&MetricData.member.1.StatisticValues.Maximum=4.0")
.append("&MetricData.member.1.StatisticValues.Minimum=1.0")
.append("&MetricData.member.1.StatisticValues.SampleCount=4.0")
.append("&MetricData.member.1.StatisticValues.Sum=10.0")
.append("&MetricData.member.1.Unit=")
.append(Unit.COUNT.toString())
.toString());
}
public void testMetricWithMultipleDimensions() throws Exception {
MetricDatum metricDatum = MetricDatum.builder()
.metricName("TestMetricName")
.dimension(new Dimension("TestDimension", "FAKE"))
.dimension(new Dimension("TestDimension2", "FAKE2"))
.unit(Unit.COUNT)
.timestamp(new Date(10000000l))
.value(5.0)
.build();
PutMetricData pmd = PutMetricData.builder()
.metricDatum(metricDatum)
.namespace("JCLOUDS/Test")
.build();
request = binder.bindToRequest(request, pmd);
Assert.assertEquals(request.getPayload().getRawContent(),
new StringBuilder()
.append("Namespace=JCLOUDS%2FTest")
.append("&MetricData.member.1.Dimensions.member.1.Name=TestDimension")
.append("&MetricData.member.1.Dimensions.member.1.Value=FAKE")
.append("&MetricData.member.1.Dimensions.member.2.Name=TestDimension2")
.append("&MetricData.member.1.Dimensions.member.2.Value=FAKE2")
.append("&MetricData.member.1.MetricName=TestMetricName")
.append("&MetricData.member.1.Timestamp=1970-01-01T02%3A46%3A40Z")
.append("&MetricData.member.1.Unit=")
.append(Unit.COUNT.toString())
.append("&MetricData.member.1.Value=5.0")
.toString());
}
public void testMetricWithMultipleDatum() throws Exception {
StatisticSet ss = StatisticSet.builder()
.maximum(4.0)
.minimum(1.0)
.sampleCount(4.0)
.sum(10.0)
.build();
MetricDatum metricDatum = MetricDatum.builder()
.metricName("TestMetricName")
.statisticSet(ss)
.dimension(new Dimension("TestDimension", "FAKE"))
.dimension(new Dimension("TestDimension2", "FAKE2"))
.unit(Unit.COUNT)
.timestamp(new Date(10000000l))
.build();
MetricDatum metricDatum2 = MetricDatum.builder()
.metricName("TestMetricName")
.dimension(new Dimension("TestDimension", "FAKE"))
.unit(Unit.COUNT)
.timestamp(new Date(10000000l))
.value(5.0)
.build();
PutMetricData pmd = PutMetricData.builder()
.metricData(ImmutableSet.of(metricDatum, metricDatum2))
.namespace("JCLOUDS/Test")
.build();
request = binder.bindToRequest(request, pmd);
Assert.assertEquals(request.getPayload().getRawContent(),
new StringBuilder()
.append("Namespace=JCLOUDS%2FTest")
.append("&MetricData.member.1.Dimensions.member.1.Name=TestDimension")
.append("&MetricData.member.1.Dimensions.member.1.Value=FAKE")
.append("&MetricData.member.1.Dimensions.member.2.Name=TestDimension2")
.append("&MetricData.member.1.Dimensions.member.2.Value=FAKE2")
.append("&MetricData.member.1.MetricName=TestMetricName")
.append("&MetricData.member.1.StatisticValues.Maximum=4.0")
.append("&MetricData.member.1.StatisticValues.Minimum=1.0")
.append("&MetricData.member.1.StatisticValues.SampleCount=4.0")
.append("&MetricData.member.1.StatisticValues.Sum=10.0")
.append("&MetricData.member.1.Timestamp=1970-01-01T02%3A46%3A40Z")
.append("&MetricData.member.1.Unit=")
.append(Unit.COUNT.toString())
.append("&MetricData.member.2.Dimensions.member.1.Name=TestDimension")
.append("&MetricData.member.2.Dimensions.member.1.Value=FAKE")
.append("&MetricData.member.2.MetricName=TestMetricName")
.append("&MetricData.member.2.Timestamp=1970-01-01T02%3A46%3A40Z")
.append("&MetricData.member.2.Unit=")
.append(Unit.COUNT.toString())
.append("&MetricData.member.2.Value=5.0")
.toString());
}
}

View File

@ -18,12 +18,7 @@
*/
package org.jclouds.cloudwatch.features;
import static org.testng.Assert.assertEquals;
import java.net.URI;
import java.util.Date;
import java.util.TimeZone;
import com.google.common.collect.ImmutableMultimap;
import org.jclouds.cloudwatch.CloudWatchClient;
import org.jclouds.cloudwatch.domain.Dimension;
import org.jclouds.cloudwatch.domain.EC2Constants;
@ -39,7 +34,11 @@ import org.jclouds.http.HttpResponse;
import org.jclouds.rest.ResourceNotFoundException;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMultimap;
import java.net.URI;
import java.util.Date;
import java.util.TimeZone;
import static org.testng.Assert.assertEquals;
/**
* @author Jeremy Whitlock, Adrian Cole
@ -239,4 +238,8 @@ public class MetricClientExpectTest extends BaseCloudWatchClientExpectTest {
"GetMetricStatisticsResponse{label=CPUUtilization, datapoints=[Datapoint{timestamp=Thu Jan 15 16:00:00 PST 2009, customUnit=null, maximum=null, minimum=null, average=0.17777777777777778, sum=null, samples=9.0, unit=Percent}, Datapoint{timestamp=Thu Jan 15 16:01:00 PST 2009, customUnit=null, maximum=null, minimum=null, average=0.1, sum=null, samples=8.0, unit=Percent}]}");
}
public void testPutMetricData() throws Exception {
}
}

View File

@ -18,13 +18,10 @@
*/
package org.jclouds.cloudwatch.features;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Calendar;
import java.util.Date;
import java.util.Set;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import junit.framework.Assert;
import org.jclouds.cloudwatch.domain.Datapoint;
import org.jclouds.cloudwatch.domain.Dimension;
import org.jclouds.cloudwatch.domain.EC2Constants;
@ -32,14 +29,24 @@ import org.jclouds.cloudwatch.domain.GetMetricStatistics;
import org.jclouds.cloudwatch.domain.GetMetricStatisticsResponse;
import org.jclouds.cloudwatch.domain.ListMetricsResponse;
import org.jclouds.cloudwatch.domain.Metric;
import org.jclouds.cloudwatch.domain.MetricDatum;
import org.jclouds.cloudwatch.domain.Namespaces;
import org.jclouds.cloudwatch.domain.PutMetricData;
import org.jclouds.cloudwatch.domain.StatisticSet;
import org.jclouds.cloudwatch.domain.Statistics;
import org.jclouds.cloudwatch.domain.Unit;
import org.jclouds.cloudwatch.internal.BaseCloudWatchClientLiveTest;
import org.jclouds.cloudwatch.options.ListMetricsOptions;
import org.jclouds.predicates.RetryablePredicate;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableSet;
import java.util.Calendar;
import java.util.Date;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* @author Jeremy Whitlock, Adrian Cole
@ -47,6 +54,100 @@ import com.google.common.collect.ImmutableSet;
@Test(groups = "live", testName = "MetricClientLiveTest")
public class MetricClientLiveTest extends BaseCloudWatchClientLiveTest {
@Test
protected void testPutMetricData() throws Exception {
String metricName = "TestMetricName" + System.currentTimeMillis();
String namespace = "JCLOUDS/Test";
Date metricTimestamp = new Date();
// CloudWatch rounds metric timestamps down to the closest minute
Date metricTimestampInCloudWatch =
new Date(metricTimestamp.getTime() - (metricTimestamp.getTime() % (60 * 1000)));
StatisticSet ss = StatisticSet.builder()
.maximum(4.0)
.minimum(1.0)
.sampleCount(4.0)
.sum(10.0)
.build();
MetricDatum metricDatum = MetricDatum.builder()
.metricName(metricName + "_1")
.statisticSet(ss)
.dimension(new Dimension("BaseMetricName", metricName))
.dimension(new Dimension("TestDimension2", "TEST2"))
.unit(Unit.COUNT)
.timestamp(metricTimestamp)
.build();
MetricDatum metricDatum2 = MetricDatum.builder()
.metricName(metricName + "_2")
.dimension(new Dimension("BaseMetricName", metricName))
.unit(Unit.COUNT)
.timestamp(metricTimestamp)
.value(10.0)
.build();
PutMetricData pmd = PutMetricData.builder()
.namespace(namespace)
.metricDatum(metricDatum)
.metricDatum(metricDatum2)
.build();
client().putMetricData(pmd);
ListMetricsOptions lmo = ListMetricsOptions.builder().namespace(namespace)
.dimension(new Dimension("BaseMetricName", metricName))
.build();
boolean success = new RetryablePredicate<ListMetricsOptions>(new Predicate<ListMetricsOptions>() {
@Override
public boolean apply(ListMetricsOptions options) {
return Iterables.size(client().listMetrics(options)) == 2;
}
}, 20, 1, TimeUnit.MINUTES).apply(lmo);
if (!success) {
Assert.fail("Unable to gather the created CloudWatch data within the time (20m) allotted.");
}
ListMetricsResponse lmr = client().listMetrics(lmo);
Date endTime = new Date(metricTimestampInCloudWatch.getTime() + (60 * 1000)); // Pad a minute just in case
Date startTime = new Date(metricTimestampInCloudWatch.getTime() - (60 * 1000)); // Pad a minute just in case
for (Metric m : lmr) {
GetMetricStatistics gms = GetMetricStatistics.builder()
.dimensions(m.getDimensions())
.namespace(namespace)
.metricName(m.getMetricName())
.endTime(endTime)
.statistic(Statistics.MAXIMUM)
.statistic(Statistics.MINIMUM)
.statistic(Statistics.SAMPLE_COUNT)
.statistic(Statistics.SUM)
.period(60)
.startTime(startTime)
.unit(Unit.COUNT)
.build();
GetMetricStatisticsResponse gmr = client().getMetricStatistics(gms);
Assert.assertEquals(1, Iterables.size(gmr));
Datapoint datapoint = gmr.iterator().next();
Assert.assertEquals(datapoint.getTimestamp(), metricTimestampInCloudWatch);
Assert.assertNull(datapoint.getCustomUnit());
Assert.assertEquals(Unit.COUNT, datapoint.getUnit());
Assert.assertNull(datapoint.getAverage());
if (m.getDimensions().size() == 1) {
Assert.assertEquals(10.0, datapoint.getMaximum());
Assert.assertEquals(10.0, datapoint.getMinimum());
Assert.assertEquals(10.0, datapoint.getSum());
Assert.assertEquals(1.0, datapoint.getSamples());
} else {
Assert.assertEquals(4.0, datapoint.getMaximum());
Assert.assertEquals(1.0, datapoint.getMinimum());
Assert.assertEquals(10.0, datapoint.getSum());
Assert.assertEquals(4.0, datapoint.getSamples());
}
}
}
// TODO: change this test to retrieve pre-seeded custom metrics
@Test
protected void testGetMetricStatistics() {

View File

@ -4,13 +4,13 @@
<member>
<Timestamp>2009-01-16T00:00:00Z</Timestamp>
<Unit>Percent</Unit>
<Samples>9.0</Samples>
<SampleCount>9.0</SampleCount>
<Average>0.17777777777777778</Average>
</member>
<member>
<Timestamp>2009-01-16T00:01:00Z</Timestamp>
<Unit>Percent</Unit>
<Samples>8.0</Samples>
<SampleCount>8.0</SampleCount>
<Average>0.1</Average>
</member>
</Datapoints>