Monitoring

The Java driver uses Yammer (and JMX) to allow for some monitoring using the Metrics class.

Description

The following links are for your reference:

The Metrics object exposes the following metrics:

Metrics
Metric Description
connectedToHosts A gauge that presents the number of hosts that are currently connected to (at least once).
errorMetrics An Error.Metrics object which groups errors which have occurred.
knownHosts A gauge that presents the number of nodes known by the driver, regardless of whether they are currently up or down).
openConnections A gauge that presents the total number of connections to nodes.
requestsTimer A timer that presents metrics on requests executed by the user.

In addition, you can register your own Yammer metric objects by extending the com.codahale.metrics.Metric interface and registering it with the metric registry. Metrics are enabled by default, but you can also disable them.

Examples

You retrieve the metrics object from your cluster.

public void printMetrics() {
   System.out.println("Metrics");
   Metrics metrics = getSession().getCluster().getMetrics();
   Gauge<Integer> gauge = metrics.getConnectedToHosts();
   Integer numberOfHosts = gauge.value();
   System.out.printf("Number of hosts: %d\n", numberOfHosts);
   Metrics.Errors errors = metrics.getErrorMetrics();
   Counter counter = errors.getReadTimeouts();
   System.out.printf("Number of read timeouts: %d\n", counter.count());
   Timer timer = metrics.getRequestsTimer();
   System.out.printf("Number of user requests: %d %s\n", timer.count(), timer.eventType());
}

You can create and register your own Yammer metric objects:

public void createNumberInsertsGauge() {
   Metric ourMetric = getSession()
     .getCluster()
     .getMetrics()
     .getRegistry()
     .allMetrics()
     .get(new MetricName(getClass(), "com.example.cassandra.numberInserts"));
   System.out.printf("Number of insert statements executed: %5d\n", ((Gauge<?>) ourMetric).value());
   }

Yammer exposes its metrics objects as JMX managed beans (MBeans). You can gather metrics from your client application from another application for reporting purposes. This example presumes that a user-defined metric has been registered by the client as in the previous example.

public void connectMBeanServer() {
   try {
      JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://:9999/jmxrmi");
      JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
      MBeanServerConnection mbsConnection = jmxc.getMBeanServerConnection();
      ObjectName objectName = new ObjectName("\"com.example.cassandra\":type=\"MetricsExample\"," +
            "name=\"com.example.cassandra.numberInserts\"");
      JmxReporter.GaugeMBean mBean = JMX.newMBeanProxy(mbsConnection, objectName, JmxReporter.GaugeMBean.class);
      System.out.printf("Number of inserts: %5d\n",  mBean.getValue());
   } catch (MalformedURLException mue) {
      mue.printStackTrace();
   } catch (IOException ioe) {
      ioe.printStackTrace();
   } catch (NullPointerException npe) {
      npe.printStackTrace();
   }
}

When running the previous example, you must pass the following properties to the JVM running your client application:

-Dcom.sun.management.jmxremote.port=9999 \
-Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.ssl=false