DEV Community

Hayden Rear
Hayden Rear

Posted on

MBeans

Java MBeans - how do you know which ones to scrape? How do they work? What in the world does this mean?

  - pattern: 'java.lang<type=GarbageCollector, name=(.*)><>CollectionCount'
    name: gc_collection_count
Enter fullscreen mode Exit fullscreen mode

Scraping MBeans

The first thing to keep in mind is that MBeans use remote method invocation to get the metrics that the MBean exposes. One of the quarks of Java MBeans is the way that they are identified. Here is an example ObjectName:

java.lang:name=G1 Old Generation,type=GarbageCollector
Enter fullscreen mode Exit fullscreen mode

And here is an example of some code where you create an MBean, and start to understand how they are indexed for a JMX Exporter with the ObjectName:

import javax.management.*;

public class Main {
    public static void main(String[] args) throws Exception {
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        ObjectName name = new ObjectName("com.example:type=MyMBean,name=MyInstance");
        My mbean = new My();
        mbs.registerMBean(mbean, name);

        System.out.println("MBean registered. Press Enter to exit.");
        System.in.read();
    }
}
Enter fullscreen mode Exit fullscreen mode

And so the question is - how can everything be identified by a string? Does the ordering matter? In fact, after the domain and the type and name, there is a list of key value pairs, and those are the properties.

Properties vs. Attributes

Properties are identifiers for the MBean. In the example, the properties are type and name. You can filter by properties using JMX in the above example. Attributes are the metrics we are exposing. In the example at the start, CollectionCount is the attribute. We can add a regex expression there as well, to expose all of the attributes. So you can also filter by attributes, in that you can only expose some of the attributes of an MBean. However, typically the properties are used to identify and filter the MBeans. You can do some cool stuff with MBeans, such as creating histograms with the different values that the attributes can have, and you do this using JMX Exporter type, which is completely different than the property "type" used to filter.

But where are the attributes?

If you look at an MBean, you will see your standard getters and setters, providing access to the attributes. These getters and setters would then be called with RMI to provide access to the data that is exposed via your favorite JMX exporter.

For the example at the beginning:

Image description

Here the collection count is exposed using the getter, getCollectionCount, and this method is called using RMI (remote method invocation) with JMX.

Finding MBeans to Scrape

The first aha moment, when it comes to Java MBeans, is having a nice user interface to look at them. And, fortunately, if you have Java JDK installed, you have that user interface installed, and it's called jconsole. So if you open up a terminal and type jconsole, and follow the prompts to connect to a Java process, then you'll see something like this:

Image description

Notice the attribute, collectionCount. And so you can see how to provide the previous pattern to access the metrics for the MBeans.

"Types" of MBeans

The last thing to keep in mind when it comes to Java MBeans is that there are two things called types. Welcome to a strongly typed language. In any case, when it comes to JMX exporter and Prometheus, there is not only the type of MBean, but there is also the type of value. Examples include counter and gauge. Counter would handle something like number of garbage collections, while gauge is some continuous value. Additionally, Java has attributeType in the MBeanAttributeInfo, which is a separate type of MBean. Quite confusing, however this attributeType is different from the type of value that Prometheus interprets.

What in the world was that?

The starting snippet is a JMX exporter rule. It matches on the MBean type, and all the various attributes. In JMX, you define these rules to determine which MBeans are exposed to the outside through the JMX exporting.

Top comments (0)