1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.metrics;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.apache.hadoop.hbase.classification.InterfaceAudience;
23 import org.apache.hadoop.metrics.MetricsRecord;
24 import org.apache.hadoop.metrics.util.MetricsBase;
25 import org.apache.hadoop.metrics.util.MetricsRegistry;
26 import org.apache.hadoop.util.StringUtils;
27
28
29
30
31
32 @Deprecated
33 @InterfaceAudience.Private
34 public class MetricsRate extends MetricsBase {
35 private static final Log LOG = LogFactory.getLog("org.apache.hadoop.hbase.metrics");
36
37 private int value;
38 private float prevRate;
39 private long ts;
40
41 public MetricsRate(final String name, final MetricsRegistry registry,
42 final String description) {
43 super(name, description);
44 this.value = 0;
45 this.prevRate = 0;
46 this.ts = System.currentTimeMillis();
47 registry.add(name, this);
48 }
49
50 public MetricsRate(final String name, final MetricsRegistry registry) {
51 this(name, registry, NO_DESCRIPTION);
52 }
53
54 public synchronized void inc(final int incr) {
55 value += incr;
56 }
57
58 public synchronized void inc() {
59 value++;
60 }
61
62 public synchronized void intervalHeartBeat() {
63 long now = System.currentTimeMillis();
64 long diff = (now-ts) / 1000;
65 if (diff < 1){
66
67
68 return;
69 }
70 this.prevRate = (float)value / diff;
71 this.value = 0;
72 this.ts = now;
73 }
74
75 @Override
76 public synchronized void pushMetric(final MetricsRecord mr) {
77 intervalHeartBeat();
78 try {
79 mr.setMetric(getName(), getPreviousIntervalValue());
80 } catch (Exception e) {
81 LOG.info("pushMetric failed for " + getName() + "\n" +
82 StringUtils.stringifyException(e));
83 }
84 }
85
86 public synchronized float getPreviousIntervalValue() {
87 return this.prevRate;
88 }
89 }