khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 1 | // Copyright 2016 Michal Witkowski. All Rights Reserved. |
| 2 | // See LICENSE for licensing terms. |
| 3 | |
| 4 | package grpc_prometheus |
| 5 | |
| 6 | import ( |
| 7 | "time" |
| 8 | |
| 9 | "google.golang.org/grpc/codes" |
| 10 | ) |
| 11 | |
| 12 | type clientReporter struct { |
| 13 | metrics *ClientMetrics |
| 14 | rpcType grpcType |
| 15 | serviceName string |
| 16 | methodName string |
| 17 | startTime time.Time |
| 18 | } |
| 19 | |
| 20 | func newClientReporter(m *ClientMetrics, rpcType grpcType, fullMethod string) *clientReporter { |
| 21 | r := &clientReporter{ |
| 22 | metrics: m, |
| 23 | rpcType: rpcType, |
| 24 | } |
| 25 | if r.metrics.clientHandledHistogramEnabled { |
| 26 | r.startTime = time.Now() |
| 27 | } |
| 28 | r.serviceName, r.methodName = splitMethodName(fullMethod) |
| 29 | r.metrics.clientStartedCounter.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName).Inc() |
| 30 | return r |
| 31 | } |
| 32 | |
| 33 | func (r *clientReporter) ReceivedMessage() { |
| 34 | r.metrics.clientStreamMsgReceived.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName).Inc() |
| 35 | } |
| 36 | |
| 37 | func (r *clientReporter) SentMessage() { |
| 38 | r.metrics.clientStreamMsgSent.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName).Inc() |
| 39 | } |
| 40 | |
| 41 | func (r *clientReporter) Handled(code codes.Code) { |
| 42 | r.metrics.clientHandledCounter.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName, code.String()).Inc() |
| 43 | if r.metrics.clientHandledHistogramEnabled { |
| 44 | r.metrics.clientHandledHistogram.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName).Observe(time.Since(r.startTime).Seconds()) |
| 45 | } |
| 46 | } |