metrics

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 23, 2022 License: MIT Imports: 7 Imported by: 0

README

metrics

grpc的server和client端指标,可以使用prometheus继续采集这些指标。

使用示例

grpc server
func UnaryServerLabels(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
	// 设置prometheus公共标签
	tag := grpc_ctxtags.NewTags().
		Set(serverNameLabelKey, serverNameLabelValue).
		Set(envLabelKey, envLabelValue)
	newCtx := grpc_ctxtags.SetInContext(ctx, tag)

	return handler(newCtx, req)
}

func getServerOptions() []grpc.ServerOption {
	var options []grpc.ServerOption

	// metrics拦截器
	option := grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
		//UnaryServerLabels,                  // 标签
		metrics.UnaryServerMetrics(
			// metrics.WithCounterMetrics(customizedCounterMetric) // 添加自定义指标
		), // 一元rpc的metrics拦截器
	))
	options = append(options, option)

	option = grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
		metrics.StreamServerMetrics(), // 流式rpc的metrics拦截器
	))
	options = append(options, option)

	return options
}

func main() {
	rand.Seed(time.Now().UnixNano())

	addr := ":8080"
	fmt.Println("start rpc server", addr)

	list, err := net.Listen("tcp", addr)
	if err != nil {
		panic(err)
	}

	server := grpc.NewServer(getServerOptions()...)
	pb.RegisterGreeterServer(server, &GreeterServer{})

	// 启动metrics服务器,默认采集grpc指标,开启、go指标
	metrics.ServerHTTPService(":9092", server)
	fmt.Println("start metrics server", ":9092")

	err = server.Serve(list)
	if err != nil {
		panic(err)
	}
}

grpc client
func getDialOptions() []grpc.DialOption {
	var options []grpc.DialOption

	// 禁用tls
	options = append(options, grpc.WithTransportCredentials(insecure.NewCredentials()))

	// Metrics
	options = append(options, grpc.WithUnaryInterceptor(metrics.UnaryClientMetrics()))
	options = append(options, grpc.WithStreamInterceptor(metrics.StreamClientMetrics()))
	return options
}

func main() {
	conn, err := grpc.Dial("127.0.0.1:8080", getDialOptions()...)

	metrics.ClientHTTPService(":9094")
	fmt.Println("start metrics server", ":9094")

	client := pb.NewGreeterClient(conn)
	i := 0
	for {
		i++
		time.Sleep(time.Millisecond * 500) // qps is 2
		err = sayHello(client, i)
		if err != nil {
			fmt.Println(err)
		}
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClientHTTPService

func ClientHTTPService(addr string) *http.Server

ClientHTTPService 初始化客户端的prometheus的exporter服务,使用 http://ip:port/metrics 获取数据

func GoHTTPService

func GoHTTPService(addr string, grpcServer *grpc.Server) *http.Server

GoHTTPService 初始化服务端的prometheus的exporter服务,使用 http://ip:port/metrics 获取数据

func StreamClientMetrics

func StreamClientMetrics() grpc.StreamClientInterceptor

StreamClientMetrics metrics stream拦截器

func StreamServerMetrics

func StreamServerMetrics(opts ...MetricsOption) grpc.StreamServerInterceptor

StreamServerMetrics metrics stream拦截器

func UnaryClientMetrics

func UnaryClientMetrics() grpc.UnaryClientInterceptor

UnaryClientMetrics metrics unary拦截器

func UnaryServerMetrics

func UnaryServerMetrics(opts ...MetricsOption) grpc.UnaryServerInterceptor

UnaryServerMetrics metrics unary拦截器

Types

type MetricsOption

type MetricsOption func(*metricsOptions)

MetricsOption 设置metrics

func WithCounterMetrics

func WithCounterMetrics(metrics ...*prometheus.CounterVec) MetricsOption

WithCounterMetrics 添加Counter类型指标

func WithGaugeMetrics

func WithGaugeMetrics(metrics ...*prometheus.GaugeVec) MetricsOption

WithGaugeMetrics 添加Gauge类型指标

func WithHistogramMetrics

func WithHistogramMetrics(metrics ...*prometheus.HistogramVec) MetricsOption

WithHistogramMetrics 添加Histogram类型指标

func WithSummaryMetrics

func WithSummaryMetrics(metrics ...*prometheus.SummaryVec) MetricsOption

WithSummaryMetrics 添加Summary类型指标

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL