Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
   *
   * Licensed under the Apache License, Version 2.0 (the "License").
   * You may not use this file except in compliance with the License.
   * A copy of the License is located at
   *
   *  http://aws.amazon.com/apache2.0
   *
  * or in the "license" file accompanying this file. This file 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 com.amazonaws.metrics.internal.cloudwatch;
 
 import static com.amazonaws.metrics.internal.cloudwatch.spi.MetricData.newMetricDatum;
 import static com.amazonaws.metrics.internal.cloudwatch.spi.RequestMetricTransformer.Utils.endTimestamp;
 
 import java.util.List;
 
 
Used to transform the predefined metrics of the AWS SDK into instances of com.amazonaws.services.cloudwatch.model.MetricDatum. See http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/ publishingMetrics.html

 
 public class PredefinedMetricTransformer {
     private static final Log log = LogFactory.getLog(PredefinedMetricTransformer.class);
     static final boolean INCLUDE_REQUEST_TYPE = true;
     static final boolean EXCLUDE_REQUEST_TYPE = !;

    
Returns a non-null list of metric datum for the metrics collected for the given request/response.

Parameters:
metricType the request metric type
 
     public List<MetricDatumtoMetricData(MetricType metricTypeRequest<?> requestResponse<?> response) {
         if (metricType instanceof Field) {
             // Predefined metrics across all aws http clients
             Field predefined = (FieldmetricType;
             switch(predefined) {
                 case :
                 case :
                 case :
                 case :
                     return metricOfCount(predefinedrequestresponse);
                 case :  // intentionally fall thru to reuse the same routine as RetryCount
                 case :
                     return metricOfRequestOrRetryCount(predefinedrequestresponse);
                 case // drop thru
                 case // drop thru
                     return latencyMetricOf(predefinedrequestresponse);
                 case :
                     return latencyOfClientExecuteTime(requestresponse);
                 case :
                 case :
                 case :
                     return latencyMetricOf(predefinedrequestresponse);
                 case :
                 case :
                     return counterMetricOf(predefinedrequestresponse);
                 default:
                     break;
             }
         }
         // Predefined metrics for specific service clients
         for (AWSMetricTransformerFactory aws: AWSMetricTransformerFactory.values()) {
             if (metricType.name().startsWith(aws.name())) {
                 List<MetricDatummetricData = aws.getRequestMetricTransformer()
                         .toMetricData(metricTyperequestresponse);
                if (metricData != null)
                    return metricData;
                break;
            }
        }
        if (.isDebugEnabled()) {
            AmazonWebServiceRequest origReq = request == null ? null : request
                    .getOriginalRequest();
            String reqClassName = origReq == null ? null : origReq.getClass().getName();
            .debug("No request metric transformer can be found for metric type "
                    + metricType.name() + " for " + reqClassName);
        }
        return Collections.emptyList();
    }

    
Returns a list with a single metric datum for the specified retry or request count predefined metric; or an empty list if there is none.

            Field metricTypeRequest<?> reqObject resp) {
        AWSRequestMetrics m = req.getAWSRequestMetrics(); 
        TimingInfo ti = m.getTimingInfo();
        // Always retrieve the request count even for retry which is equivalent
        // to the number of requests minus one.
        Number counter = ti.getCounter(..name());
        if (counter == null) {
            // this is possible if one of the request handlers screwed up
            return Collections.emptyList();
        }
        int requestCount = counter.intValue();
        if (requestCount < 1) {
            LogFactory.getLog(getClass()).warn(
                "request count must be at least one");
            return Collections.emptyList();
        }
        final double count = metricType == . 
                           ? requestCount
                           : requestCount-1 // retryCount = requestCount - 1
                           ;
        if (count < 1) {
            return Collections.emptyList();
        } else {
            return Collections.singletonList(new MetricDatum()
                .withMetricName(req.getServiceName())
                .withDimensions(new Dimension()
                    .withName(..name())
                    .withValue(metricType.name()))
                .withUnit(.)
                .withValue(Double.valueOf(count))
                .withTimestamp(endTimestamp(ti)))
                ;
        }
    }
    protected List<MetricDatummetricOfCount(
            Field metricTypeRequest<?> reqObject resp) {
        AWSRequestMetrics m = req.getAWSRequestMetrics(); 
        TimingInfo ti = m.getTimingInfo();
        Number counter = ti.getCounter(metricType.name());
        if (counter == null) {
            return Collections.emptyList();
        }
        final double count = counter.doubleValue();
        if (count < 1) {
            return Collections.emptyList();
        } else {
            return Collections.singletonList(new MetricDatum()
                .withMetricName(req.getServiceName())
                .withDimensions(new Dimension()
                    .withName(..name())
                    .withValue(metricType.name()))
                .withUnit(.)
                .withValue(Double.valueOf(count))
                .withTimestamp(endTimestamp(ti)))
                ;
        }
    }
    
    
Returns all the latency metric data recorded for the specified metric event type; or an empty list if there is none. The number of metric datum in the returned list should be exactly one when there is no retries, or more than one when there are retries.

Parameters:
includesRequestType true iff the "request" dimension is to be included;
    protected List<MetricDatumlatencyMetricOf(MetricType metricType,
            Request<?> reqObject responseboolean includesRequestType) {
        AWSRequestMetrics m = req.getAWSRequestMetrics();
        TimingInfo root = m.getTimingInfo();
        final String metricName = metricType.name();
        List<TimingInfosubMeasures =
            root.getAllSubMeasurements(metricName);
        if (subMeasures != null) {
            List<MetricDatumresult =
                new ArrayList<MetricDatum>(subMeasures.size());
            for (TimingInfo sub : subMeasures) {
                if (sub.isEndTimeKnown()) { // being defensive
                    List<Dimensiondims = new ArrayList<Dimension>();
                    dims.add(new Dimension()
                            .withName(..name())
                            .withValue(metricName));
                    // Either a non request type specific datum is created per
                    // sub-measurement, or a request type specific one is 
                    // created but not both
                    if (includesRequestType) {
                        dims.add(new Dimension()
                                .withName(..name())
                                .withValue(requestType(req)));
                    }
                    MetricDatum datum = new MetricDatum()
                        .withMetricName(req.getServiceName())
                        .withDimensions(dims)
                        .withUnit(.)
                        .withValue(sub.getTimeTakenMillisIfKnown());
                    result.add(datum);
                }
            }
            return result;
        }
        return Collections.emptyList();
    }

    
Returns a request type specific metrics for com.amazonaws.util.AWSRequestMetrics.Field.ClientExecuteTime which is special in the sense that it makes a more accurate measurement by taking the com.amazonaws.util.TimingInfo at the root into account.
    protected List<MetricDatumlatencyOfClientExecuteTime(Request<?> reqObject response) {
        AWSRequestMetrics m = req.getAWSRequestMetrics();
        TimingInfo root = m.getTimingInfo();
        final String metricName = ..name();
        if (root.isEndTimeKnown()) { // being defensive
            List<Dimensiondims = new ArrayList<Dimension>();
            dims.add(new Dimension()
                    .withName(..name())
                    .withValue(metricName));
            // request type specific
            dims.add(new Dimension()
                    .withName(..name())
                    .withValue(requestType(req)));
            MetricDatum datum = new MetricDatum()
                .withMetricName(req.getServiceName())
                .withDimensions(dims)
                .withUnit(.)
                .withValue(root.getTimeTakenMillisIfKnown());
            return Collections.singletonList(datum);
        }
        return Collections.emptyList();
    }
    
    
Returns the name of the type of request.
    private String requestType(Request<?> req) {
        return req.getOriginalRequest().getClass().getSimpleName();
    }

    
Returns a list of metric datum recorded for the specified counter metric type; or an empty list if there is none.

Parameters:
includesRequestType true iff an additional metric datum is to be created that includes the "request" dimension
    protected List<MetricDatumcounterMetricOf(MetricType type,
            Request<?> reqObject respboolean includesRequestType) {
        AWSRequestMetrics m = req.getAWSRequestMetrics(); 
        TimingInfo ti = m.getTimingInfo();
        final String metricName = type.name();
        Number counter = ti.getCounter(metricName);
        if (counter == null) {
            return Collections.emptyList();
        }
        int count = counter.intValue();
        if (count < 1) {
            LogFactory.getLog(getClass()).warn("Count must be at least one");
            return Collections.emptyList();
        }
        final List<MetricDatumresult = new ArrayList<MetricDatum>();
        final Dimension metricDimension = new Dimension()
            .withName(..name())
            .withValue(metricName);
        // non-request type specific metric datum
        final MetricDatum first = new MetricDatum()
            .withMetricName(req.getServiceName())
            .withDimensions(metricDimension)
            .withUnit(.)
            .withValue(Double.valueOf(count))
            .withTimestamp(endTimestamp(ti));
        result.add(first);
        if (includesRequestType) {
            // additional request type specific metric datum
            Dimension requestDimension = new Dimension()
                .withName(..name())
                .withValue(requestType(req));
            final MetricDatum second = 
                newMetricDatum(firstmetricDimensionrequestDimension);
            result.add(second);
        }
        return result;
    }
New to GrepCode? Check out our FAQ X