Why are micrometer metrics not showing up after the registry is added to the Cassandra driver

119 views Asked by At

I am trying to follow the directions here and I have the following

@Configuration
@EnableConfigurationProperties(CassandraProperties.class)
public class CassandraMetricsConfig {
    @Bean
    public DriverConfigLoaderBuilderCustomizer configLoaderBuilderCustomizer(CassandraProperties cassandraProperties) {
        return builder -> {
            System.out.println("Builder is getting called "+cassandraProperties.getSessionMetrics()+" "+cassandraProperties.getNodeMetrics()+"\n");
            builder.withStringList(METRICS_SESSION_ENABLED, cassandraProperties.getSessionMetrics());
            builder.withStringList(METRICS_NODE_ENABLED, cassandraProperties.getNodeMetrics());
        };
    }

    @Bean
    @ConditionalOnClass(MeterRegistry.class)
    public CqlSessionBuilderCustomizer meterRegistryBuilderCustomizer(final ObjectProvider<MeterRegistry> meterRegistry) {
        System.out.println("Yeah we got there");
        return sessionBuilder ->
                sessionBuilder.withMetricRegistry(
                        meterRegistry.getIfAvailable(
                                () -> Metrics.globalRegistry
                        )
                );
    }

    @Bean
    @Primary
    public CqlSession session() {
        return CqlSession.builder()
                .withMetricRegistry(Metrics.globalRegistry)
                .withKeyspace("test").build();
    }
}
@ConfigurationProperties("cassandra")
public class CassandraProperties {
    public CassandraProperties(List<String> sessionMetrics, List<String> nodeMetrics) {
        this.sessionMetrics = sessionMetrics;
        this.nodeMetrics = nodeMetrics;
    }
    private List<String> sessionMetrics;

    private List<String> nodeMetrics;

    protected List<String> getSessionMetrics() {
        return sessionMetrics;
    }

    protected List<String> getNodeMetrics() {
        return nodeMetrics;
    }
}
cassandra:
  session-metrics:
    - cql-requests
    - connected-nodes
  node-metrics:
    - pool.open-connections
    - pool.in-flight

Then I have the following scala controller

  @RequestMapping(Array("/"))
  def index(): String = {
    session.execute("DESC keyspaces;")
      .forEach(
        (row) => println(
          row.getString("keyspace_name")
        )
      )
    val sb: StringBuilder = new StringBuilder()
    val registry = session.getMetrics
      .orElseThrow(() => new IllegalStateException("Metrics are disabled"))
      .getRegistry

    registry
      .getMeters
      .forEach(
        (key, meter) => {
          sb.append(key)
        }
      )
    sb.toString()
  }
}

But when I run and hit the endpoint I get...

2023-10-24T10:31:24.124-04:00 ERROR 1172 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.lang.IllegalStateException: Metrics are disabled] with root cause

java.lang.IllegalStateException: Metrics are disabled

I also tried just using the global registry but that returns empty

    Metrics.globalRegistry.getMeters.forEach(
        meter => {
          System.out.println("Meter is " + meter.getId.toString)
          sb.append(meter.getId.toString)
          sb.append("\n")
        }
      )
0

There are 0 answers