Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support on jmx attributes of type java.util.Date #449

Merged
merged 6 commits into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}