From daab0a87979caa0b92238e9f14e109f4bfb008d8 Mon Sep 17 00:00:00 2001 From: Thach Le Date: Thu, 3 Oct 2024 19:24:44 +0700 Subject: [PATCH 1/4] 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); From 9abe7f39c7a103e4fb4d5d8d65b57268a9a3b18f Mon Sep 17 00:00:00 2001 From: Thach Le Date: Mon, 7 Oct 2024 19:29:55 +0700 Subject: [PATCH 2/4] Update attribute names as recommend --- .../io/lettuce/core/protocol/CommandHandler.java | 12 ++++++------ .../core/tracing/SynchronousIntegrationTests.java | 5 ++--- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/main/java/io/lettuce/core/protocol/CommandHandler.java b/src/main/java/io/lettuce/core/protocol/CommandHandler.java index 05c335267d..3cde181067 100644 --- a/src/main/java/io/lettuce/core/protocol/CommandHandler.java +++ b/src/main/java/io/lettuce/core/protocol/CommandHandler.java @@ -482,12 +482,12 @@ 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 (channel.hasAttr(ConnectionBuilder.REDIS_URI)) { + String redisUriStr = channel.attr(ConnectionBuilder.REDIS_URI).get(); + RedisURI redisURI = RedisURI.create(redisUriStr); + span.tag("server.address", redisURI.toString()); + span.tag("db.namespace", String.valueOf(redisURI.getDatabase())); + } if (tracedEndpoint != null) { span.remoteEndpoint(tracedEndpoint); diff --git a/src/test/java/io/lettuce/core/tracing/SynchronousIntegrationTests.java b/src/test/java/io/lettuce/core/tracing/SynchronousIntegrationTests.java index 3334789f3c..8800e20f84 100644 --- a/src/test/java/io/lettuce/core/tracing/SynchronousIntegrationTests.java +++ b/src/test/java/io/lettuce/core/tracing/SynchronousIntegrationTests.java @@ -75,9 +75,8 @@ 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(finishedSpan.getTags()).containsKeys("server.address"); + assertThat(finishedSpan.getTags()).containsKeys("db.namespace"); } assertThat(commands).extracting(RedisCommand::getType).contains(CommandType.PING, CommandType.HELLO); From 452903e89ef957e54f7b1c4f9d0bfa21b1250bfe Mon Sep 17 00:00:00 2001 From: Thach Le Date: Thu, 10 Oct 2024 17:28:55 +0700 Subject: [PATCH 3/4] Add db.user info --- src/main/java/io/lettuce/core/protocol/CommandHandler.java | 2 ++ .../io/lettuce/core/tracing/SynchronousIntegrationTests.java | 1 + 2 files changed, 3 insertions(+) diff --git a/src/main/java/io/lettuce/core/protocol/CommandHandler.java b/src/main/java/io/lettuce/core/protocol/CommandHandler.java index 3cde181067..b192f6e6a2 100644 --- a/src/main/java/io/lettuce/core/protocol/CommandHandler.java +++ b/src/main/java/io/lettuce/core/protocol/CommandHandler.java @@ -487,6 +487,8 @@ private void attachTracing(ChannelHandlerContext ctx, RedisCommand comm RedisURI redisURI = RedisURI.create(redisUriStr); span.tag("server.address", redisURI.toString()); span.tag("db.namespace", String.valueOf(redisURI.getDatabase())); + span.tag("db.user", Optional.ofNullable(redisURI.getCredentialsProvider().resolveCredentials().block()) + .map(RedisCredentials::getUsername).orElse("")); } if (tracedEndpoint != null) { diff --git a/src/test/java/io/lettuce/core/tracing/SynchronousIntegrationTests.java b/src/test/java/io/lettuce/core/tracing/SynchronousIntegrationTests.java index 8800e20f84..65bea1f1cf 100644 --- a/src/test/java/io/lettuce/core/tracing/SynchronousIntegrationTests.java +++ b/src/test/java/io/lettuce/core/tracing/SynchronousIntegrationTests.java @@ -77,6 +77,7 @@ public SampleTestRunnerConsumer yourCode() { assertThat(finishedSpan.getTags()).containsKeys("db.operation"); assertThat(finishedSpan.getTags()).containsKeys("server.address"); assertThat(finishedSpan.getTags()).containsKeys("db.namespace"); + assertThat(finishedSpan.getTags()).containsKeys("db.user"); } assertThat(commands).extracting(RedisCommand::getType).contains(CommandType.PING, CommandType.HELLO); From c1d74de358d0c61b1a5957136042a1001b092d4b Mon Sep 17 00:00:00 2001 From: Thach Le Date: Thu, 10 Oct 2024 17:30:43 +0700 Subject: [PATCH 4/4] Polishing --- src/main/java/io/lettuce/core/protocol/CommandHandler.java | 2 +- .../io/lettuce/core/tracing/SynchronousIntegrationTests.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/lettuce/core/protocol/CommandHandler.java b/src/main/java/io/lettuce/core/protocol/CommandHandler.java index b192f6e6a2..7c968bfadd 100644 --- a/src/main/java/io/lettuce/core/protocol/CommandHandler.java +++ b/src/main/java/io/lettuce/core/protocol/CommandHandler.java @@ -487,7 +487,7 @@ private void attachTracing(ChannelHandlerContext ctx, RedisCommand comm RedisURI redisURI = RedisURI.create(redisUriStr); span.tag("server.address", redisURI.toString()); span.tag("db.namespace", String.valueOf(redisURI.getDatabase())); - span.tag("db.user", Optional.ofNullable(redisURI.getCredentialsProvider().resolveCredentials().block()) + span.tag("user.name", Optional.ofNullable(redisURI.getCredentialsProvider().resolveCredentials().block()) .map(RedisCredentials::getUsername).orElse("")); } diff --git a/src/test/java/io/lettuce/core/tracing/SynchronousIntegrationTests.java b/src/test/java/io/lettuce/core/tracing/SynchronousIntegrationTests.java index 65bea1f1cf..8dee823075 100644 --- a/src/test/java/io/lettuce/core/tracing/SynchronousIntegrationTests.java +++ b/src/test/java/io/lettuce/core/tracing/SynchronousIntegrationTests.java @@ -77,7 +77,7 @@ public SampleTestRunnerConsumer yourCode() { assertThat(finishedSpan.getTags()).containsKeys("db.operation"); assertThat(finishedSpan.getTags()).containsKeys("server.address"); assertThat(finishedSpan.getTags()).containsKeys("db.namespace"); - assertThat(finishedSpan.getTags()).containsKeys("db.user"); + assertThat(finishedSpan.getTags()).containsKeys("user.name"); } assertThat(commands).extracting(RedisCommand::getType).contains(CommandType.PING, CommandType.HELLO);