当前位置:首页 >知识 >Go 语言开发的基于指标的监控系统 Prometheus 语言于示例:下载二进制文件

Go 语言开发的基于指标的监控系统 Prometheus 语言于示例:下载二进制文件

2024-06-30 17:24:30 [百科] 来源:避面尹邢网

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

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

01 介绍

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

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

Go 语言开发的基于指标的监控系统 Prometheus 语言于示例:下载二进制文件

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

Go 语言开发的基于指标的监控系统 Prometheus 语言于示例:下载二进制文件

02 安装、监控启动 Prometheus server

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

Go 语言开发的基于指标的监控系统 Prometheus 语言于示例:下载二进制文件

  1. 下载二进制文件。监控
  2. 解压缩二进制文件。系统
  3. 启动 Prometheus server。
cd ~/Downloadwget https://github.com/prometheus/prometheus/releases/download/v2.48.0-rc.2/prometheus-2.48.0-rc.2.darwin-amd64.tar.gztar zxvf prometheus-2.48.0-rc.2.darwin-amd64.tar.gzcd prometheus-2.48.0-rc.2.darwin-amd64lltotal 472152-rw-r--r--@ 1 frank  staff      11357 10 13 00:41 LICENSE-rw-r--r--@ 1 frank  staff       3773 10 13 00:41 NOTICEdrwxr-xr-x@ 4 frank  staff        128 10 13 00:41 console_librariesdrwxr-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 promimport "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 counterhttp_request_count_total{ code="200",method="GET",path="/metrics",service="example-service"} 3http_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语言开发栈 Go语言开发

(责任编辑:休闲)

    推荐文章
    热点阅读