Support @Trace, @Tag and @Tags work for static methods (#6685)
This commit is contained in:
parent
647e234d89
commit
411bcf230d
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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<String, Object> 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<String, Object> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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<String, Object> 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<String, Object> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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<MethodDescription> 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)),
|
||||
|
|
|
|||
|
|
@ -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<String, Object> 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<String, Object> 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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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 <code>org.apache.skywalking.apm.toolkit.trace.annotation.Trace</code>
|
||||
* by <code>TraceAnnotationMethodInterceptor</code>.
|
||||
*/
|
||||
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<MethodDescription> 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);
|
||||
|
|
|
|||
|
|
@ -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
|
||||
* <code>org.apache.skywalking.apm.toolkit.trace.annotation.Trace.operationName</code>. 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<String, Object> 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<String, Object> 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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
|
|
|
|||
Loading…
Reference in New Issue