gobblin.metrics.reporter.MetricReportReporter Maven / Gradle / Ivy
                 Go to download
                
        
                    Show more of this group  Show more artifacts with this name
Show all versions of gobblin-metrics Show documentation
                Show all versions of gobblin-metrics Show documentation
Gobblin Ingestion Framework
                
            /*
 * Copyright (C) 2014-2016 LinkedIn Corp. 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. You may obtain a copy of the
 * License at  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied.
 */
package gobblin.metrics.reporter;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import javax.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.codahale.metrics.Counter;
import com.codahale.metrics.Counting;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Meter;
import com.codahale.metrics.Metered;
import com.codahale.metrics.MetricFilter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Snapshot;
import com.codahale.metrics.Timer;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.typesafe.config.Config;
import gobblin.metrics.Measurements;
import gobblin.metrics.Metric;
import gobblin.metrics.MetricReport;
/**
 * Scheduled reporter based on {@link gobblin.metrics.MetricReport}.
 *
 * 
 *   This class will generate a metric report, and call {@link #emitReport} to actually emit the metrics.
 * 
 */
public abstract class MetricReportReporter extends ConfiguredScheduledReporter {
  private static final Logger LOGGER = LoggerFactory.getLogger(MetricReportReporter.class);
  public MetricReportReporter(Builder> builder, Config config) {
    super(builder, config);
  }
  /**
   * Builder for {@link MetricReportReporter}. Defaults to no filter, reporting rates in seconds and times in
   * milliseconds.
   */
  public static abstract class Builder> extends
      ConfiguredScheduledReporter.Builder {
 
    protected MetricFilter filter;
    protected Builder() {
      super();
      this.name = "MetricReportReporter";
      this.filter = MetricFilter.ALL;
    }
    /**
     * Only report metrics which match the given filter.
     *
     * @param filter a {@link MetricFilter}
     * @return {@code this}
     */
    public T filter(MetricFilter filter) {
      this.filter = filter;
      return self();
    }
  }
  
  /**
   * Serializes metrics and pushes the byte arrays to Kafka. Uses the serialize* methods in {@link MetricReportReporter}.
   *
   * @param gauges map of {@link com.codahale.metrics.Gauge} to report and their name.
   * @param counters map of {@link com.codahale.metrics.Counter} to report and their name.
   * @param histograms map of {@link com.codahale.metrics.Histogram} to report and their name.
   * @param meters map of {@link com.codahale.metrics.Meter} to report and their name.
   * @param timers map of {@link com.codahale.metrics.Timer} to report and their name.
   */
  @Override
  protected void report(SortedMap gauges, SortedMap counters,
      SortedMap histograms, SortedMap meters, SortedMap timers,
      Map tags) {
    List metrics = Lists.newArrayList();
    for (Map.Entry gauge : gauges.entrySet()) {
      metrics.addAll(serializeGauge(gauge.getKey(), gauge.getValue()));
    }
    for (Map.Entry counter : counters.entrySet()) {
      metrics.addAll(serializeCounter(counter.getKey(), counter.getValue()));
    }
    for (Map.Entry histogram : histograms.entrySet()) {
      metrics.addAll(serializeSnapshot(histogram.getKey(), histogram.getValue().getSnapshot()));
      metrics.addAll(serializeCounter(histogram.getKey(), histogram.getValue()));
    }
    for (Map.Entry meter : meters.entrySet()) {
      metrics.addAll(serializeMetered(meter.getKey(), meter.getValue()));
    }
    for (Map.Entry timer : timers.entrySet()) {
      metrics.addAll(serializeSnapshot(timer.getKey(), timer.getValue().getSnapshot()));
      metrics.addAll(serializeMetered(timer.getKey(), timer.getValue()));
      metrics.addAll(serializeSingleValue(timer.getKey(), timer.getValue().getCount(), Measurements.COUNT.getName()));
    }
    Map allTags = Maps.newHashMap();
    allTags.putAll(tags);
    allTags.putAll(this.tags);
    Map allTagsString = Maps.transformValues(allTags, new Function                    © 2015 - 2025 Weber Informatics LLC | Privacy Policy