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