代码里面看不明白的变量或者函数可以通过两种方式观测它的值来了解其含义
- 一种是把代码片段摘出来,写到一个测试程序里运行一下
- 另一种时日志里打印它的值来观察
第一种比较简单易行,但是代码片段要比较独立才好做,第二种要修改源码重新编译,可以在整个源码上下文中观测,但是需要编码源码,相对麻烦一些,研究源码早晚也要编译,所以先总结一下编译 Prometheus 源码的步骤。
编译 Prometheus 源码需要 node.js 和 yarn。我是在 WSL2 上编译的。
- 安装 node.js
$ curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
$ sudo apt-get install -y nodejs
- 安装 gcc
sudo apt-get install gcc g++ make
- 安装 yarn
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update
sudo apt install yarn
- 编译源码
# 避免报错 [email protected]: The platform "linux" is incompatible with this module.
yarn config set ignore-engines true
make build
耗时比较长,10分钟以上。