Skip to content

Commit

Permalink
added support on jmx attributes of type java.util.Date (prometheus#449)
Browse files Browse the repository at this point in the history
* added support on jmx attributes of type java.util.Date

Signed-off-by: jacky <jacky.chen@nzqa.govt.nz>
Signed-off-by: jacky <jac.chen@gmail.com>
  • Loading branch information
tianhai authored and qinghui-xu committed Sep 18, 2020
1 parent 12b6a77 commit 189386b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
6 changes: 5 additions & 1 deletion collector/src/main/java/io/prometheus/jmx/JmxScraper.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,11 @@ private void processBeanValue(
Object value) {
if (value == null) {
logScrape(domain + beanProperties + attrName, "null");
} else if (value instanceof Number || value instanceof String || value instanceof Boolean) {
} else if (value instanceof Number || value instanceof String || value instanceof Boolean || value instanceof java.util.Date) {
if (value instanceof java.util.Date) {
attrType = "java.lang.Double";
value = ((java.util.Date) value).getTime() / 1000.0;
}
logScrape(domain + beanProperties + attrName, value.toString());
this.receiver.recordBean(
domain,
Expand Down
26 changes: 26 additions & 0 deletions collector/src/test/java/io/prometheus/jmx/CamelMBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.prometheus.jmx;

import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.util.Date;

public interface CamelMBean {
double EXPECTED_SECONDS = 1.573285945111E9;

Date getLastExchangeFailureTimestamp();
}

class Camel implements CamelMBean {
public static void registerBean(MBeanServer mbs)
throws javax.management.JMException {
ObjectName mbeanName = new ObjectName(
"org.apache.camel:context=my-camel-context,type=routes,name=\"my-route-name\"");
Camel mbean = new Camel();
mbs.registerMBean(mbean, mbeanName);
}

@Override
public Date getLastExchangeFailureTimestamp() {
return new Date((long)(EXPECTED_SECONDS * 1000));
}
}
17 changes: 17 additions & 0 deletions collector/src/test/java/io/prometheus/jmx/JmxCollectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public static void OneTimeSetUp() throws Exception {

TomcatServlet.registerBean(mbs);
Bool.registerBean(mbs);
Camel.registerBean(mbs);
}

@Before
Expand Down Expand Up @@ -252,4 +253,20 @@ public void testDelayedStartReady() throws Exception {
Thread.sleep(2000);
assertEquals(1.0, registry.getSampleValue("boolean_Test_True", new String[]{}, new String[]{}), .001);
}

@Test
public void testCamelLastExchangFailureTimestamp() throws Exception{
String rulePattern =
"\n---\nrules:\n- pattern: 'org.apache.camel<context=([^,]+), type=routes, name=\"([^\"]+)\"><>LastExchangeFailureTimestamp'\n" +
" name: org.apache.camel.LastExchangeFailureTimestamp\n" +
" help: Exchanges Last Failure Timestamps\n" +
" type: UNTYPED\n" +
" labels:\n" +
" context: \"$1\"\n" +
" route: \"$2\"\n" +
" type: routes";
JmxCollector jc = new JmxCollector(rulePattern).register(registry);
Double actual = registry.getSampleValue("org_apache_camel_LastExchangeFailureTimestamp", new String[]{"context", "route", "type"}, new String[]{"my-camel-context", "my-route-name", "routes"});
assertEquals(Camel.EXPECTED_SECONDS, actual, 0);
}
}

0 comments on commit 189386b

Please sign in to comment.