Repository navigation
Stop Redis liveness checks from creating spans (quick fix - span reduction) #13678
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dougqh
wants to merge
4
commits into
master
Choose a base branch
from
dougqh/redis-liveness-suppression
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7708d58
Stop Jedis 3 pool liveness checks from creating spans
dougqh a8631da
Test that Jedis 3 pool liveness checks create no spans
dougqh 4ae14eb
Stop Redisson keepalive pings from creating spans
dougqh 41f2dcf
Test that Redisson keepalive pings create no spans
dougqh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
....0/src/main/java/datadog/trace/instrumentation/jedis30/PoolValidationInstrumentation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package datadog.trace.instrumentation.jedis30; | ||
|
|
||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.ClassLoaderMatchers.hasClassNamed; | ||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
| import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan; | ||
| import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.blackholeSpan; | ||
| import static net.bytebuddy.matcher.ElementMatchers.not; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import datadog.context.ContextScope; | ||
| import datadog.trace.agent.tooling.Instrumenter; | ||
| import datadog.trace.agent.tooling.InstrumenterModule; | ||
| import net.bytebuddy.asm.Advice; | ||
| import net.bytebuddy.matcher.ElementMatcher; | ||
|
|
||
| /** | ||
| * Stops Jedis 3.x pool liveness checks from producing spans. | ||
| * | ||
| * <p>The pool checks a connection by sending {@code PING} on borrow, on return and from the idle | ||
| * evictor, which {@code JedisPoolConfig} turns on by default. The evictor's checks run with no | ||
| * active trace, so each became a standalone root span. The advice runs each check under a | ||
| * black-hole span, so the {@code PING} creates no span. | ||
| * | ||
| * <p>Jedis 4.x needs no equivalent: its pool sends {@code PING} through {@code sendCommand}, which | ||
| * that integration does not trace. | ||
| */ | ||
| @AutoService(InstrumenterModule.class) | ||
| public final class PoolValidationInstrumentation extends InstrumenterModule.Tracing | ||
| implements Instrumenter.ForKnownTypes, Instrumenter.HasMethodAdvice { | ||
|
|
||
| public PoolValidationInstrumentation() { | ||
| super("jedis", "redis", "redis-pool-validation"); | ||
| } | ||
|
|
||
| @Override | ||
| public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() { | ||
| // Jedis 3.x only: ProtocolCommand arrived in 3.0, CommandObject in 4.0 | ||
| return hasClassNamed("redis.clients.jedis.commands.ProtocolCommand") | ||
| .and(not(hasClassNamed("redis.clients.jedis.CommandObject"))); | ||
| } | ||
|
|
||
| @Override | ||
| public String[] knownMatchingTypes() { | ||
| return new String[] { | ||
| "redis.clients.jedis.JedisFactory", "redis.clients.jedis.ShardedJedisPool$ShardedJedisFactory" | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public void methodAdvice(MethodTransformer transformer) { | ||
| transformer.applyAdvice( | ||
| named("validateObject"), PoolValidationInstrumentation.class.getName() + "$MuteAdvice"); | ||
| } | ||
|
|
||
| public static class MuteAdvice { | ||
| @Advice.OnMethodEnter(suppress = Throwable.class) | ||
| public static ContextScope onEnter() { | ||
| return activateSpan(blackholeSpan()); | ||
| } | ||
|
|
||
| @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
| public static void onExit(@Advice.Enter final ContextScope scope) { | ||
| if (scope != null) { | ||
| scope.close(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
104 changes: 104 additions & 0 deletions
104
...rc/test/java/datadog/trace/instrumentation/jedis30/PoolValidationInstrumentationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| package datadog.trace.instrumentation.jedis30; | ||
|
|
||
| import static datadog.trace.agent.test.utils.TraceUtils.runUnderTrace; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import datadog.trace.agent.test.AbstractInstrumentationTest; | ||
| import datadog.trace.agent.test.utils.PortUtils; | ||
| import datadog.trace.core.DDSpan; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
| import redis.clients.jedis.Jedis; | ||
| import redis.clients.jedis.JedisPool; | ||
| import redis.clients.jedis.JedisPoolConfig; | ||
| import redis.embedded.RedisServer; | ||
|
|
||
| /** | ||
| * Assertions look for specific spans rather than whole trace shapes: the server-side {@code PING} | ||
| * count is read with a traced client. | ||
| */ | ||
| class PoolValidationInstrumentationTest extends AbstractInstrumentationTest { | ||
| private static final Pattern PING_CALLS = Pattern.compile("cmdstat_ping:calls=(\\d+)"); | ||
|
|
||
| private static int port; | ||
| private static RedisServer redisServer; | ||
|
|
||
| @BeforeAll | ||
| static void startRedis() throws Exception { | ||
| port = PortUtils.randomOpenPort(); | ||
| redisServer = | ||
| RedisServer.newRedisServer() | ||
| // bind to localhost to avoid a firewall popup | ||
| .setting("bind 127.0.0.1") | ||
| .setting("maxmemory 128M") | ||
| .port(port) | ||
| .build(); | ||
| redisServer.start(); | ||
| } | ||
|
|
||
| @AfterAll | ||
| static void stopRedis() throws Exception { | ||
| redisServer.stop(); | ||
| } | ||
|
|
||
| @Test | ||
| void validationPingCreatesNoSpans() throws Exception { | ||
| JedisPoolConfig config = new JedisPoolConfig(); | ||
| config.setMaxTotal(1); | ||
| config.setTestOnBorrow(true); | ||
| long pingsBefore; | ||
| try (JedisPool pool = new JedisPool(config, "localhost", port)) { | ||
| // warm up so the traced borrow reuses an idle connection, which the pool validates | ||
| pool.getResource().close(); | ||
|
|
||
| pingsBefore = serverPingCount(); | ||
| runUnderTrace( | ||
| "parent", | ||
| () -> { | ||
| try (Jedis jedis = pool.getResource()) { | ||
| jedis.set("foo", "bar"); | ||
| } | ||
| return null; | ||
| }); | ||
| } | ||
| assertTrue(serverPingCount() > pingsBefore, "the pool didn't validate on borrow"); | ||
|
|
||
| blockUntilTracesMatch(traces -> find("SET") != null); | ||
| assertEquals(find("parent").getSpanId(), find("SET").getParentId()); | ||
| for (DDSpan span : allSpans()) { | ||
| assertFalse("PING".contentEquals(span.getResourceName()), "validation traced: " + span); | ||
| } | ||
| } | ||
|
|
||
| /** Reads how many {@code PING}s the server has handled, so validation can't pass vacuously. */ | ||
| private static long serverPingCount() { | ||
| try (Jedis jedis = new Jedis("localhost", port)) { | ||
| Matcher matcher = PING_CALLS.matcher(jedis.info("commandstats")); | ||
| return matcher.find() ? Long.parseLong(matcher.group(1)) : 0; | ||
| } | ||
| } | ||
|
|
||
| private DDSpan find(String resource) { | ||
| for (DDSpan span : allSpans()) { | ||
| if (resource.contentEquals(span.getResourceName())) { | ||
| return span; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private List<DDSpan> allSpans() { | ||
| List<DDSpan> spans = new ArrayList<>(); | ||
| for (List<DDSpan> trace : writer) { | ||
| spans.addAll(trace); | ||
| } | ||
| return spans; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
...on-3.10.3/src/test/java/datadog/trace/instrumentation/redisson30/PingSuppressionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| package datadog.trace.instrumentation.redisson30; | ||
|
|
||
| import static datadog.trace.agent.test.utils.TraceUtils.runUnderTrace; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import com.redis.testcontainers.RedisContainer; | ||
| import datadog.trace.agent.test.AbstractInstrumentationTest; | ||
| import datadog.trace.core.DDSpan; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.redisson.Redisson; | ||
| import org.redisson.api.RedissonClient; | ||
| import org.redisson.config.Config; | ||
| import org.testcontainers.containers.wait.strategy.Wait; | ||
| import org.testcontainers.utility.DockerImageName; | ||
|
|
||
| class PingSuppressionTest extends AbstractInstrumentationTest { | ||
| private static final Pattern PING_CALLS = Pattern.compile("cmdstat_ping:calls=(\\d+)"); | ||
|
|
||
| private static RedisContainer redis; | ||
|
|
||
| @BeforeAll | ||
| static void startRedis() { | ||
| redis = | ||
| new RedisContainer(DockerImageName.parse(System.getProperty("test.redis.image"))) | ||
| .waitingFor(Wait.forListeningPort()); | ||
| redis.start(); | ||
| } | ||
|
|
||
| @AfterAll | ||
| static void stopRedis() { | ||
| redis.stop(); | ||
| } | ||
|
|
||
| @Test | ||
| void keepalivePingCreatesNoSpans() throws Exception { | ||
| Config config = new Config(); | ||
| config | ||
| .useSingleServer() | ||
| .setAddress(redis.getRedisURI()) | ||
| .setConnectionMinimumIdleSize(1) | ||
| .setConnectionPoolSize(1) | ||
| .setSubscriptionConnectionMinimumIdleSize(1) | ||
| .setSubscriptionConnectionPoolSize(1) | ||
| .setPingConnectionInterval(50); | ||
| config.setUseScriptCache(false); | ||
|
|
||
| RedissonClient client = Redisson.create(config); | ||
| try { | ||
| // setting up a connection sends one traced PING, and newer versions finish setup after | ||
| // create() returns, so let it settle and only count PING spans from then on | ||
| Thread.sleep(200); | ||
| int setupPingSpans = pingSpanCount(); | ||
| long pingsBefore = serverPingCount(); | ||
|
|
||
| runUnderTrace( | ||
| "parent", | ||
| () -> { | ||
| client.getBucket("foo").set("bar"); | ||
| return null; | ||
| }); | ||
| // several ping intervals | ||
| Thread.sleep(500); | ||
| assertTrue(serverPingCount() > pingsBefore, "Redisson didn't ping"); | ||
| assertEquals(setupPingSpans, pingSpanCount(), "keepalive PING traced"); | ||
| } finally { | ||
| client.shutdown(); | ||
| } | ||
|
|
||
| blockUntilTracesMatch(traces -> find("SET") != null); | ||
| assertEquals(find("parent").getSpanId(), find("SET").getParentId()); | ||
| } | ||
|
|
||
| private int pingSpanCount() { | ||
| int count = 0; | ||
| for (DDSpan span : allSpans()) { | ||
| if ("PING".contentEquals(span.getResourceName())) { | ||
| count++; | ||
| } | ||
| } | ||
| return count; | ||
| } | ||
|
|
||
| /** Reads the server's {@code PING} count without a traced client. */ | ||
| private static long serverPingCount() throws Exception { | ||
| String info = redis.execInContainer("redis-cli", "info", "commandstats").getStdout(); | ||
| Matcher matcher = PING_CALLS.matcher(info); | ||
| return matcher.find() ? Long.parseLong(matcher.group(1)) : 0; | ||
| } | ||
|
|
||
| private DDSpan find(String resource) { | ||
| for (DDSpan span : allSpans()) { | ||
| if (resource.contentEquals(span.getResourceName())) { | ||
| return span; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private List<DDSpan> allSpans() { | ||
| List<DDSpan> spans = new ArrayList<>(); | ||
| for (List<DDSpan> trace : writer) { | ||
| spans.addAll(trace); | ||
| } | ||
| return spans; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With DD_INTEGRATIONS_ENABLED=false and DD_TRACE_JEDIS_ENABLED=true, setting DD_TRACE_REDIS_POOL_VALIDATION_ENABLED=false does not restore validation spans. InstrumenterModule ORs these integration settings when integrations default to disabled, so the enabled Jedis setting overrides the explicit suppression opt-out. Pool-validation PING spans remain suppressed despite the documented switch.
React: 👍 useful · 👎 wrong · 😕 unclear · 👀 right, but not acting on it
What each reaction means
🤖 Bits Code Review · Open Bits AI session