wu-sheng commented on a change in pull request #6973:
URL: https://github.com/apache/skywalking/pull/6973#discussion_r636941580
##########
File path:
apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/v5/InvokeInterceptor.java
##########
@@ -17,48 +17,56 @@
package org.apache.skywalking.apm.plugin.spring.mvc.v5;
-import java.lang.reflect.Method;
import org.apache.skywalking.apm.agent.core.context.ContextManager;
+import org.apache.skywalking.apm.agent.core.context.RuntimeContext;
import org.apache.skywalking.apm.agent.core.context.tag.Tags;
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
-import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
-import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
-import
org.apache.skywalking.apm.plugin.spring.mvc.commons.ReactiveRequestHolder;
-import
org.apache.skywalking.apm.plugin.spring.mvc.commons.ReactiveResponseHolder;
+import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.v2.InstanceMethodsAroundInterceptorV2;
+import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.v2.MethodInvocationContext;
import org.springframework.http.HttpStatus;
+import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
+import java.lang.reflect.Method;
+
+import static
org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.REACTIVE_ASYNC_SPAN_IN_RUNTIME_CONTEXT;
import static
org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.REQUEST_KEY_IN_RUNTIME_CONTEXT;
import static
org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.RESPONSE_KEY_IN_RUNTIME_CONTEXT;
-public class InvokeInterceptor implements InstanceMethodsAroundInterceptor {
+public class InvokeInterceptor implements InstanceMethodsAroundInterceptorV2 {
@Override
- public void beforeMethod(final EnhancedInstance objInst,
- final Method method,
- final Object[] allArguments,
- final Class<?>[] argumentsTypes,
- final MethodInterceptResult result) throws
Throwable {
+ public void beforeMethod(EnhancedInstance objInst, Method method, Object[]
allArguments, Class<?>[] argumentsTypes, MethodInvocationContext context)
throws Throwable {
ServerWebExchange exchange = (ServerWebExchange) allArguments[0];
- final ReactiveResponseHolder responseHolder = new
ReactiveResponseHolder(exchange.getResponse());
- ContextManager.getRuntimeContext()
- .put(RESPONSE_KEY_IN_RUNTIME_CONTEXT, responseHolder);
- ContextManager.getRuntimeContext()
- .put(REQUEST_KEY_IN_RUNTIME_CONTEXT, new
ReactiveRequestHolder(exchange.getRequest()));
- objInst.setSkyWalkingDynamicField(responseHolder);
+ final ServerHttpResponse response = exchange.getResponse();
+ /**
+ * First, we put the slot in the RuntimeContext,
+ * as well as the MethodInvocationContext (MIC),
+ * so that we can access it in the {@link
org.apache.skywalking.apm.plugin.spring.mvc.v5.InvokeInterceptor#afterMethod}
+ * Then we fetch the slot from {@link
org.apache.skywalking.apm.plugin.spring.mvc.commons.interceptor.AbstractMethodInterceptor#afterMethod}
+ * and fulfill the slot with the real AbstractSpan.
+ * Afterwards, we can safely remove the RuntimeContext.
+ * Finally, when the lambda passed to the {@link
reactor.core.publisher.Mono#doFinally} is executed,
+ * we can take it out from the MIC which is lexically captured by the
lambda expression.
Review comment:
```suggestion
* Finally, when the lambda executes in the {@link
reactor.core.publisher.Mono#doFinally},
* ref of span could be acquired from MIC.
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]