Skip to content

Commit 4c1b4b6

Browse files
committed
[#12564] Backport: Update forwarding server call tracking in grpc plugin
1 parent f3ad02d commit 4c1b4b6

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

agent-module/plugins/grpc/src/main/java/com/navercorp/pinpoint/plugin/grpc/interceptor/server/CopyAsyncContextInterceptor.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@
2323
import com.navercorp.pinpoint.bootstrap.logging.PluginLogger;
2424
import com.navercorp.pinpoint.common.util.ArrayUtils;
2525
import com.navercorp.pinpoint.plugin.grpc.field.accessor.ServerStreamGetter;
26+
import io.grpc.ForwardingServerCall;
2627
import io.grpc.internal.ServerStream;
2728

29+
import java.lang.reflect.Method;
30+
2831
/**
2932
* @author Taejin Koo
33+
* @author shofee
3034
*/
3135
public class CopyAsyncContextInterceptor implements AroundInterceptor {
32-
36+
private static final int MAX_DELEGATE_CALLS = 10;
3337
private final PluginLogger logger = PluginLogManager.getLogger(CopyAsyncContextInterceptor.class);
3438
private final boolean isDebug = logger.isDebugEnabled();
3539

@@ -60,6 +64,21 @@ public void after(Object target, Object[] args, Object result, Throwable throwab
6064
}
6165

6266
AsyncContext getAsyncContext(Object object) {
67+
Object target = object;
68+
try {
69+
int count = 0;
70+
while (!(target instanceof ServerStreamGetter) && (target instanceof ForwardingServerCall)) {
71+
Method delegateMethod = ForwardingServerCall.class.getDeclaredMethod("delegate");
72+
delegateMethod.setAccessible(true);
73+
target = delegateMethod.invoke(target);
74+
if (count++ >= MAX_DELEGATE_CALLS) {
75+
return null;
76+
}
77+
}
78+
} catch (Exception ignored) {
79+
return null;
80+
}
81+
6382
if (object instanceof ServerStreamGetter) {
6483
ServerStream serverStream = ((ServerStreamGetter) object)._$PINPOINT$_getServerStream();
6584
if (serverStream instanceof AsyncContextAccessor) {
@@ -68,5 +87,4 @@ AsyncContext getAsyncContext(Object object) {
6887
}
6988
return null;
7089
}
71-
7290
}

0 commit comments

Comments
 (0)