1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.metrics2.lib;
20
21 import java.util.Collection;
22 import java.util.concurrent.ConcurrentMap;
23
24 import com.google.common.base.Objects;
25 import com.google.common.collect.Maps;
26 import org.apache.hadoop.classification.InterfaceAudience;
27 import org.apache.hadoop.classification.InterfaceStability;
28 import org.apache.hadoop.metrics2.MetricsException;
29 import org.apache.hadoop.metrics2.MetricsInfo;
30 import org.apache.hadoop.metrics2.MetricsRecordBuilder;
31 import org.apache.hadoop.metrics2.MetricsTag;
32 import org.apache.hadoop.metrics2.impl.MsInfo;
33
34
35
36
37
38
39
40
41
42
43
44
45 @InterfaceAudience.Public
46 @InterfaceStability.Evolving
47 public class DynamicMetricsRegistry {
48 private final ConcurrentMap<String, MutableMetric> metricsMap =
49 Maps.newConcurrentMap();
50 private final ConcurrentMap<String, MetricsTag> tagsMap =
51 Maps.newConcurrentMap();
52 private final MetricsInfo metricsInfo;
53
54
55
56
57
58 public DynamicMetricsRegistry(String name) {
59 metricsInfo = Interns.info(name, name);
60 }
61
62
63
64
65
66 public DynamicMetricsRegistry(MetricsInfo info) {
67 metricsInfo = info;
68 }
69
70
71
72
73 public MetricsInfo info() {
74 return metricsInfo;
75 }
76
77
78
79
80
81
82 public MutableMetric get(String name) {
83 return metricsMap.get(name);
84 }
85
86
87
88
89
90
91 public MetricsTag getTag(String name) {
92 return tagsMap.get(name);
93 }
94
95
96
97
98
99
100
101
102 public MutableCounterInt newCounter(String name, String desc, int iVal) {
103 return newCounter(Interns.info(name, desc), iVal);
104 }
105
106
107
108
109
110
111
112 public MutableCounterInt newCounter(MetricsInfo info, int iVal) {
113 MutableCounterInt ret = new MutableCounterInt(info, iVal);
114 return addNewMetricIfAbsent(info.name(), ret, MutableCounterInt.class);
115 }
116
117
118
119
120
121
122
123
124 public MutableCounterLong newCounter(String name, String desc, long iVal) {
125 return newCounter(Interns.info(name, desc), iVal);
126 }
127
128
129
130
131
132
133
134 public MutableCounterLong newCounter(MetricsInfo info, long iVal) {
135 MutableCounterLong ret = new MutableCounterLong(info, iVal);
136 return addNewMetricIfAbsent(info.name(), ret, MutableCounterLong.class);
137 }
138
139
140
141
142
143
144
145
146 public MutableGaugeInt newGauge(String name, String desc, int iVal) {
147 return newGauge(Interns.info(name, desc), iVal);
148 }
149
150
151
152
153
154
155 public MutableGaugeInt newGauge(MetricsInfo info, int iVal) {
156 MutableGaugeInt ret = new MutableGaugeInt(info, iVal);
157 return addNewMetricIfAbsent(info.name(), ret, MutableGaugeInt.class);
158 }
159
160
161
162
163
164
165
166
167 public MutableGaugeLong newGauge(String name, String desc, long iVal) {
168 return newGauge(Interns.info(name, desc), iVal);
169 }
170
171
172
173
174
175
176
177 public MutableGaugeLong newGauge(MetricsInfo info, long iVal) {
178 MutableGaugeLong ret = new MutableGaugeLong(info, iVal);
179 return addNewMetricIfAbsent(info.name(), ret, MutableGaugeLong.class);
180 }
181
182
183
184
185
186
187
188
189
190
191 public MutableStat newStat(String name, String desc,
192 String sampleName, String valueName, boolean extended) {
193 MutableStat ret =
194 new MutableStat(name, desc, sampleName, valueName, extended);
195 return addNewMetricIfAbsent(name, ret, MutableStat.class);
196 }
197
198
199
200
201
202
203
204
205
206 public MutableStat newStat(String name, String desc,
207 String sampleName, String valueName) {
208 return newStat(name, desc, sampleName, valueName, false);
209 }
210
211
212
213
214
215
216 public MutableRate newRate(String name) {
217 return newRate(name, name, false);
218 }
219
220
221
222
223
224
225
226 public MutableRate newRate(String name, String description) {
227 return newRate(name, description, false);
228 }
229
230
231
232
233
234
235
236
237 public MutableRate newRate(String name, String desc, boolean extended) {
238 return newRate(name, desc, extended, true);
239 }
240
241 @InterfaceAudience.Private
242 public MutableRate newRate(String name, String desc,
243 boolean extended, boolean returnExisting) {
244 if (returnExisting) {
245 MutableMetric rate = metricsMap.get(name);
246 if (rate != null) {
247 if (rate instanceof MutableRate) return (MutableRate) rate;
248 throw new MetricsException("Unexpected metrics type "+ rate.getClass()
249 +" for "+ name);
250 }
251 }
252 MutableRate ret = new MutableRate(name, desc, extended);
253 return addNewMetricIfAbsent(name, ret, MutableRate.class);
254 }
255
256
257
258
259
260
261 public MutableHistogram newHistogram(String name) {
262 return newHistogram(name, "");
263 }
264
265
266
267
268
269
270
271 public MutableHistogram newHistogram(String name, String desc) {
272 MutableHistogram histo = new MutableHistogram(name, desc);
273 return addNewMetricIfAbsent(name, histo, MutableHistogram.class);
274 }
275
276
277
278
279
280
281 public MetricMutableQuantiles newQuantile(String name) {
282 return newQuantile(name, "");
283 }
284
285 public MetricMutableQuantiles newQuantile(String name, String desc) {
286 MetricMutableQuantiles histo = new MetricMutableQuantiles(name, desc, "Ops", "", 60);
287 return addNewMetricIfAbsent(name, histo, MetricMutableQuantiles.class);
288 }
289
290 synchronized void add(String name, MutableMetric metric) {
291 addNewMetricIfAbsent(name, metric, MutableMetric.class);
292 }
293
294
295
296
297
298
299 public void add(String name, long value) {
300 MutableMetric m = metricsMap.get(name);
301
302 if (m != null) {
303 if (m instanceof MutableStat) {
304 ((MutableStat) m).add(value);
305 }
306 else {
307 throw new MetricsException("Unsupported add(value) for metric "+ name);
308 }
309 }
310 else {
311 metricsMap.put(name, newRate(name));
312 add(name, value);
313 }
314 }
315
316
317
318
319
320
321 public DynamicMetricsRegistry setContext(String name) {
322 return tag(MsInfo.Context, name, true);
323 }
324
325
326
327
328
329
330
331
332 public DynamicMetricsRegistry tag(String name, String description, String value) {
333 return tag(name, description, value, false);
334 }
335
336
337
338
339
340
341
342
343
344 public DynamicMetricsRegistry tag(String name, String description, String value,
345 boolean override) {
346 return tag(Interns.info(name, description), value, override);
347 }
348
349
350
351
352
353
354
355
356 public DynamicMetricsRegistry tag(MetricsInfo info, String value, boolean override) {
357 MetricsTag tag = Interns.tag(info, value);
358
359 if (!override) {
360 MetricsTag existing = tagsMap.putIfAbsent(info.name(), tag);
361 if (existing != null) {
362 throw new MetricsException("Tag "+ info.name() +" already exists!");
363 }
364 return this;
365 }
366
367 tagsMap.put(info.name(), tag);
368
369 return this;
370 }
371
372 public DynamicMetricsRegistry tag(MetricsInfo info, String value) {
373 return tag(info, value, false);
374 }
375
376 Collection<MetricsTag> tags() {
377 return tagsMap.values();
378 }
379
380 Collection<MutableMetric> metrics() {
381 return metricsMap.values();
382 }
383
384
385
386
387
388
389 public void snapshot(MetricsRecordBuilder builder, boolean all) {
390 for (MetricsTag tag : tags()) {
391 builder.add(tag);
392 }
393 for (MutableMetric metric : metrics()) {
394 metric.snapshot(builder, all);
395 }
396 }
397
398 @Override public String toString() {
399 return Objects.toStringHelper(this)
400 .add("info", metricsInfo).add("tags", tags()).add("metrics", metrics())
401 .toString();
402 }
403
404
405
406
407
408 public void removeMetric(String name) {
409 metricsMap.remove(name);
410 }
411
412
413
414
415
416
417
418 public MutableGaugeLong getLongGauge(String gaugeName, long potentialStartingValue) {
419
420 MutableMetric metric = metricsMap.get(gaugeName);
421
422
423 if (metric == null) {
424
425
426 MutableGaugeLong newGauge = new MutableGaugeLong(Interns.info(gaugeName, ""),
427 potentialStartingValue);
428
429
430 metric = metricsMap.putIfAbsent(gaugeName, newGauge);
431
432
433
434 if (metric == null) {
435 return newGauge;
436 }
437 }
438
439 if (!(metric instanceof MutableGaugeLong)) {
440 throw new MetricsException("Metric already exists in registry for metric name: " + gaugeName +
441 " and not of type MetricMutableGaugeLong");
442 }
443
444 return (MutableGaugeLong) metric;
445 }
446
447
448
449
450
451
452
453 public MutableCounterLong getLongCounter(String counterName, long potentialStartingValue) {
454
455 MutableMetric counter = metricsMap.get(counterName);
456 if (counter == null) {
457 MutableCounterLong newCounter =
458 new MutableCounterLong(Interns.info(counterName, ""), potentialStartingValue);
459 counter = metricsMap.putIfAbsent(counterName, newCounter);
460 if (counter == null) {
461 return newCounter;
462 }
463 }
464
465
466 if (!(counter instanceof MutableCounterLong)) {
467 throw new MetricsException("Metric already exists in registry for metric name: " +
468 counterName + " and not of type MetricMutableCounterLong");
469 }
470
471 return (MutableCounterLong) counter;
472 }
473
474 public MutableHistogram getHistogram(String histoName) {
475
476 MutableMetric histo = metricsMap.get(histoName);
477 if (histo == null) {
478 MutableHistogram newCounter =
479 new MutableHistogram(Interns.info(histoName, ""));
480 histo = metricsMap.putIfAbsent(histoName, newCounter);
481 if (histo == null) {
482 return newCounter;
483 }
484 }
485
486
487 if (!(histo instanceof MutableHistogram)) {
488 throw new MetricsException("Metric already exists in registry for metric name: " +
489 histoName + " and not of type MutableHistogram");
490 }
491
492 return (MutableHistogram) histo;
493 }
494
495 public MetricMutableQuantiles getQuantile(String histoName) {
496
497 MutableMetric histo = metricsMap.get(histoName);
498 if (histo == null) {
499 MetricMutableQuantiles newCounter =
500 new MetricMutableQuantiles(histoName, "", "Ops", "", 60);
501 histo = metricsMap.putIfAbsent(histoName, newCounter);
502 if (histo == null) {
503 return newCounter;
504 }
505 }
506
507
508 if (!(histo instanceof MetricMutableQuantiles)) {
509 throw new MetricsException("Metric already exists in registry for metric name: " +
510 histoName + " and not of type MutableHistogram");
511 }
512
513 return (MetricMutableQuantiles) histo;
514 }
515
516 private<T extends MutableMetric> T
517 addNewMetricIfAbsent(String name,
518 T ret,
519 Class<T> metricClass) {
520
521
522
523 MutableMetric metric = metricsMap.putIfAbsent(name, ret);
524 if (metric == null) {
525 return ret;
526 }
527
528 return returnExistingWithCast(metric, metricClass, name);
529 }
530
531 @SuppressWarnings("unchecked")
532 private<T> T returnExistingWithCast(MutableMetric metric,
533 Class<T> metricClass, String name) {
534 if (!metricClass.isAssignableFrom(metric.getClass())) {
535 throw new MetricsException("Metric already exists in registry for metric name: " +
536 name + " and not of type " + metricClass +
537 " but instead of type " + metric.getClass());
538 }
539
540 return (T) metric;
541 }
542
543 public void clearMetrics() {
544 metricsMap.clear();
545 }
546 }