Example usage for com.amazonaws.services.cloudwatch AmazonCloudWatch setRegion

List of usage examples for com.amazonaws.services.cloudwatch AmazonCloudWatch setRegion

Introduction

In this page you can find the example usage for com.amazonaws.services.cloudwatch AmazonCloudWatch setRegion.

Prototype

@Deprecated
void setRegion(Region region);

Source Link

Document

An alternative to AmazonCloudWatch#setEndpoint(String) , sets the regional endpoint for this client's service calls.

Usage

From source file:org.web.online.cloudwatch.tomcat.valve.ElapsedTimeAggregator.java

License:Apache License

/**
 * Construct the instance. Sets up the Cloud Watch metric with the given parameters.
 * /*from www. j  ava 2s .c o m*/
 * @param namespace namespace value to use to push data to CloudWatch
 * @param region region used to look for instance tags and to push CloudWatch data
 * @param instanceId instanceId to use as dimension
 * @param asgName autoscaling group name to use as dimension
 * @param ec2Client ec2 client to use in querying tags to find ASG name
 * @param cloudWatchClient cloud watch client to use to push CloudWatch data
 */
public ElapsedTimeAggregator(String namespace, Region region, String instanceId, String asgName,
        AmazonEC2 ec2Client, AmazonCloudWatch cloudWatchClient) {

    this.region = region;

    if (instanceId == null) {
        throw new IllegalStateException("unable to find instance id");
    }

    // get the ASG name
    if (asgName == null) {
        ec2Client.setRegion(region);
        List<TagDescription> tagDescriptions = ec2Client.describeTags(new DescribeTagsRequest().withFilters(
                new Filter().withName("resource-id").withValues(instanceId),
                new Filter().withName("key").withValues("aws:autoscaling:groupName"))).getTags();
        if (tagDescriptions.size() == 1) {
            asgName = tagDescriptions.get(0).getValue();
        }
    }

    if (asgName == null) {
        log.warn("unable to determine AutoScalingGroupName for " + instanceId
                + ". No statistics will be published under the AutoScalingGroupName dimension");
    }

    cloudWatchClient.setRegion(region);
    this.cloudWatchClient = cloudWatchClient;

    String metricName = "ElapsedTime";
    Dimension instanceDimension = new Dimension().withName("InstanceId").withValue(instanceId);
    StatisticSet statisticSet = new StatisticSet();

    // set up the static MetricDatum and associate to the PutMetricDataRequest
    MetricDatum instanceMetricDatum = new MetricDatum().withMetricName(metricName)
            .withDimensions(instanceDimension).withStatisticValues(statisticSet)
            .withUnit(StandardUnit.Milliseconds);
    putMetricDataRequest = new PutMetricDataRequest().withNamespace(namespace)
            .withMetricData(instanceMetricDatum);

    // and a special zero value request since statistic set doesn't
    // support zero values
    MetricDatum zeroValueInstanceMetricDatum = new MetricDatum().withMetricName(metricName)
            .withDimensions(instanceDimension).withValue(0d).withUnit(StandardUnit.Milliseconds);
    zeroValuePutMetricDataRequest = new PutMetricDataRequest().withNamespace(namespace)
            .withMetricData(zeroValueInstanceMetricDatum);

    // also push metrics for the ASG dimension if we have an ASG name
    if (asgName != null) {
        Dimension asgDimension = new Dimension().withName("AutoScalingGroupName").withValue(asgName);
        MetricDatum asgMetricDatum = new MetricDatum().withMetricName(metricName).withDimensions(asgDimension)
                .withStatisticValues(statisticSet).withUnit(StandardUnit.Milliseconds);
        putMetricDataRequest.withMetricData(asgMetricDatum);
        MetricDatum zeroValueAsgMetricDatum = new MetricDatum().withMetricName(metricName)
                .withDimensions(asgDimension).withValue(0d).withUnit(StandardUnit.Milliseconds);
        zeroValuePutMetricDataRequest.withMetricData(zeroValueAsgMetricDatum);
    }
}