1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.metrics;
20
21 import org.apache.hadoop.hbase.classification.InterfaceAudience;
22 import org.apache.hadoop.metrics2.MetricsCollector;
23 import org.apache.hadoop.metrics2.MetricsSource;
24 import org.apache.hadoop.metrics2.impl.JmxCacheBuster;
25 import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
26 import org.apache.hadoop.metrics2.lib.DynamicMetricsRegistry;
27 import org.apache.hadoop.metrics2.lib.MutableFastCounter;
28 import org.apache.hadoop.metrics2.lib.MutableGaugeLong;
29 import org.apache.hadoop.metrics2.lib.MutableHistogram;
30 import org.apache.hadoop.metrics2.source.JvmMetrics;
31
32
33
34
35
36
37
38 @InterfaceAudience.Private
39 public class BaseSourceImpl implements BaseSource, MetricsSource {
40
41 private static enum DefaultMetricsSystemInitializer {
42 INSTANCE;
43 private boolean inited = false;
44
45 synchronized void init(String name) {
46 if (inited) return;
47 inited = true;
48 DefaultMetricsSystem.initialize(HBASE_METRICS_SYSTEM_NAME);
49 JvmMetrics.initSingleton(name, "");
50 }
51 }
52
53 protected final DynamicMetricsRegistry metricsRegistry;
54 protected final String metricsName;
55 protected final String metricsDescription;
56 protected final String metricsContext;
57 protected final String metricsJmxContext;
58
59 public BaseSourceImpl(
60 String metricsName,
61 String metricsDescription,
62 String metricsContext,
63 String metricsJmxContext) {
64
65 this.metricsName = metricsName;
66 this.metricsDescription = metricsDescription;
67 this.metricsContext = metricsContext;
68 this.metricsJmxContext = metricsJmxContext;
69
70 metricsRegistry = new DynamicMetricsRegistry(metricsName).setContext(metricsContext);
71 DefaultMetricsSystemInitializer.INSTANCE.init(metricsName);
72
73
74 DefaultMetricsSystem.instance().register(metricsJmxContext, metricsDescription, this);
75 init();
76
77 }
78
79 public void init() {
80 this.metricsRegistry.clearMetrics();
81 }
82
83
84
85
86
87
88
89 public void setGauge(String gaugeName, long value) {
90 MutableGaugeLong gaugeInt = metricsRegistry.getGauge(gaugeName, value);
91 gaugeInt.set(value);
92 }
93
94
95
96
97
98
99
100 public void incGauge(String gaugeName, long delta) {
101 MutableGaugeLong gaugeInt = metricsRegistry.getGauge(gaugeName, 0l);
102 gaugeInt.incr(delta);
103 }
104
105
106
107
108
109
110
111 public void decGauge(String gaugeName, long delta) {
112 MutableGaugeLong gaugeInt = metricsRegistry.getGauge(gaugeName, 0l);
113 gaugeInt.decr(delta);
114 }
115
116
117
118
119
120
121
122 public void incCounters(String key, long delta) {
123 MutableFastCounter counter = metricsRegistry.getCounter(key, 0l);
124 counter.incr(delta);
125
126 }
127
128 @Override
129 public void updateHistogram(String name, long value) {
130 MutableHistogram histo = metricsRegistry.getHistogram(name);
131 histo.add(value);
132 }
133
134
135
136
137
138
139 public void removeMetric(String key) {
140 metricsRegistry.removeMetric(key);
141 JmxCacheBuster.clearJmxCache();
142 }
143
144 @Override
145 public void getMetrics(MetricsCollector metricsCollector, boolean all) {
146 metricsRegistry.snapshot(metricsCollector.addRecord(metricsRegistry.info()), all);
147 }
148
149 public DynamicMetricsRegistry getMetricsRegistry() {
150 return metricsRegistry;
151 }
152
153 public String getMetricsContext() {
154 return metricsContext;
155 }
156
157 public String getMetricsDescription() {
158 return metricsDescription;
159 }
160
161 public String getMetricsJmxContext() {
162 return metricsJmxContext;
163 }
164
165 public String getMetricsName() {
166 return metricsName;
167 }
168
169 }