-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgrpc.go
36 lines (30 loc) · 877 Bytes
/
grpc.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
package gridlock
import (
"context"
"strings"
"github.com/luno/gridlock/api"
"google.golang.org/grpc"
)
func GRPCClientReporter(c Client) grpc.UnaryClientInterceptor {
return func(ctx context.Context,
method string, req, reply interface{},
cc *grpc.ClientConn, invoker grpc.UnaryInvoker,
opts ...grpc.CallOption,
) error {
err := invoker(ctx, method, req, reply, cc, opts...)
success := CallGood
if err != nil {
success = CallBad
}
service, _ := splitMethodName(method)
c.Record(Method{Target: service, Transport: api.TransportGRPC}, success)
return err
}
}
func splitMethodName(fullMethodName string) (string, string) {
fullMethodName = strings.TrimPrefix(fullMethodName, "/") // remove leading slash
if i := strings.Index(fullMethodName, "/"); i >= 0 {
return fullMethodName[:i], fullMethodName[i+1:]
}
return "unknown", "unknown"
}