Go 语言开发的基于指标的监控系统 Prometheus,主要采用拉取方式收集监控数据,通过 Pushgateway 也可以采用推送方式收集监控数据。
01介绍
Go 语言开发的基于指标的监控系统 Prometheus,主要采用拉取方式收集监控数据,通过 Pushgateway 也可以采用推送方式收集监控数据。
关于 Prometheus 的客户端库和 PromQL 的使用,是 Go 开发者重点需要掌握的部分。
本文我们介绍通过使用 Prometheus 官方提供的 golang 客户端库,使用 Counter 数据类型记录 HTTP 接口的调用量。
02安装、启动 Prometheus server
Prometheus server 可以直接使用二进制文件的方式安装,在Prometheus 官网[1]下载二进制文件,示例:
- 下载二进制文件。
- 解压缩二进制文件。
- 启动 Prometheus server。
cd ~/Download
wget https://github.com/prometheus/prometheus/releases/download/v2.48.0-rc.2/prometheus-2.48.0-rc.2.darwin-amd64.tar.gz
tar zxvf prometheus-2.48.0-rc.2.darwin-amd64.tar.gz
cd prometheus-2.48.0-rc.2.darwin-amd64
ll
total 472152
-rw-r--r--@ 1 frank staff 11357 10 13 00:41 LICENSE
-rw-r--r--@ 1 frank staff 3773 10 13 00:41 NOTICE
drwxr-xr-x@ 4 frank staff 128 10 13 00:41 console_libraries
drwxr-xr-x@ 9 frank staff 288 10 13 00:41 consoles
-rwxr-xr-x@ 1 frank staff 123733776 10 13 00:09 prometheus
-rw-r--r--@ 1 frank staff 934 10 13 00:41 prometheus.yml
-rwxr-xr-x@ 1 frank staff 117982832 10 13 00:11 promtool
./prometheus --config.file=prometheus.yaml
启动 Prometheus server 后,可以在浏览器访问http://localhost:9090/graph,查看 Prometheus 提供的可视化控制面板,也可以使用 Grafana。
03Golang 客户端库
安装并成功启动 Prometheus server 后,我们就可以通过 Prometheus 官方提供的 Golang 客户端库在我们的 Go 项目代码埋点。
Prometheus client 支持 4 种数据类型,分别是 Counter、Gauge、Histogram 和 Summary。
本文我们以 Counter 数据类型为例,介绍如何在 Go 项目中使用 Prometheus go client 库的函数埋点。
所谓埋点,就是在我们的 Go 项目中,导入github.com/prometheus/client_golang/prometheus,调用库函数,记录监控数据。
示例代码:
package prom
import "github.com/prometheus/client_golang/prometheus"
var (
labelNames = []string{"service", "code", "path", "method"}
RequestCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "http_request_count_total",
Help: "Total number of HTTP requests made.",
}, labelNames,
)
)
func init() {
prometheus.MustRegister(RequestCounter)
}
阅读上面这段代码,我们调用prometheus.NewCounterVec(),记录 HTTP 接口的调用量。
我们为 HTTP 接口定义 4 个标签,分别是 service,code,path,method。
然后通过/metrics接口,让 Prometheus server 拉取数据。
curl http://localhost:8080/metrics
// ...
# HELP http_request_count_total Total number of HTTP requests made.
# TYPE http_request_count_total counter
http_request_count_total{code="200",method="GET",path="/metrics",service="example-service"} 3
http_request_count_total{code="200",method="GET",path="/ping",service="example-service"} 2
// ...
04修改配置文件
接下来,我们需要修改 YAML 格式的配置文件prometheus.yaml,添加一个job。
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "prometheus"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ["localhost:9090"]
# Example service
- job_name: "example-service"
static_configs:
- targets: ["localhost:8080"]
阅读上面这段代码,我们在prometheus.yaml的scrape_configs部分,添加一个job。
然后重启 Prometheus server,使修改后的配置文件生效。
05总结
本文我们通过示例,介绍怎么使用 Prometheus 监控 Go 项目,读者朋友们可以参照文章,动手操作一遍。
感兴趣的读者朋友们,阅读Prometheus golang client[2]官方文档,了解更多。
©本文为清一色官方代发,观点仅代表作者本人,与清一色无关。清一色对文中陈述、观点判断保持中立,不对所包含内容的准确性、可靠性或完整性提供任何明示或暗示的保证。本文不作为投资理财建议,请读者仅作参考,并请自行承担全部责任。文中部分文字/图片/视频/音频等来源于网络,如侵犯到著作权人的权利,请与我们联系(微信/QQ:1074760229)。转载请注明出处:清一色财经