From c97d21411eb6d7a4b314f32b1e2d893a98c2debc Mon Sep 17 00:00:00 2001 From: Dmitrii Stepanov Date: Tue, 30 May 2023 12:47:04 +0300 Subject: [PATCH] [#3] metrics: Add gRPC middleware Signed-off-by: Dmitrii Stepanov --- metrics/grpc/client.go | 31 +++++++++++++++++++++++++++++++ metrics/grpc/server.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 metrics/grpc/client.go create mode 100644 metrics/grpc/server.go diff --git a/metrics/grpc/client.go b/metrics/grpc/client.go new file mode 100644 index 0000000..4305d53 --- /dev/null +++ b/metrics/grpc/client.go @@ -0,0 +1,31 @@ +package grpc + +import ( + "git.frostfs.info/TrueCloudLab/frostfs-observability/metrics" + grpcprom "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus" + "github.com/prometheus/client_golang/prometheus" + "google.golang.org/grpc" +) + +var clientMetrics *grpcprom.ClientMetrics = grpcprom.NewClientMetrics( + grpcprom.WithClientHandlingTimeHistogram( + grpcprom.WithHistogramBuckets(prometheus.DefBuckets), + ), + grpcprom.WithClientStreamRecvHistogram( + grpcprom.WithHistogramBuckets(prometheus.DefBuckets), + ), +) + +func init() { + metrics.Register(clientMetrics) +} + +// NewUnaryClientInterceptor returns client interceptor to collect metrics from unary RPCs. +func NewUnaryClientInterceptor() grpc.UnaryClientInterceptor { + return clientMetrics.UnaryClientInterceptor() +} + +// NewStreamClientInterceptor returns client interceptor to collect metrics from stream RPCs. +func NewStreamClientInterceptor() grpc.StreamClientInterceptor { + return clientMetrics.StreamClientInterceptor() +} diff --git a/metrics/grpc/server.go b/metrics/grpc/server.go new file mode 100644 index 0000000..b47c488 --- /dev/null +++ b/metrics/grpc/server.go @@ -0,0 +1,28 @@ +package grpc + +import ( + "git.frostfs.info/TrueCloudLab/frostfs-observability/metrics" + grpcprom "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus" + "github.com/prometheus/client_golang/prometheus" + "google.golang.org/grpc" +) + +var serverMetrics *grpcprom.ServerMetrics = grpcprom.NewServerMetrics( + grpcprom.WithServerHandlingTimeHistogram( + grpcprom.WithHistogramBuckets(prometheus.DefBuckets), + ), +) + +func init() { + metrics.Register(serverMetrics) +} + +// NewUnaryServerInterceptor returns server interceptor to collect metrics from unary RPCs. +func NewUnaryServerInterceptor() grpc.UnaryServerInterceptor { + return serverMetrics.UnaryServerInterceptor() +} + +// NewStreamServerInterceptor returns server interceptor to collect metrics from stream RPCs. +func NewStreamServerInterceptor() grpc.StreamServerInterceptor { + return serverMetrics.StreamServerInterceptor() +}