简体   繁体   中英

How to enable Cassandra CqlSession Metrics in springbootapplication

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.

Here is my code:

@Autowired
private CqlSession cqlsession;

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

Throwing IllegalArgumentException Error.

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:

The configuration 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

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

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

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

}

application.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. If you want to monitor the cql-requests you have to extend the example to configure the required properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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