Skip to content

Commit 046a8d8

Browse files
committed
Update to 0.7.0 metrics4j and fix NaN bugs in data
1 parent 9922b00 commit 046a8d8

File tree

3 files changed

+49
-38
lines changed

3 files changed

+49
-38
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
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.6.0</version>
9+
<version>0.7.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.6.0</version>
23+
<version>0.7.0</version>
2424
</dependency>
2525

2626
<dependency>

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

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,30 @@ else if (notification.getType().equals(MBeanServerNotification.UNREGISTRATION_NO
188188
logger.debug("Received MBean Server notification: {}: {}", what, beanName);
189189
}
190190

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+
191215
private abstract class AttributeSource implements MetricCollector
192216
{
193217
protected final ObjectName m_objectName;
@@ -198,14 +222,6 @@ private AttributeSource(ObjectName objectName, String attribute)
198222
m_objectName = objectName;
199223
m_attribute = attribute;
200224
}
201-
202-
protected abstract MetricValue getValue();
203-
204-
@Override
205-
public void reportMetric(MetricReporter metricReporter)
206-
{
207-
metricReporter.put("value", getValue());
208-
}
209225
}
210226

211227

@@ -217,20 +233,20 @@ private IntAttributeSource(ObjectName objectName, String attribute)
217233
}
218234

219235
@Override
220-
public MetricValue getValue()
236+
public void reportMetric(MetricReporter metricReporter)
221237
{
222-
long value = 0;
238+
Integer value = null;
223239

224240
try
225241
{
226-
value = (int)m_server.getAttribute(m_objectName, m_attribute);
242+
value = (Integer)m_server.getAttribute(m_objectName, m_attribute);
227243
}
228244
catch (Exception e)
229245
{
230246
logger.debug("Failed to read JMX attribute "+m_objectName+": "+m_attribute, e);
231247
}
232248

233-
return new LongValue(value);
249+
reportValue(metricReporter, "value", value);
234250
}
235251

236252
}
@@ -243,20 +259,20 @@ private LongAttributeSource(ObjectName objectName, String attribute)
243259
}
244260

245261
@Override
246-
public MetricValue getValue()
262+
public void reportMetric(MetricReporter metricReporter)
247263
{
248-
long value = 0;
264+
Long value = null;
249265

250266
try
251267
{
252-
value = (long)m_server.getAttribute(m_objectName, m_attribute);
268+
value = (Long)m_server.getAttribute(m_objectName, m_attribute);
253269
}
254270
catch (Exception e)
255271
{
256272
logger.debug("Failed to read JMX attribute "+m_objectName+": "+m_attribute, e);
257273
}
258274

259-
return new LongValue(value);
275+
reportValue(metricReporter, "value", value);
260276
}
261277
}
262278

@@ -269,20 +285,20 @@ private FloatAttributeSource(ObjectName objectName, String attribute)
269285
}
270286

271287
@Override
272-
public MetricValue getValue()
288+
public void reportMetric(MetricReporter metricReporter)
273289
{
274-
float value = 0;
290+
Float value = null;
275291

276292
try
277293
{
278-
value = (float)m_server.getAttribute(m_objectName, m_attribute);
294+
value = (Float)m_server.getAttribute(m_objectName, m_attribute);
279295
}
280296
catch (Exception e)
281297
{
282298
logger.debug("Failed to read JMX attribute "+m_objectName+": "+m_attribute, e);
283299
}
284300

285-
return new DoubleValue(value);
301+
reportValue(metricReporter, "value", value);
286302
}
287303
}
288304

@@ -295,20 +311,20 @@ private DoubleAttributeSource(ObjectName objectName, String attribute)
295311
}
296312

297313
@Override
298-
public MetricValue getValue()
314+
public void reportMetric(MetricReporter metricReporter)
299315
{
300-
double value = 0.0;
316+
Double value = null;
301317

302318
try
303319
{
304-
value = (double)m_server.getAttribute(m_objectName, m_attribute);
320+
value = (Double)m_server.getAttribute(m_objectName, m_attribute);
305321
}
306322
catch (Exception e)
307323
{
308324
logger.debug("Failed to read JMX attribute "+m_objectName+": "+m_attribute, e);
309325
}
310326

311-
return new DoubleValue(value);
327+
reportValue(metricReporter, "value", value);
312328
}
313329
}
314330

@@ -319,12 +335,6 @@ private CompositeAttributeSource(ObjectName objectName, String attribute)
319335
super(objectName, attribute);
320336
}
321337

322-
@Override
323-
protected MetricValue getValue()
324-
{
325-
return null;
326-
}
327-
328338
@Override
329339
public void reportMetric(MetricReporter metricReporter)
330340
{
@@ -340,19 +350,19 @@ public void reportMetric(MetricReporter metricReporter)
340350
OpenType<?> openType = type.getType(key);
341351
if (openType == SimpleType.LONG)
342352
{
343-
metricReporter.put(key, new LongValue((long) data.get(key)));
353+
reportValue(metricReporter, key, (Long)data.get(key));
344354
}
345355
else if (openType == SimpleType.INTEGER)
346356
{
347-
metricReporter.put(key, new LongValue((int) data.get(key)));
357+
reportValue(metricReporter, key, (Integer) data.get(key));
348358
}
349359
else if (openType == SimpleType.FLOAT)
350360
{
351-
metricReporter.put(key, new DoubleValue((float) data.get(key)));
361+
reportValue(metricReporter, key, (Float)data.get(key));
352362
}
353363
else if (openType == SimpleType.DOUBLE)
354364
{
355-
metricReporter.put(key, new DoubleValue((double) data.get(key)));
365+
reportValue(metricReporter, key, (Double)data.get(key));
356366
}
357367
}
358368
}

src/test/resources/metrics4j.conf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
metrics4j: {
22
_dump-file: "dump.conf"
33
sources {
4+
_collector: nullCollector
45
_trigger: myTrigger
56
_sink: slf4j
67
_formatter: template
@@ -65,11 +66,11 @@ metrics4j: {
6566
collectors: {
6667
longGauge:
6768
{
68-
_class: "org.kairosdb.metrics4j.collectors.LongGauge"
69+
_class: "org.kairosdb.metrics4j.collectors.impl.LongGauge"
6970
},
7071
nullCollector:
7172
{
72-
_class: "org.kairosdb.metrics4j.collectors.NullCollector"
73+
_class: "org.kairosdb.metrics4j.collectors.impl.NullCollector"
7374
}
7475
}
7576
sinks: {

0 commit comments

Comments
 (0)