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  public void add(final long val) {
050    histogram.update(val);
051  }
052
053  @Override
054  public long getCount() {
055    return histogram.getCount();
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}