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