Skip to content

Commit 5252005

Browse files
committed
refactor: simplify node selection logic in BulkGetCompletionListener
1 parent 3464645 commit 5252005

File tree

1 file changed

+7
-20
lines changed

1 file changed

+7
-20
lines changed

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

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ private BulkGetCompletionListener(Context parentContext, SpymemcachedRequest req
2626
@Nullable
2727
public static BulkGetCompletionListener create(
2828
Context parentContext, MemcachedConnection connection, String methodName) {
29-
MemcachedNode handlingNode = getRepresentativeNodeFromConnection(connection);
29+
MemcachedNode handlingNode = getNodeFromConnection(connection);
3030
SpymemcachedRequest request = SpymemcachedRequest.create(connection, methodName, handlingNode);
3131
if (!instrumenter().shouldStart(parentContext, request)) {
3232
return null;
@@ -35,28 +35,15 @@ public static BulkGetCompletionListener create(
3535
}
3636

3737
@Nullable
38-
private static MemcachedNode getRepresentativeNodeFromConnection(MemcachedConnection connection) {
38+
private static MemcachedNode getNodeFromConnection(MemcachedConnection connection) {
3939
try {
40-
// Strategy: Get the "most representative" node for bulk operations
41-
// We choose the last active node in the list, which often represents
42-
// the most recently added or most stable node in the cluster
4340
Collection<MemcachedNode> allNodes = connection.getLocator().getAll();
44-
45-
MemcachedNode lastActiveNode = null;
46-
MemcachedNode fallbackNode = null;
47-
48-
for (MemcachedNode node : allNodes) {
49-
if (fallbackNode == null) {
50-
fallbackNode = node;
51-
}
52-
53-
if (node.isActive()) {
54-
lastActiveNode = node;
55-
}
41+
if (allNodes.size() == 1) {
42+
return allNodes.iterator().next();
5643
}
57-
58-
// Return the last active node, or fallback to the first node
59-
return lastActiveNode != null ? lastActiveNode : fallbackNode;
44+
// For multiple nodes, return null - bulk operations span multiple servers
45+
// and we cannot accurately attribute to a single server
46+
return null;
6047
} catch (RuntimeException e) {
6148
return null;
6249
}

0 commit comments

Comments
 (0)