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.hbase.metrics.impl; 019 020import java.util.Map; 021import java.util.Optional; 022import java.util.concurrent.ConcurrentHashMap; 023import java.util.concurrent.ConcurrentMap; 024 025import org.apache.hadoop.hbase.metrics.Counter; 026import org.apache.hadoop.hbase.metrics.Gauge; 027import org.apache.hadoop.hbase.metrics.Histogram; 028import org.apache.hadoop.hbase.metrics.Meter; 029import org.apache.hadoop.hbase.metrics.Metric; 030import org.apache.hadoop.hbase.metrics.MetricRegistry; 031import org.apache.hadoop.hbase.metrics.MetricRegistryInfo; 032import org.apache.hadoop.hbase.metrics.MetricSet; 033import org.apache.hadoop.hbase.metrics.Timer; 034import org.apache.hadoop.hbase.util.CollectionUtils; 035import org.apache.yetus.audience.InterfaceAudience; 036 037/** 038 * Custom implementation of {@link MetricRegistry}. 039 */ 040@InterfaceAudience.Private 041public class MetricRegistryImpl implements MetricRegistry { 042 043 private final MetricRegistryInfo info; 044 045 private final ConcurrentMap<String, Metric> metrics; 046 047 public MetricRegistryImpl(MetricRegistryInfo info) { 048 this.info = info; 049 this.metrics = new ConcurrentHashMap<>(); 050 } 051 052 @Override 053 public Timer timer(String name) { 054 return (Timer) CollectionUtils.computeIfAbsent(metrics, name, this::createTimer); 055 } 056 057 protected Timer createTimer() { 058 return new TimerImpl(); 059 } 060 061 @Override 062 public Histogram histogram(String name) { 063 return (Histogram) CollectionUtils.computeIfAbsent(metrics, name, this::createHistogram); 064 } 065 066 protected Histogram createHistogram() { 067 return new HistogramImpl(); 068 } 069 070 @Override 071 public Meter meter(String name) { 072 return (Meter) CollectionUtils.computeIfAbsent(metrics, name, this::createMeter); 073 } 074 075 protected Meter createMeter() { 076 return new DropwizardMeter(); 077 } 078 079 @Override 080 public Counter counter(String name) { 081 return (Counter) CollectionUtils.computeIfAbsent(metrics, name, this::createCounter); 082 } 083 084 protected Counter createCounter() { 085 return new CounterImpl(); 086 } 087 088 @Override 089 public Optional<Metric> get(String name) { 090 return Optional.ofNullable(metrics.get(name)); 091 } 092 093 @Override 094 public Metric register(String name, Metric metric) { 095 return CollectionUtils.computeIfAbsent(metrics, name, () -> metric); 096 } 097 098 @Override 099 public <T> Gauge<T> register(String name, Gauge<T> gauge) { 100 return (Gauge) register(name, (Metric)gauge); 101 } 102 103 @Override 104 public void registerAll(MetricSet metricSet) { 105 metricSet.getMetrics().forEach(this::register); 106 } 107 108 @Override 109 public Map<String, Metric> getMetrics() { 110 return metrics; 111 } 112 113 @Override 114 public boolean remove(String name) { 115 return metrics.remove(name) != null; 116 } 117 118 @Override 119 public MetricRegistryInfo getMetricRegistryInfo() { 120 return info; 121 } 122}