| 1 |
package grpc_prometheus |
| 2 |
|
| 3 |
import ( |
| 4 |
"io" |
| 5 |
|
| 6 |
prom "github.com/prometheus/client_golang/prometheus" |
| 7 |
"golang.org/x/net/context" |
| 8 |
"google.golang.org/grpc" |
| 9 |
"google.golang.org/grpc/codes" |
| 10 |
) |
| 11 |
|
| 12 |
// ClientMetrics represents a collection of metrics to be registered on a |
| 13 |
// Prometheus metrics registry for a gRPC client. |
| 14 |
type ClientMetrics struct { |
| 15 |
clientStartedCounter *prom.CounterVec |
| 16 |
clientHandledCounter *prom.CounterVec |
| 17 |
clientStreamMsgReceived *prom.CounterVec |
| 18 |
clientStreamMsgSent *prom.CounterVec |
| 19 |
clientHandledHistogramEnabled bool |
| 20 |
clientHandledHistogramOpts prom.HistogramOpts |
| 21 |
clientHandledHistogram *prom.HistogramVec |
| 22 |
} |
| 23 |
|
| 24 |
// NewClientMetrics returns a ClientMetrics object. Use a new instance of |
| 25 |
// ClientMetrics when not using the default Prometheus metrics registry, for |
| 26 |
// example when wanting to control which metrics are added to a registry as |
| 27 |
// opposed to automatically adding metrics via init functions. |
| 28 |
func NewClientMetrics() *ClientMetrics { |
| 29 |
return &ClientMetrics{ |
| 30 |
clientStartedCounter: prom.NewCounterVec( |
| 31 |
prom.CounterOpts{ |
| 32 |
Name: "grpc_client_started_total", |
| 33 |
Help: "Total number of RPCs started on the client.", |
| 34 |
}, []string{"grpc_type", "grpc_service", "grpc_method"}), |
| 35 |
|
| 36 |
clientHandledCounter: prom.NewCounterVec( |
| 37 |
prom.CounterOpts{ |
| 38 |
Name: "grpc_client_handled_total", |
| 39 |
Help: "Total number of RPCs completed by the client, regardless of success or failure.", |
| 40 |
}, []string{"grpc_type", "grpc_service", "grpc_method", "grpc_code"}), |
| 41 |
|
| 42 |
clientStreamMsgReceived: prom.NewCounterVec( |
| 43 |
prom.CounterOpts{ |
| 44 |
Name: "grpc_client_msg_received_total", |
| 45 |
Help: "Total number of RPC stream messages received by the client.", |
| 46 |
}, []string{"grpc_type", "grpc_service", "grpc_method"}), |
| 47 |
|
| 48 |
clientStreamMsgSent: prom.NewCounterVec( |
| 49 |
prom.CounterOpts{ |
| 50 |
Name: "grpc_client_msg_sent_total", |
| 51 |
Help: "Total number of gRPC stream messages sent by the client.", |
| 52 |
}, []string{"grpc_type", "grpc_service", "grpc_method"}), |
| 53 |
|
| 54 |
clientHandledHistogramEnabled: false, |
| 55 |
clientHandledHistogramOpts: prom.HistogramOpts{ |
| 56 |
Name: "grpc_client_handling_seconds", |
| 57 |
Help: "Histogram of response latency (seconds) of the gRPC until it is finished by the application.", |
| 58 |
Buckets: prom.DefBuckets, |
| 59 |
}, |
| 60 |
clientHandledHistogram: nil, |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
// Describe sends the super-set of all possible descriptors of metrics |
| 65 |
// collected by this Collector to the provided channel and returns once |
| 66 |
// the last descriptor has been sent. |
| 67 |
func (m *ClientMetrics) Describe(ch chan<- *prom.Desc) { |
| 68 |
m.clientStartedCounter.Describe(ch) |
| 69 |
m.clientHandledCounter.Describe(ch) |
| 70 |
m.clientStreamMsgReceived.Describe(ch) |
| 71 |
m.clientStreamMsgSent.Describe(ch) |
| 72 |
if m.clientHandledHistogramEnabled { |
| 73 |
m.clientHandledHistogram.Describe(ch) |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
// Collect is called by the Prometheus registry when collecting |
| 78 |
// metrics. The implementation sends each collected metric via the |
| 79 |
// provided channel and returns once the last metric has been sent. |
| 80 |
func (m *ClientMetrics) Collect(ch chan<- prom.Metric) { |
| 81 |
m.clientStartedCounter.Collect(ch) |
| 82 |
m.clientHandledCounter.Collect(ch) |
| 83 |
m.clientStreamMsgReceived.Collect(ch) |
| 84 |
m.clientStreamMsgSent.Collect(ch) |
| 85 |
if m.clientHandledHistogramEnabled { |
| 86 |
m.clientHandledHistogram.Collect(ch) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
// EnableClientHandlingTimeHistogram turns on recording of handling time of RPCs. |
| 91 |
// Histogram metrics can be very expensive for Prometheus to retain and query. |
| 92 |
func (m *ClientMetrics) EnableClientHandlingTimeHistogram(opts ...HistogramOption) { |
| 93 |
for _, o := range opts { |
| 94 |
o(&m.clientHandledHistogramOpts) |
| 95 |
} |
| 96 |
if !m.clientHandledHistogramEnabled { |
| 97 |
m.clientHandledHistogram = prom.NewHistogramVec( |
| 98 |
m.clientHandledHistogramOpts, |
| 99 |
[]string{"grpc_type", "grpc_service", "grpc_method"}, |
| 100 |
) |
| 101 |
} |
| 102 |
m.clientHandledHistogramEnabled = true |
| 103 |
} |
| 104 |
|
| 105 |
// UnaryClientInterceptor is a gRPC client-side interceptor that provides Prometheus monitoring for Unary RPCs. |
| 106 |
func (m *ClientMetrics) UnaryClientInterceptor() func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { |
| 107 |
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { |
| 108 |
monitor := newClientReporter(m, Unary, method) |
| 109 |
monitor.SentMessage() |
| 110 |
err := invoker(ctx, method, req, reply, cc, opts...) |
| 111 |
if err != nil { |
| 112 |
monitor.ReceivedMessage() |
| 113 |
} |
| 114 |
monitor.Handled(grpc.Code(err)) |
| 115 |
return err |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
// StreamServerInterceptor is a gRPC client-side interceptor that provides Prometheus monitoring for Streaming RPCs. |
| 120 |
func (m *ClientMetrics) StreamClientInterceptor() func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) { |
| 121 |
return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) { |
| 122 |
monitor := newClientReporter(m, clientStreamType(desc), method) |
| 123 |
clientStream, err := streamer(ctx, desc, cc, method, opts...) |
| 124 |
if err != nil { |
| 125 |
monitor.Handled(grpc.Code(err)) |
| 126 |
return nil, err |
| 127 |
} |
| 128 |
return &monitoredClientStream{clientStream, monitor}, nil |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
func clientStreamType(desc *grpc.StreamDesc) grpcType { |
| 133 |
if desc.ClientStreams && !desc.ServerStreams { |
| 134 |
return ClientStream |
| 135 |
} else if !desc.ClientStreams && desc.ServerStreams { |
| 136 |
return ServerStream |
| 137 |
} |
| 138 |
return BidiStream |
| 139 |
} |
| 140 |
|
| 141 |
// monitoredClientStream wraps grpc.ClientStream allowing each Sent/Recv of message to increment counters. |
| 142 |
type monitoredClientStream struct { |
| 143 |
grpc.ClientStream |
| 144 |
monitor *clientReporter |
| 145 |
} |
| 146 |
|
| 147 |
func (s *monitoredClientStream) SendMsg(m interface{}) error { |
| 148 |
err := s.ClientStream.SendMsg(m) |
| 149 |
if err == nil { |
| 150 |
s.monitor.SentMessage() |
| 151 |
} |
| 152 |
return err |
| 153 |
} |
| 154 |
|
| 155 |
func (s *monitoredClientStream) RecvMsg(m interface{}) error { |
| 156 |
err := s.ClientStream.RecvMsg(m) |
| 157 |
if err == nil { |
| 158 |
s.monitor.ReceivedMessage() |
| 159 |
} else if err == io.EOF { |
| 160 |
s.monitor.Handled(codes.OK) |
| 161 |
} else { |
| 162 |
s.monitor.Handled(grpc.Code(err)) |
| 163 |
} |
| 164 |
return err |
| 165 |
} |