14
14
import javax .management .openmbean .CompositeType ;
15
15
import javax .management .openmbean .OpenType ;
16
16
import javax .management .openmbean .SimpleType ;
17
+ import java .io .IOException ;
17
18
import java .lang .instrument .Instrumentation ;
18
19
import java .lang .management .ManagementFactory ;
19
20
import java .util .ArrayList ;
20
21
import java .util .HashMap ;
21
22
import java .util .List ;
22
23
import java .util .Map ;
23
24
24
- public class JMXReporter implements NotificationListener
25
+ public class JMXReporter
25
26
{
26
27
private static final Logger logger = LoggerFactory .getLogger (JMXReporter .class );
27
28
28
- private final MBeanServer m_server ;
29
- private final Map <ObjectName , List <SourceKey >> m_sourceKeyMap = new HashMap <>();
30
-
31
29
public static void main (String [] args )
32
30
{
33
31
premain (null , null );
@@ -44,333 +42,7 @@ public static void main(String[] args)
44
42
45
43
public static void premain (String args , Instrumentation instrumentation )
46
44
{
47
- MBeanServer server = ManagementFactory .getPlatformMBeanServer ();
48
-
49
- JMXReporter reporter = new JMXReporter (server );
50
-
51
- //Register all current MBeans
52
- reporter .loadExistingMBeans ();
53
-
54
-
55
- //Add notification for future MBeans
56
- try
57
- {
58
- reporter .addMBeanNotification ();
59
- }
60
- catch (InstanceNotFoundException e )
61
- {
62
- e .printStackTrace ();
63
- }
64
- }
65
-
66
- public void close () throws ListenerNotFoundException , InstanceNotFoundException
67
- {
68
- m_server .removeNotificationListener (MBeanServerDelegate .DELEGATE_NAME , this );
69
- }
70
-
71
- public JMXReporter (MBeanServer server )
72
- {
73
- m_server = server ;
74
- }
75
-
76
- /*package*/ void loadExistingMBeans ()
77
- {
78
- for (ObjectInstance queryMBean : m_server .queryMBeans (null , null ))
79
- {
80
- registerMBean (queryMBean .getObjectName ());
81
- }
82
- }
83
-
84
- /*package*/ void addMBeanNotification () throws InstanceNotFoundException
85
- {
86
- m_server .addNotificationListener (MBeanServerDelegate .DELEGATE_NAME , this , null , null );
87
- }
88
-
89
- private void registerMBean (ObjectName beanName )
90
- {
91
- List <SourceKey > sourceKeys = new ArrayList <>();
92
- try
93
- {
94
- for (MBeanAttributeInfo attribute : m_server .getMBeanInfo (beanName ).getAttributes ())
95
- {
96
- if (attribute .isReadable () && !attribute .isWritable ())
97
- {
98
- String type = attribute .getType ();
99
- String className = beanName .getDomain ()+"." +beanName .getKeyProperty ("type" );
100
- String methodName = attribute .getName ();
101
- Map <String , String > tags = beanName .getKeyPropertyList ();
102
- //System.out.println(attribute.getName());
103
- //System.out.println(attribute.getDescription());
104
- if (type .equals ("int" ))
105
- {
106
- MetricSourceManager .addSource (className , methodName ,
107
- tags , null , new IntAttributeSource (beanName , attribute .getName ()));
108
- sourceKeys .add (new SourceKey (className , methodName , tags ));
109
- }
110
- else if (type .equals ("long" ))
111
- {
112
- MetricSourceManager .addSource (className , methodName ,
113
- tags , null , new LongAttributeSource (beanName , attribute .getName ()));
114
- sourceKeys .add (new SourceKey (className , methodName , tags ));
115
- }
116
- else if (type .equals ("float" ))
117
- {
118
- MetricSourceManager .addSource (className , methodName ,
119
- tags , null , new FloatAttributeSource (beanName , attribute .getName ()));
120
- sourceKeys .add (new SourceKey (className , methodName , tags ));
121
- }
122
- else if (type .equals ("double" ))
123
- {
124
- MetricSourceManager .addSource (className , methodName ,
125
- tags , null , new DoubleAttributeSource (beanName , attribute .getName ()));
126
- sourceKeys .add (new SourceKey (className , methodName , tags ));
127
- }
128
- else if (type .equals ("javax.management.openmbean.CompositeData" ))
129
- {
130
- MetricSourceManager .addSource (className , methodName ,
131
- tags , null , new CompositeAttributeSource (beanName , attribute .getName ()));
132
- sourceKeys .add (new SourceKey (className , methodName , tags ));
133
- }
134
- }
135
- }
136
- }
137
- catch (Exception e )
138
- {
139
- e .printStackTrace ();
140
- }
141
-
142
- if (sourceKeys .size () != 0 )
143
- m_sourceKeyMap .put (beanName , sourceKeys );
144
- }
145
-
146
- private void unregisterMBean (ObjectName beanName )
147
- {
148
- try
149
- {
150
- List <SourceKey > sourceKeys = m_sourceKeyMap .get (beanName );
151
-
152
- if (sourceKeys != null )
153
- {
154
- for (SourceKey sourceKey : sourceKeys )
155
- {
156
- MetricSourceManager .removeSource (sourceKey .getClassName (), sourceKey .getMethodName (),
157
- sourceKey .getTags ());
158
- }
159
- }
160
- }
161
- catch (Exception e )
162
- {
163
- e .printStackTrace ();
164
- }
165
- }
166
-
167
- @ Override
168
- public void handleNotification (Notification notification , Object handback )
169
- {
170
- if (!(notification instanceof MBeanServerNotification )) {
171
- System .out .println ("Ignored notification of class " + notification .getClass ().getName ());
172
- return ;
173
- }
174
- MBeanServerNotification mbsn = (MBeanServerNotification ) notification ;
175
- String what = "" ;
176
- ObjectName beanName = mbsn .getMBeanName ();
177
- if (notification .getType ().equals (MBeanServerNotification .REGISTRATION_NOTIFICATION ))
178
- {
179
- registerMBean (beanName );
180
- what = "MBean registered" ;
181
- }
182
- else if (notification .getType ().equals (MBeanServerNotification .UNREGISTRATION_NOTIFICATION ))
183
- {
184
- unregisterMBean (beanName );
185
- what = "MBean unregistered" ;
186
- }
187
-
188
- logger .debug ("Received MBean Server notification: {}: {}" , what , beanName );
189
- }
190
-
191
- private static void reportValue (MetricReporter reporter , String key , Long value )
192
- {
193
- if (value != null )
194
- reporter .put (key , new LongValue (value ));
195
- }
196
-
197
- private static void reportValue (MetricReporter reporter , String key , Integer value )
198
- {
199
- if (value != null )
200
- reporter .put (key , new LongValue (value ));
201
- }
202
-
203
- private static void reportValue (MetricReporter reporter , String key , Float value )
204
- {
205
- if (value != null && !value .isInfinite () && !value .isNaN ())
206
- reporter .put (key , new DoubleValue (value ));
207
- }
208
-
209
- private static void reportValue (MetricReporter reporter , String key , Double value )
210
- {
211
- if (value != null && !value .isInfinite () && !value .isNaN ())
212
- reporter .put (key , new DoubleValue (value ));
213
- }
214
-
215
- private abstract class AttributeSource implements MetricCollector
216
- {
217
- protected final ObjectName m_objectName ;
218
- protected final String m_attribute ;
219
-
220
- private AttributeSource (ObjectName objectName , String attribute )
221
- {
222
- m_objectName = objectName ;
223
- m_attribute = attribute ;
224
- }
225
- }
226
-
227
-
228
- private class IntAttributeSource extends AttributeSource
229
- {
230
- private IntAttributeSource (ObjectName objectName , String attribute )
231
- {
232
- super (objectName , attribute );
233
- }
234
-
235
- @ Override
236
- public void reportMetric (MetricReporter metricReporter )
237
- {
238
- Integer value = null ;
239
-
240
- try
241
- {
242
- value = (Integer )m_server .getAttribute (m_objectName , m_attribute );
243
- }
244
- catch (Exception e )
245
- {
246
- logger .debug ("Failed to read JMX attribute " +m_objectName +": " +m_attribute , e );
247
- }
248
-
249
- reportValue (metricReporter , "value" , value );
250
- }
251
-
252
- }
253
-
254
- private class LongAttributeSource extends AttributeSource
255
- {
256
- private LongAttributeSource (ObjectName objectName , String attribute )
257
- {
258
- super (objectName , attribute );
259
- }
260
-
261
- @ Override
262
- public void reportMetric (MetricReporter metricReporter )
263
- {
264
- Long value = null ;
265
-
266
- try
267
- {
268
- value = (Long )m_server .getAttribute (m_objectName , m_attribute );
269
- }
270
- catch (Exception e )
271
- {
272
- logger .debug ("Failed to read JMX attribute " +m_objectName +": " +m_attribute , e );
273
- }
274
-
275
- reportValue (metricReporter , "value" , value );
276
- }
277
- }
278
-
279
- private class FloatAttributeSource extends AttributeSource
280
- {
281
-
282
- private FloatAttributeSource (ObjectName objectName , String attribute )
283
- {
284
- super (objectName , attribute );
285
- }
286
-
287
- @ Override
288
- public void reportMetric (MetricReporter metricReporter )
289
- {
290
- Float value = null ;
291
-
292
- try
293
- {
294
- value = (Float )m_server .getAttribute (m_objectName , m_attribute );
295
- }
296
- catch (Exception e )
297
- {
298
- logger .debug ("Failed to read JMX attribute " +m_objectName +": " +m_attribute , e );
299
- }
300
-
301
- reportValue (metricReporter , "value" , value );
302
- }
303
- }
304
-
305
- private class DoubleAttributeSource extends AttributeSource
306
- {
307
-
308
- private DoubleAttributeSource (ObjectName objectName , String attribute )
309
- {
310
- super (objectName , attribute );
311
- }
312
-
313
- @ Override
314
- public void reportMetric (MetricReporter metricReporter )
315
- {
316
- Double value = null ;
317
-
318
- try
319
- {
320
- value = (Double )m_server .getAttribute (m_objectName , m_attribute );
321
- }
322
- catch (Exception e )
323
- {
324
- logger .debug ("Failed to read JMX attribute " +m_objectName +": " +m_attribute , e );
325
- }
326
-
327
- reportValue (metricReporter , "value" , value );
328
- }
329
- }
330
-
331
- private class CompositeAttributeSource extends AttributeSource
332
- {
333
- private CompositeAttributeSource (ObjectName objectName , String attribute )
334
- {
335
- super (objectName , attribute );
336
- }
337
-
338
- @ Override
339
- public void reportMetric (MetricReporter metricReporter )
340
- {
341
- try
342
- {
343
- CompositeData data = (CompositeData ) m_server .getAttribute (m_objectName , m_attribute );
344
- if (data != null )
345
- {
346
- CompositeType type = data .getCompositeType ();
347
-
348
- for (String key : type .keySet ())
349
- {
350
- OpenType <?> openType = type .getType (key );
351
- if (openType == SimpleType .LONG )
352
- {
353
- reportValue (metricReporter , key , (Long )data .get (key ));
354
- }
355
- else if (openType == SimpleType .INTEGER )
356
- {
357
- reportValue (metricReporter , key , (Integer ) data .get (key ));
358
- }
359
- else if (openType == SimpleType .FLOAT )
360
- {
361
- reportValue (metricReporter , key , (Float )data .get (key ));
362
- }
363
- else if (openType == SimpleType .DOUBLE )
364
- {
365
- reportValue (metricReporter , key , (Double )data .get (key ));
366
- }
367
- }
368
- }
369
- }
370
- catch (Exception e )
371
- {
372
- logger .debug ("Failed to read JMX attribute " +m_objectName +": " +m_attribute , e );
373
- }
374
- }
45
+ //This effectively loads metrics4j
46
+ MetricSourceManager .getSource (Metrics .class );
375
47
}
376
48
}
0 commit comments