当前位置:网站首页>Skywalking implements cross thread trace delivery
Skywalking implements cross thread trace delivery
2022-06-25 03:29:00 【Talk about 1974】
List of articles
Preface
When asynchronous multithreading is used in the process , If nothing is done ,SkyWalking Trace the execution link trace Information is bound to be interrupted . Generally speaking, it is a rigid requirement to ensure the integrity of the execution link information , At this time, in order to realize trace Cross thread transmission of information , You need to use SkyWalking Asynchronous task wrapper class
1. SkyWalking Client asynchronous task wrapper class
1.1 Use of cross thread wrapper classes
SkyWalking Of Java The client provides an asynchronous task wrapper class for multithreading trace Cross thread delivery of , At present, there are several implementations as follows :
RunnableWrapper
:Ruannable Interface The wrapper classCallableWrapper
:Callable Interface The wrapper classConsumerWrapper
: Functional interface Consumer The wrapper classSupplierWrapper
: Functional interface Supplier The wrapper classFunctionWrapper
: Functional interface Function The wrapper class
With SupplierWrapper
For example , Examples of its use are as follows :
CompletableFuture.supplyAsync(new SupplierWrapper<>(() -> {
LoggerFactory.getLogger(this.getClass()).info("SupplierWrapper");
return "nathan";
}));
1.2 The principle of wrapping classes across threads
1.2.1 @TraceCrossThread annotation
The following is a SupplierWrapper Source code , Looking at the source code of the wrapper class mentioned in the previous section, you will find that they are annotated @TraceCrossThread
modification . actually SkyWalking Will pass SPI Mechanism in skywalking-plugin.def Specify bytecode enhanced configuration class in the file , among CallableOrRunnableActivation
It's specifically for @TraceCrossThread
Annotate the configuration class for scanning
@TraceCrossThread
public class SupplierWrapper<V> implements Supplier<V> {
final Supplier<V> supplier;
public static <V> SupplierWrapper<V> of(Supplier<V> r) {
return new SupplierWrapper<>(r);
}
public SupplierWrapper(Supplier<V> supplier) {
this.supplier = supplier;
}
@Override
public V get() {
return supplier.get();
}
}
1.2.2 CallableOrRunnableActivation Enhanced configuration for
CallableOrRunnableActivation Will @TraceCrossThread
Annotations as the entry point for enhanced classes , At the same time, configure aspects for the methods in the class that need to be enhanced , So that bytecode enhancement can be implemented across threads trace Transfer function
The following is a CallableOrRunnableActivation Source code , You can see that the configuration mainly specifies two enhancements for the target class :
- Constructor enhancements
Constructor enhanced configuration to match any constructor , The enhanced section is CallableOrRunnableConstructInterceptor- Specify method enhancements
Specifies that method enhancements only match Above, 1.1 section The specific methods of the wrapper class mentioned , The enhanced section is CallableOrRunnableInvokeInterceptorThis article is only for SkyWalking Make a preliminary introduction to the enhanced configuration of , If readers are interested, they can make an in-depth analysis here
public class CallableOrRunnableActivation extends ClassInstanceMethodsEnhancePluginDefine {
public static final String ANNOTATION_NAME = "org.apache.skywalking.apm.toolkit.trace.TraceCrossThread";
private static final String INIT_METHOD_INTERCEPTOR = "org.apache.skywalking.apm.toolkit.activation.trace.CallableOrRunnableConstructInterceptor";
private static final String CALL_METHOD_INTERCEPTOR = "org.apache.skywalking.apm.toolkit.activation.trace.CallableOrRunnableInvokeInterceptor";
private static final String CALL_METHOD_NAME = "call";
private static final String RUN_METHOD_NAME = "run";
private static final String GET_METHOD_NAME = "get";
private static final String APPLY_METHOD_NAME = "apply";
private static final String ACCEPT_METHOD_NAME = "accept";
@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(CALL_METHOD_NAME)
.and(takesArguments(0))
.or(named(RUN_METHOD_NAME).and(takesArguments(0)))
.or(named(GET_METHOD_NAME).and(takesArguments(0)))
.or(named(APPLY_METHOD_NAME).and(takesArguments(1)))
.or(named(ACCEPT_METHOD_NAME).and(takesArguments(1)));
}
@Override
public String getMethodsInterceptor() {
return CALL_METHOD_INTERCEPTOR;
}
@Override
public boolean isOverrideArgs() {
return false;
}
}
};
}
@Override
protected ClassMatch enhanceClass() {
return byClassAnnotationMatch(new String[] {
ANNOTATION_NAME});
}
}
2. Custom implementation SkyWalking Cross thread wrapper classes
After the introduction in the previous section , We know @TraceCrossThread
Annotations can only be enhanced CallableOrRunnableActivation
The method specified in the configuration , use @TraceCrossThread
It's hard to achieve free customization . But copy CallableOrRunnableActivation
Construction method facet and specified method facet configured in , Realize cross thread delivery trace The asynchronous task wrapper class is not that difficult , One BiConsumerWrapper
The code example of is as follows :
This wrapper class implements trace There are only two steps in the core processing of cross thread information transmission :
- When creating a wrapper class asynchronous task , adopt
ContextManager
In the context of the thread that will submit the task trace The information is cached inside the wrapper class object- The worker thread first passes through before executing an asynchronous task
ContextManager
Create your own context , Then cache the asynchronous task trace Information is loaded into its own context , To achieve trace Cross thread delivery of
public class BiConsumerWrapper<T, U> implements BiConsumer<T, U> {
private final BiConsumer<T, U> function;
private ContextSnapshot snapshot;
public BiConsumerWrapper(BiConsumer<T, U> function) {
this.function = function;
if (ContextManager.isActive()) {
snapshot = ContextManager.capture();
}
}
@Override
public void accept(T t, U u) {
Optional.ofNullable(snapshot).ifPresent(snapshot -> {
ContextManager.createLocalSpan("BiConsumerWrapper");
ContextManager.continued(snapshot);
});
function.accept(t, u);
Optional.ofNullable(snapshot).ifPresent(value -> ContextManager.stopSpan());
}
}
边栏推荐
- 力扣每日一题-第26天-506.相对名次
- Computer wechat user picture decoded into picture in DAT format (TK version)
- JS regular matching numbers, upper and lower case letters, underscores, midlines and dots [easy to understand]
- AOSP ~ default attribute value
- Lihongyi, machine learning 6 Convolutional neural network
- Difference between left join on and join on
- Doak CMS article management system recommendation
- 大咖说*计算讲谈社|如何提出关键问题?
- 用指南针开户如何选择证券公司?哪一个是更安全的
- [i.mx6ul] u-boot migration (VI) network driver modification lan8720a
猜你喜欢
What if Alipay is controlled by risk for 7 days? Payment solution
软件测试周刊(第77期):只要放弃一次,就会滋生放弃的习性, 原本可以解决的问题也会变得无法解决。
单例的饥饿、懒汉模式案例
Pytorch learning notes (VII) ------------------ vision transformer
Introduction to database system
Yarn: unable to load file c:\users\xxx\appdata\roaming\npm\yarn PS1 because running scripts is prohibited on this system
PyTorch学习笔记(七)------------------ Vision Transformer
LeetCode 210:课程表 II (拓扑排序)
什么是SSL证书,拥有一个SSL证书有什么好处?
Copilot免费时代结束!学生党和热门开源项目维护者可白嫖
随机推荐
CMakeLists中的add_definitions()函数
运行时修改Universal Render Data
MySQL command backup
Easynvr fails to use onvif to detect the device. What is the reason why "no data" is displayed?
Add in cmakelists_ Definitions() function
DSPACE set zebra crossings and road arrows
Two way combination of business and technology to build a bank data security management system
Is it safe to open an account in the way of winning 100% of the new bonds
股票开户用客户经理发的开户链接安全吗?知道的给说一下吧
Unity archive system - file in JSON format
Single case of hungry and lazy mode
Seata四大模式之TCC模式详解及代码实现
AOSP ~ default attribute value
数组-一口气冲完快慢指针
怎么开户打新债 开户是安全的吗
@PostConstruct
用向量表示两个坐标系的变换
Refresh mechanism of vie
UnityShader入门精要——表面着色器
Is it safe to open an online stock account?