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    private MetricsRate sucessfulGetCount =
39        new MetricsRate("sucessful.get.count", registry);
40    private MetricsRate sucessfulPutCount =
41        new MetricsRate("sucessful.put.count", registry);
42    private MetricsRate sucessfulDeleteCount =
43        new MetricsRate("sucessful.delete.count", registry);
44    
45    private MetricsRate failedGetCount =
46        new MetricsRate("failed.get.count", registry);
47    private MetricsRate failedPutCount =
48        new MetricsRate("failed.put.count", registry);
49    private MetricsRate failedDeleteCount =
50        new MetricsRate("failed.delete.count", registry);
51  
52    public RESTMetrics() {
53      MetricsContext context = MetricsUtil.getContext("rest");
54      metricsRecord = MetricsUtil.createRecord(context, "rest");
55      String name = Thread.currentThread().getName();
56      metricsRecord.setTag("REST", name);
57      context.registerUpdater(this);
58      JvmMetrics.init("rest", name);
59      // expose the MBean for metrics
60      restStatistics = new RESTStatistics(registry);
61  
62    }
63  
64    public void shutdown() {
65      if (restStatistics != null) {
66        restStatistics.shutdown();
67      }
68    }
69  
70    /**
71     * Since this object is a registered updater, this method will be called
72     * periodically, e.g. every 5 seconds.
73     * @param unused 
74     */
75    public void doUpdates(MetricsContext unused) {
76      synchronized (this) {
77        requests.pushMetric(metricsRecord);
78        sucessfulGetCount.pushMetric(metricsRecord);
79        sucessfulPutCount.pushMetric(metricsRecord);
80        sucessfulDeleteCount.pushMetric(metricsRecord);
81        failedGetCount.pushMetric(metricsRecord);
82        failedPutCount.pushMetric(metricsRecord);
83        failedDeleteCount.pushMetric(metricsRecord);
84      }
85      this.metricsRecord.update();
86    }
87    
88    public void resetAllMinMax() {
89      // Nothing to do
90    }
91  
92    /**
93     * @return Count of requests.
94     */
95    public float getRequests() {
96      return requests.getPreviousIntervalValue();
97    }
98    
99    /**
100    * @param inc How much to add to requests.
101    */
102   public void incrementRequests(final int inc) {
103     requests.inc(inc);
104   }
105   
106   /**
107    * @return Count of sucessfulGetCount.
108    */
109   public float getSucessfulGetCount() {
110     return sucessfulGetCount.getPreviousIntervalValue();
111   }
112   
113   /**
114    * @param inc How much to add to sucessfulGetCount.
115    */
116   public void incrementSucessfulGetRequests(final int inc) {
117     sucessfulGetCount.inc(inc);
118   }
119   
120   /**
121    * @return Count of sucessfulGetCount.
122    */
123   public float getSucessfulPutCount() {
124     return sucessfulPutCount.getPreviousIntervalValue();
125   }
126   
127   /**
128    * @param inc How much to add to sucessfulPutCount.
129    */
130   public void incrementSucessfulPutRequests(final int inc) {
131     sucessfulPutCount.inc(inc);
132   }
133   
134   /**
135    * @return Count of failedPutCount.
136    */
137   public float getFailedPutCount() {
138     return failedPutCount.getPreviousIntervalValue();
139   }
140   
141   /**
142    * @param inc How much to add to failedPutCount.
143    */
144   public void incrementFailedPutRequests(final int inc) {
145     failedPutCount.inc(inc);
146   }
147   
148   /**
149    * @return Count of failedGetCount.
150    */
151   public float getFailedGetCount() {
152     return failedGetCount.getPreviousIntervalValue();
153   }
154   
155   /**
156    * @param inc How much to add to failedGetCount.
157    */
158   public void incrementFailedGetRequests(final int inc) {
159     failedGetCount.inc(inc);
160   }
161   
162   /**
163    * @return Count of sucessfulGetCount.
164    */
165   public float getSucessfulDeleteCount() {
166     return sucessfulDeleteCount.getPreviousIntervalValue();
167   }
168   
169   /**
170    * @param inc How much to add to sucessfulDeleteCount.
171    */
172   public void incrementSucessfulDeleteRequests(final int inc) {
173     sucessfulDeleteCount.inc(inc);
174   }
175 
176   /**
177    * @return Count of failedDeleteCount.
178    */
179   public float getFailedDeleteCount() {
180     return failedDeleteCount.getPreviousIntervalValue();
181   }
182   
183   /**
184    * @param inc How much to add to failedDeleteCount.
185    */
186   public void incrementFailedDeleteRequests(final int inc) {
187     failedDeleteCount.inc(inc);
188   }
189   
190 }