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 java.lang.reflect.Method;
022import java.util.Set;
023
024import static com.google.common.base.Preconditions.*;
025import com.google.common.collect.Sets;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029
030import org.apache.hadoop.classification.InterfaceAudience;
031import org.apache.hadoop.classification.InterfaceStability;
032import org.apache.hadoop.metrics2.MetricsRecordBuilder;
033
034/**
035 * Helper class to manage a group of mutable rate metrics
036 *
037 * This class synchronizes all accesses to the metrics it
038 * contains, so it should not be used in situations where
039 * there is high contention on the metrics.
040 * {@link MutableRatesWithAggregation} is preferable in that
041 * situation.
042 */
043@InterfaceAudience.Public
044@InterfaceStability.Evolving
045public class MutableRates extends MutableMetric {
046  static final Log LOG = LogFactory.getLog(MutableRates.class);
047  private final MetricsRegistry registry;
048  private final Set<Class<?>> protocolCache = Sets.newHashSet();
049
050  MutableRates(MetricsRegistry registry) {
051    this.registry = checkNotNull(registry, "metrics registry");
052  }
053
054  /**
055   * Initialize the registry with all the methods in a protocol
056   * so they all show up in the first snapshot.
057   * Convenient for JMX implementations.
058   * @param protocol the protocol class
059   */
060  public void init(Class<?> protocol) {
061    if (protocolCache.contains(protocol)) return;
062    protocolCache.add(protocol);
063    for (Method method : protocol.getDeclaredMethods()) {
064      String name = method.getName();
065      LOG.debug(name);
066      try { registry.newRate(name, name, false, true); }
067      catch (Exception e) {
068        LOG.error("Error creating rate metrics for "+ method.getName(), e);
069      }
070    }
071  }
072
073  /**
074   * Add a rate sample for a rate metric
075   * @param name of the rate metric
076   * @param elapsed time
077   */
078  public void add(String name, long elapsed) {
079    registry.add(name, elapsed);
080  }
081
082  @Override
083  public void snapshot(MetricsRecordBuilder rb, boolean all) {
084    registry.snapshot(rb, all);
085  }
086}