Example usage for com.amazonaws.services.cloudwatch AmazonCloudWatchClient listMetrics

List of usage examples for com.amazonaws.services.cloudwatch AmazonCloudWatchClient listMetrics

Introduction

In this page you can find the example usage for com.amazonaws.services.cloudwatch AmazonCloudWatchClient listMetrics.

Prototype

@Override
    public ListMetricsResult listMetrics() 

Source Link

Usage

From source file:org.elasticdroid.model.CloudWatchMetricsModel.java

License:Open Source License

/**
 * Retrieve the list of metrics/*w ww .ja v a 2  s  . c  o m*/
 * 
 * @return Either
 * <ul>
 *    <li>AmazonServiceException</li>
 *  <li>AmazonClientException</li>
 *  <li>List\<Metric\></li>
 * </ul>
 */
public Object retrieveMetricsList(Dimension... dimensions) {
    //the cloudwatch client to use
    AmazonCloudWatchClient cloudWatchClient = null;
    List<Metric> returnedMetrics = null;
    ArrayList<String> measureNames = new ArrayList<String>();
    ListMetricsRequest request = new ListMetricsRequest();

    //create credentials using the BasicAWSCredentials class
    BasicAWSCredentials credentials = new BasicAWSCredentials(connectionData.get("accessKey"),
            connectionData.get("secretAccessKey"));

    //create a cloudwatch client
    try {
        cloudWatchClient = new AmazonCloudWatchClient(credentials);
    } catch (AmazonServiceException amazonServiceException) {
        //if an error response is returned by AmazonIdentityManagement indicating either a 
        //problem with the data in the request, or a server side issue.
        Log.e(this.getClass().getName(), "Exception:" + amazonServiceException.getMessage());
        return amazonServiceException;
    } catch (AmazonClientException amazonClientException) {
        //If any internal errors are encountered inside the client while attempting to make 
        //the request or handle the response. For example if a network connection is not 
        //available. 
        Log.e(this.getClass().getName(), "Exception:" + amazonClientException.getMessage());
        return amazonClientException;
    }

    cloudWatchClient.setEndpoint(cloudWatchEndpoint);

    //create the request
    request = new ListMetricsRequest();
    //request.setNextToken(nextToken)
    try {
        returnedMetrics = cloudWatchClient.listMetrics().getMetrics();
    } catch (AmazonServiceException amazonServiceException) {
        //if an error response is returned by AmazonIdentityManagement indicating either a 
        //problem with the data in the request, or a server side issue.
        Log.e(this.getClass().getName(), "Exception:" + amazonServiceException.getMessage());
        return amazonServiceException;
    } catch (AmazonClientException amazonClientException) {
        //If any internal errors are encountered inside the client while attempting to make 
        //the request or handle the response. For example if a network connection is not 
        //available. 
        Log.e(this.getClass().getName(), "Exception:" + amazonClientException.getMessage());
        return amazonClientException;
    }

    //my own disgusting O(mnp) filter. This is REALLY CRAP!
    //remove all that does not fit into the dimension
    //for some reason, there isn't a way to provide dimensions using ListMetricsRequest in Java
    //you can do it in the C# API though :P
    List<Dimension> returnedDimensions;
    boolean added;
    for (Metric metric : returnedMetrics) {
        returnedDimensions = metric.getDimensions();
        added = false;
        for (Dimension returnedDimension : returnedDimensions) {
            //check if any of the dimensions passed in to the model are equal to this.
            for (Dimension dimension : dimensions) {
                if (returnedDimension.getValue().equals(dimension.getValue())) {
                    measureNames.add(metric.getMeasureName());
                    added = true;
                    break;
                }
            }

            if (added) {
                break;
            }
        }
    }

    return measureNames;
}