1

I would like to set up the java driver to collect JMX metrics using jConsole or jmxterm. How do I go about exposing those mbean metrics in the Cassandra java driver? In this case, I'm using the 4.14 java driver.

stevenlacerda
  • 341
  • 1
  • 4

1 Answers1

1

Here's a good link that helped me with the 4.14 driver:

https://docs.datastax.com/en/developer/java-driver/4.14/manual/core/metrics/

Ultimately, the JMX metrics look like the following in jConsole:

enter image description here

And here's my application code:

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example.cassandra</groupId>
  <artifactId>testing-connection</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>testing-connection</name>
  <properties>
      <java.version>1.8</java.version>
      <driver.version>4.14.1</driver.version>
      <maven.compiler.source>1.8</maven.compiler.source>
      <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
  <dependencies>

      <dependency>
          <groupId>com.datastax.oss</groupId>
          <artifactId>java-driver-core</artifactId>
          <version>${driver.version}</version>
      </dependency>
      <dependency>
          <groupId>io.dropwizard.metrics</groupId>
          <artifactId>metrics-jmx</artifactId>
          <version>4.1.2</version>
      </dependency>
  </dependencies>
</project>

application.conf:

datastax-java-driver
{
    basic {
        contact-points = [ "10.101.36.152:9042" ]
        load-balancing-policy {
            local-datacenter = "SearchGraphAnalytics"
        }
    }
    advanced.metrics {
      session.enabled = [ bytes-sent,bytes-received,connected-nodes,cql-requests,cql-client-timeouts,cql-prepared-cache-size,throttling.delay,throttling.queue-size,throttling.errors,continuous-cql-requests,graph-requests,graph-client-timeouts ]
      node.enabled = [ pool.open-connections,pool.available-streams,pool.in-flight,pool.orphaned-streams,bytes-sent,bytes-received,cql-messages,errors.request.unsent,errors.request.aborted,errors.request.write-timeouts,errors.request.read-timeouts,errors.request.unavailables,errors.request.others,retries.total,retries.aborted,retries.read-timeout,retries.write-timeout,retries.unavailable,retries.other,ignores.total,ignores.aborted,ignores.read-timeout,ignores.write-timeout,ignores.unavailable,ignores.other,speculative-executions,errors.connection.init,errors.connection.auth,graph-messages ]
    }
}

Main class

package com.example.cassandra;

import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.CqlSessionBuilder;
import com.datastax.oss.driver.api.core.metadata.Metadata;
import com.datastax.oss.driver.api.core.cql.ResultSet;
import java.util.ArrayList;

import com.codahale.metrics.jmx.JmxReporter;
import com.codahale.metrics.MetricRegistry;

public class TestingConnections {
    private CqlSession session;

    static String keyspace = "keyspace1";
    static String table = "names";

    public void connect() {
        CqlSessionBuilder builder = CqlSession.builder();
        session = builder.build();
        Metadata metadata = session.getMetadata();
        System.out.printf("Connected to cluster: %s\n", metadata.getClusterName());
    }

    public CqlSession getSession() {
        return this.session;
    }

    public void getData(String keyspace, String table) {
        ResultSet results = session.execute("select * from " + keyspace + "." + table);
        ArrayList<String> first_names = new ArrayList<String>();
        results.forEach(row -> first_names.add(row.getString("first")));
        first_names.forEach(first_name -> System.out.println(first_name));
    }


    public void close() {
            session.close();
    }

    public void registerJMX() {
        MetricRegistry registry = session.getMetrics()
                .orElseThrow(() -> new IllegalStateException("Metrics are disabled"))
                .getRegistry();

        JmxReporter reporter =
                JmxReporter.forRegistry(registry)
                        .inDomain("com.datastax.oss.driver")
                        .build();
        reporter.start();
    }

    public static void main(String[] args) {
            System.out.printf("Connecting to client");
            TestingConnections client = new TestingConnections();
            client.connect();
            client.registerJMX();
            client.getData(keyspace, table);
    }
}

That did work to generate the metrics as shown in the first screenshot. I haven't tried with metric packages other than the dropwizard (which is the default).

Also note, I am not calling client.close() because I want my session to stay open so that I can connect jConsole to my java application.

stevenlacerda
  • 341
  • 1
  • 4