0
votes

I want to capture the db query metrics from springboot cassandra application and expose to prometheus endpoint.

Already have implemenatation for springboot+ postgres and its working with r2dbc-proxy. since r2dbc not providing support for cassandra. looking for any sample implementation.

After edited code for below comment:

    String contactPoint = System.getProperty("contactPoint", "127.0.0.1");
    // init default prometheus stuff
    DefaultExports.initialize();
    // setup Prometheus HTTP server
    Optional<HTTPServer> prometheusServer = Optional.empty();
    try {
        prometheusServer = Optional.of(new HTTPServer(Integer.getInteger("prometheusPort", 9095)));
    } catch (IOException e) {
        System.out.println("Exception when creating HTTP server for Prometheus: " + e.getMessage());
    }
    Cluster cluster = Cluster.builder()
            .addContactPointsWithPorts(new InetSocketAddress(contactPoint, 9042))
            .withoutJMXReporting()
            .build();

    try (Session session = cluster.connect()) {
        MetricRegistry myRegistry = new MetricRegistry();
        myRegistry.registerAll(cluster.getMetrics().getRegistry());
        CollectorRegistry.defaultRegistry.register(new DropwizardExports(myRegistry));

        session.execute("create keyspace if not exists test with replication = {'class': 'SimpleStrategy', 'replication_factor': 1};");
        session.execute("create table if not exists test.abc (id int, t1 text, t2 text, primary key (id, t1));");
        session.execute("truncate test.abc;");

    }
    catch(IllegalStateException ex){
        System.out.println("metric registry fails to configure!!!!!");
        throw ex;
    }

}

}

2

2 Answers

1
votes

If using micrometer

  1. add dependency com.datastax.oss:java-driver-metrics-micrometer
  2. Create CqlSessionBuilderCustomizer and register MeterRegistry (io.micrometer.core.instrument.MeterRegistry) bean using withMetricRegistry method.
  3. Create DriverConfigLoaderBuilderCustomizer with used metrics (https://stackoverflow.com/a/62940370/12584290)
0
votes

DataStax Java driver exposes metrics via Dropwizard Metrics library (driver version 3.x, driver version 4.x) that could be exposed as Prometheus endpoint using via standard Prometheus libraries, like io.prometheus.simpleclient_dropwizard that is part of Prometheus Java client library.

Here is an example for driver version 4.x, but with small modification it could work with 3.x as well. The main part is following:

MetricRegistry registry = session.getMetrics()
        .orElseThrow(() -> new IllegalStateException("Metrics are disabled"))
        .getRegistry();
CollectorRegistry.defaultRegistry.register(new DropwizardExports(registry));

the rest is just creating session, exposing metrics via HTTP, etc.