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