AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當前位置:首頁 > 科技  > 軟件

Go 語言開發的基于指標的監控系統 Prometheus

來源: 責編: 時間:2023-11-06 08:54:16 320觀看
導讀01 介紹Go 語言開發的基于指標的監控系統 Prometheus,主要采用拉取方式收集監控數據,通過 Pushgateway 也可以采用推送方式收集監控數據。關于 Prometheus 的客戶端庫和 PromQL 的使用,是 Go 開發者重點需要掌握的部分。

01 介紹

Go 語言開發的基于指標的監控系統 Prometheus,主要采用拉取方式收集監控數據,通過 Pushgateway 也可以采用推送方式收集監控數據。IJA28資訊網——每日最新資訊28at.com

關于 Prometheus 的客戶端庫和 PromQL 的使用,是 Go 開發者重點需要掌握的部分。IJA28資訊網——每日最新資訊28at.com

本文我們介紹通過使用 Prometheus 官方提供的 golang 客戶端庫,使用 Counter 數據類型記錄 HTTP 接口的調用量。IJA28資訊網——每日最新資訊28at.com

02 安裝、啟動 Prometheus server

Prometheus server 可以直接使用二進制文件的方式安裝,在 Prometheus 官網[1]下載二進制文件,示例:IJA28資訊網——每日最新資訊28at.com

  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。IJA28資訊網——每日最新資訊28at.com

03 Golang 客戶端庫

安裝并成功啟動 Prometheus server 后,我們就可以通過 Prometheus 官方提供的 Golang 客戶端庫在我們的 Go 項目代碼埋點。IJA28資訊網——每日最新資訊28at.com

Prometheus client 支持 4 種數據類型,分別是 Counter、Gauge、Histogram 和 Summary。IJA28資訊網——每日最新資訊28at.com

本文我們以 Counter 數據類型為例,介紹如何在 Go 項目中使用 Prometheus go client 庫的函數埋點。IJA28資訊網——每日最新資訊28at.com

所謂埋點,就是在我們的 Go 項目中,導入 github.com/prometheus/client_golang/prometheus,調用庫函數,記錄監控數據。IJA28資訊網——每日最新資訊28at.com

示例代碼:IJA28資訊網——每日最新資訊28at.com

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 接口的調用量。IJA28資訊網——每日最新資訊28at.com

我們為 HTTP 接口定義 4 個標簽,分別是 service,code,path,method。IJA28資訊網——每日最新資訊28at.com

然后通過 /metrics 接口,讓 Prometheus server 拉取數據。IJA28資訊網——每日最新資訊28at.com

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。IJA28資訊網——每日最新資訊28at.com

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。IJA28資訊網——每日最新資訊28at.com

然后重啟 Prometheus server,使修改后的配置文件生效。IJA28資訊網——每日最新資訊28at.com

05 總結

本文我們通過示例,介紹怎么使用 Prometheus 監控 Go 項目,讀者朋友們可以參照文章,動手操作一遍。IJA28資訊網——每日最新資訊28at.com

感興趣的讀者朋友們,閱讀 Prometheus golang client[2] 官方文檔,了解更多。IJA28資訊網——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-26-17179-0.htmlGo 語言開發的基于指標的監控系統 Prometheus

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: Ydata_Profiling:自動生成數據探索報告的Python庫

下一篇: ListUtils技巧大全:提升你的Java列表操作效率

標簽:
  • 熱門焦點
Top