-
Notifications
You must be signed in to change notification settings - Fork 622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support across thread tracing for SOFA-RPC #675
Merged
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7358873
Add across thread solution for sofarpc
OrezzerO 7d9e11d
Change CHANGES.md
OrezzerO 9393f88
Add sofarpc test casees
OrezzerO 0840582
reformat code
OrezzerO 40d7b27
Simplify sofarpc-scenario test case
OrezzerO 8680f82
Add callback wrapper to ensure contextSnapshot is one time used.
OrezzerO bb384e4
Apply suggestions from code review
wu-sheng 5cccf8f
Add lombok annotations for getter methods.
OrezzerO 05b4357
Apply suggestions from code review
wu-sheng 49ffb3f
Add lombok import statement
OrezzerO File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
...in/src/main/java/org/apache/skywalking/apm/plugin/sofarpc/SofaBoltCallbackActivation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* 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.sofarpc; | ||
|
||
import net.bytebuddy.description.method.MethodDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; | ||
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.match.ClassMatch; | ||
import org.apache.skywalking.apm.agent.core.plugin.match.HierarchyMatch; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.any; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
||
public class SofaBoltCallbackActivation extends ClassInstanceMethodsEnhancePluginDefine { | ||
|
||
private static final String ENHANCE_CLASS = "com.alipay.remoting.InvokeCallback"; | ||
private static final String INIT_METHOD_INTERCEPTOR = "org.apache.skywalking.apm.plugin.sofarpc.SofaBoltCallbackConstructInterceptor"; | ||
private static final String CALL_METHOD_INTERCEPTOR = "org.apache.skywalking.apm.plugin.sofarpc.SofaBoltCallbackInvokeInterceptor"; | ||
private static final String EXCEPTION_METHOD_INTERCEPTOR = "org.apache.skywalking.apm.plugin.sofarpc.SofaBoltCallbackExceptionInterceptor"; | ||
private static final String RESPONSE_METHOD_NAME = "onResponse"; | ||
private static final String EXCEPTION_METHOD_NAME = "onException"; | ||
|
||
@Override | ||
protected ClassMatch enhanceClass() { | ||
return HierarchyMatch.byHierarchyMatch(ENHANCE_CLASS); | ||
} | ||
|
||
@Override | ||
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { | ||
return new ConstructorInterceptPoint[] { | ||
new ConstructorInterceptPoint() { | ||
@Override | ||
public ElementMatcher<MethodDescription> getConstructorMatcher() { | ||
return any(); | ||
} | ||
|
||
@Override | ||
public String getConstructorInterceptor() { | ||
return INIT_METHOD_INTERCEPTOR; | ||
} | ||
} | ||
}; | ||
|
||
} | ||
|
||
@Override | ||
public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { | ||
return new InstanceMethodsInterceptPoint[] { | ||
new InstanceMethodsInterceptPoint() { | ||
@Override | ||
public ElementMatcher<MethodDescription> getMethodsMatcher() { | ||
return named(RESPONSE_METHOD_NAME).and(takesArguments(1)); | ||
} | ||
|
||
@Override | ||
public String getMethodsInterceptor() { | ||
return CALL_METHOD_INTERCEPTOR; | ||
} | ||
|
||
@Override | ||
public boolean isOverrideArgs() { | ||
return false; | ||
} | ||
}, | ||
new InstanceMethodsInterceptPoint() { | ||
@Override | ||
public ElementMatcher<MethodDescription> getMethodsMatcher() { | ||
return named(EXCEPTION_METHOD_NAME).and(takesArguments(1)); | ||
} | ||
|
||
@Override | ||
public String getMethodsInterceptor() { | ||
return EXCEPTION_METHOD_INTERCEPTOR; | ||
} | ||
|
||
@Override | ||
public boolean isOverrideArgs() { | ||
return false; | ||
} | ||
} | ||
}; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...n/java/org/apache/skywalking/apm/plugin/sofarpc/SofaBoltCallbackConstructInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* 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.sofarpc; | ||
|
||
import org.apache.skywalking.apm.agent.core.context.ContextManager; | ||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; | ||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor; | ||
|
||
public class SofaBoltCallbackConstructInterceptor implements InstanceConstructorInterceptor { | ||
@Override | ||
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) { | ||
if (ContextManager.isActive()) { | ||
objInst.setSkyWalkingDynamicField(ContextManager.capture()); | ||
} | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...n/java/org/apache/skywalking/apm/plugin/sofarpc/SofaBoltCallbackExceptionInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* 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.sofarpc; | ||
|
||
import java.lang.reflect.Method; | ||
import org.apache.skywalking.apm.agent.core.context.ContextManager; | ||
import org.apache.skywalking.apm.agent.core.context.ContextSnapshot; | ||
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; | ||
|
||
public class SofaBoltCallbackExceptionInterceptor implements InstanceMethodsAroundInterceptor { | ||
@Override | ||
public void beforeMethod(EnhancedInstance objInst, | ||
Method method, | ||
Object[] allArguments, | ||
Class<?>[] argumentsTypes, | ||
MethodInterceptResult result) throws Throwable { | ||
ContextManager.createLocalSpan("Thread/" + objInst.getClass().getName() + "/" + method.getName()); | ||
ContextSnapshot cachedObjects = (ContextSnapshot) objInst.getSkyWalkingDynamicField(); | ||
if (cachedObjects != null) { | ||
ContextManager.continued(cachedObjects); | ||
} | ||
if (allArguments[0] instanceof Throwable) { | ||
AbstractSpan abstractSpan = ContextManager.activeSpan(); | ||
if (abstractSpan != null) { | ||
abstractSpan.log((Throwable) allArguments[0]); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public Object afterMethod(EnhancedInstance objInst, | ||
Method method, | ||
Object[] allArguments, | ||
Class<?>[] argumentsTypes, | ||
Object ret) throws Throwable { | ||
ContextManager.stopSpan(); | ||
// clear ContextSnapshot | ||
objInst.setSkyWalkingDynamicField(null); | ||
return ret; | ||
} | ||
|
||
@Override | ||
public void handleMethodException(EnhancedInstance objInst, | ||
Method method, | ||
Object[] allArguments, | ||
Class<?>[] argumentsTypes, | ||
Throwable t) { | ||
ContextManager.activeSpan().log(t); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...main/java/org/apache/skywalking/apm/plugin/sofarpc/SofaBoltCallbackInvokeInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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.sofarpc; | ||
|
||
import java.lang.reflect.Method; | ||
import org.apache.skywalking.apm.agent.core.context.ContextManager; | ||
import org.apache.skywalking.apm.agent.core.context.ContextSnapshot; | ||
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; | ||
|
||
public class SofaBoltCallbackInvokeInterceptor implements InstanceMethodsAroundInterceptor { | ||
@Override | ||
public void beforeMethod(EnhancedInstance objInst, | ||
Method method, | ||
Object[] allArguments, | ||
Class<?>[] argumentsTypes, | ||
MethodInterceptResult result) { | ||
ContextManager.createLocalSpan("Thread/" + objInst.getClass().getName() + "/" + method.getName()); | ||
ContextSnapshot cachedObjects = (ContextSnapshot) objInst.getSkyWalkingDynamicField(); | ||
if (cachedObjects != null) { | ||
ContextManager.continued(cachedObjects); | ||
} | ||
} | ||
|
||
@Override | ||
public Object afterMethod(EnhancedInstance objInst, | ||
Method method, | ||
Object[] allArguments, | ||
Class<?>[] argumentsTypes, | ||
Object ret) { | ||
ContextManager.stopSpan(); | ||
// clear ContextSnapshot | ||
objInst.setSkyWalkingDynamicField(null); | ||
return ret; | ||
} | ||
|
||
@Override | ||
public void handleMethodException(EnhancedInstance objInst, | ||
Method method, | ||
Object[] allArguments, | ||
Class<?>[] argumentsTypes, | ||
Throwable t) { | ||
ContextManager.activeSpan().log(t); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a risk of using this field to propagate tracing context. The input(inherit InvokeCallback) is a object, which could be reused by many callback invocations.
If SOFA RPC indicates this callback service should never be shared with different RPCs? Especially if this callback could be statusless.
An alternative solution is, you could use a wrapper, a plugin level InvokeCallback implementation to wrap the original one. We could make sure, that is one time used, and add a field(not need to be dynamic field) to hold the context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the context of SOFARPC, InvokeCallback is a wrapper. Users directly use the SofaResponseCallback class. Each SOFARPC callback request corresponds uniquely to an InvokeCallback.
However, in the context of SOFA BOLT (BOLT being a wrapper for Netty which SOFARPC relies on), InvokeCallback may be reused.
It's a good idea to create a plugin level wrapper, but I am afraid I will introduce an error because of cannot dealing with classloader correctly.
I plan to create a class
InvokeCallbackWrapper
in plugin code. How to make it loaded by business classloader?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All plugin interceptor relative codes are loaded in your target class loader, otherwise, all existing logic will fail. I think in this case, users of SOFA would not be aware of the wrapper, as it is only running internally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Classloader doesn't need to be worried, AFAIK.