-
Notifications
You must be signed in to change notification settings - Fork 470
/
ExtractMetrics.java
118 lines (99 loc) · 3.79 KB
/
ExtractMetrics.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package org.jgroups.util;
import org.jgroups.JChannel;
import org.jgroups.jmx.ResourceDMBean;
import org.jgroups.stack.Protocol;
import javax.management.MBeanAttributeInfo;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Predicate;
import java.util.function.Supplier;
/**
* Extracts all attributes and methods annotated with {@link org.jgroups.annotations.ManagedAttribute} and returns them
* as a map of names associated with [getter-method/description tuples]. E.g. for an attribute called foo, a method
* foo() or getFoo() is searched for.
* @author Bela Ban
* @since 5.4, 5.3.6
*/
public class ExtractMetrics {
protected JChannel ch;
protected static final Predicate<AccessibleObject> IS_NUMBER=obj -> {
if(obj instanceof Field)
return ResourceDMBean.isNumber(((Field)obj).getType());
if(obj instanceof Method) {
Method m=(Method)obj;
return ResourceDMBean.isNumber(m.getReturnType());
}
return false;
};
public static class Entry {
protected final String description;
protected final Supplier<Object> method;
protected Entry(String description, Supplier<Object> method) {
this.description=description;
this.method=method;
}
public String getDescription() {
return description;
}
public Supplier<Object> getMethod() {
return method;
}
@Override
public String toString() {
return String.format(" %s [%s]", method.get(), description);
}
}
public static Map<String,Map<String,Entry>> extract(JChannel ch) {
return extract(ch, null);
}
public static Map<String,Map<String,Entry>> extract(JChannel ch, Predicate<AccessibleObject> filter) {
Map<String,Map<String,Entry>> map=new HashMap<>();
for(Protocol p: ch.stack().getProtocols()) {
map.put(p.getName(), extract(p, filter));
}
return map;
}
public static Map<String,Entry> extract(Protocol p) {
return extract(p, null);
}
public static Map<String,Entry> extract(Protocol p, Predicate<AccessibleObject> filter) {
Map<String,Entry> map=new HashMap<>();
ResourceDMBean dm=new ResourceDMBean(p, filter);
dm.forAllAttributes((k,v) -> {
MBeanAttributeInfo info=v.info();
String descr=info != null? info.getDescription() : "n/a";
Supplier<Object> getter=() -> {
try {
return v.getter() != null? v.getter().invoke(null) : null;
}
catch(Exception e) {
System.err.printf("failed getting value for %s\n", k);
return null;
}
};
map.put(k, new Entry(descr, getter));
});
return map;
}
protected void start(boolean numeric) throws Exception {
ch=new JChannel().connect("bla").name("X");
Map<String,Map<String,Entry>> map=extract(ch, IS_NUMBER);
for(Map.Entry<String,Map<String,Entry>> e: map.entrySet()) {
System.out.printf("\n%s:\n---------------\n", e.getKey());
for(Map.Entry<String,Entry> e2: e.getValue().entrySet()) {
Entry entry=e2.getValue();
Supplier<Object> s=entry.getMethod();
if(s != null)
System.out.printf(" %s: %s\n", e2.getKey(), s.get());
}
}
Util.close(ch);
}
public static void main(String[] args) throws Throwable {
boolean numeric=args.length > 0 && Boolean.parseBoolean(args[0]);
new ExtractMetrics().start(numeric);
}
}