build(deps): bump github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus

Bumps [github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus](https://github.com/grpc-ecosystem/go-grpc-middleware) from 1.0.0 to 1.0.1.
- [Release notes](https://github.com/grpc-ecosystem/go-grpc-middleware/releases)
- [Commits](https://github.com/grpc-ecosystem/go-grpc-middleware/compare/v1.0.0...providers/prometheus/v1.0.1)

---
updated-dependencies:
- dependency-name: github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
This commit is contained in:
dependabot[bot]
2024-04-30 19:10:24 +00:00
committed by GitHub
parent c2c8730596
commit 93690baf4e
15 changed files with 2989 additions and 381 deletions

View File

@@ -7,6 +7,7 @@ import (
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors"
"github.com/prometheus/client_golang/prometheus"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
// ServerMetrics represents a collection of metrics to be registered on a
@@ -81,7 +82,7 @@ func (m *ServerMetrics) Collect(ch chan<- prometheus.Metric) {
// value, for all gRPC methods registered on a gRPC server. This is useful, to
// ensure that all metrics exist when collecting and querying.
// NOTE: This might add significant cardinality and might not be needed in future version of Prometheus (created timestamp).
func (m *ServerMetrics) InitializeMetrics(server *grpc.Server) {
func (m *ServerMetrics) InitializeMetrics(server reflection.ServiceInfoProvider) {
serviceInfo := server.GetServiceInfo()
for serviceName, info := range serviceInfo {
for _, mInfo := range info.Methods {

View File

@@ -0,0 +1,66 @@
// Copyright (c) The go-grpc-middleware Authors.
// Licensed under the Apache License 2.0.
package interceptors
import (
"fmt"
"strings"
"google.golang.org/grpc"
)
func splitFullMethodName(fullMethod string) (string, string) {
fullMethod = strings.TrimPrefix(fullMethod, "/") // remove leading slash
if i := strings.Index(fullMethod, "/"); i >= 0 {
return fullMethod[:i], fullMethod[i+1:]
}
return "unknown", "unknown"
}
type CallMeta struct {
ReqOrNil any
Typ GRPCType
Service string
Method string
IsClient bool
}
func NewClientCallMeta(fullMethod string, streamDesc *grpc.StreamDesc, reqOrNil any) CallMeta {
c := CallMeta{IsClient: true, ReqOrNil: reqOrNil, Typ: Unary}
if streamDesc != nil {
c.Typ = clientStreamType(streamDesc)
}
c.Service, c.Method = splitFullMethodName(fullMethod)
return c
}
func NewServerCallMeta(fullMethod string, streamInfo *grpc.StreamServerInfo, reqOrNil any) CallMeta {
c := CallMeta{IsClient: false, ReqOrNil: reqOrNil, Typ: Unary}
if streamInfo != nil {
c.Typ = serverStreamType(streamInfo)
}
c.Service, c.Method = splitFullMethodName(fullMethod)
return c
}
func (c CallMeta) FullMethod() string {
return fmt.Sprintf("/%s/%s", c.Service, c.Method)
}
func clientStreamType(desc *grpc.StreamDesc) GRPCType {
if desc.ClientStreams && !desc.ServerStreams {
return ClientStream
} else if !desc.ClientStreams && desc.ServerStreams {
return ServerStream
}
return BidiStream
}
func serverStreamType(info *grpc.StreamServerInfo) GRPCType {
if info.IsClientStream && !info.IsServerStream {
return ClientStream
} else if !info.IsClientStream && info.IsServerStream {
return ServerStream
}
return BidiStream
}

View File

@@ -15,9 +15,9 @@ import (
// UnaryClientInterceptor is a gRPC client-side interceptor that provides reporting for Unary RPCs.
func UnaryClientInterceptor(reportable ClientReportable) grpc.UnaryClientInterceptor {
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
r := newReport(Unary, method)
reporter, newCtx := reportable.ClientReporter(ctx, CallMeta{ReqProtoOrNil: req, Typ: r.rpcType, Service: r.service, Method: r.method})
return func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
r := newReport(NewClientCallMeta(method, nil, req))
reporter, newCtx := reportable.ClientReporter(ctx, r.callMeta)
reporter.PostMsgSend(req, nil, time.Since(r.startTime))
err := invoker(newCtx, method, req, reply, cc, opts...)
@@ -30,8 +30,8 @@ func UnaryClientInterceptor(reportable ClientReportable) grpc.UnaryClientInterce
// StreamClientInterceptor is a gRPC client-side interceptor that provides reporting for Stream RPCs.
func StreamClientInterceptor(reportable ClientReportable) grpc.StreamClientInterceptor {
return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
r := newReport(clientStreamType(desc), method)
reporter, newCtx := reportable.ClientReporter(ctx, CallMeta{ReqProtoOrNil: nil, Typ: r.rpcType, Service: r.service, Method: r.method})
r := newReport(NewClientCallMeta(method, desc, nil))
reporter, newCtx := reportable.ClientReporter(ctx, r.callMeta)
clientStream, err := streamer(newCtx, desc, cc, method, opts...)
if err != nil {
@@ -42,15 +42,6 @@ func StreamClientInterceptor(reportable ClientReportable) grpc.StreamClientInter
}
}
func clientStreamType(desc *grpc.StreamDesc) GRPCType {
if desc.ClientStreams && !desc.ServerStreams {
return ClientStream
} else if !desc.ClientStreams && desc.ServerStreams {
return ServerStream
}
return BidiStream
}
// monitoredClientStream wraps grpc.ClientStream allowing each Sent/Recv of message to report.
type monitoredClientStream struct {
grpc.ClientStream
@@ -59,14 +50,14 @@ type monitoredClientStream struct {
reporter Reporter
}
func (s *monitoredClientStream) SendMsg(m interface{}) error {
func (s *monitoredClientStream) SendMsg(m any) error {
start := time.Now()
err := s.ClientStream.SendMsg(m)
s.reporter.PostMsgSend(m, err, time.Since(start))
return err
}
func (s *monitoredClientStream) RecvMsg(m interface{}) error {
func (s *monitoredClientStream) RecvMsg(m any) error {
start := time.Now()
err := s.ClientStream.RecvMsg(m)
s.reporter.PostMsgReceive(m, err, time.Since(start))

View File

@@ -5,8 +5,6 @@ package interceptors
import (
"context"
"fmt"
"strings"
"time"
"google.golang.org/grpc/codes"
@@ -14,23 +12,6 @@ import (
type GRPCType string
// Timer is a helper interface to time functions.
// Useful for interceptors to record the total
// time elapsed since completion of a call.
type Timer interface {
ObserveDuration() time.Duration
}
// zeroTimer.
type zeroTimer struct {
}
func (zeroTimer) ObserveDuration() time.Duration {
return 0
}
var EmptyTimer = &zeroTimer{}
const (
Unary GRPCType = "unary"
ClientStream GRPCType = "client_stream"
@@ -47,25 +28,6 @@ var (
}
)
func SplitMethodName(fullMethod string) (string, string) {
fullMethod = strings.TrimPrefix(fullMethod, "/") // remove leading slash
if i := strings.Index(fullMethod, "/"); i >= 0 {
return fullMethod[:i], fullMethod[i+1:]
}
return "unknown", "unknown"
}
type CallMeta struct {
ReqProtoOrNil interface{}
Typ GRPCType
Service string
Method string
}
func (c CallMeta) FullMethod() string {
return fmt.Sprintf("/%s/%s", c.Service, c.Method)
}
type ClientReportable interface {
ClientReporter(context.Context, CallMeta) (Reporter, context.Context)
}
@@ -75,42 +37,39 @@ type ServerReportable interface {
}
// CommonReportableFunc helper allows an easy way to implement reporter with common client and server logic.
type CommonReportableFunc func(ctx context.Context, c CallMeta, isClient bool) (Reporter, context.Context)
type CommonReportableFunc func(ctx context.Context, c CallMeta) (Reporter, context.Context)
func (f CommonReportableFunc) ClientReporter(ctx context.Context, c CallMeta) (Reporter, context.Context) {
return f(ctx, c, true)
return f(ctx, c)
}
func (f CommonReportableFunc) ServerReporter(ctx context.Context, c CallMeta) (Reporter, context.Context) {
return f(ctx, c, false)
return f(ctx, c)
}
type Reporter interface {
PostCall(err error, rpcDuration time.Duration)
PostMsgSend(reqProto interface{}, err error, sendDuration time.Duration)
PostMsgReceive(replyProto interface{}, err error, recvDuration time.Duration)
PostMsgSend(reqProto any, err error, sendDuration time.Duration)
PostMsgReceive(replyProto any, err error, recvDuration time.Duration)
}
var _ Reporter = NoopReporter{}
type NoopReporter struct{}
func (NoopReporter) PostCall(error, time.Duration) {}
func (NoopReporter) PostMsgSend(interface{}, error, time.Duration) {}
func (NoopReporter) PostMsgReceive(interface{}, error, time.Duration) {}
func (NoopReporter) PostCall(error, time.Duration) {}
func (NoopReporter) PostMsgSend(any, error, time.Duration) {}
func (NoopReporter) PostMsgReceive(any, error, time.Duration) {}
type report struct {
rpcType GRPCType
service string
method string
callMeta CallMeta
startTime time.Time
}
func newReport(typ GRPCType, fullMethod string) report {
func newReport(callMeta CallMeta) report {
r := report{
startTime: time.Now(),
rpcType: typ,
callMeta: callMeta,
}
r.service, r.method = SplitMethodName(fullMethod)
return r
}

View File

@@ -14,9 +14,9 @@ import (
// UnaryServerInterceptor is a gRPC server-side interceptor that provides reporting for Unary RPCs.
func UnaryServerInterceptor(reportable ServerReportable) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
r := newReport(Unary, info.FullMethod)
reporter, newCtx := reportable.ServerReporter(ctx, CallMeta{ReqProtoOrNil: req, Typ: r.rpcType, Service: r.service, Method: r.method})
return func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
r := newReport(NewServerCallMeta(info.FullMethod, nil, req))
reporter, newCtx := reportable.ServerReporter(ctx, r.callMeta)
reporter.PostMsgReceive(req, nil, time.Since(r.startTime))
resp, err := handler(newCtx, req)
@@ -29,24 +29,15 @@ func UnaryServerInterceptor(reportable ServerReportable) grpc.UnaryServerInterce
// StreamServerInterceptor is a gRPC server-side interceptor that provides reporting for Streaming RPCs.
func StreamServerInterceptor(reportable ServerReportable) grpc.StreamServerInterceptor {
return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
r := newReport(ServerStream, info.FullMethod)
reporter, newCtx := reportable.ServerReporter(ss.Context(), CallMeta{ReqProtoOrNil: nil, Typ: StreamRPCType(info), Service: r.service, Method: r.method})
return func(srv any, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
r := newReport(NewServerCallMeta(info.FullMethod, info, nil))
reporter, newCtx := reportable.ServerReporter(ss.Context(), r.callMeta)
err := handler(srv, &monitoredServerStream{ServerStream: ss, newCtx: newCtx, reporter: reporter})
reporter.PostCall(err, time.Since(r.startTime))
return err
}
}
func StreamRPCType(info *grpc.StreamServerInfo) GRPCType {
if info.IsClientStream && !info.IsServerStream {
return ClientStream
} else if !info.IsClientStream && info.IsServerStream {
return ServerStream
}
return BidiStream
}
// monitoredStream wraps grpc.ServerStream allowing each Sent/Recv of message to report.
type monitoredServerStream struct {
grpc.ServerStream
@@ -59,14 +50,14 @@ func (s *monitoredServerStream) Context() context.Context {
return s.newCtx
}
func (s *monitoredServerStream) SendMsg(m interface{}) error {
func (s *monitoredServerStream) SendMsg(m any) error {
start := time.Now()
err := s.ServerStream.SendMsg(m)
s.reporter.PostMsgSend(m, err, time.Since(start))
return err
}
func (s *monitoredServerStream) RecvMsg(m interface{}) error {
func (s *monitoredServerStream) RecvMsg(m any) error {
start := time.Now()
err := s.ServerStream.RecvMsg(m)
s.reporter.PostMsgReceive(m, err, time.Since(start))