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 */
018
019package org.apache.hadoop.metrics2.lib;
020
021import org.apache.commons.lang3.StringUtils;
022import org.apache.hadoop.hbase.metrics.Histogram;
023import org.apache.hadoop.hbase.metrics.Interns;
024import org.apache.hadoop.hbase.metrics.Snapshot;
025import org.apache.hadoop.hbase.metrics.impl.HistogramImpl;
026import org.apache.hadoop.metrics2.MetricHistogram;
027import org.apache.hadoop.metrics2.MetricsInfo;
028import org.apache.hadoop.metrics2.MetricsRecordBuilder;
029import org.apache.yetus.audience.InterfaceAudience;
030
031/**
032 * A histogram implementation that runs in constant space, and exports to hadoop2's metrics2 system.
033 */
034@InterfaceAudience.Private
035public class MutableHistogram extends MutableMetric implements MetricHistogram {
036  protected HistogramImpl histogram;
037  protected final String name;
038  protected final String desc;
039
040  public MutableHistogram(MetricsInfo info) {
041    this(info.name(), info.description());
042  }
043
044  public MutableHistogram(String name, String description) {
045    this(name, description, Integer.MAX_VALUE << 2);
046  }
047
048  protected MutableHistogram(String name, String description, long maxExpected) {
049    this.name = StringUtils.capitalize(name);
050    this.desc = StringUtils.uncapitalize(description);
051    this.histogram = new HistogramImpl();
052  }
053
054  public void add(final long val) {
055    histogram.update(val);
056  }
057
058  public long getMax() {
059    return histogram.getMax();
060  }
061
062  @Override
063  public synchronized void snapshot(MetricsRecordBuilder metricsRecordBuilder, boolean all) {
064    snapshot(name, desc, histogram, metricsRecordBuilder, all);
065  }
066
067  public static void snapshot(String name, String desc, Histogram histogram,
068                              MetricsRecordBuilder metricsRecordBuilder, boolean all) {
069    // Get a reference to the old histogram.
070    Snapshot snapshot = histogram.snapshot();
071    if (snapshot != null) {
072      updateSnapshotMetrics(name, desc, histogram, snapshot, metricsRecordBuilder);
073    }
074  }
075
076  protected static void updateSnapshotMetrics(String name, String desc, Histogram histogram,
077      Snapshot snapshot, MetricsRecordBuilder metricsRecordBuilder) {
078    metricsRecordBuilder.addCounter(Interns.info(name + NUM_OPS_METRIC_NAME, desc),
079        histogram.getCount());
080    metricsRecordBuilder.addGauge(Interns.info(name + MIN_METRIC_NAME, desc), snapshot.getMin());
081    metricsRecordBuilder.addGauge(Interns.info(name + MAX_METRIC_NAME, desc), snapshot.getMax());
082    metricsRecordBuilder.addGauge(Interns.info(name + MEAN_METRIC_NAME, desc), snapshot.getMean());
083
084    metricsRecordBuilder.addGauge(Interns.info(name + TWENTY_FIFTH_PERCENTILE_METRIC_NAME, desc),
085        snapshot.get25thPercentile());
086    metricsRecordBuilder.addGauge(Interns.info(name + MEDIAN_METRIC_NAME, desc),
087        snapshot.getMedian());
088    metricsRecordBuilder.addGauge(Interns.info(name + SEVENTY_FIFTH_PERCENTILE_METRIC_NAME, desc),
089        snapshot.get75thPercentile());
090    metricsRecordBuilder.addGauge(Interns.info(name + NINETIETH_PERCENTILE_METRIC_NAME, desc),
091        snapshot.get90thPercentile());
092    metricsRecordBuilder.addGauge(Interns.info(name + NINETY_FIFTH_PERCENTILE_METRIC_NAME, desc),
093        snapshot.get95thPercentile());
094    metricsRecordBuilder.addGauge(Interns.info(name + NINETY_EIGHTH_PERCENTILE_METRIC_NAME, desc),
095        snapshot.get98thPercentile());
096    metricsRecordBuilder.addGauge(Interns.info(name + NINETY_NINETH_PERCENTILE_METRIC_NAME, desc),
097        snapshot.get99thPercentile());
098    metricsRecordBuilder.addGauge(
099        Interns.info(name + NINETY_NINE_POINT_NINETH_PERCENTILE_METRIC_NAME, desc),
100        snapshot.get999thPercentile());
101  }
102}