当前位置:网站首页>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
ContextManagerIn 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
ContextManagerCreate 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());
}
}
边栏推荐
- 爱
- Three key explanations of overseas e-commerce operation in 2022
- Getting started with unityshader Essentials - PBS physics based rendering
- JS regular matching numbers, upper and lower case letters, underscores, midlines and dots [easy to understand]
- 自动化测试
- Groovy之高级用法
- Unity存档系统——Json格式的文件
- AOSP ~ WIFI架构总览
- PyTorch学习笔记(七)------------------ Vision Transformer
- 指南针在上面开户安全吗?靠谱吗?
猜你喜欢

Advanced mathematics | proficient in mean value theorem problem solving routines summary

Leecode learning notes - the shortest path for a robot to reach its destination

14 bs对象.节点名称.name attrs string 获取节点名称 属性 内容

自动化测试

用向量表示两个坐标系的变换

Lihongyi, machine learning 6 Convolutional neural network

Before the age of 36, Amazon transgender hackers were sentenced to 20 years' imprisonment for stealing data from more than 100million people!

Insurance can also be bought together? Four risks that individuals can pool enough people to buy Medical Insurance in groups

XML modeling

Array - fast and slow pointer in one breath
随机推荐
Xiaomi routing R4A Gigabit version installation feed+openwrt tutorial (the full script does not need to be hard modified)
国信金太阳打新债步骤 开户是安全的吗
股票开户用客户经理发的开户链接安全吗?知道的给说一下吧
XML modeling
同花顺证券开户安全吗
We media do not know how to realize it? Sharing of 7 major realization methods
用指南针开户如何选择证券公司?哪一个是更安全的
运行时修改Universal Render Data
Administrator如何禁止另一个人踢掉自己?
Easy to use dictionary -defaultdict
How to open an account to open a new bond is it safe to open an account
Call system function security scheme
AI自己写代码让智能体进化!OpenAI的大模型有“人类思想”那味了
在线股票开户安全吗?
Doak CMS article management system recommendation
Insurance can also be bought together? Four risks that individuals can pool enough people to buy Medical Insurance in groups
大咖说*计算讲谈社|如何提出关键问题?
Please check the list of commonly used software testing tools.
单例的饥饿、懒汉模式案例
Expressing the transformation of two coordinate systems with vectors