Skip to content

Implementing New Reporters

Issac Buenrostro edited this page Aug 14, 2015 · 3 revisions

The two best entry points for implementing custom reporters are RecursiveScheduledMetricReporter and EventReporter. Each of these classes automatically schedules reporting, extracts the correct metrics, and calls a single method that must be implemented by the developer. These methods also implement builder patterns that can be extended by the developer.

It is recommended that each reporter has a constructor with signature <init>(Properties). In the near future we are planning to implement auto-starting, file-configurable reporting similar to Log4j architecture, and compliant reporters will be required to have such a constructor.

Extending Builders

The builder patterns implemented in the base reporters are designed to be extendable. The architecture is a bit complicated, but a subclass of the base reporters wanting to use builder patterns should follow this pattern:

class MyReporter extends EventReporter {

  private MyReporter(Builder<?> builder) throws IOException {
    super(builder);
    // Other initialization logic.
  }

  // Concrete implementation of extendable Builder.
  public static class BuilderImpl extends Builder<BuilderImpl> {
    private BuilderImpl(MetricContext context) {
      super(context);
    }

    @Override
    protected BuilderImpl self() {
      return this;
    }
  }

  public static class Factory {
    /**
     * Returns a new {@link MyReporter.Builder} for {@link MyReporter}.
     * Will automatically add all Context tags to the reporter.
     *
     * @param context the {@link gobblin.metrics.MetricContext} to report
     * @return MyReporter builder
     */
    public static BuilderImpl forContext(MetricContext context) {
      return new BuilderImpl(context);
    }
  }

  /**
   * Builder for {@link MyReporter}.
   */
  public static abstract class Builder<T extends EventReporter.Builder<T>>
      extends EventReporter.Builder<T> {

    // Additional instance variables needed to construct MyReporter.
    private int myBuilderVariable;

    protected Builder(MetricContext context) {
      super(context);
      this.myBuilderVariable = 0;
    }

    /**
     * Set myBuilderVariable.
     */
    public T withMyBuilderVariable(int value) {
      this.myBuilderVariable = value;
      return self();
    }

    // Other setters for Builder variables.

    /**
     * Builds and returns {@link MyReporter}.
     */
    public MyReporter build() throws IOException {
      return new MyReporter(this);
    }

  }
}

Metric Reporting

Th

Clone this wiki locally