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

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

Introduction

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

Prototype


public MetricDatum withTimestamp(java.util.Date timestamp) 

Source Link

Document

The time the metric data was received, expressed as the number of milliseconds since Jan 1, 1970 00:00:00 UTC.

Usage

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

License:Apache License

/**
 * Reports the given metrics to CloudWatch.
 *
 * @param gauges     gauge metrics.//from   ww w  .j a v a  2 s .  com
 * @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);
}