zhuanlan.zhihu.com/p/678661112
数据模型
将请求 划分为Trace、Segment、Span 三个维度,该模型已经形成了OpenTracing[2]规范
-
Trace 表示一整条调用链,包括跨进程、跨线程的所有Segment的集合。
-
Segment 表示一个进程(JVM)或线程内的所有操作的集合,即包含若干个Span。
-
Span 表示一个具体的操作。Span在不同的实现里可能有不同的划分方式,这里介绍一个比较容易理解的定义方式:
- Entry Span:入栈Span。Segment的入口,一个Segment有且仅有一个Entry Span,比如HTTP或者RPC的入口,或者MQ消费端的入口等。
- Local Span:通常用于记录一个本地方法的调用。
- Exit Span:出栈Span。Segment的出口,一个Segment可以有若干个Exit Span,比如HTTP或者RPC的出口,MQ生产端,或者DB、Cache的调用等。
关系描述
阿里的Eagleeye中的设计很巧妙,EagleEye设计了RpcId来区别同一个调用链下多个网络调用的顺序和嵌套层次
根节点的RpcId固定从0开始,id的位数("."的数量)表示了Span在这棵树中的层级,Id最后一位表示了Span在这一层级中的顺序
给定同一个Trace中的所有RpcId,便可以很容易还原出一个完整的调用链:
- 0
- 0.1
- 0.1.1
- 0.1.2
- 0.1.2.1
- 0.2
- 0.2.1
- 0.3
- 0.3.1
- 0.3.1.1
- 0.3.2
golang 项目接入 skywalking
skywalking.apache.org/zh/2023-06-…
main包 引入 skywalking-go
import (
_ "github.com/apache/skywalking-go"
)
2.从官方 SkyWalking 网站下载 Go Agent 程序 。当你使用 go build 命令进行编译时,请在 bin 目录中找到与当前操作系统匹配的代理程序,并添加 -toolexec="/path/to/go-agent" -a 参数。例如,请使用以下命令:
go build -toolexec="/path/to/go-agent" -a -o test .
Go Agent 实现
Method Interceptor
- Finding Method: Using
AST(Abstract Syntax Tree) to find method information in the target code to be enhanced. - Modifying Methods: Enhancing the specified methods and embedding interceptor code.
- Saving and Compiling: Updating the modified files in the compilation arguments.