All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gobblin.metrics.hadoop.AbstractHadoopCounterReporter Maven / Gradle / Ivy

There is a newer version: 0.8.0
Show newest version
package gobblin.metrics.hadoop;

import java.util.Map;
import java.util.SortedMap;
import java.util.concurrent.TimeUnit;

import com.codahale.metrics.Counter;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricFilter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;

import com.google.common.collect.Maps;

import gobblin.metrics.reporter.ContextAwareScheduledReporter;
import gobblin.metrics.Measurements;
import gobblin.metrics.MetricContext;


/**
 * An extension to {@link ContextAwareScheduledReporter} that serves as the basis
 * for implementations that report applicable metrics through Hadoop counters.
 *
 * @author Yinan Li
 */
public abstract class AbstractHadoopCounterReporter extends ContextAwareScheduledReporter {

  private final Map previousCounts = Maps.newHashMap();

  protected AbstractHadoopCounterReporter(MetricContext context, String name, MetricFilter filter,
      TimeUnit rateUnit, TimeUnit durationUnit) {
    super(context, name, filter, rateUnit, durationUnit);
  }

  @Override
  protected void reportInContext(MetricContext context,
                                 SortedMap gauges,
                                 SortedMap counters,
                                 SortedMap histograms,
                                 SortedMap meters,
                                 SortedMap timers) {
    for (Map.Entry entry : gauges.entrySet()) {
      Gauge gauge = entry.getValue();
      if (gauge.getValue() instanceof Long ||
          gauge.getValue() instanceof Integer ||
          gauge.getValue() instanceof Short ||
          gauge.getValue() instanceof Byte)
        reportValue(context, entry.getKey(), ((Number) gauge.getValue()).longValue());
    }

    for (Map.Entry entry : counters.entrySet()) {
      reportCount(context, entry.getKey(), entry.getValue().getCount());
    }

    for (Map.Entry entry : meters.entrySet()) {
      reportCount(context, entry.getKey(), entry.getValue().getCount());
    }

    for (Map.Entry entry : histograms.entrySet()) {
      reportCount(context, entry.getKey(), entry.getValue().getCount());
    }

    for (Map.Entry entry : timers.entrySet()) {
      reportCount(context, entry.getKey(), entry.getValue().getCount());
    }
  }

  /**
   * Report a given incremental value of a metric.
   *
   * @param context the {@link MetricContext} this is associated to
   * @param name metric name
   * @param incremental the given incremental value
   */
  protected abstract void reportIncremental(MetricContext context, String name, long incremental);

  /**
   * Report a given value of a metric.
   *
   * @param context the {@link MetricContext} this is associated to
   * @param name metric name
   * @param value the given value
   */
  protected abstract void reportValue(MetricContext context, String name, long value);

  private void reportCount(MetricContext context, String name, long currentCount) {
    reportIncremental(context, MetricRegistry.name(name, Measurements.COUNT.getName()),
        calculateIncremental(name, currentCount));
    // Remember the current count
    this.previousCounts.put(name, currentCount);
  }

  private long calculateIncremental(String name, long currentCount) {
    if (this.previousCounts.containsKey(name)) {
      return currentCount - this.previousCounts.get(name);
    }
    return currentCount;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy