grpcmetrics

package module
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2022 License: MIT Imports: 11 Imported by: 0

README

grpcmetrics

Prometheus metrics for gGPC servers and clients via VictoriaMetrics.

Drop-in replacement for go-grpc-prometheus.

Usage

Server
import (
	"net/http"
	
	"google.golang.org/grpc"
	"github.com/amenzhinsky/grpcmetrics"
	"github.com/VictoriaMetrics/metrics"
)

m := grpcmetrics.NewServerMetrics()
s := grpc.NewServer(
	grpc.ChainUnaryInterceptor(
		grpcmetrics.UnaryServerInterceptor(m),
		// other interceptors...
	),
	grpc.ChainStreamInterceptor(
		grpcmetrics.StreamServerInterceptor(m),
		// other interceptors...
	),
)

// register grpc services
grpc_health_v1.RegisterHealthServer(s, health.NewServer())

// optionally pre-populate metrics with services and methods registered by the server
m.InitializeMetrics(s)

http.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
	metrics.WritePrometheus(w, true)
})
Client
import (
	"net/http"

	"google.golang.org/grpc"
	"github.com/amenzhinsky/grpcmetrics"
	"github.com/VictoriaMetrics/metrics"
)

m := grpcmetrics.NewClientMetrics()
c, err := grpc.Dial("",
	grpc.WithChainUnaryInterceptor(
		grpcmetrics.UnaryClientInterceptor(m),
		// other interceptors...
	),
	grpc.WithChainStreamInterceptor(
		grpcmetrics.UnaryClientInterceptor(m)),
		// other interceptors...
	),
)
if err != nil {
	return err
}

http.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
	metrics.WritePrometheus(w, true)
})
Benchmarks

Benchmarks against client_golang interceptors (MacBook Air M1).

go test -run=none -bench=_client_golang$ -benchmem -benchtime=5s | sed s/_client_golang//g > old.txt
go test -run=none -bench=_metrics$ -benchmem -benchtime=5s | sed s/_metrics//g > new.txt
benchcmp old.txt new.txt

benchmark                              old ns/op     new ns/op     delta
BenchmarkScrapeClient-8                9191          553           -93.98%
BenchmarkUnaryClientInterceptor-8      358           235           -34.44%
BenchmarkStreamClientInterceptor-8     276           197           -28.58%
BenchmarkScrapeServer-8                41107         542           -98.68%
BenchmarkUnaryServerInterceptor-8      310           209           -32.72%
BenchmarkStreamServerInterceptor-8     331           244           -26.40%

benchmark                              old allocs     new allocs     delta
BenchmarkScrapeClient-8                65             9              -86.15%
BenchmarkUnaryClientInterceptor-8      5              0              -100.00%
BenchmarkStreamClientInterceptor-8     6              2              -66.67%
BenchmarkScrapeServer-8                267            9              -96.63%
BenchmarkUnaryServerInterceptor-8      5              0              -100.00%
BenchmarkStreamServerInterceptor-8     7              2              -71.43%

benchmark                              old bytes     new bytes     delta
BenchmarkScrapeClient-8                36861         992           -97.31%
BenchmarkUnaryClientInterceptor-8      288           0             -100.00%
BenchmarkStreamClientInterceptor-8     264           96            -63.64%
BenchmarkScrapeServer-8                59324         992           -98.33%
BenchmarkUnaryServerInterceptor-8      288           0             -100.00%
BenchmarkStreamServerInterceptor-8     328           80            -75.61%

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StreamClientInterceptor

func StreamClientInterceptor(m *ClientMetrics) grpc.StreamClientInterceptor

func StreamServerInterceptor

func StreamServerInterceptor(m *ServerMetrics) grpc.StreamServerInterceptor

func UnaryClientInterceptor

func UnaryClientInterceptor(m *ClientMetrics) grpc.UnaryClientInterceptor

func UnaryServerInterceptor

func UnaryServerInterceptor(m *ServerMetrics) grpc.UnaryServerInterceptor

Types

type ClientMetrics

type ClientMetrics struct {
	// contains filtered or unexported fields
}

func NewClientMetrics

func NewClientMetrics(opts ...ClientOption) *ClientMetrics

type ClientOption

type ClientOption func(m *ClientMetrics)

func WithClientHandlingTimeHistogram

func WithClientHandlingTimeHistogram(enable bool) ClientOption

func WithClientMetricsSet

func WithClientMetricsSet(s *metrics.Set) ClientOption

type ServerMetrics

type ServerMetrics struct {
	// contains filtered or unexported fields
}

func NewServerMetrics

func NewServerMetrics(opts ...ServerOption) *ServerMetrics

func (*ServerMetrics) InitializeMetrics

func (m *ServerMetrics) InitializeMetrics(s *grpc.Server)

type ServerOption

type ServerOption func(m *ServerMetrics)

func WithServerHandlingTimeHistogram

func WithServerHandlingTimeHistogram(enable bool) ServerOption

func WithServerMetricsSet

func WithServerMetricsSet(s *metrics.Set) ServerOption

Jump to

Keyboard shortcuts

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