本文介绍了如何在springbootapplication中启用Cassandra CqlSession指标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想启用cassandra cqlsession指标.当尝试注册cqlsession指标时,它在springboot应用程序中提供了optional.empty().这里正在使用cassandra datastax Java驱动程序4.6.

I want to enable cassandra cqlsession metrics. when trying to register the cqlsession metrics it provides optional.empty() in springboot application. Here am using cassandra datastax java driver 4.6.

这是我的代码:

@Autowired
private CqlSession cqlsession;

MetricRegistry metricRegistry = cqlsession.getMetrics()
            .orElseThrow(() -> new IllegalArgumentException("not able to get metrics"))
            .getRegistry();

引发IllegalArgumentException错误.

Throwing IllegalArgumentException Error.

当引用cassandra datastax的官方文档时( https://docs.datastax.com/zh-CN/developer/java-driver/4.6/manual/core/metrics/#configuration ).添加到项目中的同一组conf文件不能解决问题

when referring the official docs for cassandra datastax (https://docs.datastax.com/en/developer/java-driver/4.6/manual/core/metrics/#configuration). the same set of conf files added to the project not resoles the problem

推荐答案

我已经通过以下方法解决了这个问题:

I've solved this with following approach:

配置类

import static com.datastax.oss.driver.api.core.config.DefaultDriverOption.METRICS_NODE_ENABLED;
import static com.datastax.oss.driver.api.core.config.DefaultDriverOption.METRICS_SESSION_ENABLED;

import org.springframework.boot.autoconfigure.cassandra.DriverConfigLoaderBuilderCustomizer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
@EnableConfigurationProperties(CassandraProperties.class)
public class CassandraMetricsConfig {

    @Bean
    DriverConfigLoaderBuilderCustomizer configLoaderBuilderCustomizer(CassandraProperties cassandraProperties) {
        return builder -> {
            builder.withStringList(METRICS_SESSION_ENABLED, cassandraProperties.getSessionMetrics());
            builder.withStringList(METRICS_NODE_ENABLED, cassandraProperties.getNodeMetrics());
        };
    }
}

属性类

@ConfigurationProperties(prefix = "cassandra")
@Data
public class CassandraProperties {

    @NotNull
    private List<String> sessionMetrics = new ArrayList<>();

    @NotNull
    private List<String> nodeMetrics = new ArrayList<>();

}

application.yml

application.yml

cassandra:
  session-metrics:
    - bytes-sent
    - connected-nodes
    ...
  node-metrics:
    - pool.open-connections
    - pool.in-flight
    ...

请注意,此方法仅适用于不需要额外配置(例如cql-requests)的指标.如果要监视cql请求,则必须扩展示例以配置必需的属性.

Note this approach works only for metrics which do not require an additional configuration like cql-requests. If you want to monitor the cql-requests you have to extend the example to configure the required properties.

这篇关于如何在springbootapplication中启用Cassandra CqlSession指标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 07:36