Skip to content

Commit

Permalink
Propagate database number, user, and RedisURI into Tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
thachlp committed Oct 3, 2024
1 parent c05da9e commit daab0a8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/main/java/io/lettuce/core/protocol/CommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;

import io.lettuce.core.ClientOptions;
import io.lettuce.core.ConnectionBuilder;
import io.lettuce.core.RedisConnectionException;
import io.lettuce.core.RedisCredentials;
import io.lettuce.core.RedisException;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.push.PushListener;
import io.lettuce.core.api.push.PushMessage;
import io.lettuce.core.internal.LettuceAssert;
Expand Down Expand Up @@ -478,6 +482,13 @@ private void attachTracing(ChannelHandlerContext ctx, RedisCommand<?, ?, ?> comm
Tracer.Span span = tracer.nextSpan(context);
span.name(command.getType().name());

String redisUriStr = ctx.channel().attr(ConnectionBuilder.REDIS_URI).get();
RedisURI redisURI = RedisURI.create(redisUriStr);
span.tag("db.uri", redisURI.toString());
span.tag("db.number", String.valueOf(redisURI.getDatabase()));
span.tag("db.user", Optional.ofNullable(redisURI.getCredentialsProvider().resolveCredentials().block())
.map(RedisCredentials::getUsername).orElse(""));

if (tracedEndpoint != null) {
span.remoteEndpoint(tracedEndpoint);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public SampleTestRunnerConsumer yourCode() {
.containsEntry("net.sock.peer.addr", TestSettings.host())
.containsEntry("net.sock.peer.port", "" + TestSettings.port());
assertThat(finishedSpan.getTags()).containsKeys("db.operation");
assertThat(finishedSpan.getTags()).containsKeys("db.uri");
assertThat(finishedSpan.getTags()).containsKeys("db.number");
assertThat(finishedSpan.getTags()).containsKeys("db.user");
}

assertThat(commands).extracting(RedisCommand::getType).contains(CommandType.PING, CommandType.HELLO);
Expand Down

0 comments on commit daab0a8

Please sign in to comment.