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.CloudWatchMetricConfig.NAMESPACE_DELIMITER;
 
 import java.util.List;
 import java.util.Map;
 
An internal builder used to retrieve the next batch of requests to be sent to Amazon CloudWatch. Calling method nextUploadUnits() blocks as necessary.
 
     private static final String OS_METRIC_NAME = MachineMetric.getOSMetricName();
     private final BlockingQueue<MetricDatumqueue;
     private final long timeoutNano;
 
         this. = queue;
     }

    
Returns the next batch of com.amazonaws.services.cloudwatch.model.PutMetricDataRequest to be sent to Amazon CloudWatch, blocking as necessary to gather and accumulate the necessary statistics. If there is no metrics data, this call blocks indefinitely. If there is metrics data, this call will block up to about CloudWatchMetricConfig.getQueuePollTimeoutMilli() number of milliseconds.
 
         final Map<String,MetricDatumuniqueMetrics = new HashMap<String,MetricDatum>();
         long startNano = System.nanoTime();
         
         while(true) {
             final long elapsedNano = System.nanoTime() - startNano;
             if (elapsedNano >= ) {
                 return toPutMetricDataRequests(uniqueMetrics);
             }
             MetricDatum datum = .poll( - elapsedNano.);
             if (datum == null) {
                 // timed out
                 if (uniqueMetrics.size() > 0) {
                     // return whatever we have so far
                     return toPutMetricDataRequests(uniqueMetrics);
                 }
                 // zero AWS related metrics
                 if (AwsSdkMetrics.isMachineMetricExcluded()) {
                     // Short note: nothing to do, so just wait indefinitely.
                     // (Long note: There exists a pedagogical case where the
                     // next statement is executed followed by no subsequent AWS
                     // traffic whatsoever, and then the machine metric is enabled 
                     // via JMX.
                     // In such case, we require the metric generation to be
                     // disabled and then re-enabled (eg via JMX).
                     // So why not always wake up periodically instead of going
                     // into long wait ?
                     // I (hchar@) think we should optimize for the most typical
                     // cases instead of the edge cases. Going into long wait has
                     // the benefit of relatively less runtime footprint.)
                     datum = .take();   
                     startNano = System.nanoTime();
                 }
             }
             // Note at this point datum is null if and only if there is no
             // pending AWS related metrics but machine metrics is enabled
             if (datum != null)
                 summarize(datumuniqueMetrics);
        }
    }

    
Summarizes the given datum into the statistics of the respective unique metric.
    private void summarize(MetricDatum datumMap<StringMetricDatumuniqueMetrics) {
        Double value = datum.getValue();
        if (value == null) {
            return;
        }
        List<Dimensiondims = datum.getDimensions();
        Collections.sort(dims.);
        String metricName = datum.getMetricName();
        String key = metricName + Jackson.toJsonString(dims);
        MetricDatum statDatum = uniqueMetrics.get(key);
        if (statDatum == null) {
            statDatum = new MetricDatum()
                .withDimensions(datum.getDimensions())
                .withMetricName(metricName)
                .withUnit(datum.getUnit())
                .withStatisticValues(new StatisticSet()
                    .withMaximum(value)
                    .withMinimum(value)
                    .withSampleCount(0.0)
                    .withSum(0.0))
                ;
            uniqueMetrics.put(keystatDatum);
        }
        StatisticSet stat = statDatum.getStatisticValues();
        stat.setSampleCount(stat.getSampleCount() + 1.0);
        stat.setSum(stat.getSum() + value);
        if (value > stat.getMaximum()) {
            stat.setMaximum(value);
        } else if (value < stat.getMinimum()) {
            stat.setMinimum(value);
        }
    }
    
Consolidates the input metrics into a list of PutMetricDataRequest, each within the maximum size limit imposed by CloudWatch.
        // Opportunistically generates some machine metrics whenever there
        // is metrics consolidation
        for (MetricDatum datum.generateMetrics()) {
            summarize(datumuniqueMetrics);
        }
        List<PutMetricDataRequestlist = new ArrayList<PutMetricDataRequest>();
        List<MetricDatumdata = new ArrayList<MetricDatum>();
        for (MetricDatum muniqueMetrics.values()) {
            data.add(m);
            if (data.size() == .) {
                list.addAll(newPutMetricDataRequests(data));
                data.clear();
            }
        }
        if (data.size() > 0) {
            list.addAll(newPutMetricDataRequests(data));
        }
        return list;
    }
        List<PutMetricDataRequestlist = new ArrayList<PutMetricDataRequest>();
        final String ns = AwsSdkMetrics.getMetricNameSpace();
        PutMetricDataRequest req = newPutMetricDataRequest(datans);
        list.add(req);
        final boolean perHost = AwsSdkMetrics.isPerHostMetricEnabled();
        String perHostNameSpace = null;
        String hostName = null;
        Dimension hostDim = null;
        final boolean singleNamespace = AwsSdkMetrics.isSingleMetricNamespace();
        if (perHost) {
            hostName = AwsSdkMetrics.getHostMetricName();
            hostName = hostName == null ? "" : hostName.trim();
            if (hostName.length() == 0)
                hostName = AwsHostNameUtils.localHostName();
            hostDim = dimension(.hostName);
            if (singleNamespace) {
                req = newPutMetricDataRequest(datanshostDim);
            } else {
                perHostNameSpace = ns +  + hostName;
                req = newPutMetricDataRequest(dataperHostNameSpace);
            }
            list.add(req);
        }
        String jvmMetricName = AwsSdkMetrics.getJvmMetricName();
        if (jvmMetricName != null) {
            jvmMetricName = jvmMetricName.trim();
            if (jvmMetricName.length() > 0) {
                if (singleNamespace) {
                    Dimension jvmDim = dimension(.jvmMetricName);
                    if (perHost) {
                        // If OS metrics are already included at the per host level,
                        // there is little reason, if any, to include them at the
                        // JVM level.  Hence the filtering.
                        req = newPutMetricDataRequest(
                                filterOSMetrics(data), nshostDimjvmDim);
                    } else {
                        req = newPutMetricDataRequest(datansjvmDim);
                    }
                    
                } else {
                    String perJvmNameSpace = perHostNameSpace == null
                        ? ns +  + jvmMetricName
                        : perHostNameSpace +  + jvmMetricName
                        ;
                    // If OS metrics are already included at the per host level,
                    // there is little reason, if any, to include them at the
                    // JVM level.  Hence the filtering.
                    req = newPutMetricDataRequest
                        (perHost ? filterOSMetrics(data) : dataperJvmNameSpace);
                }
                list.add(req);
            }
        }
        return list;
    }

    
Return a collection of metrics almost the same as the input except with all OS metrics removed.
        Collection<MetricDatumoutput = new ArrayList<MetricDatum>(data.size());
        for (MetricDatum datumdata) {
            if (!.equals(datum.getMetricName()))
                output.add(datum);
        }
        return output;
    }
            Collection<MetricDatumdatafinal String namespace,
            final Dimension... extraDims) {
        if (extraDims != null) {
            // Need to add some extra dimensions.
            // To do so, we copy the metric data to avoid mutability problems.
            Collection<MetricDatumnewData = new ArrayList<MetricDatum>(data.size());
            for (MetricDatum mddata) {
                MetricDatum newMD = cloneMetricDatum(md);
                for (Dimension dimextraDims)
                    newMD.withDimensions(dim);  // add the extra dimensions to the new metric datum
                newData.add(newMD);
            }
            data = newData;
        }
        return new PutMetricDataRequest()
            .withNamespace(namespace)
            .withMetricData(data)
            ;
    }

    
Returns a metric datum cloned from the given one. Made package private only for testing purposes.
        return new MetricDatum()
            .withDimensions(md.getDimensions()) // a new collection is created
            .withMetricName(md.getMetricName())
            .withStatisticValues(md.getStatisticValues())
            .withTimestamp(md.getTimestamp())
            .withUnit(md.getUnit())
            .withValue(md.getValue());
    }
    private Dimension dimension(Dimensions nameString value) {
        return new Dimension().withName(name.toString()).withValue(value);
    }
New to GrepCode? Check out our FAQ X