|
| 1 | +package metrics |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/prometheus/client_golang/prometheus" |
| 5 | + openmetrics "github.com/prometheus/client_golang/prometheus" |
| 6 | +) |
| 7 | + |
| 8 | +// ClientMetrics represents a collection of metrics to be registered on a |
| 9 | +// Prometheus metrics registry for a gRPC client. |
| 10 | +type ClientMetrics struct { |
| 11 | + clientRegister openmetrics.Registerer |
| 12 | + |
| 13 | + clientStartedCounter *openmetrics.CounterVec |
| 14 | + clientHandledCounter *openmetrics.CounterVec |
| 15 | + clientStreamMsgReceived *openmetrics.CounterVec |
| 16 | + clientStreamMsgSent *openmetrics.CounterVec |
| 17 | + |
| 18 | + clientHandledHistogramEnabled bool |
| 19 | + clientHandledHistogramOpts openmetrics.HistogramOpts |
| 20 | + clientHandledHistogram *openmetrics.HistogramVec |
| 21 | + |
| 22 | + clientStreamRecvHistogramEnabled bool |
| 23 | + clientStreamRecvHistogramOpts openmetrics.HistogramOpts |
| 24 | + clientStreamRecvHistogram *openmetrics.HistogramVec |
| 25 | + |
| 26 | + clientStreamSendHistogramEnabled bool |
| 27 | + clientStreamSendHistogramOpts openmetrics.HistogramOpts |
| 28 | + clientStreamSendHistogram *openmetrics.HistogramVec |
| 29 | +} |
| 30 | + |
| 31 | +// NewClientMetrics returns a ClientMetrics object. Use a new instance of |
| 32 | +// ClientMetrics when not using the default Prometheus metrics registry, for |
| 33 | +// example when wanting to control which metrics are added to a registry as |
| 34 | +// opposed to automatically adding metrics via init functions. |
| 35 | +func NewClientMetrics(clientRegistry prometheus.Registerer, counterOpts ...CounterOption) *ClientMetrics { |
| 36 | + opts := counterOptions(counterOpts) |
| 37 | + return &ClientMetrics{ |
| 38 | + clientRegister: clientRegistry, |
| 39 | + clientStartedCounter: openmetrics.NewCounterVec( |
| 40 | + opts.apply(openmetrics.CounterOpts{ |
| 41 | + Name: "grpc_client_started_total", |
| 42 | + Help: "Total number of RPCs started on the client.", |
| 43 | + }), []string{"grpc_type", "grpc_service", "grpc_method"}), |
| 44 | + |
| 45 | + clientHandledCounter: openmetrics.NewCounterVec( |
| 46 | + opts.apply(openmetrics.CounterOpts{ |
| 47 | + Name: "grpc_client_handled_total", |
| 48 | + Help: "Total number of RPCs completed by the client, regardless of success or failure.", |
| 49 | + }), []string{"grpc_type", "grpc_service", "grpc_method", "grpc_code"}), |
| 50 | + |
| 51 | + clientStreamMsgReceived: openmetrics.NewCounterVec( |
| 52 | + opts.apply(openmetrics.CounterOpts{ |
| 53 | + Name: "grpc_client_msg_received_total", |
| 54 | + Help: "Total number of RPC stream messages received by the client.", |
| 55 | + }), []string{"grpc_type", "grpc_service", "grpc_method"}), |
| 56 | + |
| 57 | + clientStreamMsgSent: openmetrics.NewCounterVec( |
| 58 | + opts.apply(openmetrics.CounterOpts{ |
| 59 | + Name: "grpc_client_msg_sent_total", |
| 60 | + Help: "Total number of gRPC stream messages sent by the client.", |
| 61 | + }), []string{"grpc_type", "grpc_service", "grpc_method"}), |
| 62 | + |
| 63 | + clientHandledHistogramEnabled: false, |
| 64 | + clientHandledHistogramOpts: openmetrics.HistogramOpts{ |
| 65 | + Name: "grpc_client_handling_seconds", |
| 66 | + Help: "Histogram of response latency (seconds) of the gRPC until it is finished by the application.", |
| 67 | + Buckets: openmetrics.DefBuckets, |
| 68 | + }, |
| 69 | + clientHandledHistogram: nil, |
| 70 | + clientStreamRecvHistogramEnabled: false, |
| 71 | + clientStreamRecvHistogramOpts: openmetrics.HistogramOpts{ |
| 72 | + Name: "grpc_client_msg_recv_handling_seconds", |
| 73 | + Help: "Histogram of response latency (seconds) of the gRPC single message receive.", |
| 74 | + Buckets: openmetrics.DefBuckets, |
| 75 | + }, |
| 76 | + clientStreamRecvHistogram: nil, |
| 77 | + clientStreamSendHistogramEnabled: false, |
| 78 | + clientStreamSendHistogramOpts: openmetrics.HistogramOpts{ |
| 79 | + Name: "grpc_client_msg_send_handling_seconds", |
| 80 | + Help: "Histogram of response latency (seconds) of the gRPC single message send.", |
| 81 | + Buckets: openmetrics.DefBuckets, |
| 82 | + }, |
| 83 | + clientStreamSendHistogram: nil, |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// Register registers the provided Collector with the custom register. |
| 88 | +// returns error much like DefaultRegisterer of Prometheus. |
| 89 | +func (m *ClientMetrics) Register(c openmetrics.Collector) error { |
| 90 | + return m.clientRegister.Register(c) |
| 91 | +} |
| 92 | + |
| 93 | +// MustRegister registers the provided Collectors with the custom Registerer |
| 94 | +// and panics if any error occurs much like DefaultRegisterer of Prometheus. |
| 95 | +func (m *ClientMetrics) MustRegister(c openmetrics.Collector) { |
| 96 | + m.clientRegister.MustRegister(c) |
| 97 | +} |
| 98 | + |
| 99 | +// Describe sends the super-set of all possible descriptors of metrics |
| 100 | +// collected by this Collector to the provided channel and returns once |
| 101 | +// the last descriptor has been sent. |
| 102 | +func (m *ClientMetrics) Describe(ch chan<- *openmetrics.Desc) { |
| 103 | + m.clientStartedCounter.Describe(ch) |
| 104 | + m.clientHandledCounter.Describe(ch) |
| 105 | + m.clientStreamMsgReceived.Describe(ch) |
| 106 | + m.clientStreamMsgSent.Describe(ch) |
| 107 | + if m.clientHandledHistogramEnabled { |
| 108 | + m.clientHandledHistogram.Describe(ch) |
| 109 | + } |
| 110 | + if m.clientStreamRecvHistogramEnabled { |
| 111 | + m.clientStreamRecvHistogram.Describe(ch) |
| 112 | + } |
| 113 | + if m.clientStreamSendHistogramEnabled { |
| 114 | + m.clientStreamSendHistogram.Describe(ch) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +// Collect is called by the Prometheus registry when collecting |
| 119 | +// metrics. The implementation sends each collected metric via the |
| 120 | +// provided channel and returns once the last metric has been sent. |
| 121 | +func (m *ClientMetrics) Collect(ch chan<- openmetrics.Metric) { |
| 122 | + m.clientStartedCounter.Collect(ch) |
| 123 | + m.clientHandledCounter.Collect(ch) |
| 124 | + m.clientStreamMsgReceived.Collect(ch) |
| 125 | + m.clientStreamMsgSent.Collect(ch) |
| 126 | + if m.clientHandledHistogramEnabled { |
| 127 | + m.clientHandledHistogram.Collect(ch) |
| 128 | + } |
| 129 | + if m.clientStreamRecvHistogramEnabled { |
| 130 | + m.clientStreamRecvHistogram.Collect(ch) |
| 131 | + } |
| 132 | + if m.clientStreamSendHistogramEnabled { |
| 133 | + m.clientStreamSendHistogram.Collect(ch) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +// EnableClientHandlingTimeHistogram turns on recording of handling time of RPCs. |
| 138 | +// Histogram metrics can be very expensive for Prometheus to retain and query. |
| 139 | +func (m *ClientMetrics) EnableClientHandlingTimeHistogram(opts ...HistogramOption) error { |
| 140 | + for _, o := range opts { |
| 141 | + o(&m.clientHandledHistogramOpts) |
| 142 | + } |
| 143 | + if !m.clientHandledHistogramEnabled { |
| 144 | + m.clientHandledHistogram = openmetrics.NewHistogramVec( |
| 145 | + m.clientHandledHistogramOpts, |
| 146 | + []string{"grpc_type", "grpc_service", "grpc_method"}, |
| 147 | + ) |
| 148 | + } |
| 149 | + m.clientHandledHistogramEnabled = true |
| 150 | + return m.clientRegister.Register(m.clientHandledHistogram) |
| 151 | +} |
| 152 | + |
| 153 | +// EnableClientStreamReceiveTimeHistogram turns on recording of single message receive time of streaming RPCs. |
| 154 | +// Histogram metrics can be very expensive for Prometheus to retain and query. |
| 155 | +func (m *ClientMetrics) EnableClientStreamReceiveTimeHistogram(opts ...HistogramOption) error { |
| 156 | + for _, o := range opts { |
| 157 | + o(&m.clientStreamRecvHistogramOpts) |
| 158 | + } |
| 159 | + |
| 160 | + if !m.clientStreamRecvHistogramEnabled { |
| 161 | + m.clientStreamRecvHistogram = openmetrics.NewHistogramVec( |
| 162 | + m.clientStreamRecvHistogramOpts, |
| 163 | + []string{"grpc_type", "grpc_service", "grpc_method"}, |
| 164 | + ) |
| 165 | + } |
| 166 | + |
| 167 | + m.clientStreamRecvHistogramEnabled = true |
| 168 | + return m.clientRegister.Register(m.clientStreamRecvHistogram) |
| 169 | +} |
| 170 | + |
| 171 | +// EnableClientStreamSendTimeHistogram turns on recording of single message send time of streaming RPCs. |
| 172 | +// Histogram metrics can be very expensive for Prometheus to retain and query. |
| 173 | +func (m *ClientMetrics) EnableClientStreamSendTimeHistogram(opts ...HistogramOption) error { |
| 174 | + for _, o := range opts { |
| 175 | + o(&m.clientStreamSendHistogramOpts) |
| 176 | + } |
| 177 | + |
| 178 | + if !m.clientStreamSendHistogramEnabled { |
| 179 | + m.clientStreamSendHistogram = openmetrics.NewHistogramVec( |
| 180 | + m.clientStreamSendHistogramOpts, |
| 181 | + []string{"grpc_type", "grpc_service", "grpc_method"}, |
| 182 | + ) |
| 183 | + } |
| 184 | + |
| 185 | + m.clientStreamSendHistogramEnabled = true |
| 186 | + return m.clientRegister.Register(m.clientStreamSendHistogram) |
| 187 | +} |
0 commit comments