Skip to content

Commit 7c30f73

Browse files
committed
Moved jmx logic into metrics4j
1 parent 2d0a86f commit 7c30f73

File tree

12 files changed

+17
-693
lines changed

12 files changed

+17
-693
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ metrics4j: {
2020
```
2121

2222
Run your application and when it shuts down Metrics4j will create the file dump.conf
23-
that shows all the JMX sources that were found.
23+
that shows all the JMX sources that were found. With some applications shutdown
24+
may happen too quickly and the dump file may not be created. The dump file is also
25+
written out after the application has ran for 1 minute.
2426

2527
Replace the simple metrics4j.conf file with the one from the dump and then follow
2628
the Metrics4j documentation to add a trigger, formatter and sink so it can

pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>org.kairosdb</groupId>
88
<artifactId>JMXReporter</artifactId>
9-
<version>0.18.0</version>
9+
<version>0.19.0</version>
1010

1111
<repositories>
1212
<repository>
@@ -20,7 +20,7 @@
2020
<dependency>
2121
<groupId>org.kairosdb</groupId>
2222
<artifactId>metrics4j</artifactId>
23-
<version>0.18.0</version>
23+
<version>0.19.0</version>
2424
</dependency>
2525

2626
<dependency>

src/main/java/org/kairosdb/jmxreporter/JMXReporter.java

+4-332
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,18 @@
1414
import javax.management.openmbean.CompositeType;
1515
import javax.management.openmbean.OpenType;
1616
import javax.management.openmbean.SimpleType;
17+
import java.io.IOException;
1718
import java.lang.instrument.Instrumentation;
1819
import java.lang.management.ManagementFactory;
1920
import java.util.ArrayList;
2021
import java.util.HashMap;
2122
import java.util.List;
2223
import java.util.Map;
2324

24-
public class JMXReporter implements NotificationListener
25+
public class JMXReporter
2526
{
2627
private static final Logger logger = LoggerFactory.getLogger(JMXReporter.class);
2728

28-
private final MBeanServer m_server;
29-
private final Map<ObjectName, List<SourceKey>> m_sourceKeyMap = new HashMap<>();
30-
3129
public static void main(String[] args)
3230
{
3331
premain(null, null);
@@ -44,333 +42,7 @@ public static void main(String[] args)
4442

4543
public static void premain(String args, Instrumentation instrumentation)
4644
{
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);
37547
}
37648
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.kairosdb.jmxreporter;
2+
3+
import org.kairosdb.metrics4j.collectors.LongCollector;
4+
5+
public interface Metrics
6+
{
7+
LongCollector metrics();
8+
}

0 commit comments

Comments
 (0)