前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Go 语言开发的基于指标的监控系统 Prometheus

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

作者头像
frank.
发布2023-11-06 17:14:20
2280
发布2023-11-06 17:14:20
举报
文章被收录于专栏:Golang语言开发栈

01

介绍

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

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

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

02

安装、启动 Prometheus server

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

  1. 下载二进制文件。
  2. 解压缩二进制文件。
  3. 启动 Prometheus server。
代码语言:javascript
复制
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,调用库函数,记录监控数据。

示例代码:

代码语言:javascript
复制
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 拉取数据。

代码语言:javascript
复制
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

代码语言:javascript
复制
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.yamlscrape_configs 部分,添加一个 job

然后重启 Prometheus server,使修改后的配置文件生效。

05

总结

本文我们通过示例,介绍怎么使用 Prometheus 监控 Go 项目,读者朋友们可以参照文章,动手操作一遍。

感兴趣的读者朋友们,阅读 Prometheus golang client[2] 官方文档,了解更多。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2023-11-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Golang语言开发栈 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
Prometheus 监控服务
Prometheus 监控服务(TencentCloud Managed Service for Prometheus,TMP)是基于开源 Prometheus 构建的高可用、全托管的服务,与腾讯云容器服务(TKE)高度集成,兼容开源生态丰富多样的应用组件,结合腾讯云可观测平台-告警管理和 Prometheus Alertmanager 能力,为您提供免搭建的高效运维能力,减少开发及运维成本。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档