[#3] metrics: Add gRPC middleware

Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
Dmitrii Stepanov 2023-05-30 12:47:04 +03:00
parent 3c1b76ee51
commit c97d21411e
2 changed files with 59 additions and 0 deletions

31
metrics/grpc/client.go Normal file
View file

@ -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()
}

28
metrics/grpc/server.go Normal file
View file

@ -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()
}