From cb42c19459b57ba804b95ac78e5a6ae9e6816360 Mon Sep 17 00:00:00 2001 From: ascrutae Date: Sun, 28 Apr 2019 20:52:24 +0800 Subject: [PATCH 1/4] Fix #2546 --- .../plugin/spring/mvc/commons/Constants.java | 2 + .../IllegalMethodStackDepthException.java | 24 ++++++ .../ServletResponseNotFoundException.java | 21 +++++ .../AbstractMethodInterceptor.java | 80 ++++++++++++++----- .../webflux/v5/AbstractMethodInterceptor.java | 37 ++++++--- 5 files changed, 135 insertions(+), 29 deletions(-) create mode 100644 apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/exception/IllegalMethodStackDepthException.java create mode 100644 apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/exception/ServletResponseNotFoundException.java diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/Constants.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/Constants.java index 114f8c2d4e8a..42119d6ba789 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/Constants.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/Constants.java @@ -39,4 +39,6 @@ public class Constants { public static final String FORWARD_REQUEST_FLAG = "SW_FORWARD_REQUEST_FLAG"; public static final String WEBFLUX_REQUEST_KEY = "SW_WEBFLUX_REQUEST_KEY"; + + public static final String CONTROLLER_METHOD_STACK_DEPTH = "SW_CONTROLLER_METHOD_STACK_DEPTH"; } diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/exception/IllegalMethodStackDepthException.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/exception/IllegalMethodStackDepthException.java new file mode 100644 index 000000000000..e6181ce4d163 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/exception/IllegalMethodStackDepthException.java @@ -0,0 +1,24 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.skywalking.apm.plugin.spring.mvc.commons.exception; + +public class IllegalMethodStackDepthException extends RuntimeException { + public IllegalMethodStackDepthException(Object depth) { + + } +} diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/exception/ServletResponseNotFoundException.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/exception/ServletResponseNotFoundException.java new file mode 100644 index 000000000000..2003f641264f --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/exception/ServletResponseNotFoundException.java @@ -0,0 +1,21 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.skywalking.apm.plugin.spring.mvc.commons.exception; + +public class ServletResponseNotFoundException extends RuntimeException { +} diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/AbstractMethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/AbstractMethodInterceptor.java index 0561ff847458..c3582798ea6d 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/AbstractMethodInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/AbstractMethodInterceptor.java @@ -34,7 +34,10 @@ import org.apache.skywalking.apm.agent.core.util.MethodUtil; import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; import org.apache.skywalking.apm.plugin.spring.mvc.commons.EnhanceRequireObjectCache; +import org.apache.skywalking.apm.plugin.spring.mvc.commons.exception.IllegalMethodStackDepthException; +import org.apache.skywalking.apm.plugin.spring.mvc.commons.exception.ServletResponseNotFoundException; +import static org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.CONTROLLER_METHOD_STACK_DEPTH; import static org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.FORWARD_REQUEST_FLAG; 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; @@ -44,6 +47,7 @@ */ public abstract class AbstractMethodInterceptor implements InstanceMethodsAroundInterceptor { public abstract String getRequestURL(Method method); + public abstract String getAcceptedMethodTypes(Method method); @Override @@ -75,19 +79,44 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr HttpServletRequest request = (HttpServletRequest)ContextManager.getRuntimeContext().get(REQUEST_KEY_IN_RUNTIME_CONTEXT); if (request != null) { - ContextCarrier contextCarrier = new ContextCarrier(); - CarrierItem next = contextCarrier.items(); - while (next.hasNext()) { - next = next.next(); - next.setHeadValue(request.getHeader(next.getHeadKey())); + Object stackDepth = ContextManager.getRuntimeContext().get(CONTROLLER_METHOD_STACK_DEPTH); + Integer depth = stackDepth == null ? 0 : Integer.parseInt(stackDepth.toString()); + + if (depth == 0) { + ContextCarrier contextCarrier = new ContextCarrier(); + CarrierItem next = contextCarrier.items(); + while (next.hasNext()) { + next = next.next(); + next.setHeadValue(request.getHeader(next.getHeadKey())); + } + + AbstractSpan span = ContextManager.createEntrySpan(operationName, contextCarrier); + Tags.URL.set(span, request.getRequestURL().toString()); + Tags.HTTP.METHOD.set(span, request.getMethod()); + span.setComponent(ComponentsDefine.SPRING_MVC_ANNOTATION); + SpanLayer.asHttp(span); + } else { + AbstractSpan span = + ContextManager.createLocalSpan(buildOperationName(objInst, method)); + span.setComponent(ComponentsDefine.SPRING_MVC_ANNOTATION); } - AbstractSpan span = ContextManager.createEntrySpan(operationName, contextCarrier); - Tags.URL.set(span, request.getRequestURL().toString()); - Tags.HTTP.METHOD.set(span, request.getMethod()); - span.setComponent(ComponentsDefine.SPRING_MVC_ANNOTATION); - SpanLayer.asHttp(span); + ContextManager.getRuntimeContext().put(CONTROLLER_METHOD_STACK_DEPTH, depth++); + } + } + + private String buildOperationName(Object invoker, Method method){ + StringBuilder operationName = new StringBuilder(invoker.getClass().getName()) + .append(".").append(method.getName()).append("("); + for (Class type : method.getParameterTypes()) { + operationName.append(type.getName()).append(","); } + + if (method.getParameterTypes().length > 0){ + operationName = operationName.deleteCharAt(operationName.length() - 1); + } + + return operationName.append(")").toString(); } @Override @@ -102,19 +131,34 @@ public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allA return ret; } - HttpServletResponse response = (HttpServletResponse)ContextManager.getRuntimeContext().get(RESPONSE_KEY_IN_RUNTIME_CONTEXT); - try { - if (response != null) { - AbstractSpan span = ContextManager.activeSpan(); + HttpServletRequest request = (HttpServletRequest)ContextManager.getRuntimeContext().get(REQUEST_KEY_IN_RUNTIME_CONTEXT); + + if (request != null) { + Object stackDepth = ContextManager.getRuntimeContext().get(CONTROLLER_METHOD_STACK_DEPTH); + if (stackDepth == null) { + throw new IllegalMethodStackDepthException(stackDepth); + } + + Integer depth = Integer.parseInt(String.valueOf(stackDepth)); + AbstractSpan span = ContextManager.activeSpan(); + + if (depth == 0) { + HttpServletResponse response = (HttpServletResponse)ContextManager.getRuntimeContext().get(RESPONSE_KEY_IN_RUNTIME_CONTEXT); + if (response == null) { + throw new ServletResponseNotFoundException(); + } + if (response.getStatus() >= 400) { span.errorOccurred(); Tags.STATUS_CODE.set(span, Integer.toString(response.getStatus())); + + ContextManager.getRuntimeContext().remove(REQUEST_KEY_IN_RUNTIME_CONTEXT); + ContextManager.getRuntimeContext().remove(RESPONSE_KEY_IN_RUNTIME_CONTEXT); } - ContextManager.stopSpan(); } - } finally { - ContextManager.getRuntimeContext().remove(REQUEST_KEY_IN_RUNTIME_CONTEXT); - ContextManager.getRuntimeContext().remove(RESPONSE_KEY_IN_RUNTIME_CONTEXT); + + ContextManager.stopSpan(); + ContextManager.getRuntimeContext().put(CONTROLLER_METHOD_STACK_DEPTH, depth--); } return ret; diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/webflux-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/webflux/v5/AbstractMethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/webflux-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/webflux/v5/AbstractMethodInterceptor.java index 43baad9ed3f0..a6e7e3d1136a 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/webflux-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/webflux/v5/AbstractMethodInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/webflux-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/webflux/v5/AbstractMethodInterceptor.java @@ -31,6 +31,7 @@ import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; import org.apache.skywalking.apm.plugin.spring.mvc.commons.EnhanceRequireObjectCache; +import static org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.CONTROLLER_METHOD_STACK_DEPTH; import static org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.FORWARD_REQUEST_FLAG; import static org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.WEBFLUX_REQUEST_KEY; @@ -39,6 +40,7 @@ */ public abstract class AbstractMethodInterceptor implements InstanceMethodsAroundInterceptor { public abstract String getRequestURL(Method method); + public abstract String getAcceptedMethodTypes(Method method); @Override @@ -64,18 +66,28 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr HttpRequest request = (HttpRequest)ContextManager.getRuntimeContext().get(WEBFLUX_REQUEST_KEY); if (request != null) { - ContextCarrier contextCarrier = new ContextCarrier(); - CarrierItem next = contextCarrier.items(); - while (next.hasNext()) { - next = next.next(); - next.setHeadValue(request.headers().get(next.getHeadKey())); + + Object stackDepth = ContextManager.getRuntimeContext().get(CONTROLLER_METHOD_STACK_DEPTH); + Integer depth = stackDepth == null ? 0 : Integer.parseInt(stackDepth.toString()); + if (depth == 0) { + ContextCarrier contextCarrier = new ContextCarrier(); + CarrierItem next = contextCarrier.items(); + while (next.hasNext()) { + next = next.next(); + next.setHeadValue(request.headers().get(next.getHeadKey())); + } + + AbstractSpan span = ContextManager.createEntrySpan(requestURL, contextCarrier); + Tags.URL.set(span, request.uri()); + Tags.HTTP.METHOD.set(span, request.method().name()); + span.setComponent(ComponentsDefine.SPRING_MVC_ANNOTATION); + SpanLayer.asHttp(span); + } else { + AbstractSpan span = ContextManager.createLocalSpan(objInst.getClass().getName() + "/" + method.getName()); + span.setComponent(ComponentsDefine.SPRING_MVC_ANNOTATION); } - AbstractSpan span = ContextManager.createEntrySpan(requestURL, contextCarrier); - Tags.URL.set(span, request.uri()); - Tags.HTTP.METHOD.set(span, request.method().name()); - span.setComponent(ComponentsDefine.SPRING_MVC_ANNOTATION); - SpanLayer.asHttp(span); + ContextManager.getRuntimeContext().put(CONTROLLER_METHOD_STACK_DEPTH, depth++); } } @@ -91,7 +103,10 @@ public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allA return ret; } - ContextManager.stopSpan(); + HttpRequest request = (HttpRequest)ContextManager.getRuntimeContext().get(WEBFLUX_REQUEST_KEY); + if (request != null) { + ContextManager.stopSpan(); + } return ret; } From a4ad679bc54569a224e0529d438ba15df3693646 Mon Sep 17 00:00:00 2001 From: ascrutae Date: Sun, 28 Apr 2019 23:19:33 +0800 Subject: [PATCH 2/4] Change the stackdepth type --- .../spring/mvc/v4/SpringTestCaseHelper.java | 1 + .../IllegalMethodStackDepthException.java | 4 +- .../ServletResponseNotFoundException.java | 3 ++ .../AbstractMethodInterceptor.java | 23 ++++++----- .../mvc/commons/interceptor/StackDepth.java | 41 +++++++++++++++++++ .../webflux/v5/AbstractMethodInterceptor.java | 11 +++-- 6 files changed, 66 insertions(+), 17 deletions(-) create mode 100644 apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/StackDepth.java diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v4/SpringTestCaseHelper.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v4/SpringTestCaseHelper.java index 34b257aebae0..ffcee2a6fcc2 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v4/SpringTestCaseHelper.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v4/SpringTestCaseHelper.java @@ -32,6 +32,7 @@ public final static void createCaseHandler(HttpServletRequest request, HttpServl ContextManager.getRuntimeContext().put(Constants.RESPONSE_KEY_IN_RUNTIME_CONTEXT, response); a.handleCase(); ContextManager.stopSpan(); + ContextManager.getRuntimeContext().remove(Constants.CONTROLLER_METHOD_STACK_DEPTH); } public interface CaseHandler { diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/exception/IllegalMethodStackDepthException.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/exception/IllegalMethodStackDepthException.java index e6181ce4d163..eee2204ad8b8 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/exception/IllegalMethodStackDepthException.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/exception/IllegalMethodStackDepthException.java @@ -18,7 +18,7 @@ package org.apache.skywalking.apm.plugin.spring.mvc.commons.exception; public class IllegalMethodStackDepthException extends RuntimeException { - public IllegalMethodStackDepthException(Object depth) { - + public IllegalMethodStackDepthException() { + super("Please submit an new issue to Apache Skywalking if this Exception was happened."); } } diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/exception/ServletResponseNotFoundException.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/exception/ServletResponseNotFoundException.java index 2003f641264f..12d665c39ecc 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/exception/ServletResponseNotFoundException.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/exception/ServletResponseNotFoundException.java @@ -18,4 +18,7 @@ package org.apache.skywalking.apm.plugin.spring.mvc.commons.exception; public class ServletResponseNotFoundException extends RuntimeException { + public ServletResponseNotFoundException() { + super("Please submit an new issue to Apache Skywalking if this Exception was happened."); + } } diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/AbstractMethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/AbstractMethodInterceptor.java index 883faa17c77b..c6fbc0df6651 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/AbstractMethodInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/AbstractMethodInterceptor.java @@ -79,10 +79,9 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr HttpServletRequest request = (HttpServletRequest)ContextManager.getRuntimeContext().get(REQUEST_KEY_IN_RUNTIME_CONTEXT); if (request != null) { - Object stackDepth = ContextManager.getRuntimeContext().get(CONTROLLER_METHOD_STACK_DEPTH); - Integer depth = stackDepth == null ? 0 : Integer.parseInt(stackDepth.toString()); + StackDepth stackDepth = (StackDepth)ContextManager.getRuntimeContext().get(CONTROLLER_METHOD_STACK_DEPTH); - if (depth == 0) { + if (stackDepth == null) { ContextCarrier contextCarrier = new ContextCarrier(); CarrierItem next = contextCarrier.items(); while (next.hasNext()) { @@ -95,24 +94,27 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr Tags.HTTP.METHOD.set(span, request.getMethod()); span.setComponent(ComponentsDefine.SPRING_MVC_ANNOTATION); SpanLayer.asHttp(span); + + stackDepth = new StackDepth(); + ContextManager.getRuntimeContext().put(CONTROLLER_METHOD_STACK_DEPTH, stackDepth); } else { AbstractSpan span = ContextManager.createLocalSpan(buildOperationName(objInst, method)); span.setComponent(ComponentsDefine.SPRING_MVC_ANNOTATION); } - ContextManager.getRuntimeContext().put(CONTROLLER_METHOD_STACK_DEPTH, depth++); + stackDepth.increment(); } } - private String buildOperationName(Object invoker, Method method){ + private String buildOperationName(Object invoker, Method method) { StringBuilder operationName = new StringBuilder(invoker.getClass().getName()) .append(".").append(method.getName()).append("("); for (Class type : method.getParameterTypes()) { operationName.append(type.getName()).append(","); } - if (method.getParameterTypes().length > 0){ + if (method.getParameterTypes().length > 0) { operationName = operationName.deleteCharAt(operationName.length() - 1); } @@ -134,15 +136,14 @@ public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allA HttpServletRequest request = (HttpServletRequest)ContextManager.getRuntimeContext().get(REQUEST_KEY_IN_RUNTIME_CONTEXT); if (request != null) { - Object stackDepth = ContextManager.getRuntimeContext().get(CONTROLLER_METHOD_STACK_DEPTH); + StackDepth stackDepth = (StackDepth)ContextManager.getRuntimeContext().get(CONTROLLER_METHOD_STACK_DEPTH); if (stackDepth == null) { - throw new IllegalMethodStackDepthException(stackDepth); + throw new IllegalMethodStackDepthException(); } - Integer depth = Integer.parseInt(String.valueOf(stackDepth)); AbstractSpan span = ContextManager.activeSpan(); - if (depth == 0) { + if (stackDepth.depth() == 0) { HttpServletResponse response = (HttpServletResponse)ContextManager.getRuntimeContext().get(RESPONSE_KEY_IN_RUNTIME_CONTEXT); if (response == null) { throw new ServletResponseNotFoundException(); @@ -158,7 +159,7 @@ public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allA } ContextManager.stopSpan(); - ContextManager.getRuntimeContext().put(CONTROLLER_METHOD_STACK_DEPTH, depth--); + stackDepth.decrement(); } return ret; diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/StackDepth.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/StackDepth.java new file mode 100644 index 000000000000..118df51f47f1 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/StackDepth.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.skywalking.apm.plugin.spring.mvc.commons.interceptor; + +public class StackDepth { + + private int stackDepth; + + public StackDepth() { + this.stackDepth = 0; + } + + public int depth() { + return this.stackDepth; + } + + public StackDepth increment() { + this.stackDepth++; + return this; + } + + public StackDepth decrement() { + this.stackDepth--; + return this; + } +} diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/webflux-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/webflux/v5/AbstractMethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/webflux-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/webflux/v5/AbstractMethodInterceptor.java index 415d9df54d8b..d67e775032ca 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/webflux-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/webflux/v5/AbstractMethodInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/webflux-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/webflux/v5/AbstractMethodInterceptor.java @@ -30,6 +30,7 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; import org.apache.skywalking.apm.plugin.spring.mvc.commons.EnhanceRequireObjectCache; +import org.apache.skywalking.apm.plugin.spring.mvc.commons.interceptor.StackDepth; import static org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.CONTROLLER_METHOD_STACK_DEPTH; import static org.apache.skywalking.apm.plugin.spring.mvc.commons.Constants.FORWARD_REQUEST_FLAG; @@ -67,9 +68,9 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr HttpRequest request = (HttpRequest)ContextManager.getRuntimeContext().get(WEBFLUX_REQUEST_KEY); if (request != null) { - Object stackDepth = ContextManager.getRuntimeContext().get(CONTROLLER_METHOD_STACK_DEPTH); - Integer depth = stackDepth == null ? 0 : Integer.parseInt(stackDepth.toString()); - if (depth == 0) { + StackDepth stackDepth = (StackDepth)ContextManager.getRuntimeContext().get(CONTROLLER_METHOD_STACK_DEPTH); + + if (stackDepth == null) { ContextCarrier contextCarrier = new ContextCarrier(); CarrierItem next = contextCarrier.items(); while (next.hasNext()) { @@ -82,12 +83,14 @@ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allAr Tags.HTTP.METHOD.set(span, request.method().name()); span.setComponent(ComponentsDefine.SPRING_MVC_ANNOTATION); SpanLayer.asHttp(span); + + stackDepth = new StackDepth(); } else { AbstractSpan span = ContextManager.createLocalSpan(objInst.getClass().getName() + "/" + method.getName()); span.setComponent(ComponentsDefine.SPRING_MVC_ANNOTATION); } - ContextManager.getRuntimeContext().put(CONTROLLER_METHOD_STACK_DEPTH, depth++); + stackDepth.increment(); } } From 0782642c2db55e428cd899aa4ed8e3ad3676a383 Mon Sep 17 00:00:00 2001 From: ascrutae Date: Mon, 29 Apr 2019 08:37:27 +0800 Subject: [PATCH 3/4] Fix bug --- .../mvc/commons/interceptor/AbstractMethodInterceptor.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/AbstractMethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/AbstractMethodInterceptor.java index c6fbc0df6651..495ece0bbeaf 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/AbstractMethodInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/AbstractMethodInterceptor.java @@ -139,6 +139,8 @@ public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allA StackDepth stackDepth = (StackDepth)ContextManager.getRuntimeContext().get(CONTROLLER_METHOD_STACK_DEPTH); if (stackDepth == null) { throw new IllegalMethodStackDepthException(); + } else{ + stackDepth.decrement(); } AbstractSpan span = ContextManager.activeSpan(); @@ -159,7 +161,6 @@ public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allA } ContextManager.stopSpan(); - stackDepth.decrement(); } return ret; From 6f7e27bfd01d90003bda266fc8389ca0286210a8 Mon Sep 17 00:00:00 2001 From: ascrutae Date: Mon, 29 Apr 2019 08:44:55 +0800 Subject: [PATCH 4/4] Fix CI failed --- .../mvc/commons/interceptor/AbstractMethodInterceptor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/AbstractMethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/AbstractMethodInterceptor.java index 495ece0bbeaf..1a98e7b8cc1c 100644 --- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/AbstractMethodInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/interceptor/AbstractMethodInterceptor.java @@ -139,7 +139,7 @@ public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allA StackDepth stackDepth = (StackDepth)ContextManager.getRuntimeContext().get(CONTROLLER_METHOD_STACK_DEPTH); if (stackDepth == null) { throw new IllegalMethodStackDepthException(); - } else{ + } else { stackDepth.decrement(); }