View Javadoc

1   /*
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.rest.metrics;
22  
23  import org.apache.hadoop.hbase.metrics.MetricsRate;
24  
25  import org.apache.hadoop.metrics.MetricsContext;
26  import org.apache.hadoop.metrics.MetricsRecord;
27  import org.apache.hadoop.metrics.MetricsUtil;
28  import org.apache.hadoop.metrics.Updater;
29  import org.apache.hadoop.metrics.jvm.JvmMetrics;
30  import org.apache.hadoop.metrics.util.MetricsRegistry;
31  
32  public class RESTMetrics implements Updater {
33    private final MetricsRecord metricsRecord;
34    private final MetricsRegistry registry = new MetricsRegistry();
35    private final RESTStatistics restStatistics;
36  
37    private MetricsRate requests = new MetricsRate("requests", registry);
38  
39    public RESTMetrics() {
40      MetricsContext context = MetricsUtil.getContext("rest");
41      metricsRecord = MetricsUtil.createRecord(context, "rest");
42      String name = Thread.currentThread().getName();
43      metricsRecord.setTag("REST", name);
44      context.registerUpdater(this);
45      JvmMetrics.init("rest", name);
46      // expose the MBean for metrics
47      restStatistics = new RESTStatistics(registry);
48  
49    }
50  
51    public void shutdown() {
52      if (restStatistics != null) {
53        restStatistics.shutdown();
54      }
55    }
56  
57    /**
58     * Since this object is a registered updater, this method will be called
59     * periodically, e.g. every 5 seconds.
60     * @param unused 
61     */
62    public void doUpdates(MetricsContext unused) {
63      synchronized (this) {
64        requests.pushMetric(metricsRecord);
65      }
66      this.metricsRecord.update();
67    }
68    
69    public void resetAllMinMax() {
70      // Nothing to do
71    }
72  
73    /**
74     * @return Count of requests.
75     */
76    public float getRequests() {
77      return requests.getPreviousIntervalValue();
78    }
79    
80    /**
81     * @param inc How much to add to requests.
82     */
83    public void incrementRequests(final int inc) {
84      requests.inc(inc);
85    }
86  
87  }