metrics

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2024 License: MIT Imports: 7 Imported by: 0

README

metrics

The grpc's server-side and client-side metrics can continue to be captured using prometheus.

Example of use

grpc server
import "gitee.com/yzsunjianguo/sponge/pkg/grpc/metrics"

func UnaryServerLabels(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
	// set up prometheus custom labels
	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 interceptor
	option := grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
		//UnaryServerLabels,                  // tag
		metrics.UnaryServerMetrics(
			// metrics.WithCounterMetrics(customizedCounterMetric) // adding custom metrics
		),
	))
	options = append(options, option)

	option = grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
		metrics.StreamServerMetrics(), // metrics interceptor for streaming rpc
	))
	options = append(options, option)

	return options
}

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

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

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

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

	// start metrics server, collect grpc metrics by default, turn on, go metrics
	metrics.ServerHTTPService(":8283", server)
	fmt.Println("start metrics server", ":8283")

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

grpc client
import "gitee.com/yzsunjianguo/sponge/pkg/grpc/metrics"

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

	// use insecure transfer
	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:8282", getDialOptions()...)

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

	client := serverNameV1.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

Overview

Package metrics is grpc's server-side and client-side metrics can continue to be captured using prometheus.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClientHTTPService

func ClientHTTPService(addr string) *http.Server

ClientHTTPService initialize the client's prometheus exporter service and use http://ip:port/metrics to fetch data

func ClientRegister

func ClientRegister(mux *http.ServeMux)

ClientRegister for http routing and grpc methods

func Register

func Register(mux *http.ServeMux, grpcServer *grpc.Server)

Register for http routing and grpc methods

func ServerHTTPService

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

ServerHTTPService initialize the prometheus exporter service on the server side and fetch data using http://ip:port/metrics

func SetClientPattern

func SetClientPattern(pattern string)

SetClientPattern set the client pattern

func SetServerPattern

func SetServerPattern(pattern string)

SetServerPattern set the server pattern

func StreamClientMetrics

func StreamClientMetrics() grpc.StreamClientInterceptor

StreamClientMetrics metrics stream interceptor

func StreamServerMetrics

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

StreamServerMetrics metrics stream interceptor

func UnaryClientMetrics

func UnaryClientMetrics() grpc.UnaryClientInterceptor

UnaryClientMetrics metrics unary interceptor

func UnaryServerMetrics

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

UnaryServerMetrics metrics unary interceptor

Types

type Option

type Option func(*options)

Option set metrics

func WithCounterMetrics

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

WithCounterMetrics add Counter type indicator

func WithGaugeMetrics

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

WithGaugeMetrics add Gauge type indicator

func WithHistogramMetrics

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

WithHistogramMetrics adding Histogram type indicators

func WithSummaryMetrics

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

WithSummaryMetrics add Summary type indicator

Jump to

Keyboard shortcuts

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