如何在 springboot 应用程序中启用 Cassandra CqlSession Metrics

2024-01-12

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

这是我的代码:

@Autowired
private CqlSession cqlsession;

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

抛出 IllegalArgumentException 错误。

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


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

配置类

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

}

应用程序.yml

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 springboot 应用程序中启用 Cassandra CqlSession Metrics 的相关文章

随机推荐