/
reporter.go
114 lines (97 loc) · 3.92 KB
/
reporter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright (c) The go-grpc-middleware Authors.
// Licensed under the Apache License 2.0.
package metrics
import (
"context"
"time"
openmetrics "github.com/prometheus/client_golang/prometheus"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors"
)
type reporter struct {
clientMetrics *ClientMetrics
serverMetrics *ServerMetrics
typ interceptors.GRPCType
service, method string
startTime time.Time
kind Kind
sendTimer, receiveTimer interceptors.Timer
}
func (r *reporter) PostCall(err error, duration time.Duration) {
// get status code from error
status, _ := FromError(err)
code := status.Code()
// perform handling of metrics from code
switch r.kind {
case KindServer:
r.serverMetrics.serverHandledCounter.WithLabelValues(string(r.typ), r.service, r.method, code.String()).Inc()
if r.serverMetrics.serverHandledHistogram != nil {
r.serverMetrics.serverHandledHistogram.WithLabelValues(string(r.typ), r.service, r.method).Observe(time.Since(r.startTime).Seconds())
}
case KindClient:
r.clientMetrics.clientHandledCounter.WithLabelValues(string(r.typ), r.service, r.method, code.String()).Inc()
if r.clientMetrics.clientHandledHistogram != nil {
r.clientMetrics.clientHandledHistogram.WithLabelValues(string(r.typ), r.service, r.method).Observe(time.Since(r.startTime).Seconds())
}
}
}
func (r *reporter) PostMsgSend(_ interface{}, _ error, _ time.Duration) {
switch r.kind {
case KindServer:
r.serverMetrics.serverStreamMsgSent.WithLabelValues(string(r.typ), r.service, r.method).Inc()
case KindClient:
r.clientMetrics.clientStreamMsgSent.WithLabelValues(string(r.typ), r.service, r.method).Inc()
r.sendTimer.ObserveDuration()
}
}
func (r *reporter) PostMsgReceive(_ interface{}, _ error, _ time.Duration) {
switch r.kind {
case KindServer:
r.serverMetrics.serverStreamMsgReceived.WithLabelValues(string(r.typ), r.service, r.method).Inc()
case KindClient:
r.clientMetrics.clientStreamMsgReceived.WithLabelValues(string(r.typ), r.service, r.method).Inc()
r.receiveTimer.ObserveDuration()
}
}
type reportable struct {
clientMetrics *ClientMetrics
serverMetrics *ServerMetrics
}
func (rep *reportable) ServerReporter(ctx context.Context, meta interceptors.CallMeta) (interceptors.Reporter, context.Context) {
return rep.reporter(ctx, rep.serverMetrics, nil, meta.Typ, meta.Service, meta.Method, KindServer)
}
func (rep *reportable) ClientReporter(ctx context.Context, meta interceptors.CallMeta) (interceptors.Reporter, context.Context) {
return rep.reporter(ctx, nil, rep.clientMetrics, meta.Typ, meta.Service, meta.Method, KindClient)
}
func (rep *reportable) reporter(ctx context.Context, sm *ServerMetrics, cm *ClientMetrics, rpcType interceptors.GRPCType, service, method string, kind Kind) (interceptors.Reporter, context.Context) {
r := &reporter{
clientMetrics: cm,
serverMetrics: sm,
typ: rpcType,
service: service,
method: method,
kind: kind,
sendTimer: interceptors.EmptyTimer,
receiveTimer: interceptors.EmptyTimer,
}
switch kind {
case KindClient:
if r.clientMetrics.clientHandledHistogram != nil {
r.startTime = time.Now()
}
r.clientMetrics.clientStartedCounter.WithLabelValues(string(r.typ), r.service, r.method).Inc()
if r.clientMetrics.clientStreamSendHistogram != nil {
hist := r.clientMetrics.clientStreamSendHistogram.WithLabelValues(string(r.typ), r.service, r.method)
r.sendTimer = openmetrics.NewTimer(hist)
}
if r.clientMetrics.clientStreamRecvHistogram != nil {
hist := r.clientMetrics.clientStreamRecvHistogram.WithLabelValues(string(r.typ), r.service, r.method)
r.receiveTimer = openmetrics.NewTimer(hist)
}
case KindServer:
if r.serverMetrics.serverHandledHistogram != nil {
r.startTime = time.Now()
}
r.serverMetrics.serverStartedCounter.WithLabelValues(string(r.typ), r.service, r.method).Inc()
}
return r, ctx
}