From daab0a87979caa0b92238e9f14e109f4bfb008d8 Mon Sep 17 00:00:00 2001 From: Thach Le Date: Thu, 3 Oct 2024 19:24:44 +0700 Subject: [PATCH] Propagate database number, user, and RedisURI into Tracing --- .../java/io/lettuce/core/protocol/CommandHandler.java | 11 +++++++++++ .../core/tracing/SynchronousIntegrationTests.java | 3 +++ 2 files changed, 14 insertions(+) diff --git a/src/main/java/io/lettuce/core/protocol/CommandHandler.java b/src/main/java/io/lettuce/core/protocol/CommandHandler.java index 83bf304020..05c335267d 100644 --- a/src/main/java/io/lettuce/core/protocol/CommandHandler.java +++ b/src/main/java/io/lettuce/core/protocol/CommandHandler.java @@ -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; @@ -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 { diff --git a/src/test/java/io/lettuce/core/tracing/SynchronousIntegrationTests.java b/src/test/java/io/lettuce/core/tracing/SynchronousIntegrationTests.java index 71bb3872a1..3334789f3c 100644 --- a/src/test/java/io/lettuce/core/tracing/SynchronousIntegrationTests.java +++ b/src/test/java/io/lettuce/core/tracing/SynchronousIntegrationTests.java @@ -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);