自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Go 語(yǔ)言開(kāi)發(fā)的基于指標(biāo)的監(jiān)控系統(tǒng) Prometheus

開(kāi)發(fā) 前端
Go 語(yǔ)言開(kāi)發(fā)的基于指標(biāo)的監(jiān)控系統(tǒng) Prometheus,主要采用拉取方式收集監(jiān)控?cái)?shù)據(jù),通過(guò) Pushgateway 也可以采用推送方式收集監(jiān)控?cái)?shù)據(jù)。

01 介紹

Go 語(yǔ)言開(kāi)發(fā)的基于指標(biāo)的監(jiān)控系統(tǒng) Prometheus,主要采用拉取方式收集監(jiān)控?cái)?shù)據(jù),通過(guò) Pushgateway 也可以采用推送方式收集監(jiān)控?cái)?shù)據(jù)。

關(guān)于 Prometheus 的客戶端庫(kù)和 PromQL 的使用,是 Go 開(kāi)發(fā)者重點(diǎn)需要掌握的部分。

本文我們介紹通過(guò)使用 Prometheus 官方提供的 golang 客戶端庫(kù),使用 Counter 數(shù)據(jù)類型記錄 HTTP 接口的調(diào)用量。

02 安裝、啟動(dòng) Prometheus server

Prometheus server 可以直接使用二進(jìn)制文件的方式安裝,在 Prometheus 官網(wǎng)[1]下載二進(jìn)制文件,示例:

  1. 下載二進(jìn)制文件。
  2. 解壓縮二進(jìn)制文件。
  3. 啟動(dòng) 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

啟動(dòng) Prometheus server 后,可以在瀏覽器訪問(wèn) http://localhost:9090/graph,查看 Prometheus 提供的可視化控制面板,也可以使用 Grafana。

03 Golang 客戶端庫(kù)

安裝并成功啟動(dòng) Prometheus server 后,我們就可以通過(guò) Prometheus 官方提供的 Golang 客戶端庫(kù)在我們的 Go 項(xiàng)目代碼埋點(diǎn)。

Prometheus client 支持 4 種數(shù)據(jù)類型,分別是 Counter、Gauge、Histogram 和 Summary。

本文我們以 Counter 數(shù)據(jù)類型為例,介紹如何在 Go 項(xiàng)目中使用 Prometheus go client 庫(kù)的函數(shù)埋點(diǎn)。

所謂埋點(diǎn),就是在我們的 Go 項(xiàng)目中,導(dǎo)入 github.com/prometheus/client_golang/prometheus,調(diào)用庫(kù)函數(shù),記錄監(jiān)控?cái)?shù)據(jù)。

示例代碼:

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)
}

閱讀上面這段代碼,我們調(diào)用 prometheus.NewCounterVec(),記錄 HTTP 接口的調(diào)用量。

我們?yōu)?HTTP 接口定義 4 個(gè)標(biāo)簽,分別是 service,code,path,method。

然后通過(guò) /metrics 接口,讓 Prometheus server 拉取數(shù)據(jù)。

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 修改配置文件

接下來(lái),我們需要修改 YAML 格式的配置文件 prometheus.yaml,添加一個(gè) 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"]

閱讀上面這段代碼,我們?cè)?nbsp;prometheus.yaml 的 scrape_configs 部分,添加一個(gè) job。

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

05 總結(jié)

本文我們通過(guò)示例,介紹怎么使用 Prometheus 監(jiān)控 Go 項(xiàng)目,讀者朋友們可以參照文章,動(dòng)手操作一遍。

感興趣的讀者朋友們,閱讀 Prometheus golang client[2] 官方文檔,了解更多。

責(zé)任編輯:武曉燕 來(lái)源: Golang語(yǔ)言開(kāi)發(fā)棧
相關(guān)推薦

2022-09-04 23:24:45

Go語(yǔ)言監(jiān)控

2021-10-14 08:07:33

Go 應(yīng)用Prometheus監(jiān)控

2022-03-14 08:25:49

云原生prometheusPushProx

2022-07-08 08:00:31

Prometheus監(jiān)控

2022-03-13 23:51:39

Web項(xiàng)目Go

2022-05-12 08:01:26

vmagentprometheus

2023-09-06 08:46:47

2024-06-14 08:19:45

2022-11-08 00:00:00

監(jiān)控系統(tǒng)Prometheus

2020-05-18 12:04:17

PrometheusMySQL監(jiān)控

2021-03-26 20:37:14

Prometheus監(jiān)控指標(biāo)

2023-04-27 07:06:09

Categraf夜鶯

2022-08-27 21:37:57

PrometheusRedis?監(jiān)控

2020-12-30 08:09:46

運(yùn)維Prometheus 監(jiān)控

2012-09-18 10:48:47

服務(wù)器虛擬化Hypervisor虛擬化

2024-02-21 16:13:36

CNCF開(kāi)源監(jiān)控工具Prometheus

2020-12-29 10:45:22

運(yùn)維Prometheus-監(jiān)控

2024-06-11 00:05:00

CasaOS云存儲(chǔ)管理

2023-12-29 08:01:52

自定義指標(biāo)模板

2025-03-11 00:25:00

Springmetrics數(shù)據(jù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)