Search code examples
javajmxhystrixservo

How to query the servo MBean for Hystrix?


I am able to access the MBeans in JConsole, and they show up like com.netflix.servo > HystrixCommand > countSuccess > (actual commands and their attributes)

I couldn't find any examples on how to query these objects and the values, e.g. the countSuccess, countFailure etc.

The closest I came was ObjectName o = new ObjectName("com.netflix.servo:name=countSuccess,instance=T6JmxStatCommand,type=HystrixCommand"); which is at https://github.com/n0rad/hands-on-hystrix/blob/master/src/test/java/fr/n0rad/hands/on/hystrix/t6/T6JmxStatMain.java but apparently it doesn't work.

The mbeans were registered via this code: HystrixPlugins.getInstance().registerMetricsPublisher(HystrixServoMetricsPublisher.getInstance());


Solution

  • Found out there was nothing wrong with the object name as queried. In fact it is the same one found in the JConsole; full syntax shown here on the right in the screenshot: enter image description here

    Plus, I needed to add a few system properties to the Eclipse run configuration: -Dcom.sun.management.jmxremote.rmi.port=8700 -Dcom.sun.management.jmxremote.port=8600 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false

    Then I can connect using port 8600. In code:

    package com.awgtek.miscpocs.lognfetch.client;
    
    import javax.management.MBeanServerConnection;
    import javax.management.ObjectName;
    import javax.management.remote.JMXConnector;
    import javax.management.remote.JMXConnectorFactory;
    import javax.management.remote.JMXServiceURL;
    
    public class TestJMXConn {
    
        public static void main(String[] args) throws Exception {
          JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:8600/jmxrmi");
          JMXConnector connect = JMXConnectorFactory.connect(url);
          MBeanServerConnection mbsc = connect.getMBeanServerConnection();
         // ObjectName o = new ObjectName("com.netflix.servo:name=countSuccess,type=HystrixCommand,instance=LogAndFetchRestServicePostCommand");
          ObjectName o = new ObjectName("com.netflix.servo:name=countSuccess,type=HystrixCommand,instance=LogAndFetchRestServiceGetCommand");
          Object value = mbsc.getAttribute(o, "value");
          System.out.println("the value: " + value);
          connect.close();
    
    
        }
    
    }