Go 语言开发的基于指标的监控系统 Prometheus

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

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=<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] 官方文档,了解更多。

责任编辑:武晓燕 来源: Golang语言开发栈
相关推荐

2022-09-04 23:24:45

Go语言监控

2021-10-14 08:07:33

Go 应用Prometheus监控

2022-03-14 08:25:49

云原生prometheusPushProx

2022-07-08 08:00:31

Prometheus监控

2022-05-12 08:01:26

vmagentprometheus

2022-03-13 23:51:39

Web项目Go

2023-09-06 08:46:47

2022-11-08 00:00:00

监控系统Prometheus

2021-03-26 20:37:14

Prometheus监控指标

2020-05-18 12:04:17

PrometheusMySQL监控

2023-04-27 07:06:09

Categraf夜莺

2022-08-27 21:37:57

PrometheusRedis​监控

2020-12-30 08:09:46

运维Prometheus 监控

2012-09-18 10:48:47

服务器虚拟化Hypervisor虚拟化

2024-02-21 16:13:36

CNCF开源监控工具Prometheus

2020-12-29 10:45:22

运维Prometheus-监控

2023-12-29 08:01:52

自定义指标模板

2022-03-01 16:26:09

链路监控日志监控分布式系统

2020-12-30 05:34:25

监控PrometheusGrafana

2020-05-18 14:55:34

监控系统架构技术
点赞
收藏

51CTO技术栈公众号