blob: cbf15322996e4f75899bbed5161044b123192df6 [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 clientReporter struct {
13 metrics *ClientMetrics
14 rpcType grpcType
15 serviceName string
16 methodName string
17 startTime time.Time
18}
19
20func 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
33func (r *clientReporter) ReceivedMessage() {
34 r.metrics.clientStreamMsgReceived.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName).Inc()
35}
36
37func (r *clientReporter) SentMessage() {
38 r.metrics.clientStreamMsgSent.WithLabelValues(string(r.rpcType), r.serviceName, r.methodName).Inc()
39}
40
41func (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}