31
31
import java .util .Optional ;
32
32
import java .util .Set ;
33
33
import java .util .TreeSet ;
34
+ import java .util .Collections ;
35
+ import java .util .stream .Collectors ;
34
36
import javax .management .Attribute ;
35
37
import javax .management .AttributeList ;
36
38
import javax .management .JMException ;
@@ -72,6 +74,7 @@ public interface MBeanReceiver {
72
74
void recordBean (
73
75
String domain ,
74
76
LinkedHashMap <String , String > beanProperties ,
77
+ Map <String , String > attributesAsLabelsWithValues ,
75
78
LinkedList <String > attrKeys ,
76
79
String attrName ,
77
80
String attrType ,
@@ -85,6 +88,7 @@ void recordBean(
85
88
private final String password ;
86
89
private final boolean ssl ;
87
90
private final List <ObjectName > includeObjectNames , excludeObjectNames ;
91
+ private final List <JmxCollector .MetricCustomizer > metricCustomizers ;
88
92
private final ObjectNameAttributeFilter objectNameAttributeFilter ;
89
93
private final JmxMBeanPropertyCache jmxMBeanPropertyCache ;
90
94
@@ -109,6 +113,7 @@ public JmxScraper(
109
113
List <ObjectName > includeObjectNames ,
110
114
List <ObjectName > excludeObjectNames ,
111
115
ObjectNameAttributeFilter objectNameAttributeFilter ,
116
+ List <JmxCollector .MetricCustomizer > metricCustomizers ,
112
117
MBeanReceiver receiver ,
113
118
JmxMBeanPropertyCache jmxMBeanPropertyCache ) {
114
119
this .jmxUrl = jmxUrl ;
@@ -118,6 +123,7 @@ public JmxScraper(
118
123
this .ssl = ssl ;
119
124
this .includeObjectNames = includeObjectNames ;
120
125
this .excludeObjectNames = excludeObjectNames ;
126
+ this .metricCustomizers = metricCustomizers ;
121
127
this .objectNameAttributeFilter = objectNameAttributeFilter ;
122
128
this .jmxMBeanPropertyCache = jmxMBeanPropertyCache ;
123
129
}
@@ -253,6 +259,12 @@ private void scrapeBean(MBeanServerConnection beanConn, ObjectName mBeanName) {
253
259
254
260
final String mBeanNameString = mBeanName .toString ();
255
261
final String mBeanDomain = mBeanName .getDomain ();
262
+ JmxCollector .MetricCustomizer metricCustomizer = getMetricCustomizer (mBeanName );
263
+ Map <String , String > attributesAsLabelsWithValues = Collections .emptyMap ();
264
+ if (metricCustomizer != null ) {
265
+ attributesAsLabelsWithValues =
266
+ getAttributesAsLabelsWithValues (metricCustomizer , attributes );
267
+ }
256
268
257
269
for (Object object : attributes ) {
258
270
// The contents of an AttributeList should all be Attribute instances, but we'll verify
@@ -280,6 +292,7 @@ private void scrapeBean(MBeanServerConnection beanConn, ObjectName mBeanName) {
280
292
mBeanName ,
281
293
mBeanDomain ,
282
294
jmxMBeanPropertyCache .getKeyPropertyList (mBeanName ),
295
+ attributesAsLabelsWithValues ,
283
296
new LinkedList <>(),
284
297
mBeanAttributeInfo .getName (),
285
298
mBeanAttributeInfo .getType (),
@@ -300,6 +313,35 @@ private void scrapeBean(MBeanServerConnection beanConn, ObjectName mBeanName) {
300
313
}
301
314
}
302
315
316
+ private Map <String , String > getAttributesAsLabelsWithValues (JmxCollector .MetricCustomizer metricCustomizer , AttributeList attributes ) {
317
+ Map <String , Object > attributeMap = attributes .asList ().stream ()
318
+ .collect (Collectors .toMap (Attribute ::getName , Attribute ::getValue ));
319
+ Map <String , String > attributesAsLabelsWithValues = new HashMap <>();
320
+ for (String attributeAsLabel : metricCustomizer .attributesAsLabels ) {
321
+ Object attrValue = attributeMap .get (attributeAsLabel );
322
+ if (attrValue != null ) {
323
+ attributesAsLabelsWithValues .put (attributeAsLabel , attrValue .toString ());
324
+ }
325
+ }
326
+ return attributesAsLabelsWithValues ;
327
+ }
328
+
329
+ private JmxCollector .MetricCustomizer getMetricCustomizer (ObjectName mBeanName ) {
330
+ if (!metricCustomizers .isEmpty ()) {
331
+ for (JmxCollector .MetricCustomizer metricCustomizer : metricCustomizers ) {
332
+ if (filterMbeanByDomainAndProperties (mBeanName , metricCustomizer )) {
333
+ return metricCustomizer ;
334
+ }
335
+ }
336
+ }
337
+ return null ;
338
+ }
339
+
340
+ private boolean filterMbeanByDomainAndProperties (ObjectName mBeanName , JmxCollector .MetricCustomizer metricCustomizer ) {
341
+ return metricCustomizer .mbeanFilter .domain .equals (mBeanName .getDomain ()) &&
342
+ mBeanName .getKeyPropertyList ().entrySet ().containsAll (metricCustomizer .mbeanFilter .properties .entrySet ());
343
+ }
344
+
303
345
private void processAttributesOneByOne (
304
346
MBeanServerConnection beanConn ,
305
347
ObjectName mbeanName ,
@@ -318,6 +360,7 @@ private void processAttributesOneByOne(
318
360
mbeanName ,
319
361
mbeanName .getDomain (),
320
362
jmxMBeanPropertyCache .getKeyPropertyList (mbeanName ),
363
+ new HashMap <>(),
321
364
new LinkedList <>(),
322
365
attr .getName (),
323
366
attr .getType (),
@@ -335,6 +378,7 @@ private void processBeanValue(
335
378
ObjectName objectName ,
336
379
String domain ,
337
380
LinkedHashMap <String , String > beanProperties ,
381
+ Map <String , String > attributesAsLabelsWithValues ,
338
382
LinkedList <String > attrKeys ,
339
383
String attrName ,
340
384
String attrType ,
@@ -352,7 +396,7 @@ private void processBeanValue(
352
396
}
353
397
LOGGER .log (FINE , "%s%s%s scrape: %s" , domain , beanProperties , attrName , value );
354
398
this .receiver .recordBean (
355
- domain , beanProperties , attrKeys , attrName , attrType , attrDescription , value );
399
+ domain , beanProperties , attributesAsLabelsWithValues , attrKeys , attrName , attrType , attrDescription , value );
356
400
} else if (value instanceof CompositeData ) {
357
401
LOGGER .log (FINE , "%s%s%s scrape: compositedata" , domain , beanProperties , attrName );
358
402
CompositeData composite = (CompositeData ) value ;
@@ -366,6 +410,7 @@ private void processBeanValue(
366
410
objectName ,
367
411
domain ,
368
412
beanProperties ,
413
+ attributesAsLabelsWithValues ,
369
414
attrKeys ,
370
415
key ,
371
416
typ ,
@@ -432,6 +477,7 @@ private void processBeanValue(
432
477
objectName ,
433
478
domain ,
434
479
l2s ,
480
+ attributesAsLabelsWithValues ,
435
481
attrNames ,
436
482
name ,
437
483
typ ,
@@ -452,6 +498,7 @@ private void processBeanValue(
452
498
objectName ,
453
499
domain ,
454
500
beanProperties ,
501
+ attributesAsLabelsWithValues ,
455
502
attrKeys ,
456
503
attrName ,
457
504
attrType ,
@@ -464,6 +511,7 @@ private void processBeanValue(
464
511
objectName ,
465
512
domain ,
466
513
beanProperties ,
514
+ attributesAsLabelsWithValues ,
467
515
attrKeys ,
468
516
attrName ,
469
517
attrType ,
@@ -479,6 +527,7 @@ private static class StdoutWriter implements MBeanReceiver {
479
527
public void recordBean (
480
528
String domain ,
481
529
LinkedHashMap <String , String > beanProperties ,
530
+ Map <String , String > attributesAsLabelsWithValues ,
482
531
LinkedList <String > attrKeys ,
483
532
String attrName ,
484
533
String attrType ,
@@ -503,6 +552,7 @@ public static void main(String[] args) throws Exception {
503
552
objectNames ,
504
553
new LinkedList <>(),
505
554
objectNameAttributeFilter ,
555
+ new LinkedList <>(),
506
556
new StdoutWriter (),
507
557
new JmxMBeanPropertyCache ())
508
558
.doScrape ();
@@ -515,6 +565,7 @@ public static void main(String[] args) throws Exception {
515
565
objectNames ,
516
566
new LinkedList <>(),
517
567
objectNameAttributeFilter ,
568
+ new LinkedList <>(),
518
569
new StdoutWriter (),
519
570
new JmxMBeanPropertyCache ())
520
571
.doScrape ();
@@ -527,6 +578,7 @@ public static void main(String[] args) throws Exception {
527
578
objectNames ,
528
579
new LinkedList <>(),
529
580
objectNameAttributeFilter ,
581
+ new LinkedList <>(),
530
582
new StdoutWriter (),
531
583
new JmxMBeanPropertyCache ())
532
584
.doScrape ();
0 commit comments