From 68c58d608900f08233c805e996e7cd145ccba0e4 Mon Sep 17 00:00:00 2001 From: jacky Date: Tue, 12 Nov 2019 22:24:53 +1300 Subject: [PATCH 1/6] added support on jmx attributes of type java.util.Date Signed-off-by: jacky Signed-off-by: jacky --- .../java/io/prometheus/jmx/JmxScraper.java | 11 ++++++--- .../java/io/prometheus/jmx/CamelMBean.java | 23 +++++++++++++++++++ .../io/prometheus/jmx/JmxCollectorTest.java | 16 +++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 collector/src/test/java/io/prometheus/jmx/CamelMBean.java diff --git a/collector/src/main/java/io/prometheus/jmx/JmxScraper.java b/collector/src/main/java/io/prometheus/jmx/JmxScraper.java index ec3adbc0..e4b19af8 100644 --- a/collector/src/main/java/io/prometheus/jmx/JmxScraper.java +++ b/collector/src/main/java/io/prometheus/jmx/JmxScraper.java @@ -190,7 +190,12 @@ 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) { + //output Date as Long (the number of milliseconds since January 1, 1970, 00:00:00 GMT) + if( value instanceof java.util.Date){ + attrType = "java.lang.Long"; + value = ((java.util.Date)value).getTime(); + } logScrape(domain + beanProperties + attrName, value.toString()); this.receiver.recordBean( domain, @@ -259,7 +264,7 @@ private void processBeanValue( // Skip appending 'value' to the name attrNames = attrKeys; name = attrName; - } + } processBeanValue( domain, l2s, @@ -303,7 +308,7 @@ public void recordBean( String attrDescription, Object value) { System.out.println(domain + - beanProperties + + beanProperties + attrKeys + attrName + ": " + value); diff --git a/collector/src/test/java/io/prometheus/jmx/CamelMBean.java b/collector/src/test/java/io/prometheus/jmx/CamelMBean.java new file mode 100644 index 00000000..83af932e --- /dev/null +++ b/collector/src/test/java/io/prometheus/jmx/CamelMBean.java @@ -0,0 +1,23 @@ +package io.prometheus.jmx; + +import javax.management.MBeanServer; +import javax.management.ObjectName; + +public interface CamelMBean { + long 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 long getLastExchangeFailureTimestamp() { + return 1573285945806L; + } +} diff --git a/collector/src/test/java/io/prometheus/jmx/JmxCollectorTest.java b/collector/src/test/java/io/prometheus/jmx/JmxCollectorTest.java index dc35734f..0005d57d 100644 --- a/collector/src/test/java/io/prometheus/jmx/JmxCollectorTest.java +++ b/collector/src/test/java/io/prometheus/jmx/JmxCollectorTest.java @@ -31,6 +31,7 @@ public static void OneTimeSetUp() throws Exception { TomcatServlet.registerBean(mbs); Bool.registerBean(mbs); + Camel.registerBean(mbs); } @Before @@ -252,4 +253,19 @@ 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<>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); + assertEquals(Double.valueOf(1573285945806L), registry.getSampleValue("org_apache_camel_LastExchangeFailureTimestamp", new String[]{"context", "route", "type"}, new String[]{"my-camel-context", "my-route-name", "routes"})); + } } From 8379d4b41e785de919ab8101165fb4688995e95e Mon Sep 17 00:00:00 2001 From: jacky Date: Wed, 13 Nov 2019 08:54:00 +1300 Subject: [PATCH 2/6] output number of seconds instead of milliseconds to align with prometheus conventions Signed-off-by: jacky --- .../src/main/java/io/prometheus/jmx/JmxScraper.java | 6 +++--- .../src/test/java/io/prometheus/jmx/CamelMBean.java | 11 ++++++++--- .../test/java/io/prometheus/jmx/JmxCollectorTest.java | 2 +- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/collector/src/main/java/io/prometheus/jmx/JmxScraper.java b/collector/src/main/java/io/prometheus/jmx/JmxScraper.java index e4b19af8..ab4951c9 100644 --- a/collector/src/main/java/io/prometheus/jmx/JmxScraper.java +++ b/collector/src/main/java/io/prometheus/jmx/JmxScraper.java @@ -191,10 +191,10 @@ private void processBeanValue( if (value == null) { logScrape(domain + beanProperties + attrName, "null"); } else if (value instanceof Number || value instanceof String || value instanceof Boolean || value instanceof java.util.Date) { - //output Date as Long (the number of milliseconds since January 1, 1970, 00:00:00 GMT) - if( value instanceof java.util.Date){ + //output Date as Long (the number of seconds since January 1, 1970, 00:00:00 GMT) + if(value instanceof java.util.Date){ attrType = "java.lang.Long"; - value = ((java.util.Date)value).getTime(); + value = ((java.util.Date)value).getTime() / 1000; } logScrape(domain + beanProperties + attrName, value.toString()); this.receiver.recordBean( diff --git a/collector/src/test/java/io/prometheus/jmx/CamelMBean.java b/collector/src/test/java/io/prometheus/jmx/CamelMBean.java index 83af932e..8c9bf378 100644 --- a/collector/src/test/java/io/prometheus/jmx/CamelMBean.java +++ b/collector/src/test/java/io/prometheus/jmx/CamelMBean.java @@ -2,9 +2,13 @@ import javax.management.MBeanServer; import javax.management.ObjectName; +import java.util.Calendar; +import java.util.Date; +import java.util.Random; public interface CamelMBean { - long getLastExchangeFailureTimestamp(); + long EXPECTED_DATE_IN_SECONDS = 1573285945L; + Date getLastExchangeFailureTimestamp(); } class Camel implements CamelMBean{ @@ -17,7 +21,8 @@ public static void registerBean(MBeanServer mbs) } @Override - public long getLastExchangeFailureTimestamp() { - return 1573285945806L; + public Date getLastExchangeFailureTimestamp() { + int theMillisecondAtTheMoment = Calendar.getInstance().get(Calendar.MILLISECOND); + return new Date(EXPECTED_DATE_IN_SECONDS * 1000 + theMillisecondAtTheMoment); } } diff --git a/collector/src/test/java/io/prometheus/jmx/JmxCollectorTest.java b/collector/src/test/java/io/prometheus/jmx/JmxCollectorTest.java index 0005d57d..3b655c60 100644 --- a/collector/src/test/java/io/prometheus/jmx/JmxCollectorTest.java +++ b/collector/src/test/java/io/prometheus/jmx/JmxCollectorTest.java @@ -266,6 +266,6 @@ public void testCamelLastExchangFailureTimestamp() throws Exception{ " route: \"$2\"\n" + " type: routes"; JmxCollector jc = new JmxCollector(rulePattern).register(registry); - assertEquals(Double.valueOf(1573285945806L), registry.getSampleValue("org_apache_camel_LastExchangeFailureTimestamp", new String[]{"context", "route", "type"}, new String[]{"my-camel-context", "my-route-name", "routes"})); + assertEquals(Double.valueOf(Camel.EXPECTED_DATE_IN_SECONDS), registry.getSampleValue("org_apache_camel_LastExchangeFailureTimestamp", new String[]{"context", "route", "type"}, new String[]{"my-camel-context", "my-route-name", "routes"})); } } From 7a78acab7a047c59214fe0354ce5f0cc22cca50d Mon Sep 17 00:00:00 2001 From: jacky Date: Thu, 14 Nov 2019 08:54:51 +1300 Subject: [PATCH 3/6] output as Double on java.util.Date attributes, decimal portion represents milliseconds Signed-off-by: jacky --- .../java/io/prometheus/jmx/JmxScraper.java | 131 +++++++++--------- .../java/io/prometheus/jmx/CamelMBean.java | 7 +- .../io/prometheus/jmx/JmxCollectorTest.java | 2 +- 3 files changed, 68 insertions(+), 72 deletions(-) diff --git a/collector/src/main/java/io/prometheus/jmx/JmxScraper.java b/collector/src/main/java/io/prometheus/jmx/JmxScraper.java index ab4951c9..e1dd18f7 100644 --- a/collector/src/main/java/io/prometheus/jmx/JmxScraper.java +++ b/collector/src/main/java/io/prometheus/jmx/JmxScraper.java @@ -38,13 +38,13 @@ class JmxScraper { public static interface MBeanReceiver { void recordBean( - String domain, - LinkedHashMap beanProperties, - LinkedList attrKeys, - String attrName, - String attrType, - String attrDescription, - Object value); + String domain, + LinkedHashMap beanProperties, + LinkedList attrKeys, + String attrName, + String attrType, + String attrDescription, + Object value); } private final MBeanReceiver receiver; @@ -69,30 +69,30 @@ public JmxScraper(String jmxUrl, String username, String password, boolean ssl, } /** - * Get a list of mbeans on host_port and scrape their values. - * - * Values are passed to the receiver in a single thread. - */ + * Get a list of mbeans on host_port and scrape their values. + * + * Values are passed to the receiver in a single thread. + */ public void doScrape() throws Exception { MBeanServerConnection beanConn; JMXConnector jmxc = null; if (jmxUrl.isEmpty()) { - beanConn = ManagementFactory.getPlatformMBeanServer(); + beanConn = ManagementFactory.getPlatformMBeanServer(); } else { - Map environment = new HashMap(); - if (username != null && username.length() != 0 && password != null && password.length() != 0) { - String[] credent = new String[] {username, password}; - environment.put(javax.management.remote.JMXConnector.CREDENTIALS, credent); - } - if (ssl) { - environment.put(Context.SECURITY_PROTOCOL, "ssl"); - SslRMIClientSocketFactory clientSocketFactory = new SslRMIClientSocketFactory(); - environment.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, clientSocketFactory); - environment.put("com.sun.jndi.rmi.factory.socket", clientSocketFactory); - } + Map environment = new HashMap(); + if (username != null && username.length() != 0 && password != null && password.length() != 0) { + String[] credent = new String[] {username, password}; + environment.put(javax.management.remote.JMXConnector.CREDENTIALS, credent); + } + if (ssl) { + environment.put(Context.SECURITY_PROTOCOL, "ssl"); + SslRMIClientSocketFactory clientSocketFactory = new SslRMIClientSocketFactory(); + environment.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, clientSocketFactory); + environment.put("com.sun.jndi.rmi.factory.socket", clientSocketFactory); + } - jmxc = JMXConnectorFactory.connect(new JMXServiceURL(jmxUrl), environment); - beanConn = jmxc.getMBeanServerConnection(); + jmxc = JMXConnectorFactory.connect(new JMXServiceURL(jmxUrl), environment); + beanConn = jmxc.getMBeanServerConnection(); } try { // Query MBean names, see #89 for reasons queryMBeans() is used instead of queryNames() @@ -118,22 +118,22 @@ public void doScrape() throws Exception { logger.fine("TIME: " + (System.nanoTime() - start) + " ns for " + objectName.toString()); } } finally { - if (jmxc != null) { - jmxc.close(); - } + if (jmxc != null) { + jmxc.close(); + } } } private void scrapeBean(MBeanServerConnection beanConn, ObjectName mbeanName) { MBeanInfo info; try { - info = beanConn.getMBeanInfo(mbeanName); + info = beanConn.getMBeanInfo(mbeanName); } catch (IOException e) { - logScrape(mbeanName.toString(), "getMBeanInfo Fail: " + e); - return; + logScrape(mbeanName.toString(), "getMBeanInfo Fail: " + e); + return; } catch (JMException e) { - logScrape(mbeanName.toString(), "getMBeanInfo Fail: " + e); - return; + logScrape(mbeanName.toString(), "getMBeanInfo Fail: " + e); + return; } MBeanAttributeInfo[] attrInfos = info.getAttributes(); @@ -191,10 +191,9 @@ private void processBeanValue( if (value == null) { logScrape(domain + beanProperties + attrName, "null"); } else if (value instanceof Number || value instanceof String || value instanceof Boolean || value instanceof java.util.Date) { - //output Date as Long (the number of seconds since January 1, 1970, 00:00:00 GMT) if(value instanceof java.util.Date){ - attrType = "java.lang.Long"; - value = ((java.util.Date)value).getTime() / 1000; + attrType = "java.lang.Double"; + value = (double) ((java.util.Date) value).getTime() / 1000; } logScrape(domain + beanProperties + attrName, value.toString()); this.receiver.recordBean( @@ -251,7 +250,7 @@ private void processBeanValue( // Nested tabulardata will repeat the 'key' label, so // append a suffix to distinguish each. while (l2s.containsKey(idx)) { - idx = idx + "_"; + idx = idx + "_"; } l2s.put(idx, obj.toString()); } @@ -266,13 +265,13 @@ private void processBeanValue( name = attrName; } processBeanValue( - domain, - l2s, - attrNames, - name, - typ, - type.getDescription(), - composite.get(valueIdx)); + domain, + l2s, + attrNames, + name, + typ, + type.getDescription(), + composite.get(valueIdx)); } } else { logScrape(domain, "not a correct tabulardata format"); @@ -300,18 +299,18 @@ private static void logScrape(String name, String msg) { private static class StdoutWriter implements MBeanReceiver { public void recordBean( - String domain, - LinkedHashMap beanProperties, - LinkedList attrKeys, - String attrName, - String attrType, - String attrDescription, - Object value) { + String domain, + LinkedHashMap beanProperties, + LinkedList attrKeys, + String attrName, + String attrType, + String attrDescription, + Object value) { System.out.println(domain + - beanProperties + - attrKeys + - attrName + - ": " + value); + beanProperties + + attrKeys + + attrName + + ": " + value); } } @@ -319,20 +318,20 @@ public void recordBean( * Convenience function to run standalone. */ public static void main(String[] args) throws Exception { - List objectNames = new LinkedList(); - objectNames.add(null); - if (args.length >= 3){ + List objectNames = new LinkedList(); + objectNames.add(null); + if (args.length >= 3){ new JmxScraper(args[0], args[1], args[2], false, objectNames, new LinkedList(), new StdoutWriter(), new JmxMBeanPropertyCache()).doScrape(); } - else if (args.length > 0){ - new JmxScraper(args[0], "", "", false, objectNames, new LinkedList(), - new StdoutWriter(), new JmxMBeanPropertyCache()).doScrape(); - } - else { - new JmxScraper("", "", "", false, objectNames, new LinkedList(), - new StdoutWriter(), new JmxMBeanPropertyCache()).doScrape(); - } + else if (args.length > 0){ + new JmxScraper(args[0], "", "", false, objectNames, new LinkedList(), + new StdoutWriter(), new JmxMBeanPropertyCache()).doScrape(); + } + else { + new JmxScraper("", "", "", false, objectNames, new LinkedList(), + new StdoutWriter(), new JmxMBeanPropertyCache()).doScrape(); + } } } diff --git a/collector/src/test/java/io/prometheus/jmx/CamelMBean.java b/collector/src/test/java/io/prometheus/jmx/CamelMBean.java index 8c9bf378..fd4c1ddd 100644 --- a/collector/src/test/java/io/prometheus/jmx/CamelMBean.java +++ b/collector/src/test/java/io/prometheus/jmx/CamelMBean.java @@ -2,12 +2,10 @@ import javax.management.MBeanServer; import javax.management.ObjectName; -import java.util.Calendar; import java.util.Date; -import java.util.Random; public interface CamelMBean { - long EXPECTED_DATE_IN_SECONDS = 1573285945L; + long EXPECTED_DATE_IN_MILLISECONDS = 1573285945111L; Date getLastExchangeFailureTimestamp(); } @@ -22,7 +20,6 @@ public static void registerBean(MBeanServer mbs) @Override public Date getLastExchangeFailureTimestamp() { - int theMillisecondAtTheMoment = Calendar.getInstance().get(Calendar.MILLISECOND); - return new Date(EXPECTED_DATE_IN_SECONDS * 1000 + theMillisecondAtTheMoment); + return new Date(EXPECTED_DATE_IN_MILLISECONDS); } } diff --git a/collector/src/test/java/io/prometheus/jmx/JmxCollectorTest.java b/collector/src/test/java/io/prometheus/jmx/JmxCollectorTest.java index 3b655c60..9054d0fd 100644 --- a/collector/src/test/java/io/prometheus/jmx/JmxCollectorTest.java +++ b/collector/src/test/java/io/prometheus/jmx/JmxCollectorTest.java @@ -266,6 +266,6 @@ public void testCamelLastExchangFailureTimestamp() throws Exception{ " route: \"$2\"\n" + " type: routes"; JmxCollector jc = new JmxCollector(rulePattern).register(registry); - assertEquals(Double.valueOf(Camel.EXPECTED_DATE_IN_SECONDS), registry.getSampleValue("org_apache_camel_LastExchangeFailureTimestamp", new String[]{"context", "route", "type"}, new String[]{"my-camel-context", "my-route-name", "routes"})); + assertEquals(Double.valueOf((double)Camel.EXPECTED_DATE_IN_MILLISECONDS / 1000), registry.getSampleValue("org_apache_camel_LastExchangeFailureTimestamp", new String[]{"context", "route", "type"}, new String[]{"my-camel-context", "my-route-name", "routes"})); } } From ecb43f7d7700d2142aa081e90e17c57fc385dedf Mon Sep 17 00:00:00 2001 From: jacky Date: Thu, 14 Nov 2019 09:07:50 +1300 Subject: [PATCH 4/6] fixed formatting Signed-off-by: jacky --- .../java/io/prometheus/jmx/JmxScraper.java | 126 +++++++++--------- .../java/io/prometheus/jmx/CamelMBean.java | 29 ++-- .../io/prometheus/jmx/JmxCollectorTest.java | 22 +-- 3 files changed, 89 insertions(+), 88 deletions(-) diff --git a/collector/src/main/java/io/prometheus/jmx/JmxScraper.java b/collector/src/main/java/io/prometheus/jmx/JmxScraper.java index e1dd18f7..f402c2f1 100644 --- a/collector/src/main/java/io/prometheus/jmx/JmxScraper.java +++ b/collector/src/main/java/io/prometheus/jmx/JmxScraper.java @@ -38,13 +38,13 @@ class JmxScraper { public static interface MBeanReceiver { void recordBean( - String domain, - LinkedHashMap beanProperties, - LinkedList attrKeys, - String attrName, - String attrType, - String attrDescription, - Object value); + String domain, + LinkedHashMap beanProperties, + LinkedList attrKeys, + String attrName, + String attrType, + String attrDescription, + Object value); } private final MBeanReceiver receiver; @@ -69,30 +69,30 @@ public JmxScraper(String jmxUrl, String username, String password, boolean ssl, } /** - * Get a list of mbeans on host_port and scrape their values. - * - * Values are passed to the receiver in a single thread. - */ + * Get a list of mbeans on host_port and scrape their values. + * + * Values are passed to the receiver in a single thread. + */ public void doScrape() throws Exception { MBeanServerConnection beanConn; JMXConnector jmxc = null; if (jmxUrl.isEmpty()) { - beanConn = ManagementFactory.getPlatformMBeanServer(); + beanConn = ManagementFactory.getPlatformMBeanServer(); } else { - Map environment = new HashMap(); - if (username != null && username.length() != 0 && password != null && password.length() != 0) { - String[] credent = new String[] {username, password}; - environment.put(javax.management.remote.JMXConnector.CREDENTIALS, credent); - } - if (ssl) { - environment.put(Context.SECURITY_PROTOCOL, "ssl"); - SslRMIClientSocketFactory clientSocketFactory = new SslRMIClientSocketFactory(); - environment.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, clientSocketFactory); - environment.put("com.sun.jndi.rmi.factory.socket", clientSocketFactory); - } + Map environment = new HashMap(); + if (username != null && username.length() != 0 && password != null && password.length() != 0) { + String[] credent = new String[] {username, password}; + environment.put(javax.management.remote.JMXConnector.CREDENTIALS, credent); + } + if (ssl) { + environment.put(Context.SECURITY_PROTOCOL, "ssl"); + SslRMIClientSocketFactory clientSocketFactory = new SslRMIClientSocketFactory(); + environment.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, clientSocketFactory); + environment.put("com.sun.jndi.rmi.factory.socket", clientSocketFactory); + } - jmxc = JMXConnectorFactory.connect(new JMXServiceURL(jmxUrl), environment); - beanConn = jmxc.getMBeanServerConnection(); + jmxc = JMXConnectorFactory.connect(new JMXServiceURL(jmxUrl), environment); + beanConn = jmxc.getMBeanServerConnection(); } try { // Query MBean names, see #89 for reasons queryMBeans() is used instead of queryNames() @@ -118,22 +118,22 @@ public void doScrape() throws Exception { logger.fine("TIME: " + (System.nanoTime() - start) + " ns for " + objectName.toString()); } } finally { - if (jmxc != null) { - jmxc.close(); - } + if (jmxc != null) { + jmxc.close(); + } } } private void scrapeBean(MBeanServerConnection beanConn, ObjectName mbeanName) { MBeanInfo info; try { - info = beanConn.getMBeanInfo(mbeanName); + info = beanConn.getMBeanInfo(mbeanName); } catch (IOException e) { - logScrape(mbeanName.toString(), "getMBeanInfo Fail: " + e); - return; + logScrape(mbeanName.toString(), "getMBeanInfo Fail: " + e); + return; } catch (JMException e) { - logScrape(mbeanName.toString(), "getMBeanInfo Fail: " + e); - return; + logScrape(mbeanName.toString(), "getMBeanInfo Fail: " + e); + return; } MBeanAttributeInfo[] attrInfos = info.getAttributes(); @@ -250,7 +250,7 @@ private void processBeanValue( // Nested tabulardata will repeat the 'key' label, so // append a suffix to distinguish each. while (l2s.containsKey(idx)) { - idx = idx + "_"; + idx = idx + "_"; } l2s.put(idx, obj.toString()); } @@ -265,13 +265,13 @@ private void processBeanValue( name = attrName; } processBeanValue( - domain, - l2s, - attrNames, - name, - typ, - type.getDescription(), - composite.get(valueIdx)); + domain, + l2s, + attrNames, + name, + typ, + type.getDescription(), + composite.get(valueIdx)); } } else { logScrape(domain, "not a correct tabulardata format"); @@ -299,18 +299,18 @@ private static void logScrape(String name, String msg) { private static class StdoutWriter implements MBeanReceiver { public void recordBean( - String domain, - LinkedHashMap beanProperties, - LinkedList attrKeys, - String attrName, - String attrType, - String attrDescription, - Object value) { + String domain, + LinkedHashMap beanProperties, + LinkedList attrKeys, + String attrName, + String attrType, + String attrDescription, + Object value) { System.out.println(domain + - beanProperties + - attrKeys + - attrName + - ": " + value); + beanProperties + + attrKeys + + attrName + + ": " + value); } } @@ -318,20 +318,20 @@ public void recordBean( * Convenience function to run standalone. */ public static void main(String[] args) throws Exception { - List objectNames = new LinkedList(); - objectNames.add(null); - if (args.length >= 3){ + List objectNames = new LinkedList(); + objectNames.add(null); + if (args.length >= 3){ new JmxScraper(args[0], args[1], args[2], false, objectNames, new LinkedList(), new StdoutWriter(), new JmxMBeanPropertyCache()).doScrape(); } - else if (args.length > 0){ - new JmxScraper(args[0], "", "", false, objectNames, new LinkedList(), - new StdoutWriter(), new JmxMBeanPropertyCache()).doScrape(); - } - else { - new JmxScraper("", "", "", false, objectNames, new LinkedList(), - new StdoutWriter(), new JmxMBeanPropertyCache()).doScrape(); - } + else if (args.length > 0){ + new JmxScraper(args[0], "", "", false, objectNames, new LinkedList(), + new StdoutWriter(), new JmxMBeanPropertyCache()).doScrape(); + } + else { + new JmxScraper("", "", "", false, objectNames, new LinkedList(), + new StdoutWriter(), new JmxMBeanPropertyCache()).doScrape(); + } } } diff --git a/collector/src/test/java/io/prometheus/jmx/CamelMBean.java b/collector/src/test/java/io/prometheus/jmx/CamelMBean.java index fd4c1ddd..1a9e62d3 100644 --- a/collector/src/test/java/io/prometheus/jmx/CamelMBean.java +++ b/collector/src/test/java/io/prometheus/jmx/CamelMBean.java @@ -5,21 +5,22 @@ import java.util.Date; public interface CamelMBean { - long EXPECTED_DATE_IN_MILLISECONDS = 1573285945111L; - Date getLastExchangeFailureTimestamp(); + long EXPECTED_DATE_IN_MILLISECONDS = 1573285945111L; + + 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); - } +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(EXPECTED_DATE_IN_MILLISECONDS); - } + @Override + public Date getLastExchangeFailureTimestamp() { + return new Date(EXPECTED_DATE_IN_MILLISECONDS); + } } diff --git a/collector/src/test/java/io/prometheus/jmx/JmxCollectorTest.java b/collector/src/test/java/io/prometheus/jmx/JmxCollectorTest.java index 9054d0fd..e434d1e5 100644 --- a/collector/src/test/java/io/prometheus/jmx/JmxCollectorTest.java +++ b/collector/src/test/java/io/prometheus/jmx/JmxCollectorTest.java @@ -256,16 +256,16 @@ public void testDelayedStartReady() throws Exception { @Test public void testCamelLastExchangFailureTimestamp() throws Exception{ - String rulePattern = - "\n---\nrules:\n- pattern: 'org.apache.camel<>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); - assertEquals(Double.valueOf((double)Camel.EXPECTED_DATE_IN_MILLISECONDS / 1000), registry.getSampleValue("org_apache_camel_LastExchangeFailureTimestamp", new String[]{"context", "route", "type"}, new String[]{"my-camel-context", "my-route-name", "routes"})); + String rulePattern = + "\n---\nrules:\n- pattern: 'org.apache.camel<>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); + assertEquals(Double.valueOf((double)Camel.EXPECTED_DATE_IN_MILLISECONDS / 1000), registry.getSampleValue("org_apache_camel_LastExchangeFailureTimestamp", new String[]{"context", "route", "type"}, new String[]{"my-camel-context", "my-route-name", "routes"})); } } From 3cc8b0fe7ecf80e4a278525db4f4d40839fd4e94 Mon Sep 17 00:00:00 2001 From: jacky Date: Thu, 14 Nov 2019 09:14:22 +1300 Subject: [PATCH 5/6] fixed white spaces Signed-off-by: jacky --- collector/src/main/java/io/prometheus/jmx/JmxScraper.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/collector/src/main/java/io/prometheus/jmx/JmxScraper.java b/collector/src/main/java/io/prometheus/jmx/JmxScraper.java index f402c2f1..f2f37349 100644 --- a/collector/src/main/java/io/prometheus/jmx/JmxScraper.java +++ b/collector/src/main/java/io/prometheus/jmx/JmxScraper.java @@ -263,7 +263,7 @@ private void processBeanValue( // Skip appending 'value' to the name attrNames = attrKeys; name = attrName; - } + } processBeanValue( domain, l2s, @@ -307,7 +307,7 @@ public void recordBean( String attrDescription, Object value) { System.out.println(domain + - beanProperties + + beanProperties + attrKeys + attrName + ": " + value); From be117b7c33659962bfb4401343e1335962c8205b Mon Sep 17 00:00:00 2001 From: jacky Date: Thu, 14 Nov 2019 23:51:19 +1300 Subject: [PATCH 6/6] update formatting and test Signed-off-by: jacky --- collector/src/main/java/io/prometheus/jmx/JmxScraper.java | 4 ++-- collector/src/test/java/io/prometheus/jmx/CamelMBean.java | 4 ++-- .../src/test/java/io/prometheus/jmx/JmxCollectorTest.java | 3 ++- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/collector/src/main/java/io/prometheus/jmx/JmxScraper.java b/collector/src/main/java/io/prometheus/jmx/JmxScraper.java index f2f37349..89451356 100644 --- a/collector/src/main/java/io/prometheus/jmx/JmxScraper.java +++ b/collector/src/main/java/io/prometheus/jmx/JmxScraper.java @@ -191,9 +191,9 @@ private void processBeanValue( if (value == null) { logScrape(domain + beanProperties + attrName, "null"); } else if (value instanceof Number || value instanceof String || value instanceof Boolean || value instanceof java.util.Date) { - if(value instanceof java.util.Date){ + if (value instanceof java.util.Date) { attrType = "java.lang.Double"; - value = (double) ((java.util.Date) value).getTime() / 1000; + value = ((java.util.Date) value).getTime() / 1000.0; } logScrape(domain + beanProperties + attrName, value.toString()); this.receiver.recordBean( diff --git a/collector/src/test/java/io/prometheus/jmx/CamelMBean.java b/collector/src/test/java/io/prometheus/jmx/CamelMBean.java index 1a9e62d3..103b5e51 100644 --- a/collector/src/test/java/io/prometheus/jmx/CamelMBean.java +++ b/collector/src/test/java/io/prometheus/jmx/CamelMBean.java @@ -5,7 +5,7 @@ import java.util.Date; public interface CamelMBean { - long EXPECTED_DATE_IN_MILLISECONDS = 1573285945111L; + double EXPECTED_SECONDS = 1.573285945111E9; Date getLastExchangeFailureTimestamp(); } @@ -21,6 +21,6 @@ public static void registerBean(MBeanServer mbs) @Override public Date getLastExchangeFailureTimestamp() { - return new Date(EXPECTED_DATE_IN_MILLISECONDS); + return new Date((long)(EXPECTED_SECONDS * 1000)); } } diff --git a/collector/src/test/java/io/prometheus/jmx/JmxCollectorTest.java b/collector/src/test/java/io/prometheus/jmx/JmxCollectorTest.java index e434d1e5..05334e9a 100644 --- a/collector/src/test/java/io/prometheus/jmx/JmxCollectorTest.java +++ b/collector/src/test/java/io/prometheus/jmx/JmxCollectorTest.java @@ -266,6 +266,7 @@ public void testCamelLastExchangFailureTimestamp() throws Exception{ " route: \"$2\"\n" + " type: routes"; JmxCollector jc = new JmxCollector(rulePattern).register(registry); - assertEquals(Double.valueOf((double)Camel.EXPECTED_DATE_IN_MILLISECONDS / 1000), registry.getSampleValue("org_apache_camel_LastExchangeFailureTimestamp", new String[]{"context", "route", "type"}, new String[]{"my-camel-context", "my-route-name", "routes"})); + 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); } }