Add Custom Metrics to Java Apps by Using Third-Party Tools
If you develop a Java application in the Neo environment, you can make use of some default metrics such as memory and CPU usage, busy threads, and requests per minute. If they are not enough, there is the option to add custom ones based on JMX MBeans. For more information about the process, see JMX Checks.
All the available MBeans can be accessed via the JMX Console. Log on to SAP BTP cockpit, open your region in the Neo Environment, and navigate to Global Account -> Subaccount -> Java applications -> your Java application -> JMX Console. You can directly check MBeans’ attribute values, or even invoke their operations and check the outcome. Moreover, these are all MBeans which might be used for creating custom metrics: some default ones coming from Tomcat or the JVM itself as well as all MBeans registered by you (if any).
Although quite straightforward indeed, implementing MBeans might still be tedious. In this blog post, I will describe how to make use of a third-party tools for introducing a variety of different metrics to your application. The tool being used in the example is Dropwizard Metrics. However, the same approach applies for others as well.
In short, a metrics API is used inside the application conveniently. MBeans are automatically generated and registered, and thus the only step left is to register the custom metrics into SAP Monitoring service out of them. Support for custom thresholds and alerting is coming out of the box, no question.
So first, let’s add the dependency to Dropwizard Metrics in our Java app. When the project is built by Maven, add these Maven dependencies:
<dependency> <groupId>io.dropwizard.metrics</groupId> <artifactId>metrics-core</artifactId> <version>4.0.2</version> </dependency> <dependency> <groupId>io.dropwizard.metrics</groupId> <artifactId>metrics-jmx</artifactId> <version>4.0.2</version> </dependency>
Alternatively, these two JARs might be added under WEB-INF/lib:
WEB-INF/lib |- metrics-core-4.0.2 |- metrics-jmx-4.0.2
Now, let’s start using the API inside our Java application. You need to create MetricRegistry, and then the needed metrics are registered programmatically.
MetricRegistry metricRegistry = new MetricRegistry(); // metrics Meter requests = metricRegistry.meter("requests"); Counter errors = metricRegistry.counter("errors"); Histogram responseTimes = metricRegistry.histogram("responseTimes"); Timer timers = metricRegistry.timer("timers");
To report the metrics via JMX, you need to trigger JMXReporter:
JmxReporter jmxReporter = JmxReporter.forRegistry(metricRegistry).build(); jmxReporter.start();
And that’s all. To update metric values, call the methods according to the API, e.g. errors.inc(), etc. For more details, see Getting Started.
Hence, MBeans are automatically created and made visible in the JMX Console.
And beyond the simplicity, getting so much data out of the box is the other benefit of using such a specialized tool. For example, in case of a timer, you directly get more than 15 different measures which might be converted to metrics, or at least being observed via the JMX Console!
Note: For the MBeans to get visible in the JMX Console, make sure that the code for creation of the metrics and JMXReporter has been executed at least once, e.g. in case of a servlet, make sure to call it once.
You then create a custom check based on some of these metrics. You can use the create-jmx-check command. Set appropriate warning and critical thresholds
Note: MBeans ObjectNames might be built in different ways. Use the Details view in the JMX Console (the third tab of the picture above) in order to get the proper ObjectName of the MBean. This name is then used in the command line when registering the JMX check.
Metrics created this way are first-level citizens of SAP Monitoring service. They are shown in the SAP BTP cockpit along with all the others, values are kept for 2 months, plus they might be retrieved via the Metrics REST API of SAP Monitoring service. To get notified via email upon any warning or critical thresholds – register an alert recipient, i.e., an email address as described at set-alert-recipients.
Adding custom metrics to Java apps has never been easier. Check it out and bring your observability capabilities on the next level.