简体   繁体   English

如何在springbootapplication中启用Cassandra CqlSession Metrics

[英]How to enable Cassandra CqlSession Metrics in springbootapplication

I want to enable cassandra cqlsession metrics.我想启用 cassandra cqlsession 指标。 when trying to register the cqlsession metrics it provides optional.empty() in springboot application.当尝试注册 cqlsession 指标时,它在 springboot 应用程序中提供 optional.empty()。 Here am using cassandra datastax java driver 4.6.这里我使用 cassandra datastax java 驱动程序 4.6。

Here is my code:这是我的代码:

@Autowired
private CqlSession cqlsession;

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

Throwing IllegalArgumentException Error.引发 IllegalArgumentException 错误。

when referring the official docs for cassandra datastax ( https://docs.datastax.com/en/developer/java-driver/4.6/manual/core/metrics/#configuration ).当参考 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添加到项目中的同一组 conf 文件不能解决问题

I've solved this with following approach:我用以下方法解决了这个问题:

The configuration class配置class

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

The property class属性 class

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

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

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

}

application.yml应用程序.yml

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

Note this approach works only for metrics which do not require an additional configuration like cql-requests.请注意,此方法仅适用于不需要额外配置(如 cql-requests)的指标。 If you want to monitor the cql-requests you have to extend the example to configure the required properties.如果要监视 cql 请求,则必须扩展示例以配置所需的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM