@Override
  public void start(CoprocessorEnvironment env) throws IOException {
    // start for the RegionServerObserver will be called only once in the lifetime of the
    // server. We will construct and register all metrics that we will track across method
    // invocations.

    if (env instanceof RegionCoprocessorEnvironment) {
      // Obtain the MetricRegistry for the RegionServer. Metrics from this registry will be reported
      // at the region server level per-regionserver.
      MetricRegistry registry =
        ((RegionCoprocessorEnvironment) env).getMetricRegistryForRegionServer();
      observer = new ExampleRegionObserver();

      if (preGetCounter == null) {
        // Create a new Counter, or get the already registered counter.
        // It is much better to only call this once and save the Counter as a class field instead
        // of creating the counter every time a coprocessor method is invoked. This will negate
        // any performance bottleneck coming from map lookups tracking metrics in the registry.
        // Returned counter instance is shared by all coprocessors of the same class in the same
        // region server.
        preGetCounter = registry.counter("preGetRequests");
      }

      if (costlyOperationTimer == null) {
        // Create a Timer to track execution times for the costly operation.
        costlyOperationTimer = registry.timer("costlyOperation");
      }

      if (flushCounter == null) {
        // Track the number of flushes that have completed
        flushCounter = registry.counter("flushesCompleted");
      }

      if (filesCompactedCounter == null) {
        // Track the number of files that were compacted (many files may be rewritten in a single
        // compaction).
        filesCompactedCounter = registry.counter("filesCompacted");
      }
    }
  }
An example coprocessor that collects some metrics to demonstrate the usage of exporting custom
metrics from the coprocessor.
<p>
These metrics will be available through the regular Hadoop metrics2 sinks (ganglia, opentsdb,
etc) as well as JMX output. You can view a snapshot of the metrics by going to the http web UI of
the regionserver page, something like http://myregionserverhost:16030/jmx
</p>
@see ExampleMasterObserverWithMetrics
hbase-examples/src/main/java/org/apache/hadoop/hbase/coprocessor/example/ExampleRegionObserverWithMetrics.java