@@ -7,16 +7,18 @@ import (
77 prom "github.com/prometheus/client_golang/prometheus"
88 "google.golang.org/grpc"
99 "google.golang.org/grpc/codes"
10+ "google.golang.org/grpc/stats"
1011 "google.golang.org/grpc/status"
1112)
1213
1314// ClientMetrics represents a collection of metrics to be registered on a
1415// Prometheus metrics registry for a gRPC client.
1516type ClientMetrics struct {
16- clientStartedCounter * prom.CounterVec
17- clientHandledCounter * prom.CounterVec
18- clientStreamMsgReceived * prom.CounterVec
19- clientStreamMsgSent * prom.CounterVec
17+ clientStartedCounter * prom.CounterVec
18+ clientStartedCounterOpts prom.CounterOpts
19+ clientHandledCounter * prom.CounterVec
20+ clientStreamMsgReceived * prom.CounterVec
21+ clientStreamMsgSent * prom.CounterVec
2022
2123 clientHandledHistogramEnabled bool
2224 clientHandledHistogramOpts prom.HistogramOpts
@@ -29,6 +31,14 @@ type ClientMetrics struct {
2931 clientStreamSendHistogramEnabled bool
3032 clientStreamSendHistogramOpts prom.HistogramOpts
3133 clientStreamSendHistogram * prom.HistogramVec
34+
35+ clientMsgSizeReceivedHistogramEnabled bool
36+ clientMsgSizeReceivedHistogramOpts prom.HistogramOpts
37+ clientMsgSizeReceivedHistogram * prom.HistogramVec
38+
39+ clientMsgSizeSentHistogramEnabled bool
40+ clientMsgSizeSentHistogramOpts prom.HistogramOpts
41+ clientMsgSizeSentHistogram * prom.HistogramVec
3242}
3343
3444// NewClientMetrics returns a ClientMetrics object. Use a new instance of
@@ -82,7 +92,21 @@ func NewClientMetrics(counterOpts ...CounterOption) *ClientMetrics {
8292 Help : "Histogram of response latency (seconds) of the gRPC single message send." ,
8393 Buckets : prom .DefBuckets ,
8494 },
85- clientStreamSendHistogram : nil ,
95+ clientStreamSendHistogram : nil ,
96+ clientMsgSizeReceivedHistogramEnabled : false ,
97+ clientMsgSizeReceivedHistogramOpts : prom.HistogramOpts {
98+ Name : "grpc_client_msg_size_received_bytes" ,
99+ Help : "Histogram of message sizes received by the client." ,
100+ Buckets : defMsgBytesBuckets ,
101+ },
102+ clientMsgSizeReceivedHistogram : nil ,
103+ clientMsgSizeSentHistogramEnabled : false ,
104+ clientMsgSizeSentHistogramOpts : prom.HistogramOpts {
105+ Name : "grpc_client_msg_size_sent_bytes" ,
106+ Help : "Histogram of message sizes sent by the client." ,
107+ Buckets : defMsgBytesBuckets ,
108+ },
109+ clientMsgSizeSentHistogram : nil ,
86110 }
87111}
88112
@@ -103,6 +127,12 @@ func (m *ClientMetrics) Describe(ch chan<- *prom.Desc) {
103127 if m .clientStreamSendHistogramEnabled {
104128 m .clientStreamSendHistogram .Describe (ch )
105129 }
130+ if m .clientMsgSizeReceivedHistogramEnabled {
131+ m .clientMsgSizeReceivedHistogram .Describe (ch )
132+ }
133+ if m .clientMsgSizeSentHistogramEnabled {
134+ m .clientMsgSizeSentHistogram .Describe (ch )
135+ }
106136}
107137
108138// Collect is called by the Prometheus registry when collecting
@@ -122,6 +152,12 @@ func (m *ClientMetrics) Collect(ch chan<- prom.Metric) {
122152 if m .clientStreamSendHistogramEnabled {
123153 m .clientStreamSendHistogram .Collect (ch )
124154 }
155+ if m .clientMsgSizeReceivedHistogramEnabled {
156+ m .clientMsgSizeReceivedHistogram .Collect (ch )
157+ }
158+ if m .clientMsgSizeSentHistogramEnabled {
159+ m .clientMsgSizeSentHistogram .Collect (ch )
160+ }
125161}
126162
127163// EnableClientHandlingTimeHistogram turns on recording of handling time of RPCs.
@@ -173,6 +209,38 @@ func (m *ClientMetrics) EnableClientStreamSendTimeHistogram(opts ...HistogramOpt
173209 m .clientStreamSendHistogramEnabled = true
174210}
175211
212+ // EnableMsgSizeReceivedBytesHistogram turns on recording of received message size of RPCs.
213+ // Histogram metrics can be very expensive for Prometheus to retain and query. It takes
214+ // options to configure histogram options such as the defined buckets.
215+ func (m * ClientMetrics ) EnableMsgSizeReceivedBytesHistogram (opts ... HistogramOption ) {
216+ for _ , o := range opts {
217+ o (& m .clientMsgSizeReceivedHistogramOpts )
218+ }
219+ if ! m .clientMsgSizeReceivedHistogramEnabled {
220+ m .clientMsgSizeReceivedHistogram = prom .NewHistogramVec (
221+ m .clientMsgSizeReceivedHistogramOpts ,
222+ []string {"grpc_service" , "grpc_method" , "grpc_stats" },
223+ )
224+ }
225+ m .clientMsgSizeReceivedHistogramEnabled = true
226+ }
227+
228+ // EnableMsgSizeSentBytesHistogram turns on recording of sent message size of RPCs.
229+ // Histogram metrics can be very expensive for Prometheus to retain and query. It
230+ // takes options to configure histogram options such as the defined buckets.
231+ func (m * ClientMetrics ) EnableMsgSizeSentBytesHistogram (opts ... HistogramOption ) {
232+ for _ , o := range opts {
233+ o (& m .clientMsgSizeSentHistogramOpts )
234+ }
235+ if ! m .clientMsgSizeSentHistogramEnabled {
236+ m .clientMsgSizeSentHistogram = prom .NewHistogramVec (
237+ m .clientMsgSizeSentHistogramOpts ,
238+ []string {"grpc_service" , "grpc_method" , "grpc_stats" },
239+ )
240+ }
241+ m .clientMsgSizeSentHistogramEnabled = true
242+ }
243+
176244// UnaryClientInterceptor is a gRPC client-side interceptor that provides Prometheus monitoring for Unary RPCs.
177245func (m * ClientMetrics ) UnaryClientInterceptor () func (ctx context.Context , method string , req , reply interface {}, cc * grpc.ClientConn , invoker grpc.UnaryInvoker , opts ... grpc.CallOption ) error {
178246 return func (ctx context.Context , method string , req , reply interface {}, cc * grpc.ClientConn , invoker grpc.UnaryInvoker , opts ... grpc.CallOption ) error {
@@ -202,6 +270,13 @@ func (m *ClientMetrics) StreamClientInterceptor() func(ctx context.Context, desc
202270 }
203271}
204272
273+ // NewClientStatsHandler is a gRPC client-side stats.Handler that providers Prometheus monitoring for RPCs.
274+ func (m * ClientMetrics ) NewClientStatsHandler () stats.Handler {
275+ return & clientStatsHandler {
276+ clientMetrics : m ,
277+ }
278+ }
279+
205280func clientStreamType (desc * grpc.StreamDesc ) grpcType {
206281 if desc .ClientStreams && ! desc .ServerStreams {
207282 return ClientStream
0 commit comments