Monitoring JVM Metrics with the Heroku Java Agent

Table of Contents

The Heroku Java memory logging agent is an extremely lightweight Java agent that can be used to send memory usage information to your logs. This article describes how to configure the Heroku Java agent and connect its log output to the Librato add-on to enable monitoring and alerting.

Adding the agent to a Maven app

Use the Maven dependency plugin to download the Heroku agent and copy it into your target/dependency directory by adding this configuration to the plugins section of your pom.xml

These logs can be funneled into an add-on provider to enable alerting and monitoring.

Monitoring with Librato

Librato’s log integration allows you to publish your own custom metrics directly to Librato through your Heroku log stream. This includes the logs from the Heroku Java agent.

Create a new Librato add-on by running this command:

$ heroku addons:create librato

Then open the add-on interface in the Heroku Dashboard or by running:

$ heroku addons:open librato

The default “Heroku Overview” space includes charts for response time, error codes, and more. But it does not include JVM metrics by default.

To view JVM metrics, create a new space. Then add a new chart and choose “Stacked”. Librato will prompt you to choose the metrics to include in the chart. Search for “mem.jvm.heap.used” and “mem.jvm.nonheap.used”. Save your changes, and you’ll see something like this:

Alerting with Librato

To enable alerting, you must upgrade your Librato add-on to a paid plan. For example:

$ heroku addons:upgrade librato:nickel

Then open the Librato dashboard again, and select the option to create a new alert. The interface will prompt you to choose a metric to base the alerting on. Select “mem.jvm.nonheap.used”, which is a common value to be concerned about. Then select a maximum threshold such as 100, which refers to megabytes because the Java agent reports in megabytes. The alert will look like this:

You can configure notification preferences through the Librato interface.