diff --git a/CHANGES.md b/CHANGES.md index b4b12e4bc..c1e5075d8 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -32,6 +32,7 @@ Release Notes. * Fix springmvc reactive api can't collect HTTP statusCode. * Fix bug that asynchttpclient plugin does not record the response status code. * Fix spanLayer is null in optional plugin(gateway-2.0.x-plugin gateway-2.1.x-plugin). +* Support @Trace, @Tag and @Tags work for static methods. #### OAP-Backend * Allow user-defined `JAVA_OPTS` in the startup script. diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/BaseTagAnnotationInterceptor.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/BaseTagAnnotationInterceptor.java new file mode 100644 index 000000000..fa123a06a --- /dev/null +++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/BaseTagAnnotationInterceptor.java @@ -0,0 +1,78 @@ +/* + * 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.toolkit.activation.trace; + +import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; +import org.apache.skywalking.apm.agent.core.util.CustomizeExpression; +import org.apache.skywalking.apm.toolkit.activation.util.TagUtil; +import org.apache.skywalking.apm.toolkit.trace.Tag; +import org.apache.skywalking.apm.toolkit.trace.Tags; + +import java.lang.reflect.Method; +import java.util.Map; + +public class BaseTagAnnotationInterceptor { + void beforeMethod(Method method, Object[] allArguments) { + if (!ContextManager.isActive()) { + return; + } + final AbstractSpan activeSpan = ContextManager.activeSpan(); + final Map context = CustomizeExpression.evaluationContext(allArguments); + + final Tags tags = method.getAnnotation(Tags.class); + if (tags != null && tags.value().length > 0) { + for (final Tag tag : tags.value()) { + if (!TagUtil.isReturnTag(tag.value())) { + TagUtil.tagParamsSpan(activeSpan, context, tag); + } + } + } + final Tag tag = method.getAnnotation(Tag.class); + if (tag != null && !TagUtil.isReturnTag(tag.value())) { + TagUtil.tagParamsSpan(activeSpan, context, tag); + } + } + + void afterMethod(Method method, Object ret) { + if (ret == null || !ContextManager.isActive()) { + return; + } + final AbstractSpan localSpan = ContextManager.activeSpan(); + final Map context = CustomizeExpression.evaluationReturnContext(ret); + final Tags tags = method.getAnnotation(Tags.class); + if (tags != null && tags.value().length > 0) { + for (final Tag tag : tags.value()) { + if (TagUtil.isReturnTag(tag.value())) { + TagUtil.tagReturnSpanSpan(localSpan, context, tag); + } + } + } + final Tag tag = method.getAnnotation(Tag.class); + if (tag != null && TagUtil.isReturnTag(tag.value())) { + TagUtil.tagReturnSpanSpan(localSpan, context, tag); + } + } + + void handleMethodException(Throwable t) { + if (ContextManager.isActive()) { + ContextManager.activeSpan().log(t); + } + } +} diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/BaseTraceAnnotationInterceptor.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/BaseTraceAnnotationInterceptor.java new file mode 100644 index 000000000..08f47e510 --- /dev/null +++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/BaseTraceAnnotationInterceptor.java @@ -0,0 +1,89 @@ +/* + * 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.toolkit.activation.trace; + +import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; +import org.apache.skywalking.apm.agent.core.util.CustomizeExpression; +import org.apache.skywalking.apm.agent.core.util.MethodUtil; +import org.apache.skywalking.apm.toolkit.activation.ToolkitPluginConfig; +import org.apache.skywalking.apm.toolkit.activation.util.TagUtil; +import org.apache.skywalking.apm.toolkit.trace.Tag; +import org.apache.skywalking.apm.toolkit.trace.Tags; +import org.apache.skywalking.apm.toolkit.trace.Trace; + +import java.lang.reflect.Method; +import java.util.Map; + +public class BaseTraceAnnotationInterceptor { + void beforeMethod(Method method, Object[] allArguments) { + Trace trace = method.getAnnotation(Trace.class); + String operationName = trace.operationName(); + if (operationName.length() == 0 || ToolkitPluginConfig.Plugin.Toolkit.USE_QUALIFIED_NAME_AS_OPERATION_NAME) { + operationName = MethodUtil.generateOperationName(method); + } + + final AbstractSpan localSpan = ContextManager.createLocalSpan(operationName); + + final Map context = CustomizeExpression.evaluationContext(allArguments); + + final org.apache.skywalking.apm.toolkit.trace.Tags tags = method.getAnnotation(Tags.class); + if (tags != null && tags.value().length > 0) { + for (final Tag tag : tags.value()) { + if (!TagUtil.isReturnTag(tag.value())) { + TagUtil.tagParamsSpan(localSpan, context, tag); + } + } + } + final Tag tag = method.getAnnotation(Tag.class); + if (tag != null && !TagUtil.isReturnTag(tag.value())) { + TagUtil.tagParamsSpan(localSpan, context, tag); + } + } + + void afterMethod(Method method, Object ret) { + try { + if (ret == null) { + return; + } + final AbstractSpan localSpan = ContextManager.activeSpan(); + final Map context = CustomizeExpression.evaluationReturnContext(ret); + final Tags tags = method.getAnnotation(Tags.class); + if (tags != null && tags.value().length > 0) { + for (final Tag tag : tags.value()) { + if (TagUtil.isReturnTag(tag.value())) { + TagUtil.tagReturnSpanSpan(localSpan, context, tag); + } + } + } + final Tag tag = method.getAnnotation(Tag.class); + if (tag != null && TagUtil.isReturnTag(tag.value())) { + TagUtil.tagReturnSpanSpan(localSpan, context, tag); + } + } finally { + ContextManager.stopSpan(); + } + } + + void handleMethodException(Throwable t) { + if (ContextManager.isActive()) { + ContextManager.activeSpan().log(t); + } + } +} diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/TagAnnotationActivation.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/TagAnnotationActivation.java index fcab17bf2..b6b952ae9 100644 --- a/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/TagAnnotationActivation.java +++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/TagAnnotationActivation.java @@ -23,7 +23,8 @@ import net.bytebuddy.matcher.ElementMatcher; import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; import org.apache.skywalking.apm.agent.core.plugin.interceptor.DeclaredInstanceMethodsInterceptPoint; import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; -import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.StaticMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassEnhancePluginDefine; import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; import static net.bytebuddy.matcher.ElementMatchers.isAnnotatedWith; @@ -36,9 +37,10 @@ import static org.apache.skywalking.apm.agent.core.plugin.match.logical.LogicalM /** * Intercepts all methods annotated with {@link org.apache.skywalking.apm.toolkit.trace.Tag} */ -public class TagAnnotationActivation extends ClassInstanceMethodsEnhancePluginDefine { +public class TagAnnotationActivation extends ClassEnhancePluginDefine { public static final String TAG_ANNOTATION_METHOD_INTERCEPTOR = "org.apache.skywalking.apm.toolkit.activation.trace.TagAnnotationMethodInterceptor"; + public static final String TAG_ANNOTATION_STATIC_METHOD_INTERCEPTOR = "org.apache.skywalking.apm.toolkit.activation.trace.TagAnnotationStaticMethodInterceptor"; public static final String TAG_ANNOTATION = "org.apache.skywalking.apm.toolkit.trace.Tag"; public static final String TAGS_ANNOTATION = "org.apache.skywalking.apm.toolkit.trace.Tags"; public static final String TRACE_ANNOTATION = "org.apache.skywalking.apm.toolkit.trace.Trace"; @@ -70,6 +72,28 @@ public class TagAnnotationActivation extends ClassInstanceMethodsEnhancePluginDe }; } + @Override + public StaticMethodsInterceptPoint[] getStaticMethodsInterceptPoints() { + return new StaticMethodsInterceptPoint[]{ + new StaticMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return isAnnotatedWith(named(TAG_ANNOTATION)); + } + + @Override + public String getMethodsInterceptor() { + return TAG_ANNOTATION_STATIC_METHOD_INTERCEPTOR; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + } + }; + } + @Override protected ClassMatch enhanceClass() { return and(not(byMethodAnnotationMatch(TRACE_ANNOTATION)), diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/TagAnnotationMethodInterceptor.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/TagAnnotationMethodInterceptor.java index 50ef2197b..07b025b77 100644 --- a/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/TagAnnotationMethodInterceptor.java +++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/TagAnnotationMethodInterceptor.java @@ -19,77 +19,32 @@ package org.apache.skywalking.apm.toolkit.activation.trace; import java.lang.reflect.Method; -import java.util.Map; -import org.apache.skywalking.apm.agent.core.context.ContextManager; -import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; -import org.apache.skywalking.apm.toolkit.activation.util.TagUtil; 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.agent.core.util.CustomizeExpression; -import org.apache.skywalking.apm.toolkit.trace.Tag; -import org.apache.skywalking.apm.toolkit.trace.Tags; -public class TagAnnotationMethodInterceptor implements InstanceMethodsAroundInterceptor { +public class TagAnnotationMethodInterceptor extends BaseTagAnnotationInterceptor implements InstanceMethodsAroundInterceptor { @Override public void beforeMethod(final EnhancedInstance objInst, final Method method, final Object[] allArguments, - final Class[] argumentsTypes, final MethodInterceptResult result) { - - if (!ContextManager.isActive()) { - return; - } - - final AbstractSpan activeSpan = ContextManager.activeSpan(); - final Map context = CustomizeExpression.evaluationContext(allArguments); - - final Tags tags = method.getAnnotation(Tags.class); - if (tags != null && tags.value().length > 0) { - for (final Tag tag : tags.value()) { - if (!TagUtil.isReturnTag(tag.value())) { - TagUtil.tagParamsSpan(activeSpan, context, tag); - } - } - } - - final Tag tag = method.getAnnotation(Tag.class); - if (tag != null && !TagUtil.isReturnTag(tag.value())) { - TagUtil.tagParamsSpan(activeSpan, context, tag); - } + final Class[] argumentsTypes, final MethodInterceptResult result) { + super.beforeMethod(method, allArguments); } @Override public Object afterMethod( - final EnhancedInstance objInst, - final Method method, - final Object[] allArguments, - final Class[] argumentsTypes, - final Object ret) { - if (ret == null || !ContextManager.isActive()) { - return ret; - } - final AbstractSpan localSpan = ContextManager.activeSpan(); - final Map context = CustomizeExpression.evaluationReturnContext(ret); - final Tags tags = method.getAnnotation(Tags.class); - if (tags != null && tags.value().length > 0) { - for (final Tag tag : tags.value()) { - if (TagUtil.isReturnTag(tag.value())) { - TagUtil.tagReturnSpanSpan(localSpan, context, tag); - } - } - } - final Tag tag = method.getAnnotation(Tag.class); - if (tag != null && TagUtil.isReturnTag(tag.value())) { - TagUtil.tagReturnSpanSpan(localSpan, context, tag); - } + final EnhancedInstance objInst, + final Method method, + final Object[] allArguments, + final Class[] argumentsTypes, + final Object ret) { + super.afterMethod(method, ret); return ret; } @Override public void handleMethodException(final EnhancedInstance objInst, final Method method, final Object[] allArguments, - final Class[] argumentsTypes, final Throwable t) { - if (ContextManager.isActive()) { - ContextManager.activeSpan().log(t); - } + final Class[] argumentsTypes, final Throwable t) { + super.handleMethodException(t); } } diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/TagAnnotationStaticMethodInterceptor.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/TagAnnotationStaticMethodInterceptor.java new file mode 100644 index 000000000..97640670b --- /dev/null +++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/TagAnnotationStaticMethodInterceptor.java @@ -0,0 +1,45 @@ +/* + * 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.toolkit.activation.trace; + +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.StaticMethodsAroundInterceptor; + +import java.lang.reflect.Method; + +public class TagAnnotationStaticMethodInterceptor extends BaseTagAnnotationInterceptor implements StaticMethodsAroundInterceptor { + @Override + public void beforeMethod(Class clazz, Method method, Object[] allArguments, Class[] parameterTypes, + MethodInterceptResult result) { + super.beforeMethod(method, allArguments); + } + + @Override + public Object afterMethod(Class clazz, Method method, Object[] allArguments, Class[] parameterTypes, + Object ret) { + super.afterMethod(method, ret); + return ret; + } + + @Override + public void handleMethodException(Class clazz, Method method, Object[] allArguments, Class[] parameterTypes, + Throwable t) { + super.handleMethodException(t); + } +} diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/TraceAnnotationActivation.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/TraceAnnotationActivation.java index fa70702d8..dc426c5bf 100644 --- a/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/TraceAnnotationActivation.java +++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/TraceAnnotationActivation.java @@ -22,7 +22,8 @@ import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.matcher.ElementMatcher; import org.apache.skywalking.apm.agent.core.plugin.interceptor.DeclaredInstanceMethodsInterceptPoint; import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; -import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.StaticMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassEnhancePluginDefine; import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; import org.apache.skywalking.apm.agent.core.plugin.match.MethodAnnotationMatch; import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; @@ -34,9 +35,10 @@ import static net.bytebuddy.matcher.ElementMatchers.named; * {@link TraceAnnotationActivation} enhance all method that annotated with org.apache.skywalking.apm.toolkit.trace.annotation.Trace * by TraceAnnotationMethodInterceptor. */ -public class TraceAnnotationActivation extends ClassInstanceMethodsEnhancePluginDefine { +public class TraceAnnotationActivation extends ClassEnhancePluginDefine { public static final String TRACE_ANNOTATION_METHOD_INTERCEPTOR = "org.apache.skywalking.apm.toolkit.activation.trace.TraceAnnotationMethodInterceptor"; + public static final String TRACE_ANNOTATION_STATIC_METHOD_INTERCEPTOR = "org.apache.skywalking.apm.toolkit.activation.trace.TraceAnnotationStaticMethodInterceptor"; public static final String TRACE_ANNOTATION = "org.apache.skywalking.apm.toolkit.trace.Trace"; @Override @@ -66,6 +68,28 @@ public class TraceAnnotationActivation extends ClassInstanceMethodsEnhancePlugin }; } + @Override + public StaticMethodsInterceptPoint[] getStaticMethodsInterceptPoints() { + return new StaticMethodsInterceptPoint[]{ + new StaticMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return isAnnotatedWith(named(TRACE_ANNOTATION)); + } + + @Override + public String getMethodsInterceptor() { + return TRACE_ANNOTATION_STATIC_METHOD_INTERCEPTOR; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + } + }; + } + @Override protected ClassMatch enhanceClass() { return MethodAnnotationMatch.byMethodAnnotationMatch(TRACE_ANNOTATION); diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/TraceAnnotationMethodInterceptor.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/TraceAnnotationMethodInterceptor.java index e11a1f72c..ffc51f44b 100644 --- a/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/TraceAnnotationMethodInterceptor.java +++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/TraceAnnotationMethodInterceptor.java @@ -19,83 +19,33 @@ package org.apache.skywalking.apm.toolkit.activation.trace; import java.lang.reflect.Method; -import java.util.Map; -import org.apache.skywalking.apm.agent.core.context.ContextManager; -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.agent.core.util.CustomizeExpression; -import org.apache.skywalking.apm.agent.core.util.MethodUtil; -import org.apache.skywalking.apm.toolkit.activation.ToolkitPluginConfig; -import org.apache.skywalking.apm.toolkit.activation.util.TagUtil; -import org.apache.skywalking.apm.toolkit.trace.Tag; -import org.apache.skywalking.apm.toolkit.trace.Tags; -import org.apache.skywalking.apm.toolkit.trace.Trace; /** * {@link TraceAnnotationMethodInterceptor} create a local span and set the operation name which fetch from * org.apache.skywalking.apm.toolkit.trace.annotation.Trace.operationName. if the fetch value is blank * string, and the operation name will be the method name. */ -public class TraceAnnotationMethodInterceptor implements InstanceMethodsAroundInterceptor { +public class TraceAnnotationMethodInterceptor extends BaseTraceAnnotationInterceptor implements InstanceMethodsAroundInterceptor { @Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, MethodInterceptResult result) throws Throwable { - Trace trace = method.getAnnotation(Trace.class); - String operationName = trace.operationName(); - if (operationName.length() == 0 || ToolkitPluginConfig.Plugin.Toolkit.USE_QUALIFIED_NAME_AS_OPERATION_NAME) { - operationName = MethodUtil.generateOperationName(method); - } - - final AbstractSpan localSpan = ContextManager.createLocalSpan(operationName); - - final Map context = CustomizeExpression.evaluationContext(allArguments); - - final Tags tags = method.getAnnotation(Tags.class); - if (tags != null && tags.value().length > 0) { - for (final Tag tag : tags.value()) { - if (!TagUtil.isReturnTag(tag.value())) { - TagUtil.tagParamsSpan(localSpan, context, tag); - } - } - } - final Tag tag = method.getAnnotation(Tag.class); - if (tag != null && !TagUtil.isReturnTag(tag.value())) { - TagUtil.tagParamsSpan(localSpan, context, tag); - } + super.beforeMethod(method, allArguments); } @Override public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, Object ret) throws Throwable { - try { - if (ret == null) { - return ret; - } - final AbstractSpan localSpan = ContextManager.activeSpan(); - final Map context = CustomizeExpression.evaluationReturnContext(ret); - final Tags tags = method.getAnnotation(Tags.class); - if (tags != null && tags.value().length > 0) { - for (final Tag tag : tags.value()) { - if (TagUtil.isReturnTag(tag.value())) { - TagUtil.tagReturnSpanSpan(localSpan, context, tag); - } - } - } - final Tag tag = method.getAnnotation(Tag.class); - if (tag != null && TagUtil.isReturnTag(tag.value())) { - TagUtil.tagReturnSpanSpan(localSpan, context, tag); - } - } finally { - ContextManager.stopSpan(); - } + super.afterMethod(method, ret); return ret; } @Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, Throwable t) { - ContextManager.activeSpan().log(t); + super.handleMethodException(t); } } diff --git a/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/TraceAnnotationStaticMethodInterceptor.java b/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/TraceAnnotationStaticMethodInterceptor.java new file mode 100644 index 000000000..f08c61b7c --- /dev/null +++ b/apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/TraceAnnotationStaticMethodInterceptor.java @@ -0,0 +1,45 @@ +/* + * 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.toolkit.activation.trace; + +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.StaticMethodsAroundInterceptor; + +import java.lang.reflect.Method; + +public class TraceAnnotationStaticMethodInterceptor extends BaseTraceAnnotationInterceptor implements StaticMethodsAroundInterceptor { + @Override + public void beforeMethod(Class clazz, Method method, Object[] allArguments, Class[] parameterTypes, + MethodInterceptResult result) { + super.beforeMethod(method, allArguments); + } + + @Override + public Object afterMethod(Class clazz, Method method, Object[] allArguments, Class[] parameterTypes, + Object ret) { + super.afterMethod(method, ret); + return ret; + } + + @Override + public void handleMethodException(Class clazz, Method method, Object[] allArguments, Class[] parameterTypes, + Throwable t) { + super.handleMethodException(t); + } +} diff --git a/test/plugin/scenarios/apm-toolkit-trace-scenario/config/expectedData.yaml b/test/plugin/scenarios/apm-toolkit-trace-scenario/config/expectedData.yaml index 2bcc1cd19..fc5dfe133 100644 --- a/test/plugin/scenarios/apm-toolkit-trace-scenario/config/expectedData.yaml +++ b/test/plugin/scenarios/apm-toolkit-trace-scenario/config/expectedData.yaml @@ -130,6 +130,21 @@ segmentItems: tags: - {key: username, value: zhangsan} skipAnalysis: 'true' + - operationName: test.apache.skywalking.apm.testcase.toolkit.controller.TestService.testStatic(java.lang.String,java.lang.Integer) + parentSpanId: 0 + spanId: 10 + spanLayer: Unknown + startTime: nq 0 + endTime: nq 0 + componentId: 0 + isError: false + spanType: Local + peer: '' + tags: + - {key: p1, value: lisi} + - {key: p2, value: '16'} + - {key: username, value: lisi} + skipAnalysis: 'true' - operationName: /case/tool-kit parentSpanId: -1 spanId: 0 diff --git a/test/plugin/scenarios/apm-toolkit-trace-scenario/src/main/java/test/apache/skywalking/apm/testcase/toolkit/controller/TestController.java b/test/plugin/scenarios/apm-toolkit-trace-scenario/src/main/java/test/apache/skywalking/apm/testcase/toolkit/controller/TestController.java index 1755eea08..8f3d221b2 100644 --- a/test/plugin/scenarios/apm-toolkit-trace-scenario/src/main/java/test/apache/skywalking/apm/testcase/toolkit/controller/TestController.java +++ b/test/plugin/scenarios/apm-toolkit-trace-scenario/src/main/java/test/apache/skywalking/apm/testcase/toolkit/controller/TestController.java @@ -58,6 +58,7 @@ public class TestController { testService.testErrorThrowable(); testService.testTagAnnotation("testTagAnnotationParam1", "testTagAnnotationParam2"); testService.testTagAnnotationReturnInfo("zhangsan", 15); + TestService.testStatic("lisi", 16); TraceContext.putCorrelation(CORRELATION_CONTEXT_KEY, CORRELATION_CONTEXT_VALUE); ActiveSpan.tag("traceID", TraceContext.traceId()); ActiveSpan.tag("segmentID", TraceContext.segmentId()); diff --git a/test/plugin/scenarios/apm-toolkit-trace-scenario/src/main/java/test/apache/skywalking/apm/testcase/toolkit/controller/TestService.java b/test/plugin/scenarios/apm-toolkit-trace-scenario/src/main/java/test/apache/skywalking/apm/testcase/toolkit/controller/TestService.java index f37ac97a6..ac21326d2 100644 --- a/test/plugin/scenarios/apm-toolkit-trace-scenario/src/main/java/test/apache/skywalking/apm/testcase/toolkit/controller/TestService.java +++ b/test/plugin/scenarios/apm-toolkit-trace-scenario/src/main/java/test/apache/skywalking/apm/testcase/toolkit/controller/TestService.java @@ -41,6 +41,14 @@ public class TestService { return thread; }); + @Trace + @Tag(key = "p1", value = "arg[0]") + @Tag(key = "p2", value = "arg[1]") + @Tag(key = "username", value = "returnedObj.username") + public static User testStatic(final String username, final Integer age) { + return new User(username, age); + } + @Trace public void testTag() { ActiveSpan.tag("key", "value");