mrproliu commented on code in PR #13: URL: https://github.com/apache/skywalking-go/pull/13#discussion_r1175192269
########## docs/en/concepts-and-designs/key-principles.md: ########## @@ -0,0 +1,174 @@ +# Key Principle + +Enhancing applications in hybrid compilation is very important for SkyWalking Go Agent. +In this section, I will delve deeper into several key technical points. + +## Method Interceptor + +Method interception is particularly important in SkyWalking Go, as it enables the creation of plugins. In SkyWalking Go, method interception mainly involves the following key points: + +1. **Finding Method**: Using `AST` to find method information in the target code to be enhanced. +2. **Modifying Methods**: Enhancing the specified methods and embedding interceptor code. +3. **Saving and Compiling**: Updating the modified files in the compilation arguments. + +### Finding Method + +When looking for methods, the SkyWalking Go Agent need to search according to the provided compilation arguments, which mainly include the following two parts: + +1. **Package information**: Based on the package name provided by the arguments, the Agent can find the specific plugin. +2 **Go files**: When a matching plugin is found, Agent can read the `.go` files and use `AST` to parse the method information contained in these files. When the method information matches the method information required by the plugin for interception, Agent can consider the method found. + +### Modifying Methods + +After finding the method, the SkyWalking Go Agent needs to modify the method implication and embed the interceptor code. + +#### Change Method Body + +When intercepting a method, the first thing to do is to modify the method and [embed the template code](../../../tools/go-agent/instrument/plugins/templates/method_inserts.tmpl). +This code segment includes two method executions: + +1. **Before method execution**: Pass in the current method's arguments, instances, and other information. +2. **After method execution**: Using the `defer` method, intercept the result parameters after the code execution is completed. + +Based on these two methods, the agent can intercept before and after method execution. + +In order not to affect the line of code execution, this code segment will only be executed in the **same line as the first statement in the method**. +This ensures that when an exception occurs in the framework code execution, the exact location can still be found without being affected by the enhanced code. + +#### Write Adapter File Review Comment: Sure, Updated. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
