khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -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 serverReporter struct { |
| 13 | metrics *ServerMetrics |
| 14 | rpcType grpcType |
| 15 | serviceName string |
| 16 | methodName string |
| 17 | startTime time.Time |
| 18 | } |
| 19 | |
| 20 | func newServerReporter(m *ServerMetrics, rpcType grpcType, fullMethod string) *serverReporter { |
| 21 | r := &serverReporter{ |
| 22 | metrics: m, |
| 23 | rpcType: rpcType, |
| 24 | } |
| 25 | if r.metrics.serverHandledHistogramEnabled { |
| 26 | r.startTime = time.Now() |
| 27 | } |
| 28 | r.serviceName, r.methodName = splitMethodName(fullMethod) |
| 29 | r.metrics.serverStartedCounter.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName).Inc() |
| 30 | return r |
| 31 | } |
| 32 | |
| 33 | func (r *serverReporter) ReceivedMessage() { |
| 34 | r.metrics.serverStreamMsgReceived.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName).Inc() |
| 35 | } |
| 36 | |
| 37 | func (r *serverReporter) SentMessage() { |
| 38 | r.metrics.serverStreamMsgSent.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName).Inc() |
| 39 | } |
| 40 | |
| 41 | func (r *serverReporter) Handled(code codes.Code) { |
| 42 | r.metrics.serverHandledCounter.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName, code.String()).Inc() |
| 43 | if r.metrics.serverHandledHistogramEnabled { |
| 44 | r.metrics.serverHandledHistogram.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName).Observe(time.Since(r.startTime).Seconds()) |
| 45 | } |
| 46 | } |