Go 语言开发的基于指标的监控系统 Prometheus
创始人
2025-07-05 16:41:10
0

01 介绍

Go 语言开发的基于指标的监控系统 Prometheus,主要采用拉取方式收集监控数据,通过 Pushgateway 也可以采用推送方式收集监控数据。

关于 Prometheus 的客户端库和 PromQL 的使用,是 Go 开发者重点需要掌握的部分。

本文我们介绍通过使用 Prometheus 官方提供的 golang 客户端库,使用 Counter 数据类型记录 HTTP 接口的调用量。

02 安装、启动 Prometheus server

Prometheus server 可以直接使用二进制文件的方式安装,在 Prometheus 官网[1]下载二进制文件,示例:

  1. 下载二进制文件。
  2. 解压缩二进制文件。
  3. 启动 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。

03 Golang 客户端库

安装并成功启动 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=` 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] 官方文档,了解更多。

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...