前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >gRPC: 如何在 gRPC 服务中加入 Prometheus 监控?

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

原创
作者头像
尹东勋
修改2021-12-13 01:19:04
1.5K0
修改2021-12-13 01:19:04
举报
文章被收录于专栏:开源 & 技术分享

介绍

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

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

  • 我们将会使用 rk-boot 来启动 gRPC 服务。
  • 我们将会使用 rk-prom 来启动 prometheus 客户端。

请访问如下地址获取完整教程:https://rkdev.info/cn https://rkdocs.netlify.app/cn (备用)

安装

代码语言:txt
复制
go get github.com/rookie-ninja/rk-boot
go get github.com/rookie-ninja/rk-grpc

快速开始

详细文档可参考:

1.创建 boot.yaml

代码语言:txt
复制
---
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

代码语言:txt
复制
package main

import (
	"context"
	"github.com/rookie-ninja/rk-boot"
	_ "github.com/rookie-ninja/rk-grpc/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

代码语言:txt
复制
$ go run main.go

4.验证

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

Prometheus 客户端中添加监控

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

名字

详情

RK 自定义的结构,通过 MetricsSet 注册 Prometheus 的 Counter,Gauge,Histogram 和 Summary

Prometheus 会通过 Registrerer 来管理 Counter,Gauge,Histogram 和 Summary

Counter 是一个累积度量,表示单个单调增加的计数器,其值只能增加或重置为零

Gauge 值可以随意加减

Histogram 进行采样(通常是请求持续时间或响应大小之类的内容)并将它们计算在可配置的桶中,同时还提供所有观测值的总和

与 Histogram 类似,摘要样本观察(通常是请求持续时间和响应大小之类的东西)

Prometheus Namespace

Prometheus 监控名格式: namespace_subSystem_metricsName

Prometheus SubSystem

Prometheus 监控名格式: namespace_subSystem_metricsName

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

代码语言:txt
复制
package main

import (
	"context"
	"github.com/rookie-ninja/rk-boot"
	"github.com/rookie-ninja/rk-grpc/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.GetEntry("greeter").(*rkgrpc.GrpcEntry).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

代码语言:txt
复制
$ go run main.go

3.验证

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

推送到 prometheus pushgateway

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

1.boot.yaml 中启动 pusher

代码语言:txt
复制
---
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

代码语言:txt
复制
$ docker run prom/pushgateway -p 9091:9091

3.启动 main.go

代码语言:txt
复制
$ go run main.go

4.验证

访问:http://localhost:9091/metrics

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 介绍
  • 安装
  • 快速开始
    • 1.创建 boot.yaml
      • 2.创建 main.go
        • 3.启动 main.go
          • 4.验证
          • Prometheus 客户端中添加监控
            • 1.在 main.go 中添加监控项
              • 2.启动 main.go
                • 3.验证
                • 推送到 prometheus pushgateway
                  • 1.boot.yaml 中启动 pusher
                    • 2.在本地启动 pushgateway
                      • 3.启动 main.go
                        • 4.验证
                        相关产品与服务
                        Prometheus 监控服务
                        Prometheus 监控服务(TencentCloud Managed Service for Prometheus,TMP)是基于开源 Prometheus 构建的高可用、全托管的服务,与腾讯云容器服务(TKE)高度集成,兼容开源生态丰富多样的应用组件,结合腾讯云可观测平台-告警管理和 Prometheus Alertmanager 能力,为您提供免搭建的高效运维能力,减少开发及运维成本。
                        领券
                        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档