forked from easyCZ/connect-go-prometheus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterceptor.go
202 lines (172 loc) · 5.22 KB
/
interceptor.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package connect_go_prometheus
import (
"context"
"strings"
"time"
"connectrpc.com/connect"
"github.com/cockroachdb/errors"
prom "github.com/prometheus/client_golang/prometheus"
"google.golang.org/protobuf/proto"
)
const (
CodeOk = "ok"
)
func NewInterceptor(opts ...InterceptorOption) *Interceptor {
options := evaluteInterceptorOptions(&interceptorOptions{
client: DefaultClientMetrics,
server: DefaultServerMetrics,
}, opts...)
return &Interceptor{
client: options.client,
server: options.server,
}
}
var _ connect.Interceptor = (*Interceptor)(nil)
type Interceptor struct {
client *Metrics
server *Metrics
}
func (i *Interceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFunc {
return connect.UnaryFunc(func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {
// Short-circuit, not configured to report for either client or server.
if i.client == nil && i.server == nil {
return next(ctx, req)
}
now := time.Now()
callType := streamTypeString(req.Spec().StreamType)
callPackage, callMethod := procedureToPackageAndMethod(req.Spec().Procedure)
var reporter *Metrics
if req.Spec().IsClient {
reporter = i.client
} else {
reporter = i.server
}
var code string
if reporter != nil {
var bytes *prom.CounterVec
if reporter.isClient {
bytes = reporter.bytesSent
} else {
bytes = reporter.bytesReceived
}
if bytes != nil {
bytes.WithLabelValues(callType, callPackage, callMethod).Add(float64(proto.Size(req.Any().(proto.Message))))
}
reporter.ReportStarted(callType, callPackage, callMethod)
defer func() {
reporter.ReportHandled(callType, callPackage, callMethod, code)
reporter.ReportHandledSeconds(callType, callPackage, callMethod, code, time.Since(now).Seconds())
}()
}
resp, err := next(ctx, req)
code = codeOf(err)
if err == nil && reporter != nil {
var bytes *prom.CounterVec
if reporter.isClient {
bytes = reporter.bytesReceived
} else {
bytes = reporter.bytesSent
}
if bytes != nil {
bytes.WithLabelValues(callType, callPackage, callMethod).Add(float64(proto.Size(resp.Any().(proto.Message))))
}
}
return resp, err
})
}
func (i *Interceptor) WrapStreamingClient(next connect.StreamingClientFunc) connect.StreamingClientFunc {
return connect.StreamingClientFunc(func(ctx context.Context, spec connect.Spec) connect.StreamingClientConn {
// Short-circuit, not configured to report for client.
if i.client == nil {
return next(ctx, spec)
}
now := time.Now()
callType := streamTypeString(spec.StreamType)
callPackage, callMethod := procedureToPackageAndMethod(spec.Procedure)
i.client.ReportStarted(callType, callPackage, callMethod)
onClose := func(err error) {
code := codeOf(err)
i.client.ReportHandled(callType, callPackage, callMethod, code)
i.client.ReportHandledSeconds(callType, callPackage, callMethod, code, time.Since(now).Seconds())
}
conn := next(ctx, spec)
return newStreamingClientConn(conn, i, onClose)
})
}
func (i *Interceptor) WrapStreamingHandler(next connect.StreamingHandlerFunc) connect.StreamingHandlerFunc {
return connect.StreamingHandlerFunc(func(ctx context.Context, shc connect.StreamingHandlerConn) error {
// Short-circuit, not configured to report for server.
if i.server == nil {
return next(ctx, shc)
}
now := time.Now()
callType := streamTypeString(shc.Spec().StreamType)
callPackage, callMethod := procedureToPackageAndMethod(shc.Spec().Procedure)
var code string
i.server.ReportStarted(callType, callPackage, callMethod)
defer func() {
i.server.ReportHandled(callType, callPackage, callMethod, code)
i.server.ReportHandledSeconds(callType, callPackage, callMethod, code, time.Since(now).Seconds())
}()
shc = newStreamingHandlerConn(shc, i)
err := next(ctx, shc)
code = codeOf(err)
return err
})
}
func procedureToPackageAndMethod(procedure string) (string, string) {
procedure = strings.TrimPrefix(procedure, "/") // remove leading slash
if i := strings.Index(procedure, "/"); i >= 0 {
return procedure[:i], procedure[i+1:]
}
return "unknown", "unknown"
}
func streamTypeString(st connect.StreamType) string {
switch st {
case connect.StreamTypeUnary:
return "unary"
case connect.StreamTypeClient:
return "client_stream"
case connect.StreamTypeServer:
return "server_stream"
case connect.StreamTypeBidi:
return "bidi"
default:
return "unknown"
}
}
func codeOf(err error) string {
if err == nil {
return CodeOk
}
code := connect.CodeOf(err)
if code == connect.CodeUnknown {
if errors.Is(err, context.Canceled) {
code = connect.CodeCanceled
} else if errors.Is(err, context.DeadlineExceeded) {
code = connect.CodeDeadlineExceeded
}
}
return code.String()
}
type interceptorOptions struct {
client *Metrics
server *Metrics
}
type InterceptorOption func(*interceptorOptions)
func WithClientMetrics(m *Metrics) InterceptorOption {
return func(io *interceptorOptions) {
io.client = m
}
}
func WithServerMetrics(m *Metrics) InterceptorOption {
return func(io *interceptorOptions) {
io.server = m
}
}
func evaluteInterceptorOptions(defaults *interceptorOptions, opts ...InterceptorOption) *interceptorOptions {
for _, opt := range opts {
opt(defaults)
}
return defaults
}