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