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 java.util.Set;
 
 
This is the default implementation of an AWS SDK service metric collection system.

 
 {
     static final double NANO_PER_SEC = ..toNanos(1);
     protected final static Log log = LogFactory.getLog(ServiceMetricCollectorSupport.class);
     private final BlockingQueue<MetricDatumqueue;
 
         this. = queue;
     }
 
     @Override
     public void collectByteThroughput(ByteThroughputProvider provider) {
         try {
             collectByteThroughput0(provider);
         } catch(Exception ex) { // defensive code
             if (.isDebugEnabled()) {
                 .debug("Ignoring unexpected failure"ex);
             }
         }
     }
    
    
Returns the number of bytes per second, given the byte count and duration in nano seconds. Duration of zero nanosecond will be treated as 1 nanosecond.
 
     double bytesPerSecond(double byteCountdouble durationNano) {
         if (byteCount < 0 || durationNano < 0)
             throw new IllegalArgumentException();
         if (durationNano == 0) {
             durationNano = 1.0;   // defend against division by zero
             if (.isDebugEnabled()) {
                 .debug("Set zero to one to avoid division by zero; but should never get here!");
             }
         }
         double bytesPerSec = (byteCount / durationNano) * ;
         if (bytesPerSec == 0) {
             if (.isDebugEnabled()) {
                 .debug("zero bytes per sec.  Really ?");
             }
         }
         return bytesPerSec;
     }
     
     private void collectByteThroughput0(ByteThroughputProvider provider) {
         final ThroughputMetricType throughputType = provider.getThroughputMetricType();
         final ServiceMetricType byteCountType = throughputType.getByteCountMetricType();
         final Set<MetricTypemetrics = AwsSdkMetrics.getPredefinedMetrics();
         final double byteCount = provider.getByteCount();
         double durationNano = provider.getDurationNano();
         double bytesPerSec = bytesPerSecond(byteCountdurationNano);
         if (metrics.contains(throughputType)) {
             // Throughput metric
             final Dimension throughputDimension = new Dimension()
                 .withName(..name())
                .withValue(throughputType.name());
            final MetricDatum throughputDatum = new MetricDatum()
                .withMetricName(throughputType.getServiceName())
                .withDimensions(throughputDimension)
                .withUnit(.)
                .withValue(bytesPerSec);
            safeAddMetricsToQueue(throughputDatum);
        }
        if (metrics.contains(byteCountType)) {
            // Byte count metric
            final Dimension byteCountDimension = new Dimension()
                .withName(..name())
                .withValue(byteCountType.name());
            final MetricDatum byteCountDatum = new MetricDatum()
                .withMetricName(byteCountType.getServiceName())
                .withDimensions(byteCountDimension)
                .withUnit(.)
                .withValue(byteCount);
            safeAddMetricsToQueue(byteCountDatum);
        }
    }
    @Override
    public void collectLatency(ServiceLatencyProvider provider) {
        final ServiceMetricType type = provider.getServiceMetricType();
        final Set<MetricTypemetrics = AwsSdkMetrics.getPredefinedMetrics();
        if (metrics.contains(type)) {
            final Dimension dim = new Dimension()
                .withName(..name())
                .withValue(type.name());
            final MetricDatum datum = new MetricDatum()
                .withMetricName(type.getServiceName())
                .withDimensions(dim)
                .withUnit(.)
                .withValue(provider.getDurationMilli());
            safeAddMetricsToQueue(datum);
        }
    }
    private void safeAddMetricsToQueue(MetricDatum metric) {
        try {
            if (!addMetricsToQueue(metric)) {
                if (.isDebugEnabled()) {
                    .debug("Failed to add to the metrics queue (due to no space available) for "
                            + metric.getMetricName());
                }
            }
        } catch (RuntimeException ex) {
            .warn("Failed to add to the metrics queue for metric: " + metric,
                    ex);
        }
    }
    
Adds the given metric to the queue, returning true if successful or false if no space available.
    protected boolean addMetricsToQueue(MetricDatum metric) {
        return .offer(metric); 
    }
New to GrepCode? Check out our FAQ X