forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interceptor.go
104 lines (85 loc) · 2.86 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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package grpcmetrics
import (
"context"
"strings"
"time"
"github.com/hyperledger/fabric/common/metrics"
"google.golang.org/grpc"
"google.golang.org/grpc/status"
)
type UnaryMetrics struct {
RequestDuration metrics.Histogram
RequestsReceived metrics.Counter
RequestsCompleted metrics.Counter
}
func UnaryServerInterceptor(um *UnaryMetrics) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
service, method := serviceMethod(info.FullMethod)
um.RequestsReceived.With("service", service, "method", method).Add(1)
startTime := time.Now()
resp, err := handler(ctx, req)
st, _ := status.FromError(err)
duration := time.Since(startTime)
um.RequestDuration.With(
"service", service, "method", method, "code", st.Code().String(),
).Observe(duration.Seconds())
um.RequestsCompleted.With("service", service, "method", method, "code", st.Code().String()).Add(1)
return resp, err
}
}
type StreamMetrics struct {
RequestDuration metrics.Histogram
RequestsReceived metrics.Counter
RequestsCompleted metrics.Counter
MessagesSent metrics.Counter
MessagesReceived metrics.Counter
}
func StreamServerInterceptor(sm *StreamMetrics) grpc.StreamServerInterceptor {
return func(svc interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
sm := sm
service, method := serviceMethod(info.FullMethod)
sm.RequestsReceived.With("service", service, "method", method).Add(1)
wrappedStream := &serverStream{
ServerStream: stream,
messagesSent: sm.MessagesSent.With("service", service, "method", method),
messagesReceived: sm.MessagesReceived.With("service", service, "method", method),
}
startTime := time.Now()
err := handler(svc, wrappedStream)
st, _ := status.FromError(err)
duration := time.Since(startTime)
sm.RequestDuration.With(
"service", service, "method", method, "code", st.Code().String(),
).Observe(duration.Seconds())
sm.RequestsCompleted.With("service", service, "method", method, "code", st.Code().String()).Add(1)
return err
}
}
func serviceMethod(fullMethod string) (service, method string) {
normalizedMethod := strings.Replace(fullMethod, ".", "_", -1)
parts := strings.SplitN(normalizedMethod, "/", -1)
if len(parts) != 3 {
return "unknown", "unknown"
}
return parts[1], parts[2]
}
type serverStream struct {
grpc.ServerStream
messagesSent metrics.Counter
messagesReceived metrics.Counter
}
func (ss *serverStream) SendMsg(msg interface{}) error {
ss.messagesSent.Add(1)
return ss.ServerStream.SendMsg(msg)
}
func (ss *serverStream) RecvMsg(msg interface{}) error {
err := ss.ServerStream.RecvMsg(msg)
if err == nil {
ss.messagesReceived.Add(1)
}
return err
}