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