Tracing Redis calls in Lettuce.

The problem

Tracing Redis calls in Lettuce and sending the tracing information to Zipkin.

The solution

There is built-in Brave tracing support in Lettuce since version 5.1, and tracing can be configured through ClientResources, using the following code.

// create or use an existent brave Tracing instance
brave.Tracing clientTracing = ...
// create an instance of `BraveTracing`
io.lettuce.core.tracing.Tracing tracing = BraveTracing.create(clientTracing);
// build the ClientResources with tracing support.
ClientResources clientResources = DefaultClientResources.builder().tracing(tracing).build();
// So we got the ClientResources with zipkin tracing support, so wonderful, cheers!

If Spring Cloud Sleuth is used, Redis tracing is not enabled by default, this can be changed by simply creating a customized ClientResources bean object.

  @Bean(destroyMethod = "shutdown")
  public ClientResources lettuceClientResources(brave.Tracing tracing) {
    BraveTracing redisBraveTracing = BraveTracing.create(tracing);
    Builder builder = DefaultClientResources.builder();

This is all we need to do, Long live Spring Boot, Spring Cloud banzai!

By the way, nothing was mentioned in either Lettuce documentation or Spring Cloud Sleuth on how we can trace Redis calls. I had to dig into the source, after that, I added some description on tracing in Lettuce documentation, hoping it can help a bit.

You’re welcome! 🙂