GRPC: 如何在 gRPC 服务中加入 Prometheus 监控?

网友投稿 516 2022-05-30

介绍

本文将介绍如何在 gRPC 微服务中,加入 prometheus 监控。

gRPC 函数的自动监控,将会在后续的文章中介绍,这里我们只介绍如何在 gRPC 代码中,实现 prometheus 监控。

我们将会使用 rk-boot 来启动 gRPC 服务。

我们将会使用 rk-prom 来启动 prometheus 客户端。

安装

go get github.com/rookie-ninja/rk-boot

快速开始

详细文档可参考:

官方文档

或者,Github

1.创建 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.创建 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.启动 main.go

$ go run main.go

4.验证

访问:http://localhost:8080/metrics

Prometheus 客户端中添加监控

我们需要先了解 Prometheus 中的如下概念。

1.在 main.go 中添加监控项

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.启动 main.go

$ go run main.go

3.验证

访问:http://localhost:8080/metrics

GRPC: 如何在 gRPC 服务中加入 Prometheus 监控?

推送到 prometheus pushgateway

接下来,我们看一下,如何让 gRPC 服务,自动把监控数据推送到远程 Pushgateway 中。

1.boot.yaml 中启动 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.在本地启动 pushgateway

我们使用 docker 启动 pushgateway

$ docker run prom/pushgateway -p 9091:9091

3.启动 main.go

$ go run main.go

4.验证

访问:http://localhost:8080/metrics

Docker

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:云合同电子印章/签章授权、在线验签功能正式上线发布
下一篇:C80迁移到651使用651集群资源方法
相关文章