001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.metrics2.lib;
019
020import org.apache.commons.lang3.StringUtils;
021import org.apache.hadoop.hbase.metrics.Histogram;
022import org.apache.hadoop.hbase.metrics.Interns;
023import org.apache.hadoop.hbase.metrics.Snapshot;
024import org.apache.hadoop.hbase.metrics.impl.HistogramImpl;
025import org.apache.hadoop.metrics2.MetricHistogram;
026import org.apache.hadoop.metrics2.MetricsInfo;
027import org.apache.hadoop.metrics2.MetricsRecordBuilder;
028import org.apache.yetus.audience.InterfaceAudience;
029
030/**
031 * A histogram implementation that runs in constant space, and exports to hadoop2's metrics2 system.
032 */
033@InterfaceAudience.Private
034public class MutableHistogram extends MutableMetric implements MetricHistogram {
035  protected HistogramImpl histogram;
036  protected final String name;
037  protected final String desc;
038
039  public MutableHistogram(MetricsInfo info) {
040    this(info.name(), info.description());
041  }
042
043  public MutableHistogram(String name, String description) {
044    this.name = StringUtils.capitalize(name);
045    this.desc = StringUtils.uncapitalize(description);
046    this.histogram = new HistogramImpl();
047  }
048
049  @Override
050  public void add(final long val) {
051    histogram.update(val);
052  }
053
054  @Override
055  public long getCount() {
056    return histogram.getCount();
057  }
058
059  public long getMax() {
060    return histogram.getMax();
061  }
062
063  @Override
064  public synchronized void snapshot(MetricsRecordBuilder metricsRecordBuilder, boolean all) {
065    snapshot(name, desc, histogram, metricsRecordBuilder, all);
066  }
067
068  public static void snapshot(String name, String desc, Histogram histogram,
069    MetricsRecordBuilder metricsRecordBuilder, boolean all) {
070    // Get a reference to the old histogram.
071    Snapshot snapshot = histogram.snapshot();
072    if (snapshot != null) {
073      updateSnapshotMetrics(name, desc, histogram, snapshot, metricsRecordBuilder);
074    }
075  }
076
077  protected static void updateSnapshotMetrics(String name, String desc, Histogram histogram,
078    Snapshot snapshot, MetricsRecordBuilder metricsRecordBuilder) {
079    metricsRecordBuilder.addCounter(Interns.info(name + NUM_OPS_METRIC_NAME, desc),
080      histogram.getCount());
081    metricsRecordBuilder.addGauge(Interns.info(name + MIN_METRIC_NAME, desc), snapshot.getMin());
082    metricsRecordBuilder.addGauge(Interns.info(name + MAX_METRIC_NAME, desc), snapshot.getMax());
083    metricsRecordBuilder.addGauge(Interns.info(name + MEAN_METRIC_NAME, desc), snapshot.getMean());
084
085    metricsRecordBuilder.addGauge(Interns.info(name + TWENTY_FIFTH_PERCENTILE_METRIC_NAME, desc),
086      snapshot.get25thPercentile());
087    metricsRecordBuilder.addGauge(Interns.info(name + MEDIAN_METRIC_NAME, desc),
088      snapshot.getMedian());
089    metricsRecordBuilder.addGauge(Interns.info(name + SEVENTY_FIFTH_PERCENTILE_METRIC_NAME, desc),
090      snapshot.get75thPercentile());
091    metricsRecordBuilder.addGauge(Interns.info(name + NINETIETH_PERCENTILE_METRIC_NAME, desc),
092      snapshot.get90thPercentile());
093    metricsRecordBuilder.addGauge(Interns.info(name + NINETY_FIFTH_PERCENTILE_METRIC_NAME, desc),
094      snapshot.get95thPercentile());
095    metricsRecordBuilder.addGauge(Interns.info(name + NINETY_EIGHTH_PERCENTILE_METRIC_NAME, desc),
096      snapshot.get98thPercentile());
097    metricsRecordBuilder.addGauge(Interns.info(name + NINETY_NINETH_PERCENTILE_METRIC_NAME, desc),
098      snapshot.get99thPercentile());
099    metricsRecordBuilder.addGauge(
100      Interns.info(name + NINETY_NINE_POINT_NINETH_PERCENTILE_METRIC_NAME, desc),
101      snapshot.get999thPercentile());
102  }
103}