Skip to content

Commit 3464645

Browse files
committed
Unify future operation handling
1 parent 6b042db commit 3464645

File tree

6 files changed

+14
-25
lines changed

6 files changed

+14
-25
lines changed

instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/BulkGetCompletionListener.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import io.opentelemetry.api.trace.Span;
1111
import io.opentelemetry.context.Context;
12+
import java.util.Collection;
1213
import java.util.concurrent.ExecutionException;
1314
import javax.annotation.Nullable;
1415
import net.spy.memcached.MemcachedConnection;
@@ -39,13 +40,12 @@ private static MemcachedNode getRepresentativeNodeFromConnection(MemcachedConnec
3940
// Strategy: Get the "most representative" node for bulk operations
4041
// We choose the last active node in the list, which often represents
4142
// the most recently added or most stable node in the cluster
42-
java.util.Collection<net.spy.memcached.MemcachedNode> allNodes =
43-
connection.getLocator().getAll();
43+
Collection<MemcachedNode> allNodes = connection.getLocator().getAll();
4444

4545
MemcachedNode lastActiveNode = null;
4646
MemcachedNode fallbackNode = null;
4747

48-
for (net.spy.memcached.MemcachedNode node : allNodes) {
48+
for (MemcachedNode node : allNodes) {
4949
if (fallbackNode == null) {
5050
fallbackNode = node;
5151
}

instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/GetCompletionListener.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
package io.opentelemetry.javaagent.instrumentation.spymemcached;
77

8-
import static io.opentelemetry.javaagent.instrumentation.spymemcached.SpymemcachedSingletons.GET_FUTURE_OPERATION;
8+
import static io.opentelemetry.javaagent.instrumentation.spymemcached.SpymemcachedSingletons.FUTURE_OPERATION;
99
import static io.opentelemetry.javaagent.instrumentation.spymemcached.SpymemcachedSingletons.instrumenter;
1010

1111
import io.opentelemetry.api.trace.Span;
@@ -30,7 +30,6 @@ public static GetCompletionListener create(
3030
MemcachedConnection connection,
3131
String methodName,
3232
GetFuture<?> future) {
33-
// Extract handling node from future before creating span
3433
MemcachedNode handlingNode = extractHandlingNodeFromFuture(future);
3534
SpymemcachedRequest request = SpymemcachedRequest.create(connection, methodName, handlingNode);
3635
if (!instrumenter().shouldStart(parentContext, request)) {
@@ -41,7 +40,7 @@ public static GetCompletionListener create(
4140

4241
@Nullable
4342
private static MemcachedNode extractHandlingNodeFromFuture(GetFuture<?> future) {
44-
Operation operation = GET_FUTURE_OPERATION.get(future);
43+
Operation operation = FUTURE_OPERATION.get(future);
4544
if (operation != null) {
4645
return operation.getHandlingNode();
4746
}

instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/OperationCompletionListener.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
package io.opentelemetry.javaagent.instrumentation.spymemcached;
77

8-
import static io.opentelemetry.javaagent.instrumentation.spymemcached.SpymemcachedSingletons.OPERATION_FUTURE_OPERATION;
8+
import static io.opentelemetry.javaagent.instrumentation.spymemcached.SpymemcachedSingletons.FUTURE_OPERATION;
99
import static io.opentelemetry.javaagent.instrumentation.spymemcached.SpymemcachedSingletons.instrumenter;
1010

1111
import io.opentelemetry.api.trace.Span;
@@ -30,7 +30,6 @@ public static OperationCompletionListener create(
3030
MemcachedConnection connection,
3131
String methodName,
3232
OperationFuture<?> future) {
33-
// Extract handling node from future before creating span
3433
MemcachedNode handlingNode = extractHandlingNodeFromFuture(future);
3534
SpymemcachedRequest request = SpymemcachedRequest.create(connection, methodName, handlingNode);
3635
if (!instrumenter().shouldStart(parentContext, request)) {
@@ -41,7 +40,7 @@ public static OperationCompletionListener create(
4140

4241
@Nullable
4342
private static MemcachedNode extractHandlingNodeFromFuture(OperationFuture<?> future) {
44-
Operation operation = OPERATION_FUTURE_OPERATION.get(future);
43+
Operation operation = FUTURE_OPERATION.get(future);
4544
if (operation != null) {
4645
return operation.getHandlingNode();
4746
}

instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/SetOperationInstrumentation.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55

66
package io.opentelemetry.javaagent.instrumentation.spymemcached;
77

8-
import static io.opentelemetry.javaagent.instrumentation.spymemcached.SpymemcachedSingletons.GET_FUTURE_OPERATION;
9-
import static io.opentelemetry.javaagent.instrumentation.spymemcached.SpymemcachedSingletons.OPERATION_FUTURE_OPERATION;
8+
import static io.opentelemetry.javaagent.instrumentation.spymemcached.SpymemcachedSingletons.FUTURE_OPERATION;
109
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
1110
import static net.bytebuddy.matcher.ElementMatchers.named;
1211
import static net.bytebuddy.matcher.ElementMatchers.namedOneOf;
1312
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
1413

1514
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
1615
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
16+
import java.util.concurrent.Future;
1717
import net.bytebuddy.asm.Advice;
1818
import net.bytebuddy.description.type.TypeDescription;
1919
import net.bytebuddy.matcher.ElementMatcher;
@@ -42,10 +42,8 @@ public static class SetOperationAdvice {
4242
@Advice.OnMethodExit(suppress = Throwable.class)
4343
public static void onExit(@Advice.This Object future, @Advice.Argument(0) Operation operation) {
4444

45-
if (future instanceof OperationFuture) {
46-
OPERATION_FUTURE_OPERATION.set((OperationFuture<?>) future, operation);
47-
} else if (future instanceof GetFuture) {
48-
GET_FUTURE_OPERATION.set((GetFuture<?>) future, operation);
45+
if (future instanceof OperationFuture || future instanceof GetFuture) {
46+
FUTURE_OPERATION.set((Future<?>) future, operation);
4947
}
5048
}
5149
}

instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/SpymemcachedSingletons.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,17 @@
1111
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientSpanNameExtractor;
1212
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
1313
import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor;
14-
import io.opentelemetry.instrumentation.api.semconv.network.NetworkAttributesExtractor;
1514
import io.opentelemetry.instrumentation.api.semconv.network.ServerAttributesExtractor;
1615
import io.opentelemetry.instrumentation.api.util.VirtualField;
17-
import net.spy.memcached.internal.GetFuture;
18-
import net.spy.memcached.internal.OperationFuture;
16+
import java.util.concurrent.Future;
1917
import net.spy.memcached.ops.Operation;
2018

2119
public final class SpymemcachedSingletons {
2220
private static final String INSTRUMENTATION_NAME = "io.opentelemetry.spymemcached-2.12";
2321

2422
private static final Instrumenter<SpymemcachedRequest, Object> INSTRUMENTER;
2523

26-
public static final VirtualField<OperationFuture<?>, Operation> OPERATION_FUTURE_OPERATION;
27-
28-
public static final VirtualField<GetFuture<?>, Operation> GET_FUTURE_OPERATION;
24+
public static final VirtualField<Future<?>, Operation> FUTURE_OPERATION;
2925

3026
static {
3127
SpymemcachedAttributesGetter dbAttributesGetter = new SpymemcachedAttributesGetter();
@@ -39,12 +35,10 @@ public final class SpymemcachedSingletons {
3935
DbClientSpanNameExtractor.create(dbAttributesGetter))
4036
.addAttributesExtractor(DbClientAttributesExtractor.create(dbAttributesGetter))
4137
.addAttributesExtractor(ServerAttributesExtractor.create(netAttributesGetter))
42-
.addAttributesExtractor(NetworkAttributesExtractor.create(netAttributesGetter))
4338
.addOperationMetrics(DbClientMetrics.get())
4439
.buildInstrumenter(SpanKindExtractor.alwaysClient());
4540

46-
OPERATION_FUTURE_OPERATION = VirtualField.find(OperationFuture.class, Operation.class);
47-
GET_FUTURE_OPERATION = VirtualField.find(GetFuture.class, Operation.class);
41+
FUTURE_OPERATION = VirtualField.find(Future.class, Operation.class);
4842
}
4943

5044
public static Instrumenter<SpymemcachedRequest, Object> instrumenter() {

instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/SyncCompletionListener.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ private static MemcachedNode getHandlingNodeForKey(MemcachedConnection connectio
4040
// Use the connection's locator to find the primary node for this key
4141
return connection.getLocator().getPrimary(key);
4242
} catch (RuntimeException e) {
43-
// If we can't determine the node, return null
4443
return null;
4544
}
4645
}

0 commit comments

Comments
 (0)