Example usage for com.amazonaws.services.cloudwatch.model MetricDatum getStatisticValues

List of usage examples for com.amazonaws.services.cloudwatch.model MetricDatum getStatisticValues

Introduction

In this page you can find the example usage for com.amazonaws.services.cloudwatch.model MetricDatum getStatisticValues.

Prototype


public StatisticSet getStatisticValues() 

Source Link

Document

The statistical values for the metric.

Usage

From source file:com.amazon.kinesis.streaming.agent.metrics.AccumulatingMetricsScope.java

License:Open Source License

@Override
protected void realAddData(String name, double value, StandardUnit unit) {
    MetricDatum datum = data.get(name);
    if (datum == null) {
        data.put(name, new MetricDatum().withMetricName(name).withUnit(unit).withStatisticValues(
                new StatisticSet().withMaximum(value).withMinimum(value).withSampleCount(1.0).withSum(value)));
    } else {/* w  w  w  .ja va 2  s  .  c om*/
        if (!datum.getUnit().equals(unit.name())) {
            throw new IllegalArgumentException("Cannot add to existing metric with different unit");
        }
        StatisticSet statistics = datum.getStatisticValues();
        statistics.setMaximum(Math.max(value, statistics.getMaximum()));
        statistics.setMinimum(Math.min(value, statistics.getMinimum()));
        statistics.setSampleCount(statistics.getSampleCount() + 1);
        statistics.setSum(statistics.getSum() + value);
    }
}

From source file:com.amazon.kinesis.streaming.agent.metrics.LogMetricsScope.java

License:Open Source License

@Override
protected void realCommit() {
    if (!data.values().isEmpty()) {
        StringBuilder output = new StringBuilder();
        output.append("Metrics:\n");

        output.append("Dimensions: ");
        boolean needsComma = false;
        for (Dimension dimension : getDimensions()) {
            output.append(String.format("%s[%s: %s]", needsComma ? ", " : "", dimension.getName(),
                    dimension.getValue()));
            needsComma = true;//from  w  w  w .  j  ava2  s.c  om
        }
        output.append("\n");

        for (MetricDatum datum : data.values()) {
            StatisticSet statistics = datum.getStatisticValues();
            output.append(
                    String.format("Name=%50s\tMin=%.2f\tMax=%.2f\tCount=%.2f\tSum=%.2f\tAvg=%.2f\tUnit=%s\n",
                            datum.getMetricName(), statistics.getMinimum(), statistics.getMaximum(),
                            statistics.getSampleCount(), statistics.getSum(),
                            statistics.getSum() / statistics.getSampleCount(), datum.getUnit()));
        }
        LOGGER.debug(output.toString());
    }
}

From source file:com.amazon.kinesis.streaming.agent.metrics.MetricAccumulatingQueue.java

License:Open Source License

private void accumulate(MetricDatum oldDatum, MetricDatum newDatum) {
    if (!oldDatum.getUnit().equals(newDatum.getUnit())) {
        throw new IllegalArgumentException("Unit mismatch for datum named " + oldDatum.getMetricName());
    }/*from w ww .ja v a  2 s.  co  m*/

    StatisticSet oldStats = oldDatum.getStatisticValues();
    StatisticSet newStats = newDatum.getStatisticValues();

    oldStats.setSampleCount(oldStats.getSampleCount() + newStats.getSampleCount());
    oldStats.setMaximum(Math.max(oldStats.getMaximum(), newStats.getMaximum()));
    oldStats.setMinimum(Math.min(oldStats.getMinimum(), newStats.getMinimum()));
    oldStats.setSum(oldStats.getSum() + newStats.getSum());
}

From source file:com.blacklocus.metrics.CloudWatchReporter.java

License:Apache License

@Override
public void report(SortedMap<String, Gauge> gauges, SortedMap<String, Counter> counters,
        SortedMap<String, Histogram> histograms, SortedMap<String, Meter> meters,
        SortedMap<String, Timer> timers) {

    try {//from  w w  w  .j  a va 2 s .co m
        List<MetricDatum> data = new ArrayList<MetricDatum>(
                gauges.size() + counters.size() + meters.size() + 2 * histograms.size() + 2 * timers.size());
        // something like that

        for (Map.Entry<String, Gauge> gaugeEntry : gauges.entrySet()) {
            reportGauge(gaugeEntry, "gauge", data);
        }

        for (Map.Entry<String, Counter> counterEntry : counters.entrySet()) {
            reportCounter(counterEntry, "counterSum", data);
        }

        for (Map.Entry<String, Meter> meterEntry : meters.entrySet()) {
            reportCounter(meterEntry, "meterSum", data);
        }

        for (Map.Entry<String, Histogram> histogramEntry : histograms.entrySet()) {
            reportCounter(histogramEntry, "histogramCount", data);
            reportSampling(histogramEntry, "histogramSet", 1.0, data);
        }

        for (Map.Entry<String, Timer> timerEntry : timers.entrySet()) {
            reportCounter(timerEntry, "timerCount", data);
            reportSampling(timerEntry, "timerSet", 0.000001, data); // nanos -> millis
        }

        // CloudWatch rejects any Statistic Sets with sample count == 0
        Collection<MetricDatum> nonEmptyData = Collections2.filter(data, new Predicate<MetricDatum>() {
            @Override
            public boolean apply(MetricDatum input) {
                if (input == null) {
                    return false;
                } else if (input.getStatisticValues() != null) {
                    return input.getStatisticValues().getSampleCount() > 0;
                }
                return true;
            }
        });
        // Each CloudWatch API request may contain at maximum 20 datums.
        Iterable<List<MetricDatum>> dataPartitions = Iterables.partition(nonEmptyData, 20);
        List<Future<?>> cloudWatchFutures = Lists.newArrayListWithExpectedSize(data.size());

        for (List<MetricDatum> dataSubset : dataPartitions) {
            cloudWatchFutures.add(cloudWatch.putMetricDataAsync(
                    new PutMetricDataRequest().withNamespace(metricNamespace).withMetricData(dataSubset)));
        }
        for (Future<?> cloudWatchFuture : cloudWatchFutures) {
            // We can't let an exception leak out of here, or else the reporter will cease running per mechanics of
            // java.util.concurrent.ScheduledExecutorService.scheduleAtFixedRate(Runnable, long, long, TimeUnit unit)
            try {
                // See what happened in case of an error.
                cloudWatchFuture.get();
            } catch (Exception e) {
                LOG.error(
                        "Exception reporting metrics to CloudWatch. The data sent in this CloudWatch API request "
                                + "may have been discarded.",
                        e);
            }
        }

        LOG.info("Sent {} metric data to CloudWatch. namespace: {}", data.size(), metricNamespace);

    } catch (RuntimeException e) {
        LOG.error("Error marshalling CloudWatch metrics.", e);
    }
}

From source file:com.github.lpezet.antiope.metrics.aws.BlockingRequestBuilder.java

License:Open Source License

/**
 * Summarizes the given datum into the statistics of the respective unique metric.
 *///w  w w . ja  v a2s.  co  m
private void summarize(MetricDatum pDatum, Map<String, MetricDatum> pUniqueMetrics) {
    Double pValue = pDatum.getValue();
    if (pValue == null) {
        return;
    }
    List<Dimension> oDims = pDatum.getDimensions();
    Collections.sort(oDims, DimensionComparator.INSTANCE);
    String oMetricName = pDatum.getMetricName();
    String k = oMetricName + Jackson.toJsonString(oDims);
    MetricDatum oStatDatum = pUniqueMetrics.get(k);
    if (oStatDatum == null) {
        oStatDatum = new MetricDatum().withDimensions(pDatum.getDimensions()).withMetricName(oMetricName)
                .withUnit(pDatum.getUnit()).withStatisticValues(new StatisticSet().withMaximum(pValue)
                        .withMinimum(pValue).withSampleCount(0.0).withSum(0.0));
        pUniqueMetrics.put(k, oStatDatum);
    }
    StatisticSet oStat = oStatDatum.getStatisticValues();
    oStat.setSampleCount(oStat.getSampleCount() + 1.0);
    oStat.setSum(oStat.getSum() + pValue);
    if (pValue > oStat.getMaximum()) {
        oStat.setMaximum(pValue);
    } else if (pValue < oStat.getMinimum()) {
        oStat.setMinimum(pValue);
    }
}

From source file:com.github.lpezet.antiope.metrics.aws.BlockingRequestBuilder.java

License:Open Source License

/**
 * Returns a metric datum cloned from the given one.
 * Made package private only for testing purposes.
 *//*from  w w  w.j  a va2  s .  c  om*/
final MetricDatum cloneMetricDatum(MetricDatum pMd) {
    return new MetricDatum().withDimensions(pMd.getDimensions()) // a new collection is created
            .withMetricName(pMd.getMetricName()).withStatisticValues(pMd.getStatisticValues())
            .withTimestamp(pMd.getTimestamp()).withUnit(pMd.getUnit()).withValue(pMd.getValue());
}

From source file:com.github.lpezet.antiope.metrics.aws.spi.MetricData.java

License:Open Source License

/**
 * Returns a new metric datum cloned from the given metric datum, but
 * replacing the dimensions with the newly specified ones.
 *//* w  w  w . j  ava 2  s.c  o  m*/
public static MetricDatum newMetricDatum(MetricDatum pFrom, Dimension... pDimensions) {
    return new MetricDatum().withMetricName(pFrom.getMetricName()).withDimensions(pDimensions)
            .withUnit(pFrom.getUnit()).withValue(pFrom.getValue())
            .withStatisticValues(pFrom.getStatisticValues()).withTimestamp(pFrom.getTimestamp());
}

From source file:com.jlhood.metrics.CloudWatchReporter.java

License:Apache License

/**
 * Reports the given metrics to CloudWatch.
 *
 * @param gauges     gauge metrics.// w w  w.jav  a2  s .co m
 * @param counters   counter metrics.
 * @param histograms histogram metrics.
 * @param meters     meter metrics.
 * @param timers     timer metrics.
 */
void report(SortedMap<String, Gauge> gauges, SortedMap<String, Counter> counters,
        SortedMap<String, Histogram> histograms, SortedMap<String, Meter> meters,
        SortedMap<String, Timer> timers) {

    // Just an estimate to reduce resizing.
    List<MetricDatum> data = new ArrayList<MetricDatum>(
            gauges.size() + counters.size() + meters.size() + 2 * histograms.size() + 2 * timers.size());

    // Translate various metric classes to MetricDatum
    for (Map.Entry<String, Gauge> gaugeEntry : gauges.entrySet()) {
        reportGauge(gaugeEntry, typeDimValGauge, data);
    }
    for (Map.Entry<String, Counter> counterEntry : counters.entrySet()) {
        reportCounter(counterEntry, typeDimValCounterCount, data);
    }
    for (Map.Entry<String, Meter> meterEntry : meters.entrySet()) {
        reportCounter(meterEntry, typeDimValMeterCount, data);
    }
    for (Map.Entry<String, Histogram> histogramEntry : histograms.entrySet()) {
        reportCounter(histogramEntry, typeDimValHistoSamples, data);
        reportSampling(histogramEntry, typeDimValHistoStats, 1.0, data);
    }
    for (Map.Entry<String, Timer> timerEntry : timers.entrySet()) {
        reportCounter(timerEntry, typeDimValTimerSamples, data);
        reportSampling(timerEntry, typeDimValTimerStats, 0.000001, data); // nanos -> millis
    }

    // Filter out unreportable entries.
    Collection<MetricDatum> nonEmptyData = Collections2.filter(data, new Predicate<MetricDatum>() {
        @Override
        public boolean apply(MetricDatum input) {
            if (input == null) {
                return false;
            } else if (input.getStatisticValues() != null) {
                // CloudWatch rejects any Statistic Sets with sample count == 0, which it probably should reject.
                return input.getStatisticValues().getSampleCount() > 0;
            }
            return true;
        }
    });

    // Whether to use local "now" (true, new Date()) or cloudwatch service "now" (false, leave null).
    if (timestampLocal) {
        Date now = new Date();
        for (MetricDatum datum : nonEmptyData) {
            datum.withTimestamp(now);
        }
    }

    // Finally, apply any user-level filter.
    Collection<MetricDatum> filtered = Collections2.filter(nonEmptyData, reporterFilter);

    // Each CloudWatch API request may contain at maximum 20 datums. Break into partitions of 20.
    Iterable<List<MetricDatum>> dataPartitions = Iterables.partition(filtered, 20);
    List<Future<?>> cloudWatchFutures = Lists.newArrayListWithExpectedSize(filtered.size());

    // Submit asynchronously with threads.
    for (List<MetricDatum> dataSubset : dataPartitions) {
        cloudWatchFutures.add(cloudWatch.putMetricDataAsync(
                new PutMetricDataRequest().withNamespace(metricNamespace).withMetricData(dataSubset)));
    }

    // Wait for CloudWatch putMetricData futures to be fulfilled.
    for (Future<?> cloudWatchFuture : cloudWatchFutures) {
        try {
            cloudWatchFuture.get();
        } catch (Exception e) {
            LOG.error(
                    "Exception reporting metrics to CloudWatch. Some or all of the data in this CloudWatch API request "
                            + "may have been discarded, did not make it to CloudWatch.",
                    e);
        }
    }

    LOG.debug("Sent {} metric data to CloudWatch. namespace: {}", filtered.size(), metricNamespace);
}

From source file:org.web.online.cloudwatch.tomcat.valve.ElapsedTimeAggregator.java

License:Apache License

/**
 * Collect the aggregated values (min, max, count, sum) into a
 * StatisticSet and send the data to Cloud Watch
 *///from   w  ww  .java  2s  . com
@Override
public void run() {

    PutMetricDataRequest localPutMetricDataRequest = zeroValuePutMetricDataRequest;
    MetricDatum metricDatum = localPutMetricDataRequest.getMetricData().get(0);

    if (sampleCount > 0) {
        localPutMetricDataRequest = putMetricDataRequest;
        metricDatum = localPutMetricDataRequest.getMetricData().get(0);
        StatisticSet statisticSet = metricDatum.getStatisticValues();
        synchronized (lock) {
            statisticSet.setMaximum(maximum);
            statisticSet.setMinimum(minimum);
            statisticSet.setSampleCount(sampleCount);
            statisticSet.setSum(sum);

            minimum = Double.MAX_VALUE;
            maximum = Double.MIN_VALUE;
            sampleCount = 0;
            sum = 0;
        }
    }

    metricDatum.setTimestamp(new Date());

    if (log.isDebugEnabled()) {
        log.debug("sending " + localPutMetricDataRequest);
    }

    cloudWatchClient.putMetricData(localPutMetricDataRequest);
}