This repository has been archived by the owner on Apr 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from grpc-ecosystem/feature/client-metrics
add Client-Side monitoring.
- Loading branch information
Showing
6 changed files
with
463 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2016 Michal Witkowski. All Rights Reserved. | ||
// See LICENSE for licensing terms. | ||
|
||
// gRPC Prometheus monitoring interceptors for client-side gRPC. | ||
|
||
package grpc_prometheus | ||
|
||
import ( | ||
"io" | ||
|
||
"golang.org/x/net/context" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/codes" | ||
) | ||
|
||
// UnaryClientInterceptor is a gRPC client-side interceptor that provides Prometheus monitoring for Unary RPCs. | ||
func UnaryClientInterceptor(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { | ||
monitor := newClientReporter(Unary, method) | ||
monitor.SentMessage() | ||
err := invoker(ctx, method, req, reply, cc, opts...) | ||
if err != nil { | ||
monitor.ReceivedMessage() | ||
} | ||
monitor.Handled(grpc.Code(err)) | ||
return err | ||
} | ||
|
||
// StreamServerInterceptor is a gRPC client-side interceptor that provides Prometheus monitoring for Streaming RPCs. | ||
func StreamClientInterceptor(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) { | ||
monitor := newClientReporter(clientStreamType(desc), method) | ||
clientStream, err := streamer(ctx, desc, cc, method, opts...) | ||
if err != nil { | ||
monitor.Handled(grpc.Code(err)) | ||
return nil, err | ||
} | ||
return &monitoredClientStream{clientStream, monitor}, nil | ||
} | ||
|
||
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 increment counters. | ||
type monitoredClientStream struct { | ||
grpc.ClientStream | ||
monitor *clientReporter | ||
} | ||
|
||
func (s *monitoredClientStream) SendMsg(m interface{}) error { | ||
err := s.ClientStream.SendMsg(m) | ||
if err == nil { | ||
s.monitor.SentMessage() | ||
} | ||
return err | ||
} | ||
|
||
func (s *monitoredClientStream) RecvMsg(m interface{}) error { | ||
err := s.ClientStream.RecvMsg(m) | ||
if err == nil { | ||
s.monitor.ReceivedMessage() | ||
} else if err == io.EOF { | ||
s.monitor.Handled(codes.OK) | ||
} else { | ||
s.monitor.Handled(grpc.Code(err)) | ||
} | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Copyright 2016 Michal Witkowski. All Rights Reserved. | ||
// See LICENSE for licensing terms. | ||
|
||
package grpc_prometheus | ||
|
||
import ( | ||
"time" | ||
|
||
"google.golang.org/grpc/codes" | ||
|
||
prom "github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
var ( | ||
clientStartedCounter = prom.NewCounterVec( | ||
prom.CounterOpts{ | ||
Namespace: "grpc", | ||
Subsystem: "client", | ||
Name: "started_total", | ||
Help: "Total number of RPCs started on the client.", | ||
}, []string{"grpc_type", "grpc_service", "grpc_method"}) | ||
|
||
clientHandledCounter = prom.NewCounterVec( | ||
prom.CounterOpts{ | ||
Namespace: "grpc", | ||
Subsystem: "client", | ||
Name: "handled_total", | ||
Help: "Total number of RPCs completed by the client, regardless of success or failure.", | ||
}, []string{"grpc_type", "grpc_service", "grpc_method", "grpc_code"}) | ||
|
||
clientStreamMsgReceived = prom.NewCounterVec( | ||
prom.CounterOpts{ | ||
Namespace: "grpc", | ||
Subsystem: "client", | ||
Name: "msg_received_total", | ||
Help: "Total number of RPC stream messages received by the client.", | ||
}, []string{"grpc_type", "grpc_service", "grpc_method"}) | ||
|
||
clientStreamMsgSent = prom.NewCounterVec( | ||
prom.CounterOpts{ | ||
Namespace: "grpc", | ||
Subsystem: "client", | ||
Name: "msg_sent_total", | ||
Help: "Total number of gRPC stream messages sent by the client.", | ||
}, []string{"grpc_type", "grpc_service", "grpc_method"}) | ||
|
||
clientHandledHistogramEnabled = false | ||
clientHandledHistogramOpts = prom.HistogramOpts{ | ||
Namespace: "grpc", | ||
Subsystem: "client", | ||
Name: "handling_seconds", | ||
Help: "Histogram of response latency (seconds) of the gRPC until it is finished by the application.", | ||
Buckets: prom.DefBuckets, | ||
} | ||
clientHandledHistogram *prom.HistogramVec | ||
) | ||
|
||
func init() { | ||
prom.MustRegister(clientStartedCounter) | ||
prom.MustRegister(clientHandledCounter) | ||
prom.MustRegister(clientStreamMsgReceived) | ||
prom.MustRegister(clientStreamMsgSent) | ||
} | ||
|
||
// EnableClientHandlingTimeHistogram turns on recording of handling time of RPCs. | ||
// Histogram metrics can be very expensive for Prometheus to retain and query. | ||
func EnableClientHandlingTimeHistogram(opts ...HistogramOption) { | ||
for _, o := range opts { | ||
o(&clientHandledHistogramOpts) | ||
} | ||
if !clientHandledHistogramEnabled { | ||
clientHandledHistogram = prom.NewHistogramVec( | ||
clientHandledHistogramOpts, | ||
[]string{"grpc_type", "grpc_service", "grpc_method"}, | ||
) | ||
prom.Register(clientHandledHistogram) | ||
} | ||
clientHandledHistogramEnabled = true | ||
} | ||
|
||
type clientReporter struct { | ||
rpcType grpcType | ||
serviceName string | ||
methodName string | ||
startTime time.Time | ||
} | ||
|
||
func newClientReporter(rpcType grpcType, fullMethod string) *clientReporter { | ||
r := &clientReporter{rpcType: rpcType} | ||
if clientHandledHistogramEnabled { | ||
r.startTime = time.Now() | ||
} | ||
r.serviceName, r.methodName = splitMethodName(fullMethod) | ||
clientStartedCounter.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName).Inc() | ||
return r | ||
} | ||
|
||
func (r *clientReporter) ReceivedMessage() { | ||
clientStreamMsgReceived.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName).Inc() | ||
} | ||
|
||
func (r *clientReporter) SentMessage() { | ||
clientStreamMsgSent.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName).Inc() | ||
} | ||
|
||
func (r *clientReporter) Handled(code codes.Code) { | ||
clientHandledCounter.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName, code.String()).Inc() | ||
if clientHandledHistogramEnabled { | ||
clientHandledHistogram.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName).Observe(time.Since(r.startTime).Seconds()) | ||
} | ||
} |
Oops, something went wrong.