本文整理汇总了Java中com.amazonaws.services.cloudwatch.model.GetMetricStatisticsRequest.setStartTime方法的典型用法代码示例。如果您正苦于以下问题:Java GetMetricStatisticsRequest.setStartTime方法的具体用法?Java GetMetricStatisticsRequest.setStartTime怎么用?Java GetMetricStatisticsRequest.setStartTime使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.amazonaws.services.cloudwatch.model.GetMetricStatisticsRequest
的用法示例。
在下文中一共展示了GetMetricStatisticsRequest.setStartTime方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: monitorCPUUsage
import com.amazonaws.services.cloudwatch.model.GetMetricStatisticsRequest; //导入方法依赖的package包/类
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());
}
}
示例2: getMetricStaticticsTest
import com.amazonaws.services.cloudwatch.model.GetMetricStatisticsRequest; //导入方法依赖的package包/类
/**
* 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;
}
示例3: setRequestCollectionWindow
import com.amazonaws.services.cloudwatch.model.GetMetricStatisticsRequest; //导入方法依赖的package包/类
/**
* Sets the window of time for the statistics collection. If the last collection time is passed in the compute stats request
* then that value is used for getting the stats data from the provider else the default configured window for stats
* collection is used.
*
* Also, if the last collection time is really a long time ago, then the maximum collection window is honored to collect
* the stats from the provider.
*/
private void setRequestCollectionWindow(Long defaultStartWindowMicros,
Long lastCollectionTimeMicros,
Long collectionPeriodInSeconds,
GetMetricStatisticsRequest request) {
long endTimeMicros = StatsUtil.computeIntervalBeginMicros(Utils.getNowMicrosUtc(),
TimeUnit.SECONDS.toMillis(collectionPeriodInSeconds));
request.setEndTime(new Date(TimeUnit.MICROSECONDS.toMillis(endTimeMicros)));
Long maxCollectionWindowStartTime = TimeUnit.MICROSECONDS.toMillis(endTimeMicros) -
TimeUnit.MICROSECONDS.toMillis(defaultStartWindowMicros);
// If the last collection time is available, then the stats data from the provider will be
// fetched from that time onwards. Else, the stats collection is performed starting from the
// default configured window.
if (lastCollectionTimeMicros == null) {
request.setStartTime(new Date(maxCollectionWindowStartTime));
return;
}
if (lastCollectionTimeMicros != 0) {
if (lastCollectionTimeMicros > endTimeMicros) {
throw new IllegalStateException(
"The last stats collection time cannot be in the future.");
// Check if the last collection time calls for collection to earlier than the
// maximum defined windows size.
// In that case default to the maximum collection window.
} else if (TimeUnit.MICROSECONDS
.toMillis(lastCollectionTimeMicros) < maxCollectionWindowStartTime) {
request.setStartTime(new Date(maxCollectionWindowStartTime));
return;
}
long beginMicros = StatsUtil.computeIntervalBeginMicros(lastCollectionTimeMicros,
TimeUnit.SECONDS.toMillis(collectionPeriodInSeconds));
request.setStartTime(new Date(
TimeUnit.MICROSECONDS.toMillis(beginMicros)));
}
}
示例4: getMetric
import com.amazonaws.services.cloudwatch.model.GetMetricStatisticsRequest; //导入方法依赖的package包/类
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);
}
示例5: getRequestCount
import com.amazonaws.services.cloudwatch.model.GetMetricStatisticsRequest; //导入方法依赖的package包/类
/**
* 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;
}
示例6: getResponseCountForMetric
import com.amazonaws.services.cloudwatch.model.GetMetricStatisticsRequest; //导入方法依赖的package包/类
/**
* 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;
}