从spring boot应用中获取cassandra cql指标。

问题描述 投票:0回答:1

我想从springboot cassandra应用程序中捕获数据库查询指标,并暴露给prometheus终端。

已经有了springboot+postgres的实现,并且它可以和r2dbc-proxy一起工作。

在编辑了下面评论的代码后。

    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;
    }

}

}

spring-boot prometheus cassandra-3.0 spring-data-cassandra
1个回答
0
投票

DataStax的Java驱动程序通过Dropwizard Metrics库来暴露度量值。驱动程序版本3.x, 驱动程序版本4.x),可以通过标准的普罗米修斯库,如 io.prometheus.simpleclient_dropwizard 属于 普罗米修斯Java客户端库.

这里有一个 驱动程序4.x版本的例子但只要稍加修改,也可以在3.x版本中使用。主要部分如下。

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

剩下的就是创建session,通过HTTP暴露指标等。

© www.soinside.com 2019 - 2024. All rights reserved.