From 57146b3ba7e8010ac8147ac2e59c73a3d417f744 Mon Sep 17 00:00:00 2001 From: Lauri Tulmin Date: Tue, 12 Oct 2021 20:41:34 +0300 Subject: [PATCH] Spymemcached: add shouldStart checks (#4358) --- .../BulkGetCompletionListener.java | 16 +++++++++++-- .../spymemcached/CompletionListener.java | 6 ++--- .../spymemcached/GetCompletionListener.java | 17 ++++++++++++-- .../MemcachedClientInstrumentation.java | 23 ++++++++++++------- .../OperationCompletionListener.java | 17 ++++++++++++-- .../spymemcached/SyncCompletionListener.java | 16 +++++++++++-- 6 files changed, 75 insertions(+), 20 deletions(-) diff --git a/instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/BulkGetCompletionListener.java b/instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/BulkGetCompletionListener.java index 768894d8807f..3cf32c0d618f 100644 --- a/instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/BulkGetCompletionListener.java +++ b/instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/BulkGetCompletionListener.java @@ -5,18 +5,30 @@ package io.opentelemetry.javaagent.instrumentation.spymemcached; +import static io.opentelemetry.javaagent.instrumentation.spymemcached.SpymemcachedSingletons.instrumenter; + import io.opentelemetry.api.trace.Span; import io.opentelemetry.context.Context; import java.util.concurrent.ExecutionException; import net.spy.memcached.MemcachedConnection; import net.spy.memcached.internal.BulkGetFuture; +import org.checkerframework.checker.nullness.qual.Nullable; public class BulkGetCompletionListener extends CompletionListener> implements net.spy.memcached.internal.BulkGetCompletionListener { - public BulkGetCompletionListener( + private BulkGetCompletionListener(Context parentContext, SpymemcachedRequest request) { + super(parentContext, request); + } + + @Nullable + public static BulkGetCompletionListener create( Context parentContext, MemcachedConnection connection, String methodName) { - super(parentContext, connection, methodName); + SpymemcachedRequest request = SpymemcachedRequest.create(connection, methodName); + if (!instrumenter().shouldStart(parentContext, request)) { + return null; + } + return new BulkGetCompletionListener(parentContext, request); } @Override diff --git a/instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/CompletionListener.java b/instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/CompletionListener.java index cf5a75b8a41f..01d7600e13df 100644 --- a/instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/CompletionListener.java +++ b/instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/CompletionListener.java @@ -12,7 +12,6 @@ import io.opentelemetry.instrumentation.api.config.Config; import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; -import net.spy.memcached.MemcachedConnection; public abstract class CompletionListener { @@ -28,9 +27,8 @@ public abstract class CompletionListener { private final Context context; private final SpymemcachedRequest request; - protected CompletionListener( - Context parentContext, MemcachedConnection connection, String methodName) { - request = SpymemcachedRequest.create(connection, methodName); + protected CompletionListener(Context parentContext, SpymemcachedRequest request) { + this.request = request; context = instrumenter().start(parentContext, request); } diff --git a/instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/GetCompletionListener.java b/instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/GetCompletionListener.java index f44c11694ccc..f5e078524911 100644 --- a/instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/GetCompletionListener.java +++ b/instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/GetCompletionListener.java @@ -5,17 +5,30 @@ package io.opentelemetry.javaagent.instrumentation.spymemcached; +import static io.opentelemetry.javaagent.instrumentation.spymemcached.SpymemcachedSingletons.instrumenter; + import io.opentelemetry.api.trace.Span; import io.opentelemetry.context.Context; import java.util.concurrent.ExecutionException; import net.spy.memcached.MemcachedConnection; import net.spy.memcached.internal.GetFuture; +import org.checkerframework.checker.nullness.qual.Nullable; public class GetCompletionListener extends CompletionListener> implements net.spy.memcached.internal.GetCompletionListener { - public GetCompletionListener( + + private GetCompletionListener(Context parentContext, SpymemcachedRequest request) { + super(parentContext, request); + } + + @Nullable + public static GetCompletionListener create( Context parentContext, MemcachedConnection connection, String methodName) { - super(parentContext, connection, methodName); + SpymemcachedRequest request = SpymemcachedRequest.create(connection, methodName); + if (!instrumenter().shouldStart(parentContext, request)) { + return null; + } + return new GetCompletionListener(parentContext, request); } @Override diff --git a/instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/MemcachedClientInstrumentation.java b/instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/MemcachedClientInstrumentation.java index 9f727748e8ce..edfabf5ed788 100644 --- a/instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/MemcachedClientInstrumentation.java +++ b/instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/MemcachedClientInstrumentation.java @@ -72,8 +72,11 @@ public static void methodExit( if (future != null) { OperationCompletionListener listener = - new OperationCompletionListener(currentContext(), client.getConnection(), methodName); - future.addListener(listener); + OperationCompletionListener.create( + currentContext(), client.getConnection(), methodName); + if (listener != null) { + future.addListener(listener); + } } } } @@ -99,8 +102,10 @@ public static void methodExit( if (future != null) { GetCompletionListener listener = - new GetCompletionListener(currentContext(), client.getConnection(), methodName); - future.addListener(listener); + GetCompletionListener.create(currentContext(), client.getConnection(), methodName); + if (listener != null) { + future.addListener(listener); + } } } } @@ -126,8 +131,10 @@ public static void methodExit( if (future != null) { BulkGetCompletionListener listener = - new BulkGetCompletionListener(currentContext(), client.getConnection(), methodName); - future.addListener(listener); + BulkGetCompletionListener.create(currentContext(), client.getConnection(), methodName); + if (listener != null) { + future.addListener(listener); + } } } } @@ -145,7 +152,7 @@ public static SyncCompletionListener methodEnter( return null; } - return new SyncCompletionListener(currentContext(), client.getConnection(), methodName); + return SyncCompletionListener.create(currentContext(), client.getConnection(), methodName); } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) @@ -153,7 +160,7 @@ public static void methodExit( @Advice.Enter SyncCompletionListener listener, @Advice.Thrown Throwable thrown, @Advice.Local("otelCallDepth") CallDepth callDepth) { - if (callDepth.decrementAndGet() > 0) { + if (callDepth.decrementAndGet() > 0 || listener == null) { return; } diff --git a/instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/OperationCompletionListener.java b/instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/OperationCompletionListener.java index eb5c406c1230..ebfbbc50157a 100644 --- a/instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/OperationCompletionListener.java +++ b/instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/OperationCompletionListener.java @@ -5,17 +5,30 @@ package io.opentelemetry.javaagent.instrumentation.spymemcached; +import static io.opentelemetry.javaagent.instrumentation.spymemcached.SpymemcachedSingletons.instrumenter; + import io.opentelemetry.api.trace.Span; import io.opentelemetry.context.Context; import java.util.concurrent.ExecutionException; import net.spy.memcached.MemcachedConnection; import net.spy.memcached.internal.OperationFuture; +import org.checkerframework.checker.nullness.qual.Nullable; public class OperationCompletionListener extends CompletionListener> implements net.spy.memcached.internal.OperationCompletionListener { - public OperationCompletionListener( + + private OperationCompletionListener(Context parentContext, SpymemcachedRequest request) { + super(parentContext, request); + } + + @Nullable + public static OperationCompletionListener create( Context parentContext, MemcachedConnection connection, String methodName) { - super(parentContext, connection, methodName); + SpymemcachedRequest request = SpymemcachedRequest.create(connection, methodName); + if (!instrumenter().shouldStart(parentContext, request)) { + return null; + } + return new OperationCompletionListener(parentContext, request); } @Override diff --git a/instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/SyncCompletionListener.java b/instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/SyncCompletionListener.java index e52c72c00a49..d8d7a9f6c859 100644 --- a/instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/SyncCompletionListener.java +++ b/instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/SyncCompletionListener.java @@ -5,9 +5,12 @@ package io.opentelemetry.javaagent.instrumentation.spymemcached; +import static io.opentelemetry.javaagent.instrumentation.spymemcached.SpymemcachedSingletons.instrumenter; + import io.opentelemetry.api.trace.Span; import io.opentelemetry.context.Context; import net.spy.memcached.MemcachedConnection; +import org.checkerframework.checker.nullness.qual.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -15,9 +18,18 @@ public class SyncCompletionListener extends CompletionListener { private static final Logger logger = LoggerFactory.getLogger(SyncCompletionListener.class); - public SyncCompletionListener( + private SyncCompletionListener(Context parentContext, SpymemcachedRequest request) { + super(parentContext, request); + } + + @Nullable + public static SyncCompletionListener create( Context parentContext, MemcachedConnection connection, String methodName) { - super(parentContext, connection, methodName); + SpymemcachedRequest request = SpymemcachedRequest.create(connection, methodName); + if (!instrumenter().shouldStart(parentContext, request)) { + return null; + } + return new SyncCompletionListener(parentContext, request); } @Override