ABAP Netweaver, Hybris Commerce和SAP 云平臺(tái)的登錄認(rèn)證
662
2025-04-04
介紹
本文將介紹如何在 gRPC 微服務(wù)中,加入 prometheus 監(jiān)控。
gRPC 函數(shù)的自動(dòng)監(jiān)控,將會(huì)在后續(xù)的文章中介紹,這里我們只介紹如何在 gRPC 代碼中,實(shí)現(xiàn) prometheus 監(jiān)控。
我們將會(huì)使用 rk-boot 來啟動(dòng) gRPC 服務(wù)。
我們將會(huì)使用 rk-prom 來啟動(dòng) prometheus 客戶端。
安裝
go get github.com/rookie-ninja/rk-boot
快速開始
詳細(xì)文檔可參考:
官方文檔
或者,Github
1.創(chuàng)建 boot.yaml
--- grpc: - name: greeter # Name of grpc entry port: 8080 # Port of grpc entry enabled: true # Enable grpc entry prom: enabled: true # Enable prometheus client # path: "metrics" # Default value is "metrics", set path as needed.
2.創(chuàng)建 main.go
package main import ( "context" "github.com/rookie-ninja/rk-boot" ) // Application entrance. func main() { // Create a new boot instance. boot := rkboot.NewBoot() // Bootstrap boot.Bootstrap(context.Background()) // Wait for shutdown sig boot.WaitForShutdownSig(context.Background()) }
3.啟動(dòng) main.go
$ go run main.go
4.驗(yàn)證
訪問:http://localhost:8080/metrics
Prometheus 客戶端中添加監(jiān)控
我們需要先了解 Prometheus 中的如下概念。
1.在 main.go 中添加監(jiān)控項(xiàng)
package main import ( "context" "github.com/rookie-ninja/rk-boot" "github.com/rookie-ninja/rk-prom" ) // Application entrance. func main() { // Create a new boot instance. boot := rkboot.NewBoot() // Bootstrap boot.Bootstrap(context.Background()) // Create a metrics set into prometheus.Registerer set := rkprom.NewMetricsSet("rk", "demo", boot.GetGrpcEntry("greeter").GwEntry.PromEntry.Registerer) // Register counter, gauge, histogram, summary set.RegisterCounter("my_counter", "label") set.RegisterGauge("my_gauge", "label") set.RegisterHistogram("my_histogram", []float64{}, "label") set.RegisterSummary("my_summary", rkprom.SummaryObjectives, "label") // Increase counter, gauge, histogram, summary with label value set.GetCounterWithValues("my_counter", "value").Inc() set.GetGaugeWithValues("my_gauge", "value").Add(1.0) set.GetHistogramWithValues("my_histogram", "value").Observe(0.1) set.GetSummaryWithValues("my_summary", "value").Observe(0.1) // Wait for shutdown sig boot.WaitForShutdownSig(context.Background()) }
2.啟動(dòng) main.go
$ go run main.go
3.驗(yàn)證
訪問:http://localhost:8080/metrics
推送到 prometheus pushgateway
接下來,我們看一下,如何讓 gRPC 服務(wù),自動(dòng)把監(jiān)控?cái)?shù)據(jù)推送到遠(yuǎn)程 Pushgateway 中。
1.boot.yaml 中啟動(dòng) pusher
--- grpc: - name: greeter # Name of grpc entry port: 8080 # Port of grpc entry enabled: true # Enable grpc entry prom: enabled: true # Enable prometheus client pusher: enabled : true # Enable backend job push metrics to remote pushgateway jobName: "demo" # Name of current push job remoteAddress: "localhost:9091" # Remote address of pushgateway intervalMs: 2000 # Push interval in milliseconds # basicAuth: "user:pass" # Basic auth of pushgateway # cert: # ref: "ref" # Cert reference defined in CertEntry. Please see advanced user guide for details.
2.在本地啟動(dòng) pushgateway
我們使用 docker 啟動(dòng) pushgateway
$ docker run prom/pushgateway -p 9091:9091
3.啟動(dòng) main.go
$ go run main.go
4.驗(yàn)證
訪問:http://localhost:8080/metrics
Docker
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。