可视化节点数据
最新版本的 Substrate 开放了指标,比如你节点连接了多少同伴节点,使用了多少内存,等等。 你可使用 Prometheus 和 Grafana 这类的工具来可视化这些指标。
注意:过去 Substrate 是直接开放了 Grafana JSON 的端点。 但是现在已经被 Prometheus 指标端点替代了。
一个合理的架构大概是这样:
+-----------+ +-------------+ +---------+
| Substrate | | Prometheus | | Grafana |
+-----------+ +-------------+ +---------+
| -----------------\ | |
| | 每一分钟|-| |
| |----------------| | |
| | |
| 获取当前指标值| |
|<---------------------------------| |
| | |
| `substrate_peers_count 5` | |
|--------------------------------->| |
| | --------------------------------------------------------------------\ |
| |-|在本地数据库中保存指标值与相应的时间戳 | |
| | |-------------------------------------------------------------------| |
| | -------------------------------\ |
| | | 每次用户打开图表 |-|
| | |------------------------------| |
| | |
| | 从 time-X 到 time-Y 中获取指标值 'substrate_peers_count ' |
| |<-------------------------------------------------------------------------|
| | |
| | `substrate_peers_count (1582023828, 5), (1582023847, 4) [...]` |
| |------------------------------------------------------------------------->|
| | |
重新生成图表
前往:https://textart.io/sequence
object Substrate Prometheus Grafana
note left of Prometheus: Every 1 minute
Prometheus->Substrate: GET current metric values
Substrate->Prometheus: `substrate_peers_count 5`
note right of Prometheus: Save metric value with corresponding time stamp in local database
note left of Grafana: Every time user opens graphs
Grafana->Prometheus: GET values of metric `substrate_peers_count` from time-X to time-Y
Prometheus->Grafana: `substrate_peers_count (1582023828, 5), (1582023847, 4) [...]`
安装 Prometheus 和 Grafana
注意:为了 测试,我们建议您直接下载已编译好的
bin
程序,而不是在本地作完整安装或在 Docker 里运行。 就下载
适合你本地架构的版本,然后在工作目录
里运行它。 上面的链接提供了相关指引,本指南也会假定你是这样做。
启动一个 Substrate 节点
Substrate 开放了端点让指标数据可以 Prometheus 可读格式 于端口 9615
被读取。 你可以使用 --prometheus-port <PORT>
來更改端口,而且允许它可在本地 --prometheus-external
以外访问到。
# clear the dev database
./target/release/node-template purge-chain --dev -y
# start the template node with
# optional --prometheus-port <PORT>
# or --prometheus-external flags added
./target/release/node-template --dev
配置 Prometheus 去扫描你的 Substrate 节点
在安装 Prometheus 的工作目录中,你会找到 prometheus.yml
的配置文件。 让我们修改 (或创建一个新的文件) 来配置 Prometheus,通过添加到目标数组来扫描开放的端点。 如果您是修改默认值,这里是要修改的地方:
# --snip--
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "substrate_node"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
# Override the global default and scrape targets from this job every 5 seconds.
# ** NOTE: you want to have this *LESS THAN* the block time in order to ensure
# ** that you have a data point for every block!
scrape_interval: 5s
static_configs:
- targets: ["localhost:9615"]
注意:你会想有
scrape_interval
时间小于 出块时间来确保每个区块都有数据!
配置完 prometheus.yml 文件后,现在可开启 Prometheus。 假定您是下载了二进制文件,那就 cd
到安装目录并运行:
# specify a custom config file instead if you made one here:
./prometheus --config.file prometheus.yml
保持这个进程一直运行着。
检查所有 Prometheus 指标
在一个新的终端中,我们可以对 prometheus 快速进行状态检查:
curl localhost:9615/metrics
应该返回类似以下的信息:
# HELP substrate_block_height Block height info of the chain
# TYPE substrate_block_height gauge
substrate_block_height{status="best"} 7
substrate_block_height{status="finalized"} 4
# HELP substrate_build_info A metric with a constant '1' value labeled by name, version
# TYPE substrate_build_info gauge
substrate_build_info{name="available-vacation-6791",version="2.0.0-4d97032-x86_64-linux-gnu"} 1
# HELP substrate_database_cache_bytes RocksDB cache size in bytes
# TYPE substrate_database_cache_bytes gauge
substrate_database_cache_bytes 0
# HELP substrate_finality_grandpa_precommits_total Total number of GRANDPA precommits cast locally.
# TYPE substrate_finality_grandpa_precommits_total counter
substrate_finality_grandpa_precommits_total 31
# HELP substrate_finality_grandpa_prevotes_total Total number of GRANDPA prevotes cast locally.
# TYPE substrate_finality_grandpa_prevotes_total counter
substrate_finality_grandpa_prevotes_total 31
#
# --snip--
#
或者,在浏览器中打开相同的 URL (http://localhost:9615/metrics) 来查看所有指标数据。
注意:这里你可看到每个开放被 Grafana 监测的指标的
HELP
字段。
使用 Grafana 来观看 Prometheus 指标
Grafana 运行后,在浏览器中打开它 (默认是 https://localhost:3000/)。 登录 (默认用户 admin
和密码 admin
),并导航到 数据源 页面。
然后您需要选择 Prometheus
为数据源类型,并指定在哪里查找它。 在 Substrate 节点和 Prometheus 都在运行下,Prometheus 會在另一个,而不是您节点開放給 Prometheus,的端口上開放。这不是您在 prometheus.yml
中设置的端口 (https://localhost:9615)。
配置 Grafana 在默认端口上寻找 Prometheus:https://localhost:9090 (除非你更改了端口设置)。 点击 Save & Test
以确保您有正确的数据源设置。 现在你可配置一个新的仪表板 !
Grafana 仪版表模板
如果你需要一个基本的仪表板,这里是一个模板示例,你可以在 Grafana 里选 import
来获取有关你节点的基本信息:

If you create your own, the prometheus docs for grafana use may be helpful.
If you do create one, consider uploading it to the community list of dashboards and letting the substrate builder community know it exists by listing in on Awesome Substrate! Here is ours on the grafana public dashboards.
后续步骤
进一步学习
- 学习如何 创建 Substrate 私有网络。
- Further configuration, notification services, and permanent installation of Substrate/Polkadot monitoring tools.
例子
- 查看 波卡网络 的 Grafana 仪表版配置。
- Grafana Template for a Substrate Node Template.
参考文档
- 访问 Substrate Prometheus Exporter 源码。
- See the docs for prometheus in substrate.