spring-data-redis icon indicating copy to clipboard operation
spring-data-redis copied to clipboard

There is no way to get Lettuce Redis Connection pool metrics

Open veera-chenna opened this issue 3 years ago • 4 comments

We wanted to get metrics from the Lettuce Redis connection pool. But I don't see any meter binder for redis connection pools unlike JDBC Datasource. Is there any other way we can get the metrics from the Redis Connection pools?

In case of enabling the JMX, there is no way to set the JMX base name and prefixes on the commons-pool2. Can anyone help on this?

veera-chenna avatar Feb 25 '22 12:02 veera-chenna

LettucePoolingConnectionProvider manages multiple connection pools for various purposes and there's no hook to customize individual pool instances. We're currently working to introduce better observability means to our Spring Data modules so I'm labeling this ticket as observability enhancement.

mp911de avatar Feb 28 '22 07:02 mp911de

Is there any progress on this is issue?

kandogu avatar Nov 15 '22 21:11 kandogu

This ticket requires further investigation. Maybe one or more pool customizer functions in the client configuration would be useful so you can capture the pools as side-effect of such a function.

mp911de avatar Nov 17 '22 08:11 mp911de

@veera-chenna We used reflection to capture Pool Metrics. Please find sample code below -

@Component
@Slf4j
public class RedisMetrics {

    private Map<Class<?>, GenericObjectPool<StatefulConnection<?, ?>>> pools;

    public RedisMetrics(@Qualifier("lettuceClusterConnectionFactory") LettuceConnectionFactory factory) throws IllegalAccessException {
        LettuceConnectionProvider lettuceConnectionProvider = (LettuceConnectionProvider) FieldUtils.readField(factory, "connectionProvider", true);

        pools = (Map<Class<?>, GenericObjectPool<StatefulConnection<?, ?>>>) FieldUtils.readField(lettuceConnectionProvider, "pools", true);
    }

    @Scheduled(fixedRate = 1000)
    public void publishRedisMetrics() {
        pools.forEach((key, value) -> {
            Metrics.summary("lettuce_cluster_pool_metrics", "label", ACTIVE_CONNECTIONS).record(value.getNumActive());
            Metrics.summary("lettuce_cluster_pool_metrics", "label", IDLE_CONNECTIONS).record(value.getNumIdle());
            Metrics.summary("lettuce_cluster_pool_metrics", "label", WAITERS_COUNT).record(value.getNumWaiters());
            Metrics.summary("lettuce_cluster_pool_metrics", "label", MEAN_WAITING_TIME).record(value.getMeanBorrowWaitTimeMillis());
            Metrics.summary("lettuce_cluster_pool_metrics", "label", MAX_WAITING_TIME).record(value.getMaxBorrowWaitTimeMillis());
        });
    }
}

sahu-sandeep avatar Dec 15 '22 05:12 sahu-sandeep