Collecting custom metrics from EC2 instances

You can collect custom metrics from your applications, for example, the number of active sessions, response latency, and many more. More importantly, you can report custom metrics in application and business terms. For example, average number of orders processed per minute, today, on an e-commerce site. These business metrics can help you with capacity planning and allocating suitable budgets for your cloud infrastructure based on the business impact.

Custom metrics help you monitor your applications, directly, from CloudWatch. Using these custom metrics, you can create alarms that can, for example, add instances in the autoscaling group. There is a simple PUT API call to collect custom metrics. AWS also provides monitoring scripts for Linux and Windows that send custom metrics to the AWS CloudWatch.

How to do it…

  1. Installing AWS Java SDK.

    It helps you access AWS CloudWatch service from Java applications. In your Maven dependency section, add the following dependency for AWS Java SDK Version 1.9.28.1:

    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk</artifactId>
        <version>1.9.28.1</version>
    </dependency>
  2. Collecting custom metrics from the EC2 instance.

    In our example program here, we collect custom metrics into AWS CloudWatch. The Java program inserts session metric into AWS CloudWatch:

    // Insert custom metrics into CloudWatch.
        public static void InsertMetric() {
    
    // Create BasicAWSCredentials with Access Key Id and Secret Access Key.
            BasicAWSCredentials credentials = new BasicAWSCredentials(
                    "Access Key Id",
                    "Secret Access Key");
    
    // Create CloudWatch client.
            AmazonCloudWatchClient cloudWatchClient = new AmazonCloudWatchClient(
                    credentials);
    
    // Set endpoint.
            cloudWatchClient.setEndpoint("monitoring.ap- southeast-1.amazonaws.com");
    
    // Initialize PutMetricDataRequest.
            PutMetricDataRequest putMetricDataRequest = new PutMetricDataRequest();
    
    // Set Namespace.
            putMetricDataRequest.setNamespace("Production");
    
    // Initialize Dimension.
            Dimension dimension = new Dimension();
    
    // Set dimension name.
            dimension.setName("ApacheInstance");
    
    // Set dimesion value.
            dimension.setValue("app01");
    
    // Initialize MetricDatum.
            MetricDatum sessionMetric = new MetricDatum();
    
    // Add dimension.
            sessionMetric.getDimensions().add(dimension);
    
    // Set metric name.
            sessionMetric.setMetricName("SessionMetric");
    
    // Set timestamp.
            sessionMetric.setTimestamp (Calendar.getInstance().getTime());
    
    // Set value.
            sessionMetric.setValue(1.0);
    		
    // Add metric to request.
            putMetricDataRequest.getMetricData() .add(sessionMetric);
    
    // Send metric request to CloudWatch.
            cloudWatchClient.putMetricData (putMetricDataRequest);
    }
  3. Get metric statistics.

    The following Java program retrieves statistics for the metric we created earlier:

    // Retrieve statistics for the specified metric.
        public static void GetMetricStatistics() {
    
    // Create BasicAWSCredentials with Access Key Id and Secret Access Key.
            BasicAWSCredentials credentials = new BasicAWSCredentials("Access Key Id",
                                " Secret Access Key ");
    
    // Create CloudWatch client.
            AmazonCloudWatchClient cloudWatchClient = new AmazonCloudWatchClient(credentials);
    
    // Set endpoint.
            cloudWatchClient.setEndpoint("monitoring.ap- southeast-1.amazonaws.com");
    
    // Initialize GetMetricStatisticsRequest.
            GetMetricStatisticsRequest getMetricStatisticsRequest=new GetMetricStatisticsRequest();
    
    // Set namespace.
            getMetricStatisticsRequest.setNamespace ("Production");
    
    // Initialize Dimension.
            Dimension dimension = new Dimension();
    
    // Set dimension name.
            dimension.setName("ApacheInstance");
    
    // Set dimesion value.
            dimension.setValue("app01");
    
    // Add dimension to request.
            getMetricStatisticsRequest.getDimensions() .add(dimension);
    
    // The metric statistics to return. Valid values are Average, Sum,
    // SampleCount, Maximum and Minimum.
            List<String> statistics = new ArrayList<String>();
    
    // Get average value.
            statistics.add("Average");
    
    // Add to request.
            getMetricStatisticsRequest.setStatistics (statistics);
    
    // The granularity, in seconds, of the returned data points.
            getMetricStatisticsRequest.setPeriod(300);
    
    // Get the calender object.
            Calendar cal = Calendar.getInstance();
    
    // Get the current time.
            Date now = cal.getTime();
    
    // Reset hours.
            cal.add(Calendar.HOUR, -1);
    
    // Get one hour back time.
            Date oneHourBack = cal.getTime();
    
    // The time stamp to use for determining the first data point to return.
            getMetricStatisticsRequest.setStartTime (oneHourBack);
    
    // The time stamp to use for determining the last data point to return
            getMetricStatisticsRequest.setEndTime(now);
    
    // Set metric name
            getMetricStatisticsRequest.setMetricName ("SessionMetric");
    
    // Get metric statistics.
            GetMetricStatisticsResult result = cloudWatchClient
            .getMetricStatistics(getMetricStatisticsRequest);
    
    // Get the label.
            String lable = result.getLabel();
    
    // Get the data points of specified metric.
            List<Datapoint> datapoints = result.getDatapoints();
    
    // Iterate through data points.
            for (Datapoint datapoint : datapoints) {
    
    // The average of metric values that correspond to the datapoint.
                double avg = datapoint.getAverage();
    
    // The time stamp used for the datapoint.
                Date timeStamp = datapoint.getTimestamp();
            }
        }

How it works…

In this recipe, we showed you the steps for collecting custom metrics from a Java program. In the first step, we need to install the AWS Java SDK. In our example, we used Production as our namespace. In order to uniquely identify each Apache server in our production deployment, we added the ApacheInstance dimension with a value of app01. Each Apache server in the production deployment is going to have this value for the dimension. Instead of hardcoding access key ID and secret access key, we can create the EC2 instance with an IAM role that has the access to CloudWatch. Using CloudWatch API, you can create your own dashboards and graphs based on the statistics. In our example, we insert the active user count metric into CloudWatch.

There's more…

We can use Elastic Beanstalk to gather and analyze additional health information on your environment. Elastic Beanstalk uses a health agent to monitor server logs and system metrics along with EBS data ad autoscaling to provide detailed health-related information on the EC2 instances. This information uses colors (green, red, and yellow), an indicator for the severity of the issues and a message indicating the cause. The health status can be viewed in real time on the AWS Management Console or through CLI commands. To record and track the enhanced health reporting over time, the information gathered by Elastic Beanstalk can be published to Amazon CloudWatch.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset