blob: aa9db5401a488b863dbf6a07b5f56483439ee66f [file] [log] [blame]
khenaidooab1f7bd2019-11-14 14:00:27 -05001// Copyright 2016 Michal Witkowski. All Rights Reserved.
2// See LICENSE for licensing terms.
3
4package grpc_prometheus
5
6import (
7 "time"
8
9 "google.golang.org/grpc/codes"
10)
11
12type serverReporter struct {
13 metrics *ServerMetrics
14 rpcType grpcType
15 serviceName string
16 methodName string
17 startTime time.Time
18}
19
20func 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
33func (r *serverReporter) ReceivedMessage() {
34 r.metrics.serverStreamMsgReceived.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName).Inc()
35}
36
37func (r *serverReporter) SentMessage() {
38 r.metrics.serverStreamMsgSent.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName).Inc()
39}
40
41func (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}