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

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

The newest version!
/*
 * 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.hadoop;

import java.util.concurrent.TimeUnit;

import org.apache.hadoop.mapreduce.TaskInputOutputContext;

import com.codahale.metrics.MetricFilter;

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


/**
 * An implementation of {@link gobblin.metrics.reporter.ContextAwareScheduledReporter} that reports applicable
 * metrics as Hadoop counters using a {@link org.apache.hadoop.mapreduce.TaskInputOutputContext}.
 *
 * @param  the input key type of {@code hadoopContext}
 * @param  the input value type of {@code hadoopContext}
 * @param  the output key type of {@code hadoopContext}
 * @param  the output value type of {@code hadoopContext}
 *
 * @author Yinan Li
 */
public class NewAPIHadoopCounterReporter extends AbstractHadoopCounterReporter {

  private final TaskInputOutputContext hadoopContext;

  protected NewAPIHadoopCounterReporter(MetricContext context, String name, MetricFilter filter,
      TimeUnit rateUnit, TimeUnit durationUnit, TaskInputOutputContext hadoopContext) {
    super(context, name, filter, rateUnit, durationUnit);
    this.hadoopContext = hadoopContext;
  }

  @Override
  protected void reportIncremental(MetricContext context, String name, long incremental) {
    this.hadoopContext.getCounter(context.getName(), name).increment(incremental);
  }

  @Override
  protected void reportValue(MetricContext context, String name, long value) {
    this.hadoopContext.getCounter(context.getName(), name).setValue(value);
  }

  /**
   * Create a new {@link gobblin.metrics.hadoop.NewAPIHadoopCounterReporter.Builder}
   * that uses the simple name of {@link NewAPIHadoopCounterReporter} as the reporter name.
   *
   * @param hadoopContext a {@link org.apache.hadoop.mapreduce.TaskInputOutputContext}
   *                      used to access Hadoop counters
   * @param  the input key type of {@code hadoopContext}
   * @param  the input value type of {@code hadoopContext}
   * @param  the output key type of {@code hadoopContext}
   * @param  the output value type of {@code hadoopContext}
   * @return a new {@link gobblin.metrics.hadoop.NewAPIHadoopCounterReporter.Builder}
   */
  public static  Builder builder(
      TaskInputOutputContext hadoopContext) {
    return builder(NewAPIHadoopCounterReporter.class.getName(), hadoopContext);
  }

  /**
   * Create a new {@link gobblin.metrics.hadoop.NewAPIHadoopCounterReporter.Builder}
   * that uses a given reporter name.
   *
   * @param name the given reporter name
   * @param hadoopContext a {@link org.apache.hadoop.mapreduce.TaskInputOutputContext}
   *                      used to access Hadoop counters
   * @param  the input key type of {@code hadoopContext}
   * @param  the input value type of {@code hadoopContext}
   * @param  the output key type of {@code hadoopContext}
   * @param  the output value type of {@code hadoopContext}
   * @return a new {@link gobblin.metrics.hadoop.NewAPIHadoopCounterReporter.Builder}
   */
  public static  Builder builder(String name,
      TaskInputOutputContext hadoopContext) {
    return new Builder(name, hadoopContext);
  }

  /**
   * A builder class for {@link NewAPIHadoopCounterReporter}.
   *
   * @param  the input key type of {@code hadoopContext}
   * @param  the input value type of {@code hadoopContext}
   * @param  the output key type of {@code hadoopContext}
   * @param  the output value type of {@code hadoopContext}
   */
  public static class Builder extends ContextAwareScheduledReporter.Builder<
      NewAPIHadoopCounterReporter, Builder> {

    private final TaskInputOutputContext hadoopContext;

    public Builder(String name, TaskInputOutputContext hadoopContext) {
      super(name);
      this.hadoopContext = hadoopContext;
    }

    @Override
    public NewAPIHadoopCounterReporter build(MetricContext context) {
      return new NewAPIHadoopCounterReporter(
          context, this.name, this.filter, this.rateUnit, this.durationUnit, this.hadoopContext);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy