Spymemcached: add shouldStart checks
MODIFY
instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/BulkGetCompletionListener.java
instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/BulkGetCompletionListener.java
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<BulkGetFuture<?>>
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

MODIFY
instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/CompletionListener.java
instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/CompletionListener.java
@@ -12,7 +12,6 @@ import io.opentelemetry.context.Context;
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<T> {
@@ -28,9 +27,8 @@ public abstract class CompletionListener<T> {
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);
}

MODIFY
instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/GetCompletionListener.java
instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/GetCompletionListener.java
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<GetFuture<?>>
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

MODIFY
instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/MemcachedClientInstrumentation.java
instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/MemcachedClientInstrumentation.java
@@ -72,11 +72,14 @@ public class MemcachedClientInstrumentation implements TypeInstrumentation {
if (future != null) {
OperationCompletionListener listener =
- new OperationCompletionListener(currentContext(), client.getConnection(), methodName);
+ OperationCompletionListener.create(
+ currentContext(), client.getConnection(), methodName);
+ if (listener != null) {
future.addListener(listener);
}
}
}
+ }
@SuppressWarnings("unused")
public static class AsyncGetAdvice {
@@ -99,11 +102,13 @@ public class MemcachedClientInstrumentation implements TypeInstrumentation {
if (future != null) {
GetCompletionListener listener =
- new GetCompletionListener(currentContext(), client.getConnection(), methodName);
+ GetCompletionListener.create(currentContext(), client.getConnection(), methodName);
+ if (listener != null) {
future.addListener(listener);
}
}
}
+ }
@SuppressWarnings("unused")
public static class AsyncBulkAdvice {
@@ -126,11 +131,13 @@ public class MemcachedClientInstrumentation implements TypeInstrumentation {
if (future != null) {
BulkGetCompletionListener listener =
- new BulkGetCompletionListener(currentContext(), client.getConnection(), methodName);
+ BulkGetCompletionListener.create(currentContext(), client.getConnection(), methodName);
+ if (listener != null) {
future.addListener(listener);
}
}
}
+ }
@SuppressWarnings("unused")
public static class SyncOperationAdvice {
@@ -145,7 +152,7 @@ public class MemcachedClientInstrumentation implements TypeInstrumentation {
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 class MemcachedClientInstrumentation implements TypeInstrumentation {
@Advice.Enter SyncCompletionListener listener,
@Advice.Thrown Throwable thrown,
@Advice.Local("otelCallDepth") CallDepth callDepth) {
- if (callDepth.decrementAndGet() > 0) {
+ if (callDepth.decrementAndGet() > 0 || listener == null) {
return;
}

MODIFY
instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/OperationCompletionListener.java
instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/OperationCompletionListener.java
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<OperationFuture<?>>
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

MODIFY
instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/SyncCompletionListener.java
instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/SyncCompletionListener.java
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<Void> {
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

Spymemcached: add shouldStart checks (#4358)