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.hadoop.hbase.metrics.Interns; 022import org.apache.hadoop.hbase.metrics.Snapshot; 023import org.apache.hadoop.metrics2.MetricHistogram; 024import org.apache.hadoop.metrics2.MetricsInfo; 025import org.apache.hadoop.metrics2.MetricsRecordBuilder; 026import org.apache.yetus.audience.InterfaceAudience; 027 028/** 029 * Extended histogram implementation with metric range counters. 030 */ 031@InterfaceAudience.Private 032public abstract class MutableRangeHistogram extends MutableHistogram implements MetricHistogram { 033 034 public MutableRangeHistogram(MetricsInfo info) { 035 this(info.name(), info.description()); 036 } 037 038 public MutableRangeHistogram(String name, String description) { 039 super(name, description); 040 } 041 042 /** 043 * Returns the type of range histogram size or time 044 */ 045 public abstract String getRangeType(); 046 047 /** 048 * Returns the ranges to be counted 049 */ 050 public abstract long[] getRanges(); 051 052 053 @Override 054 public synchronized void snapshot(MetricsRecordBuilder metricsRecordBuilder, boolean all) { 055 // Get a reference to the old histogram. 056 Snapshot snapshot = histogram.snapshot(); 057 if (snapshot != null) { 058 updateSnapshotMetrics(name, desc, histogram, snapshot, metricsRecordBuilder); 059 updateSnapshotRangeMetrics(metricsRecordBuilder, snapshot); 060 } 061 } 062 063 public void updateSnapshotRangeMetrics(MetricsRecordBuilder metricsRecordBuilder, 064 Snapshot snapshot) { 065 long priorRange = 0; 066 long cumNum = 0; 067 068 final long[] ranges = getRanges(); 069 final String rangeType = getRangeType(); 070 for (int i = 0; i < ranges.length - 1; i++) { 071 long val = snapshot.getCountAtOrBelow(ranges[i]); 072 if (val - cumNum > 0) { 073 metricsRecordBuilder.addCounter( 074 Interns.info(name + "_" + rangeType + "_" + priorRange + "-" + ranges[i], desc), 075 val - cumNum); 076 } 077 priorRange = ranges[i]; 078 cumNum = val; 079 } 080 long val = snapshot.getCount(); 081 if (val - cumNum > 0) { 082 metricsRecordBuilder.addCounter( 083 Interns.info(name + "_" + rangeType + "_" + ranges[ranges.length - 1] + "-inf", desc), 084 val - cumNum); 085 } 086 } 087}